package com.xisc.pm.infra.repository.impl;

import com.google.common.collect.Maps;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.domain.entity.PmProCont;
import com.xisc.pm.domain.repository.PmContMscTechRepository;
import com.xisc.pm.domain.vo.QdOrderTechVO;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.helper.BatchSqlHelper;
import org.hzero.mybatis.util.Sqls;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 合同产线工艺参数表 资源库实现
 *
 * @author 曾俊 jun.zeng@hand-china.com 2024-07-29 16:24:25
 */
@Component
public class PmContMscTechRepositoryImpl extends BaseRepositoryImpl<PmContMscTech> implements PmContMscTechRepository {

    @Override
    public int deleteByOrderNoList(List<String> orderNoList, Long tenantId) {
        if (null == orderNoList || orderNoList.isEmpty()) {
            return 0;
        }
        List<PmContMscTech> list = this.selectByOrderNoList(orderNoList, tenantId);
        return this.batchDeleteByPrimaryKey(list);
    }

    @Override
    public List<PmContMscTech> selectByOrderNoList(List<String> orderNoList, Long tenantId) {
        if (null == orderNoList || orderNoList.isEmpty()) {
            return null;
        }
        return this.selectByCondition(Condition.builder(PmContMscTech.class).andWhere(Sqls.custom()
                .andIn(PmContMscTech.FIELD_ORDER_NO, orderNoList).andEqualTo(PmContMscTech.FIELD_TENANT_ID, tenantId))
                .build());
    }

    @Override
    public PmContMscTech selectByOrderNoAndMscLineNo(String orderNo, String mscLineNo) {
        List<PmContMscTech> pmContMscTechs = this.selectByCondition(Condition.builder(PmContMscTech.class)
                .andWhere(Sqls.custom().andEqualTo(PmContMscTech.FIELD_ORDER_NO, orderNo)
                        .andEqualTo(PmContMscTech.FIELD_MSC_LINE_NO, mscLineNo))
                .build());
        if (pmContMscTechs != null && !pmContMscTechs.isEmpty()) {
            return pmContMscTechs.get(0);
        }
        return null;
    }

