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

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

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.stereotype.Component;

import com.xisc.pm.domain.entity.PmPlateSpecBt;
import com.xisc.pm.domain.repository.PmPlateSpecBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmPlateSpecBtMapper;

import io.choerodon.core.exception.CommonException;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.mybatis.pagehelper.domain.Sort.Direction;

/**
 * 钢板轧制规格基准表(PmPlateSpecBt)资源库
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-09-05 14:45:53
 */
@Component
public class PmPlateSpecBtRepositoryImpl extends BaseRepositoryImpl<PmPlateSpecBt> implements PmPlateSpecBtRepository {

    @Resource
    private PmPlateSpecBtMapper pmPlateSpecBtMapper;

    @Override
    public List<PmPlateSpecBt> selectList(PmPlateSpecBt pmPlateSpecBt) {
        return pmPlateSpecBtMapper.selectList(pmPlateSpecBt);
    }

    @Override
    public PmPlateSpecBt selectByPrimary(Long pptabId) {
        PmPlateSpecBt pmPlateSpecBt = new PmPlateSpecBt();
        pmPlateSpecBt.setPptabId(pptabId);
        List<PmPlateSpecBt> pmPlateSpecBts = pmPlateSpecBtMapper.selectList(pmPlateSpecBt);
        if (CollectionUtils.isEmpty(pmPlateSpecBts)) {
            return null;
        }
        return pmPlateSpecBts.get(0);
    }

    /**
     * 钢板轧制规格基准表PM_PLATE_SPEC_BT，获取组合板长度上限COMBINE_PLATE_LEN_MAX，根据：  轧制模式ROLL_MODE(
     * * 匹配所有轧制模式) =
     * roll_mode，轴合同AD模式（若轴合同AD模式为‘D’，则匹配基表的AD_TYPE_MODE为‘D’，否则匹配基表AD_TYPE_MODE为‘ND’），
     * 组板方式，强度级别代码，轴合同全程热处理类型（根据轴合同号，产线号去合同工艺参数表PM_CONT_MSC_TECH获取全程热处理类型WHOLE_HEAT_TREATMENT_TYPE，
     * 若含Q，则匹配基表WHOLE_HEAT_TREATMENT_TYPE为‘Q’的记录，否则匹配基表WHOLE_HEAT_TREATMENT_TYPE为‘*’的值），轧制产线号;
     * 
     */
    @Override
    public PmPlateSpecBt selectLenAndWidthLimit(BigDecimal orderThick ,BigDecimal szWidth,String rollMode, String adModeCode, String formPlateMode, String stCode,
            String heatTreatMode, String rollLineNo, Long tenantId) {
        List<String> rollModeList = new ArrayList<String>();
        rollModeList.add(PmPlateConstants.ALL);
        if(rollMode != null) {
            rollModeList.add(rollMode);
        }
        
        List<String> adModeCodeList = new ArrayList<String>();
        adModeCodeList.add(PmPlateConstants.ALL);
        if(adModeCode != null) {
            adModeCodeList.add(adModeCode);
        }
        
        List<String> formPlateModeList = new ArrayList<String>();
        formPlateModeList.add(PmPlateConstants.ALL);
        if(formPlateMode != null) {
            formPlateModeList.add(formPlateMode);
        }
        
        List<String> stCodeList = new ArrayList<String>();
        stCodeList.add(PmPlateConstants.ALL);
        if(StringUtils.isNotBlank(stCode)) {
            stCodeList.add(stCode);
        }
        
        List<String> heatTreatModeList = new ArrayList<String>();
        if(StringUtils.isNotBlank(heatTreatMode) && heatTreatMode.contains("Q")) {
            heatTreatModeList.add("Q");
            heatTreatModeList.add(PmPlateConstants.ALL);
        } else {
            heatTreatModeList.add(PmPlateConstants.ALL);
        }
        
        List<PmPlateSpecBt> plateSpecBts = PageHelper.doSort(new Sort(Direction.DESC, PmPlateSpecBt.FIELD_ST_CODE , PmPlateSpecBt.FIELD_FORM_PLATE_MODE 
                ,PmPlateSpecBt.FIELD_AD_TYPE_MODE , PmPlateSpecBt.FIELD_ROLL_MODE , PmPlateSpecBt.FIELD_WHOLE_HEAT_TREATMENT_TYPE)
                , () -> this.selectByCondition(Condition.builder(PmPlateSpecBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmPlateSpecBt.FIELD_TENANT_ID, tenantId)
                        .andIn(PmPlateSpecBt.FIELD_ST_CODE, stCodeList)
                        .andIn(PmPlateSpecBt.FIELD_FORM_PLATE_MODE, formPlateModeList)
                        .andEqualTo(PmPlateSpecBt.FIELD_ROLL_LINE_NO, rollLineNo)
                        .andIn(PmPlateSpecBt.FIELD_WHOLE_HEAT_TREATMENT_TYPE, heatTreatModeList)
                        .andIn(PmPlateSpecBt.FIELD_AD_TYPE_MODE, adModeCodeList)
                        .andGreaterThanOrEqualTo(PmPlateSpecBt.FIELD_PLATE_THICK_TO, orderThick)
                        .andLessThanOrEqualTo(PmPlateSpecBt.FIELD_PLATE_THICK_FROM, orderThick)
                        .andGreaterThanOrEqualTo(PmPlateSpecBt.FIELD_COMBINE_PLATE_WIDTH_TO, szWidth)
                        .andLessThanOrEqualTo(PmPlateSpecBt.FIELD_COMBINE_PLATE_WIDTH_FROM, szWidth)
                        .andIn(PmPlateSpecBt.FIELD_ROLL_MODE, rollModeList))
                .build()));
        
        if(plateSpecBts == null || plateSpecBts.isEmpty()) {
            // 未配置到钢板轧制规格基准表！,强度代码【{0}】,组板方式【{1}】,AD类型【{2}】,轧制模式【{3}】,轴合同厚度【{4}】组合板宽度【{5}】,热处理模式【{6}】
            throw new CommonException("pm.pm_plate_spec_bt_001" , stCode , formPlateMode , adModeCode , rollMode , orderThick.toPlainString() ,szWidth.toPlainString() , heatTreatMode);
        }
        return plateSpecBts.get(0);
    }
    
    @Override
    public BigDecimal maxCombinePlateLenMax(Long tenantId) {
        return pmPlateSpecBtMapper.maxCombinePlateLenMax(tenantId);
    }

}
