package com.yiqi.admin.controller.order;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.BaseEntity;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.core.domain.entity.SysUser;
import com.yiqi.common.enums.SourceType;
import com.yiqi.order.domain.vo.OrderClothItemFlowRecordVO;
import com.yiqi.system.service.ISysOrgService;
import com.yiqi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.order.domain.OrderClothItemFlowRecord;
import com.yiqi.order.service.IOrderClothItemFlowRecordService;

/**
 * 洗衣订单衣服流程日志Controller
 *
 * @author xiaohei
 * @date 2023-12-05
 */
@Api(tags = "管理后台 - 洗衣订单衣服流程日志")
@RestController
@RequestMapping(UrlConstants.managerApi + "/order/clothFlowRecord")
public class OrderClothItemFlowRecordController extends BaseController {
    @Autowired
    private IOrderClothItemFlowRecordService orderClothItemFlowRecordService;
    @Autowired
    private ISysOrgService sysOrgService;
    @Autowired
    private ISysUserService sysUserService;


    @GetMapping(value = "listByOrderClothId")
    @ApiOperation("根据衣服编号查询衣服流程日志")
    public R<List<OrderClothItemFlowRecordVO>> listByOrderClothId(@RequestParam Long orderClothId) throws Exception{
        List<OrderClothItemFlowRecord> list = orderClothItemFlowRecordService.list(new QueryWrapper<OrderClothItemFlowRecord>()
                .lambda().eq(OrderClothItemFlowRecord::getOrderClothItemId,orderClothId));
        Set<Long> employeeIds = list.stream().map(BaseEntity::getCreateById).collect(Collectors.toSet());
        if(employeeIds.size() == 0){
            return R.ok(new ArrayList<>());
        }
        List<SysUser> sysUsers = sysUserService.selectUserByIds(employeeIds);
        Map<Long, SysUser> sysUserMap = sysUsers
                .stream().collect(Collectors.toMap(SysUser::getUserId,SysUser -> SysUser));
//        Map<Long,SysOrg> storeMap = new HashMap<>();
//        Map<Long,SysOrg> factoryMap = new HashMap<>();
//        if(sysUsers.stream().filter(sysUser -> sysUser.getUserType()
//                .equals(SourceType.FACTORY.getCode())).map(SysUser::getFactoryId).collect(Collectors.toSet()).size() > 0){
//            storeMap = sysOrgService.list(new QueryWrapper<SysOrg>().lambda()
//                    .in(SysOrg::getId,sysUsers.stream().filter(sysUser -> sysUser.getUserType()
//                            .equals(SourceType.STORE.getCode())).map(SysUser::getStoreId).collect(Collectors.toSet())))
//                    .stream().collect(Collectors.toMap(SysOrg::getId,SysOrg -> SysOrg));
//        }
//        if(sysUsers.stream().filter(sysUser -> sysUser.getUserType()
//                .equals(SourceType.FACTORY.getCode())).map(SysUser::getFactoryId).collect(Collectors.toSet()).size() > 0){
//            factoryMap =  sysOrgService.list(new QueryWrapper<SysOrg>().lambda()
//                    .in(false,SysOrg::getId,sysUsers.stream().filter(sysUser -> sysUser.getUserType()
//                            .equals(SourceType.FACTORY.getCode())).map(SysUser::getFactoryId).collect(Collectors.toSet())))
//                    .stream().collect(Collectors.toMap(SysOrg::getId,SysOrg -> SysOrg));
//        }
        List<OrderClothItemFlowRecordVO> orderClothItemFlowRecordVOS = new ArrayList<>();
        for (OrderClothItemFlowRecord orderClothItemFlowRecord : list) {
            OrderClothItemFlowRecordVO orderClothItemFlowRecordVO = new OrderClothItemFlowRecordVO();
            BeanUtils.copyProperties(orderClothItemFlowRecord,orderClothItemFlowRecordVO);
            SysUser user = sysUserMap.get(orderClothItemFlowRecordVO.getCreateById());
            if (user == null){
                orderClothItemFlowRecordVO.setOperateName("未知");
                orderClothItemFlowRecordVO.setOperateOrgName("未知");
            }else {
                orderClothItemFlowRecordVO.setOperateName(user.getNickName());
                if(user.getUserType().equals(SourceType.STORE.getCode())){
                    orderClothItemFlowRecordVO.setOperateOrgName(user.getStoreName());
                }
                if(user.getUserType().equals(SourceType.FACTORY.getCode())){
                    orderClothItemFlowRecordVO.setOperateOrgName(user.getFactoryName());
                }
            }
            orderClothItemFlowRecordVOS.add(orderClothItemFlowRecordVO);
        }
        return R.ok(orderClothItemFlowRecordVOS);
    }
}