    @Override
    public PmContMscTech selectWholeBacklog(String orderNo, String mscLineNo) {
        List<PmContMscTech> pmContMscTechs = this.selectByCondition(Condition.builder(PmContMscTech.class)
                .andWhere(Sqls.custom().andEqualTo(PmContMscTech.FIELD_ORDER_NO, orderNo)
                        .andEqualTo(PmContMscTech.FIELD_MSC_LINE_NO, mscLineNo)
                        .andEqualTo(PmContMscTech.FIELD_WHOLE_BACKLOG, "whole_backlog"))
                .build());
        if (pmContMscTechs != null && !pmContMscTechs.isEmpty()) {
            return pmContMscTechs.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<PmContMscTech> selectByOrderNoAndMscLineNo(List<String> orderNoList, String mscLineNo, Long tenantId) {
        List<PmContMscTech> pmContMscTechs = this.selectByCondition(Condition.builder(PmContMscTech.class)
                .andWhere(Sqls.custom().andIn(PmContMscTech.FIELD_ORDER_NO, orderNoList)
                        .andEqualTo(PmContMscTech.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmContMscTech.FIELD_MSC_LINE_NO, mscLineNo))
                .build());
        return pmContMscTechs;
    }

    @Override
    public List<PmContMscTech> selectByOrderNoAndMscLineNo(List<String> orderNoList, List<String> mscLineNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmContMscTech.class)
                .andWhere(Sqls.custom().andIn(PmContMscTech.FIELD_ORDER_NO, orderNoList)
                        .andEqualTo(PmContMscTech.FIELD_TENANT_ID, tenantId)
                        .andIn(PmContMscTech.FIELD_MSC_LINE_NO, mscLineNoList))
                .build());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PmContMscTech> createPmContMscTech(List<PmProCont> pmProConts, List<QdOrderTechVO> qdOrderTechVOs,
            Long tenantId, Map<String, String> qdToLineupResultSteelMap) {
        List<PmContMscTech> pmContMscTechs = new ArrayList<PmContMscTech>();
        Map<String, PmProCont> pmProContMap = Maps.uniqueIndex(pmProConts, PmProCont::getOrderNo);
        for (QdOrderTechVO qdOrderTechVO : qdOrderTechVOs) {
            PmContMscTech pmContMscTech = new PmContMscTech();
            pmContMscTech.setTenantId(tenantId);
            pmContMscTech.setOrderNo(qdOrderTechVO.getOrderNo());
            PmProCont pmProCont = pmProContMap.get(pmContMscTech.getOrderNo());
            pmContMscTech.setProContId(pmProCont.getProContId());
            pmContMscTech.setMscLineNo(qdOrderTechVO.getMscLineNo());
            pmContMscTech.setSlabPlaceCode(qdToLineupResultSteelMap.getOrDefault(Strings.concat(qdOrderTechVO.getOrderNo(), qdOrderTechVO.getMscLineNo()), null));
            pmContMscTech.setPlatePdQty(BigDecimal.ZERO);
            pmContMscTech.setSlabPdQty(BigDecimal.ZERO);
            pmContMscTech.setCcPdQty(BigDecimal.ZERO);
            pmContMscTech.setWholeBacklog(qdOrderTechVO.getWholeBacklog());
            pmContMscTech.setWholeBacklogCode(Constants.WholeBacklogCode.CODE_13);
            pmContMscTech.setStNo(qdOrderTechVO.getStNo());
            pmContMscTech.setStNo1(" ");
            pmContMscTech.setStNo2(" ");
            pmContMscTech.setStNo3(" ");
            pmContMscTech.setStNo4(" ");
            pmContMscTech.setStNo5(" ");
            pmContMscTech.setStNo6(" ");
            pmContMscTech.setStNo7(" ");
            pmContMscTech.setStNo8(" ");
            pmContMscTech.setStNo9(" ");
            pmContMscTech.setSlabPlaceStd(qdOrderTechVO.getSlabPlaceStd());
            pmContMscTech.setSlabFinishCode(qdOrderTechVO.getSlabFinishCode());
            pmContMscTech.setFlameCleanDiv(qdOrderTechVO.getFlameCleanDiv());
            pmContMscTech.setSlabDest(qdOrderTechVO.getSlabDest());
            pmContMscTech.setExpandQuot(qdOrderTechVO.getExpandQuot());
            pmContMscTech.setAdTypeCode(qdOrderTechVO.getCoolType());
            pmContMscTech.setPlateDtCode(qdOrderTechVO.getPlateDtCode());
            pmContMscTech.setGaugeMulMin1(qdOrderTechVO.getGaugeMulMin1());
            pmContMscTech.setMinCr1Thick(qdOrderTechVO.getMinCr1Thick());
            pmContMscTech.setRollDirectCode(qdOrderTechVO.getRollDirectCode());
            pmContMscTech.setCtrlRollCode(qdOrderTechVO.getCtrlRollCode());
            pmContMscTech.setCtrlRollCode1(qdOrderTechVO.getCtrlRollCode1());
            pmContMscTech.setCtrlRollCode34(qdOrderTechVO.getCtrlRollCode34());
            pmContMscTech.setAdModeCode(qdOrderTechVO.getAdModeCode());
            pmContMscTech.setAdModeCode34(qdOrderTechVO.getAdModeCode34());
            pmContMscTech.setRollMode(qdOrderTechVO.getRollMode());
            pmContMscTech.setCompressMin(qdOrderTechVO.getCompressMin());
            pmContMscTech.setMultiPlateFlag(Utils.getYesOrNo(qdOrderTechVO.getMultiPlateFlag()));
            if(pmContMscTech.getMultiPlateFlag() == null) {
                pmContMscTech.setMultiPlateFlag(0L);
            }
            pmContMscTech.setRfPath(qdOrderTechVO.getRfPath());
            pmContMscTech.setFurType(qdOrderTechVO.getFurType());
            pmContMscTech.setWidenMin(qdOrderTechVO.getWidenMin());
            pmContMscTech.setMiddleCoolMode(qdOrderTechVO.getMiddleCoolMode());
            pmContMscTech.setMaxMiddleCoolSpeed(qdOrderTechVO.getMaxMiddleCoolSpeed());
            pmContMscTech.setHotRectifyFlag(Utils.getYesOrNo(qdOrderTechVO.getHotRectifyFlag()));
            pmContMscTech.setHotRectifyMethodCode(qdOrderTechVO.getHotRectifyMethodCode());
            pmContMscTech.setHotSendDiv(StringUtils.isEmpty(qdOrderTechVO.getHotSendDiv()) ? "H" : qdOrderTechVO.getHotSendDiv());
            pmContMscTech.setHotChargeFlag(Utils.getYesOrNo(qdOrderTechVO.getHotChargeFlag()));
            pmContMscTech.setGFlag(Utils.getYesOrNo(qdOrderTechVO.getGFlag()));
            pmContMscTech.setCsFlag(Utils.getYesOrNo(qdOrderTechVO.getCsFlag()));
            pmContMscTech.setDssFlag(Utils.getYesOrNo(qdOrderTechVO.getDssFlag()));
            pmContMscTech.setSsFlag(Utils.getYesOrNo(qdOrderTechVO.getSsFlag()));
            pmContMscTech.setDsFlag(Utils.getYesOrNo(qdOrderTechVO.getDsFlag()));
            pmContMscTech.setHeadCut(qdOrderTechVO.getHeadCut() != null ? qdOrderTechVO.getHeadCut() : BigDecimal.ZERO);
            pmContMscTech.setTailCut(qdOrderTechVO.getTailCut() != null ? qdOrderTechVO.getHeadCut() : BigDecimal.ZERO);
            pmContMscTech.setUltDetectMethodCode(qdOrderTechVO.getUtDetectMethodCode());
            pmContMscTech.setUltStgCode(qdOrderTechVO.getUtPdCode());
            pmContMscTech.setUltCode(qdOrderTechVO.getUtStdName());
            pmContMscTech.setCoolCode(qdOrderTechVO.getCoolCode());
            pmContMscTech.setCutModeCode(qdOrderTechVO.getCutModeCode());
            pmContMscTech.setHeatTreatMode(qdOrderTechVO.getHeatTreatMode());
            pmContMscTech.setWholeHeatTreatmentType(qdOrderTechVO.getWholeHeatTreatmentType());
            pmContMscTech.setSlabThkMin(qdOrderTechVO.getCompressMin());
            pmContMscTech.setSlabThkMax(qdOrderTechVO.getSlabThkMax() == null ? null : new BigDecimal(qdOrderTechVO.getSlabThkMax()));
//            pmContMscTech.setKeepTempAim(qdOrderTechVO.getke);
            pmContMscTechs.add(pmContMscTech);
        }
        try {
            BatchSqlHelper.open();
            BatchSqlHelper.size(1000);
            return this.batchInsertSelective(pmContMscTechs);
        } finally {
            BatchSqlHelper.clear();
        }
    }

}
