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

import com.google.common.collect.Lists;
import com.xisc.pm.domain.entity.PmIngotThickAddvBt;
import com.xisc.pm.domain.entity.PmThickAddvBt;
import com.xisc.pm.domain.repository.PmIngotThickAddvBtRepository;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmIngotThickAddvBtMapper;
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.boot.platform.lov.adapter.LovAdapter;
import org.hzero.boot.platform.lov.dto.LovValueDTO;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 钢锭厚度余量基准表(PmIngotThickAddvBt)资源库
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-09-05 15:15:06
 */
@Component
public class PmIngotThickAddvBtRepositoryImpl extends BaseRepositoryImpl<PmIngotThickAddvBt>
        implements PmIngotThickAddvBtRepository {
    @Resource
    private PmIngotThickAddvBtMapper pmIngotThickAddvBtMapper;
    @Autowired
    private LovAdapter lovAdapter;

    @Override
    public List<PmIngotThickAddvBt> selectList(PmIngotThickAddvBt pmIngotThickAddvBt) {
        return pmIngotThickAddvBtMapper.selectList(pmIngotThickAddvBt);
    }

    @Override
    public PmIngotThickAddvBt selectByPrimary(Long pitabId) {
        PmIngotThickAddvBt pmIngotThickAddvBt = new PmIngotThickAddvBt();
        pmIngotThickAddvBt.setPitabId(pitabId);
        List<PmIngotThickAddvBt> pmIngotThickAddvBts = pmIngotThickAddvBtMapper.selectList(pmIngotThickAddvBt);
        if (CollectionUtils.isEmpty(pmIngotThickAddvBts)) {
            return null;
        }
        return pmIngotThickAddvBts.get(0);
    }

    @Override
    public PmIngotThickAddvBt selectTaimAddv(Long tenantId, String ingotType, String rollMode, String heatTreatMode,
            String rollDirectCode, BigDecimal orderThick, BigDecimal szWidth) {
        List<String> rollModeList = Lists.newArrayList(rollMode, 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<PmIngotThickAddvBt> pmIngotThickAddvBts = PageHelper.doSort(new Sort(Direction.DESC ,PmIngotThickAddvBt.FIELD_ROLL_MODE) ,()->this.selectByCondition(Condition.builder(PmIngotThickAddvBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmIngotThickAddvBt.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmIngotThickAddvBt.FIELD_ROLL_DIRECT_CODE, rollDirectCode)
                        .andEqualTo(PmIngotThickAddvBt.FIELD_INGOT_TYPE, ingotType)
                        .andIn(PmIngotThickAddvBt.FIELD_ROLL_MODE, rollModeList)
                        .andIn(PmIngotThickAddvBt.FIELD_HEAT_TREAT_MODE, 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))
                .build()));
        if(pmIngotThickAddvBts == null || pmIngotThickAddvBts.isEmpty()) {
            List<LovValueDTO> ingotTypeList = lovAdapter.queryLovValue(Constants.LovCode.INGOT_TYPE, tenantId);
            Map<String, String> ingotLovMap = new HashMap<>(ingotTypeList.size());
            if (CollectionUtils.isNotEmpty(ingotTypeList)) {
                ingotLovMap = ingotTypeList.stream().collect(Collectors.toMap(LovValueDTO::getValue, LovValueDTO::getMeaning));
            }
            // 未匹配到钢锭厚度余量基准表，锭坯类型代码【{0}】，轴合同热处理类型【{1}】，轴合同订货厚度【{2}】，组合板宽度【{3}】,设计产线号【{4}】,轧制方向【{5}】,轧制模式【{6}】
            throw new CommonException("pm.pm_ingot_thick_addv_bt_001" , ingotLovMap.getOrDefault(ingotType, ingotType), heatTreatMode ,orderThick.toPlainString() ,szWidth.toPlainString() , rollDirectCode , rollMode );
        }

        return pmIngotThickAddvBts.get(0);
    }

}
