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

import com.xisc.pm.domain.entity.PmProCont;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.entity.PmSampleReserveBt;
import com.xisc.pm.domain.repository.PmProContProdTtrRepository;
import com.xisc.pm.domain.vo.QdOrderVO;
import com.xisc.pm.domain.vo.QdToLineupResultSteelVO;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.util.DateUtils;
import com.xisc.pm.infra.util.Utils;
import io.choerodon.core.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.hzero.core.base.BaseConstants;
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 java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 生产合同产品属性表 资源库实现
 *
 * @author 曾俊 jun.zeng@hand-china.com 2024-07-29 14:28:18
 */
@Component
public class PmProContProdTtrRepositoryImpl extends BaseRepositoryImpl<PmProContProdTtr>
        implements PmProContProdTtrRepository {

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

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

    @Override
    public List<PmProContProdTtr> createProContProdTtr(List<PmProCont> pmProConts, Map<String, QdOrderVO> poOrderMap , Set<String> diOrderSet,
                                                       Map<String, PmSampleReserveBt> pmSampleReserveBtMap,  Map<String, String> qdOrderTechMap) {
        if (pmProConts == null || pmProConts.isEmpty()) {
            return new ArrayList<PmProContProdTtr>(1);
        }
        List<PmProContProdTtr> pmProContProdTtrs = new ArrayList<PmProContProdTtr>(pmProConts.size());
        for (PmProCont pmProCont : pmProConts) {
            // 2. 调用质量管理模块“生产合同收池”接口，获取如下数据并在生产合同产品属性表
            QdOrderVO qdToPoOrder = poOrderMap.get(pmProCont.getOrderNo());
            if (qdToPoOrder == null) {
                // 合同跟踪表中未匹配到对应的合同数据
                throw new CommonException("pm.pm_pro_cont_prod_ttr_001", pmProCont.getOrderNo());
            }
            PmProContProdTtr pmProContProdTtr = new PmProContProdTtr();
            pmProContProdTtr.setTenantId(pmProCont.getTenantId());
            pmProContProdTtr.setProContId(pmProCont.getProContId());
            pmProContProdTtr.setOrderNo(pmProCont.getOrderNo());
            pmProContProdTtr.setOrderWt(qdToPoOrder.getOrderWt());
            pmProContProdTtr.setOrderQty(qdToPoOrder.getOrderQty());
            pmProContProdTtr.setDelivyNumTolPlus(qdToPoOrder.getDelivyNumTolPlus());
            pmProContProdTtr.setDelivyNumTolMinus(qdToPoOrder.getDelivyNumTolMinus());

            if (!qdOrderTechMap.containsKey(pmProCont.getOrderNo())) {
                throw new CommonException("pm.pro_cont_prod_attr_002", pmProCont.getOrderNo());
            }
            PmSampleReserveBt pmSampleReserveBt = null;
            // 获取钢种对应试样信息
            if (qdOrderTechMap.get(pmProCont.getOrderNo()) != null ) {
                if (pmSampleReserveBtMap.containsKey(qdOrderTechMap.get(pmProCont.getOrderNo()))) {
                    pmSampleReserveBt = pmSampleReserveBtMap.get(qdOrderTechMap.get(pmProCont.getOrderNo()));
                }
                if (pmSampleReserveBt == null && pmSampleReserveBtMap.containsKey("*")) {
                    pmSampleReserveBt = pmSampleReserveBtMap.get("*");
                }
                if (pmSampleReserveBt == null) {
                    throw new CommonException("pm.pro_cont_prod_attr_001", pmProCont.getOrderNo(),
                            qdOrderTechMap.get(pmProCont.getOrderNo()));
                }
                pmProContProdTtr.setSampleLenHead(pmSampleReserveBt.getSampleLenHead());
                pmProContProdTtr.setSampleLenTail(pmSampleReserveBt.getSampleLenTail());
            }
            if(qdToPoOrder.getOrderUnitAimWt() != null) {
                pmProContProdTtr.setOrderUnitAimWt(qdToPoOrder.getOrderUnitAimWt());
            } else {
                pmProContProdTtr.setOrderUnitAimWt(qdToPoOrder.getOrderWt().divide(qdToPoOrder.getOrderQty() , PmPlateConstants.WT_SCALE , RoundingMode.HALF_UP));
            }
            pmProContProdTtr.setOrderUnitMinWt(qdToPoOrder.getOrderUnitMinWt());
            pmProContProdTtr.setOrderUnitMaxWt(qdToPoOrder.getOrderUnitMaxWt());
            pmProContProdTtr.setOrderTypeCode(qdToPoOrder.getOrderTypeCode());
            pmProContProdTtr.setUrgOrderFlag(Utils.getYesOrNo(qdToPoOrder.getUrgOrderFlag()));
            pmProContProdTtr.setDelivyDate(DateUtils.parseDate(qdToPoOrder.getDelivyDate(), DateUtils.YMD));
            pmProContProdTtr.setExportFlag(Utils.getYesOrNo(qdToPoOrder.getExportFlag()));
            pmProContProdTtr.setPsc(qdToPoOrder.getPsc());
            pmProContProdTtr.setMsc(qdToPoOrder.getMsc());
            pmProContProdTtr.setApn(qdToPoOrder.getApn());
            pmProContProdTtr.setSgStd(qdToPoOrder.getSgStd());
            pmProContProdTtr.setSgSign(qdToPoOrder.getSgSign());
            pmProContProdTtr.setSgCode(qdToPoOrder.getSgCode());
            pmProContProdTtr.setNewTestNo(qdToPoOrder.getNewTestNo());
            pmProContProdTtr.setOrderThick(qdToPoOrder.getOrderThick());
            pmProContProdTtr.setOrderWidth(qdToPoOrder.getOrderWidth());
            pmProContProdTtr.setFinCustCode(qdToPoOrder.getFinCustCode());
            pmProContProdTtr.setOrderMaxLen(qdToPoOrder.getOrderMaxLen());
            pmProContProdTtr.setOrderMinLen(qdToPoOrder.getOrderMinLen());
            pmProContProdTtr.setOrderShortMax(qdToPoOrder.getOrderShortMax());
            pmProContProdTtr.setOrderShortMin(qdToPoOrder.getOrderShortMin());
            pmProContProdTtr.setOrderShortRate(qdToPoOrder.getOrderShortRate());
            pmProContProdTtr.setOrderNoErp(qdToPoOrder.getOrderNoErp());
            pmProContProdTtr.setOrderCustCname(qdToPoOrder.getOrderCustCname());
            pmProContProdTtr.setConsignCustCname(qdToPoOrder.getConsignCustCname());
            pmProContProdTtr.setDelivyPlaceName(qdToPoOrder.getDelivyPlaceName());
            pmProContProdTtr.setTrnpModeCode(qdToPoOrder.getTrnpModeCode());
            pmProContProdTtr.setPlanerFlag(qdToPoOrder.getPlanerFlag());
            if(BaseConstants.Flag.YES.toString().equals(qdToPoOrder.getGiveSampleCode())) {
                pmProContProdTtr.setXzFlag(1L);
            } else {
                pmProContProdTtr.setXzFlag(0L);
            }
            
            pmProContProdTtr.setOrderLen(qdToPoOrder.getOrderLen());
            pmProContProdTtr.setWidthDiv(Utils.getYesOrNo(qdToPoOrder.getWidthDiv()));
            pmProContProdTtr.setWidthMax(qdToPoOrder.getWidthMax());
            pmProContProdTtr.setWidthMin(qdToPoOrder.getWidthMin());
            pmProContProdTtr.setLenDiv(Utils.getYesOrNo(qdToPoOrder.getLenDiv()));
            pmProContProdTtr.setProdCodeHp(qdToPoOrder.getProdCodeHp());
            pmProContProdTtr.setProjectCode(qdToPoOrder.getProjectCode());
            pmProContProdTtr.setThickTolMax(qdToPoOrder.getThickTolMax());
            pmProContProdTtr.setThickTolMin(qdToPoOrder.getThickTolMin());
            pmProContProdTtr.setWidthTolMax(qdToPoOrder.getWidthTolMax());
            pmProContProdTtr.setWidthTolMin(qdToPoOrder.getWidthTolMin());
            pmProContProdTtr.setLenTolMax(qdToPoOrder.getLenTolMax());
            pmProContProdTtr.setLenTolMin(qdToPoOrder.getLenTolMin());
            pmProContProdTtr.setSampleLenHead(qdToPoOrder.getSampleLenHead() != null ? qdToPoOrder.getSampleLenHead() : BigDecimal.valueOf(0));
            pmProContProdTtr.setSampleLenTail(qdToPoOrder.getSampleLenTail() != null ? qdToPoOrder.getSampleLenTail() : BigDecimal.valueOf(0));
            pmProContProdTtr.setWidthAddv(qdToPoOrder.getWidthAddv() != null ? qdToPoOrder.getWidthAddv() : BigDecimal.ZERO);
            pmProContProdTtr.setShpCode(qdToPoOrder.getShpCode());
            pmProContProdTtr.setShotFlag(Utils.getYesOrNo(qdToPoOrder.getShotFlag()));
            pmProContProdTtr.setHullSectionNo(qdToPoOrder.getHullSectionNo());
            pmProContProdTtr.setSectionManage(qdToPoOrder.getSectionManage());
            pmProContProdTtr.setTwoRollFlag(qdToPoOrder.getTwoRollMark());
            pmProContProdTtr.setTwoRollOrderFlag(qdToPoOrder.getTwoRollOrderMark());
            pmProContProdTtr.setSampleModeCode(qdToPoOrder.getSampleModeCode() != null ? qdToPoOrder.getSampleModeCode() : "B");
            pmProContProdTtr.setPickPatternCode(qdToPoOrder.getPickPatternCode() != null ? qdToPoOrder.getPickPatternCode() : "P");
            pmProContProdTtr.setAccpAuthCode(qdToPoOrder.getAccpAuthCode());
            pmProContProdTtr.setAccptModeCode(qdToPoOrder.getAccptModeCode());
            pmProContProdTtr.setCheckMethodCode(qdToPoOrder.getCheckMethodCode());
            if (StringUtils.isNotBlank(qdToPoOrder.getCheckMethodCode())
                    && (qdToPoOrder.getCheckMethodCode().charAt(0) == 'B'
                            || qdToPoOrder.getCheckMethodCode().charAt(0) == 'C'
                            || qdToPoOrder.getCheckMethodCode().charAt(0) == 'D')) {
                pmProContProdTtr.setSampleCheckFlag(BaseConstants.Flag.YES.longValue());
            } else {
                pmProContProdTtr.setSampleCheckFlag(BaseConstants.Flag.NO.longValue());
            }

            pmProContProdTtr.setProdSrMark(qdToPoOrder.getProdSrMark());
            
            if(StringUtils.isBlank(qdToPoOrder.getTrimCode()) || BaseConstants.Flag.NO.toString().equals(qdToPoOrder.getTrimCode())) {
                pmProContProdTtr.setProdSideMark("M");
            } else {
                pmProContProdTtr.setProdSideMark("C");
            }
            
            pmProContProdTtr.setProdShotMark(qdToPoOrder.getProdShotMark());
            pmProContProdTtr.setProdNMark(qdToPoOrder.getProdNMark());
            pmProContProdTtr.setProdQMark(qdToPoOrder.getProdQMark());
            pmProContProdTtr.setProdTMark(qdToPoOrder.getProdTMark());
            pmProContProdTtr.setProdAMark(qdToPoOrder.getProdAMark());
            pmProContProdTtr.setBacklogUtMark(qdToPoOrder.getBacklogUtMark());
            pmProContProdTtr.setBacklogNMark(qdToPoOrder.getBacklogNMark());
            pmProContProdTtr.setBacklogQMark(qdToPoOrder.getBacklogQMark());
            pmProContProdTtr.setBacklogTMark(qdToPoOrder.getBacklogTMark());
            pmProContProdTtr.setBacklogAMark(qdToPoOrder.getBacklogAMark());
            pmProContProdTtr.setBacklogDemagMark(qdToPoOrder.getBacklogDemagMark());
            pmProContProdTtr.setBacklogPtMark(qdToPoOrder.getBacklogPtMark());
            pmProContProdTtr.setBacklogMtMark(qdToPoOrder.getBacklogMtMark());
            pmProContProdTtr.setBacklogTmcpMark(qdToPoOrder.getBacklogTmcpMark());
            pmProContProdTtr.setNeedLecquering(qdToPoOrder.getNeedLecquering());
            if(StringUtils.isBlank(qdToPoOrder.getShotPaintCode())) {
                pmProContProdTtr.setShotPaintCode(BaseConstants.Flag.NO.toString());
            } else {
                pmProContProdTtr.setShotPaintCode(String.valueOf(qdToPoOrder.getShotPaintCode().charAt(0)));
            }
            
            pmProContProdTtr.setGiveSampleCode(qdToPoOrder.getGiveSampleCode());
            pmProContProdTtr.setSampleLenGive(qdToPoOrder.getSampleLenGive());
            pmProContProdTtr.setTrimCode(qdToPoOrder.getTrimCode());
            pmProContProdTtr.setTsMax(qdToPoOrder.getTsMax());
            pmProContProdTtr.setTsMin(qdToPoOrder.getTsMin());
            pmProContProdTtr.setProdDensity(qdToPoOrder.getProdDensity());
            pmProContProdTtr.setDiFlag(diOrderSet.contains(pmProCont.getOrderNo()) ? BaseConstants.FlagChar.YES : BaseConstants.FlagChar.NO);
            pmProContProdTtr.setIngotRoll("1".equals(qdToPoOrder.getIngotRoll()) ? qdToPoOrder.getIngotRoll() : "0");
            pmProContProdTtrs.add(pmProContProdTtr);
        }
        try {
            BatchSqlHelper.open();
            BatchSqlHelper.size(1000);
            return this.batchInsertSelective(pmProContProdTtrs);
        } finally {
            BatchSqlHelper.clear();
        }
    }

    @Override
    public List<QdToLineupResultSteelVO> selectSlabPlaceCode(List<String> mscLineNoList, List<String> orderNoList, Long tenantId) {
        return Collections.emptyList();
    }
}
