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

import com.xisc.pm.api.dto.PmProContYieldRequestDTO;
import com.xisc.pm.domain.entity.PmContMscTech;
import com.xisc.pm.domain.entity.PmPlateCutLenBt;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.repository.PmPlateCutLenBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmPlateCutLenBtMapper;
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;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 钢板切缝切头尾范围基准表(PmPlateCutLenBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-24 09:26:44
 */
@Component
public class PmPlateCutLenBtRepositoryImpl extends BaseRepositoryImpl<PmPlateCutLenBt>
        implements PmPlateCutLenBtRepository {
    @Resource
    private PmPlateCutLenBtMapper pmPlateCutLenBtMapper;

    @Override
    public List<PmPlateCutLenBt> selectList(PmPlateCutLenBt pmPlateCutLenBt) {
        return pmPlateCutLenBtMapper.selectList(pmPlateCutLenBt);
    }

    @Override
    public PmPlateCutLenBt selectByPrimary(Long ppclbId) {
        PmPlateCutLenBt pmPlateCutLenBt = new PmPlateCutLenBt();
        pmPlateCutLenBt.setPpclbId(ppclbId);
        List<PmPlateCutLenBt> pmPlateCutLenBts = pmPlateCutLenBtMapper.selectList(pmPlateCutLenBt);
        if (CollectionUtils.isEmpty(pmPlateCutLenBts)) {
            return null;
        }
        return pmPlateCutLenBts.get(0);
    }

    @Override
    public PmPlateCutLenBt selectPmPlateCutLenBt(PmProContYieldRequestDTO pmProContYield,
            PmProContProdTtr axisPmProContProdTtr, PmContMscTech axisPmContMscTech, String broadFlag,
            String mscLineNo) {
        List<String> rollModeList = new ArrayList<String>();
        if(StringUtils.isNotBlank(axisPmContMscTech.getRollMode())) {
            rollModeList.add(axisPmContMscTech.getRollMode());
        }
        rollModeList.add(PmPlateConstants.ALL);
        
        List<PmPlateCutLenBt> pmPlateCutLenBts = PageHelper.doSort(new Sort(Direction.DESC , PmPlateCutLenBt.FIELD_ROLL_MODE),()->this.selectByCondition(Condition.builder(PmPlateCutLenBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmPlateCutLenBt.FIELD_TENANT_ID, pmProContYield.getTenantId())
                        .andIn(PmPlateCutLenBt.FIELD_ROLL_MODE, rollModeList)
                        .andEqualTo(PmPlateCutLenBt.FIELD_ROLL_DIRECT_CODE, axisPmContMscTech.getRollDirectCode())
                        .andEqualTo(PmPlateCutLenBt.FIELD_BRR_NEED, broadFlag)
                        .andGreaterThanOrEqualTo(PmPlateCutLenBt.FIELD_COMBINE_PLATE_WIDTH_TO,
                                pmProContYield.getCompositeBoard().getSzWidth())
                        .andLessThanOrEqualTo(PmPlateCutLenBt.FIELD_COMBINE_PLATE_WIDTH_FROM,
                                pmProContYield.getCompositeBoard().getSzWidth())
                        .andGreaterThanOrEqualTo(PmPlateCutLenBt.FIELD_PLATE_THICK_TO,
                                axisPmProContProdTtr.getOrderThick())
                        .andLessThanOrEqualTo(PmPlateCutLenBt.FIELD_PLATE_THICK_FROM,
                                axisPmProContProdTtr.getOrderThick())
                        .andGreaterThanOrEqualTo(PmPlateCutLenBt.FIELD_SLAB_THICK_TO,
                                pmProContYield.getPmContFilterRequest().getMoldThick())
                        .andLessThanOrEqualTo(PmPlateCutLenBt.FIELD_SLAB_THICK_FROM,
                                pmProContYield.getPmContFilterRequest().getMoldThick())
                        .andEqualTo(PmPlateCutLenBt.FIELD_MSC_LINE_NO, mscLineNo))
                .build()));
        if (CollectionUtils.isEmpty(pmPlateCutLenBts)) {
            // 未获取到钢板切缝切头尾范围基准表数据，轧制模式【{0}】，轧制方向【{1}】，是否展宽【{2}】，组合板宽，轴合同厚【{3}】，结晶器厚【{4}】
            throw new CommonException("pm.pm_pro_cont_023",rollModeList.get(0) , axisPmContMscTech.getRollDirectCode() , broadFlag 
                    ,pmProContYield.getCompositeBoard().getSzWidth().toPlainString() , axisPmProContProdTtr.getOrderThick().toPlainString() 
                    , pmProContYield.getPmContFilterRequest().getMoldThick().toPlainString(), mscLineNo);
        }
        return pmPlateCutLenBts.get(0);
    }
    
    @Override
    public List<PmPlateCutLenBt> selectPmPlateCutLenForPlate(PmProContYieldRequestDTO pmProContYield,
            PmProContProdTtr axisPmProContProdTtr, PmContMscTech axisPmContMscTech, String broadFlag,
            String mscLineNo) {
        List<String> rollModeList = new ArrayList<String>();
        if(StringUtils.isNotBlank(axisPmContMscTech.getRollMode())) {
            rollModeList.add(axisPmContMscTech.getRollMode());
        }
        rollModeList.add(PmPlateConstants.ALL);
        
        List<PmPlateCutLenBt> pmPlateCutLenBts = PageHelper.doSort(new Sort(Direction.DESC , PmPlateCutLenBt.FIELD_ROLL_MODE),()->this.selectByCondition(Condition.builder(PmPlateCutLenBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmPlateCutLenBt.FIELD_TENANT_ID, pmProContYield.getTenantId())
                        .andIn(PmPlateCutLenBt.FIELD_ROLL_MODE, rollModeList)
                        .andEqualTo(PmPlateCutLenBt.FIELD_ROLL_DIRECT_CODE, axisPmContMscTech.getRollDirectCode())
                        .andEqualTo(PmPlateCutLenBt.FIELD_BRR_NEED, broadFlag)
                        .andGreaterThanOrEqualTo(PmPlateCutLenBt.FIELD_COMBINE_PLATE_WIDTH_TO,
                                pmProContYield.getCompositeBoard().getSzWidth())
                        .andLessThanOrEqualTo(PmPlateCutLenBt.FIELD_COMBINE_PLATE_WIDTH_FROM,
                                pmProContYield.getCompositeBoard().getSzWidth())
                        .andGreaterThanOrEqualTo(PmPlateCutLenBt.FIELD_PLATE_THICK_TO,
                                axisPmProContProdTtr.getOrderThick())
                        .andLessThanOrEqualTo(PmPlateCutLenBt.FIELD_PLATE_THICK_FROM,
                                axisPmProContProdTtr.getOrderThick())
                        .andEqualTo(PmPlateCutLenBt.FIELD_MSC_LINE_NO, mscLineNo))
                .build()));
        if (CollectionUtils.isEmpty(pmPlateCutLenBts)) {
            // 未获取到钢板切缝切头尾范围基准表数据，轧制模式【{0}】，轧制方向【{1}】，是否展宽【{2}】，组合板宽，轴合同厚【{3}】，结晶器厚【{4}】
            throw new CommonException("pm.pm_pro_cont_023",rollModeList.get(0) , axisPmContMscTech.getRollDirectCode() , broadFlag 
                    ,pmProContYield.getCompositeBoard().getSzWidth().toPlainString() , axisPmProContProdTtr.getOrderThick().toPlainString() 
                    , pmProContYield.getPmContFilterRequest().getMoldThick().toPlainString(), mscLineNo);
        }
        return pmPlateCutLenBts;
    }

}
