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

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

import javax.annotation.Resource;

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.PmPdSlabInfo;
import com.xisc.pm.domain.entity.PmSlabSpecBt;
import com.xisc.pm.domain.repository.PmSlabSpecBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmSlabSpecBtMapper;
import com.xisc.pm.infra.util.Utils;

/**
 * 连铸板坯生产规格基准表(PmSlabSpecBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-23 08:24:44
 */
@Component
public class PmSlabSpecBtRepositoryImpl extends BaseRepositoryImpl<PmSlabSpecBt> implements PmSlabSpecBtRepository {
    @Resource
    private PmSlabSpecBtMapper pmSlabSpecBtMapper;

    @Override
    public List<PmSlabSpecBt> selectList(PmSlabSpecBt pmSlabSpecBt) {
        return pmSlabSpecBtMapper.selectList(pmSlabSpecBt);
    }

    @Override
    public List<PmSlabSpecBt> selectByLineNoAndCcType(Long tenantId, String rollLineNo, String ccType,
            String... orderBy) {
        return this.selectByCondition(Condition.builder(PmSlabSpecBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmSlabSpecBt.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmSlabSpecBt.FIELD_ROLL_LINE_NO, rollLineNo)
                        .andEqualTo(PmSlabSpecBt.FIELD_CC_TYPE, ccType))
                .orderByDesc(orderBy).build());
    }

    @Override
    public List<PmSlabSpecBt> selectBy(Long tenantId, String rollLineNo, String ccType, String furType,
            BigDecimal thick, BigDecimal width) {
        List<PmSlabSpecBt> pmSlabSpecBts = this.selectByCondition(Condition.builder(PmSlabSpecBt.class).andWhere(Sqls
                .custom().andEqualTo(PmSlabSpecBt.FIELD_TENANT_ID, tenantId)
                .andEqualTo(PmSlabSpecBt.FIELD_ROLL_LINE_NO, rollLineNo)
                .andEqualTo(PmSlabSpecBt.FIELD_FUR_TYPE, furType , true)
                        .andEqualTo(PmSlabSpecBt.FIELD_CC_TYPE, ccType , true)
                .andGreaterThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_WIDTH_TO, width)
                .andLessThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_WIDTH_FROM, width)
                .andGreaterThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_THICK_TO, thick)
                .andLessThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_THICK_FROM, thick)).build());
        return pmSlabSpecBts;
    }

    @Override
    public PmSlabSpecBt selectMinSlabLenByPmPdSlabInfo(PmPdSlabInfo pmPdSlabInfo) {
        List<PmSlabSpecBt> pmSlabSpecBts = this.selectByCondition(Condition.builder(PmSlabSpecBt.class).andWhere(Sqls
                .custom().andEqualTo(PmSlabSpecBt.FIELD_TENANT_ID, pmPdSlabInfo.getTenantId())
                .andEqualTo(PmSlabSpecBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(pmPdSlabInfo.getMscLineNo()))
                .andEqualTo(PmSlabSpecBt.FIELD_FUR_TYPE, PmPlateConstants.FUR_TYPR_C)
//                        .andEqualTo(PmSlabSpecBt.FIELD_CC_TYPE, pmPdSlabInfo.getCcType())
                .andGreaterThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_WIDTH_TO, pmPdSlabInfo.getMatActWidth())
                .andLessThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_WIDTH_FROM, pmPdSlabInfo.getMatActWidth())
                .andGreaterThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_THICK_TO, pmPdSlabInfo.getMatActThick())
                .andLessThanOrEqualTo(PmSlabSpecBt.FIELD_SLAB_THICK_FROM, pmPdSlabInfo.getMatActThick())).build());
        if (pmSlabSpecBts == null || pmSlabSpecBts.isEmpty()) {
            return null;
        }
        PmSlabSpecBt minLenConfig = pmSlabSpecBts.get(0);
        for (PmSlabSpecBt pmSlabSpecBt : pmSlabSpecBts) {
            if (minLenConfig.getSlabMinLen().compareTo(pmSlabSpecBt.getSlabMinLen()) >= 0) {
                minLenConfig = pmSlabSpecBt;
            }
        }
        return minLenConfig;
    }
}
