package com.xisc.pm.app.service.strategy.secondarydesign;

import com.xisc.pm.api.dto.PmProContFilterRequestDTO;
import com.xisc.pm.api.dto.PmProContYieldRequestDTO;
import com.xisc.pm.domain.entity.PmCmdLargePlt;
import com.xisc.pm.domain.entity.PmCmdSlab;
import com.xisc.pm.domain.entity.PmCompositeSlab;
import com.xisc.pm.domain.entity.PmCompositeSlabList;
import com.xisc.pm.domain.entity.PmPdContSum;
import com.xisc.pm.domain.entity.PmPdSlab;
import com.xisc.pm.domain.repository.MmPlateRepository;
import com.xisc.pm.domain.repository.PmCompositeSlabListRepository;
import com.xisc.pm.domain.repository.PmCompositeSlabRepository;
import com.xisc.pm.domain.vo.MmPlateVO;
import com.xisc.pm.domain.vo.QeSampleAddtionRequestVO;
import com.xisc.pm.domain.vo.WmStockInfoVO1;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.enums.PdMatType;
import com.xisc.pm.infra.mapper.PmCompositeSlabListMapper;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.collections.CollectionUtils;
import org.hzero.boot.platform.lov.adapter.LovAdapter;
import org.hzero.boot.platform.lov.dto.LovValueDTO;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (复合板)二次设计
 * 
 * @author jun.zeng
 *
 */
@Component
public class CompositSlabSecondaryDesignStrategy extends SlabSecondaryDesignStrategy {

    @Autowired
    private PmCompositeSlabListRepository pmCompositeSlabListRepository;

    @Autowired
    private PmCompositeSlabListMapper pmCompositeSlabListMapper;
    
    @Autowired
    private PmCompositeSlabRepository pmCompositeSlabRepository;
    @Autowired
    private MmPlateRepository mmPlateRepository;
    @Autowired
    private LovAdapter lovAdapter;

    @Override
    protected PdMatType getMatDesignKind() {
        return PdMatType.FHB;
    }

