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.PmThickAddvBt;
import com.xisc.pm.domain.repository.PmThickAddvBtRepository;
import com.xisc.pm.domain.vo.QdToPoOrderVO1;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmThickAddvBtMapper;
import com.xisc.pm.infra.mapper.QdToPoOrderMapper;

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;

/**
 * 钢板厚度余量基准表(PmThickAddvBt)资源库
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-09-05 15:04:57
 */
@Component
public class PmThickAddvBtRepositoryImpl extends BaseRepositoryImpl<PmThickAddvBt> implements PmThickAddvBtRepository {
    @Resource
    private PmThickAddvBtMapper pmThickAddvBtMapper;
    
    @Resource
    private QdToPoOrderMapper qdToPoOrderMapper;

    @Override
    public List<PmThickAddvBt> selectList(PmThickAddvBt pmThickAddvBt) {
        return pmThickAddvBtMapper.selectList(pmThickAddvBt);
    }

    @Override
    public PmThickAddvBt selectByPrimary(Long pptabId) {
        PmThickAddvBt pmThickAddvBt = new PmThickAddvBt();
        pmThickAddvBt.setPptabId(pptabId);
        List<PmThickAddvBt> pmThickAddvBts = pmThickAddvBtMapper.selectList(pmThickAddvBt);
        if (CollectionUtils.isEmpty(pmThickAddvBts)) {
            return null;
        }
        return pmThickAddvBts.get(0);
    }
    
    @Override
    public PmThickAddvBt selectTaimAddv(Long tenantId, String axisOrderNo,
            String heatTreatMode, String mscLineNo, BigDecimal orderThick, BigDecimal szWidth) {
        QdToPoOrderVO1 qdToPoOrderVO = new QdToPoOrderVO1();
        qdToPoOrderVO.setTenantId(tenantId);
        qdToPoOrderVO.setOrderNo(axisOrderNo);
        List<String> thkTolTypeList = new ArrayList<String>();
        List<QdToPoOrderVO1> qdToPoOrderVOs = qdToPoOrderMapper.selectList(qdToPoOrderVO);
        if(qdToPoOrderVOs == null || qdToPoOrderVOs.isEmpty() || StringUtils.isBlank(qdToPoOrderVOs.get(0).getThkTolType())) {
            thkTolTypeList.add(PmPlateConstants.ALL);
        } else {
            thkTolTypeList.add(qdToPoOrderVOs.get(0).getThkTolType());
            thkTolTypeList.add(PmPlateConstants.ALL);
        }
        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<PmThickAddvBt> pmThickAddvBts = PageHelper.doSort(new Sort(Direction.DESC, PmThickAddvBt.FIELD_TOL_TYPE_CODE ,PmThickAddvBt.FIELD_WHOLE_HEAT_TREATMENT_TYPE), ()->this
                .selectByCondition(Condition.builder(PmThickAddvBt.class)
                        .andWhere(Sqls.custom().andEqualTo(PmThickAddvBt.FIELD_TENANT_ID, tenantId)
                                .andIn(PmThickAddvBt.FIELD_TOL_TYPE_CODE, thkTolTypeList)
                                .andIn(PmThickAddvBt.FIELD_WHOLE_HEAT_TREATMENT_TYPE, heatTreatModeList)
                                .andGreaterThanOrEqualTo(PmThickAddvBt.FIELD_PLATE_THICK_TO, orderThick)
                                .andLessThanOrEqualTo(PmThickAddvBt.FIELD_PLATE_THICK_FROM, orderThick)
                                .andGreaterThanOrEqualTo(PmThickAddvBt.FIELD_COMBINE_PLATE_WIDTH_TO,szWidth)
                                .andLessThanOrEqualTo(PmThickAddvBt.FIELD_COMBINE_PLATE_WIDTH_FROM, szWidth)
                                .andEqualTo(PmThickAddvBt.FIELD_MSC_LINE_NO, mscLineNo))
                        .build()));
        if(pmThickAddvBts != null && !pmThickAddvBts.isEmpty()) {
            return pmThickAddvBts.get(0);
        } else {
            // 未匹配到厚度余量基准表数据，公差类型代码【{0}】，轴合同热处理类型【{1}】，轴合同订货厚度【{2}】，组合板宽度【{3}】,设计产线号【{4}】
            throw new CommonException("pm.pm_pro_cont_021" , thkTolTypeList.get(0) , heatTreatMode , orderThick.toPlainString() ,szWidth.toPlainString() , mscLineNo );
        }
    }

}
