package cn.iocoder.yudao.module.mes.controller.admin.app.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.service.product.ErpProductService;
import cn.iocoder.yudao.module.mes.controller.admin.app.vo.AppOrgWorkerVO;
import cn.iocoder.yudao.module.mes.controller.admin.app.vo.AppProductVo;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.ProcessTypeEnum;
import cn.iocoder.yudao.module.mes.controller.admin.plan.vo.PlanStatusEnum;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.orgworker.OrgWorkerDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.plan.PlanDO;
import cn.iocoder.yudao.module.mes.service.organization.OrganizationService;
import cn.iocoder.yudao.module.mes.service.plan.PlanService;
import cn.iocoder.yudao.module.mes.service.producereport.ProduceReportDetailService;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * APPService 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class AppServiceImpl implements AppService {


    @Resource
    private OrganizationService organizationService;
    @Resource
    private PlanService planService;
    @Resource
    private ProduceReportDetailService produceReportDetailService;
    @Resource
    private ErpProductService productService;
    @Override
    public  List<AppOrgWorkerVO> getOrgList(List<OrgWorkerDO> list){
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<Long, OrganizationDO> map = organizationService.getMap(
                convertSet(list, OrgWorkerDO::getOrgId));

        return BeanUtils.toBean(list, AppOrgWorkerVO.class, item -> {
            MapUtils.findAndThen(map, item.getOrgId(),
                    org -> item.setOrgName(org.getName()).setOrgType(org.getOrgType())
                            .setValue(org.getId()).setText(org.getName())
            );

        });
    }
    @Override
    public  List<AppOrgWorkerVO> getUserList(List<AdminUserRespDTO> list){
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AppOrgWorkerVO> workerList = new ArrayList<>();
        for (AdminUserRespDTO user : list) {
            AppOrgWorkerVO workerVO = new AppOrgWorkerVO().setValue(user.getId()).setText(user.getNickname());
            workerList.add(workerVO);
        }
       return workerList;
    }
    @Override
    public  List<AppProductVo> getProductList(List<ErpProductRespVO> list){
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AppProductVo> workerList = new ArrayList<>();
        for (ErpProductRespVO product : list) {
            AppProductVo workerVO = new AppProductVo().setValue(product.getId()).setText(product.getName())
                    .setProductId(product.getId()).setProductName(product.getName());
            workerList.add(workerVO);
        }
        return workerList;
    }
    //获取正在开工的计划某工序的报工情况
    @Override
    public List<AppProductVo> getPlanProductList(String orgType){
        List<Integer> status = new ArrayList<>();
        status.add(PlanStatusEnum.量产.getValue());
        List<PlanDO> planList = planService.getPlanByStatus(status);
        Map<Long, ErpProductDO> map = productService.getProductMap(
                convertSet(planList, PlanDO::getProductId));

        List<AppProductVo> appProductVoList = new ArrayList<>();
        for (PlanDO plan :planList) {
            Long finish = produceReportDetailService.sumByPlanAndOrgType(plan.getId(), orgType);
            AppProductVo appProductVo = new AppProductVo().setValue(plan.getId())
                    .setProductId(plan.getProductId()).setProductName(map.get(plan.getProductId()).getName());
            StringBuilder builder = new StringBuilder().append(map.get(plan.getProductId()).getName()).append("-");
            if(orgType.equals(ProcessTypeEnum.热压.getValue())){
                if(plan.getReyaNumber() - finish > 0)
                   builder.append("需:").append(plan.getReyaNumber() - finish);
                else builder.append("超:").append(finish - plan.getReyaNumber());
            }
            else{
                if(plan.getPlanNumber() - finish > 0)
                    builder.append("需:").append(plan.getPlanNumber() - finish);
                else builder.append("超:").append(finish - plan.getPlanNumber());
            }
           // builder.append(" [").append(plan.getCode()).append("]");
            appProductVo.setText(builder.toString());
            appProductVoList.add(appProductVo);
        }
       return appProductVoList;
    }
}