    @Override
    protected PmProContFilterRequestDTO buildPmProContFilterRequestDTO(
            PmProContYieldRequestDTO pmProContYieldRequestDTO, PmPdSlab salbInfo, PmCmdLargePlt pmCmdLargePlt) {
        PmProContFilterRequestDTO contFilterRequestDTO = super.buildPmProContFilterRequestDTO(pmProContYieldRequestDTO,
                salbInfo, pmCmdLargePlt);
        String baseMatNo = salbInfo.getSlabNo();
        List<PmCompositeSlabList> pmCompositeSlabLists = pmCompositeSlabListRepository.selectByCondition(Condition.builder(PmCompositeSlabList.class)
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlabList.FIELD_TENANT_ID, salbInfo.getTenantId())
                        .andEqualTo(PmCompositeSlabList.FIELD_BASE_MAT_NO, baseMatNo))
                .build());
        contFilterRequestDTO.setPlateId(pmCompositeSlabLists.get(0).getListId());
        contFilterRequestDTO.setMatDesignKind(PdMatType.FHB.getCode());
        return contFilterRequestDTO;
    }

    @Override
    protected void updateCompositeParam(List<QeSampleAddtionRequestVO> qeSampleAddtionRequestList, List<PmCmdLargePlt> pmCmdLargePltDbList, Long tenantId) {
        // 复合坯产线号传加工厂代码
        List<String> tmpSlabNoList = qeSampleAddtionRequestList.stream().map(QeSampleAddtionRequestVO::getTmpSlabNo).distinct().collect(Collectors.toList());
        List<WmStockInfoVO1> wmStockInfoVO1List = pmCompositeSlabListMapper.selectStockCodeBySlabNo(tmpSlabNoList, tenantId);
        Map<String, String>StockInfoVO1Map = new HashMap<>(wmStockInfoVO1List.size());
        if (CollectionUtils.isNotEmpty(wmStockInfoVO1List)) {
            StockInfoVO1Map = wmStockInfoVO1List.stream().filter(item->item.getFactoryCode() != null)
                    .collect(Collectors.toMap(WmStockInfoVO1::getTmpSlabNo, WmStockInfoVO1::getFactoryCode));
        }

        // 获取炉号为试样号
        List<String> slabNoList = pmCmdLargePltDbList.stream().map(PmCmdLargePlt::getSlabNo).collect(Collectors.toList());
        Map<String, String> tmpSlabNoMap = pmCmdLargePltDbList.stream().collect(Collectors.toMap(PmCmdLargePlt::getTmpSlabNo, PmCmdLargePlt::getSlabNo));
        List<PmCompositeSlabList> pmCompositeSlabList = pmCompositeSlabListRepository.selectByCondition(Condition.builder(PmCompositeSlabList.class)
                .andWhere(Sqls.custom().andIn(PmCompositeSlabList.FIELD_BASE_MAT_NO, slabNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlabList.FIELD_TENANT_ID, tenantId)).build());

        List<PmCompositeSlab> pmCompositeSlabs = pmCompositeSlabRepository.selectByCondition(Condition.builder(PmCompositeSlab.class)
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlab.FIELD_SLAB_ID, pmCompositeSlabList.get(0).getSlabId()))
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlab.FIELD_TENANT_ID, tenantId)).build());
        // 查询prodYear
        List<MmPlateVO> mmPlateByMatNoList = mmPlateRepository.getMmPlateByMatNo(slabNoList, tenantId);
        Map<String, String> mmPlateByMatNoMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(mmPlateByMatNoList)) {
            mmPlateByMatNoMap = mmPlateByMatNoList.stream().collect(Collectors.toMap(MmPlateVO::getMatNo, MmPlateVO::getProdYear));
        }

        // 查询值集
        List<LovValueDTO> lovValueDTOList = lovAdapter.queryLovValue(Constants.LovCode.COMP_SLAB_MSC_LINE_CODE_FLAG, tenantId);
        if (CollectionUtils.isEmpty(lovValueDTOList)) {
            throw new CommonException("pm.composite_slab_023",Constants.LovCode.COMP_SLAB_MSC_LINE_CODE_FLAG);
        }
        String mscFlag = lovValueDTOList.get(0).getMeaning();

        for (QeSampleAddtionRequestVO qeSampleAddtionRequestVO : qeSampleAddtionRequestList) {
            // 若为1试样添加接口则传加工厂代码，若为0试样添加则不传加工场代码
            if ("1".equals(mscFlag)) {
                qeSampleAddtionRequestVO.setMscLineNo(StockInfoVO1Map.getOrDefault(qeSampleAddtionRequestVO.getTmpSlabNo(), null));
            } else {
                qeSampleAddtionRequestVO.setMscLineNo(null);
            }
            qeSampleAddtionRequestVO.setPono(pmCompositeSlabs.get(0).getSampleLotNo());
            qeSampleAddtionRequestVO.setProdYear(mmPlateByMatNoMap.getOrDefault(tmpSlabNoMap.get(qeSampleAddtionRequestVO.getTmpSlabNo()), null));
            qeSampleAddtionRequestVO.setSmpTime(null);
        }
    }

    @Override
    public void checkNeedSlabLen(PmCmdSlab pmCmdSlab) {
    }

    @Override
    public void callMmMatStatusModify(Long tenantId, List<PmPdSlab> slabInfoList , List<String> tmpSlabNoList) {
    }
    
    /**
     * @param slabNo
     * @return
     */
    public String addSplittingProcess(String slabNo , String rollLineNo, Long tenantId) {
        List<PmCompositeSlabList> pmCompositeSlabLists = pmCompositeSlabListRepository.selectByCondition(Condition.builder(PmCompositeSlabList.class)
                .andWhere(Sqls.custom().andEqualTo(PmCompositeSlabList.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmCompositeSlabList.FIELD_BASE_MAT_NO, slabNo))
                .build());
        if(pmCompositeSlabLists != null && !pmCompositeSlabLists.isEmpty()) {
            Long slabId = pmCompositeSlabLists.get(0).getSlabId();
            PmCompositeSlab pmCompositeSlab = pmCompositeSlabRepository.selectByPrimary(slabId);
            if(pmCompositeSlab.getSlabNo().startsWith("F")){
                switch (rollLineNo) {
                    case "10":
                        return "1S1";
                    case "20":
                        return "2S1";
                    case "30":
                        return "5S1";
                    default:
                }
            }
        }
        return null;
    }
    
    @Override
    public void abandonContractEventCommand(List<String> virtualSlabNoList ,Map<String ,PmPdSlab> slabInfo, Map<String, List<PmPdContSum>> pmPdContSumListMap, Long tenantId) {
        
    }

    @Override
    public void abandonContractEvent(List<String> virtualSlabNoList, Long tenantId, String pdMatStatus, String prodcCd, String wholeBacklogCode, String eventCode) {

    }
}
