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.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.PmSlabScrapLenBt;
import com.xisc.pm.domain.repository.PmSlabScrapLenBtRepository;
import com.xisc.pm.infra.mapper.PmSlabScrapLenBtMapper;
import com.xisc.pm.infra.util.Utils;

/**
 * 余坯组板采用判定基准表(PmSlabScrapLenBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-23 14:28:34
 */
@Component
public class PmSlabScrapLenBtRepositoryImpl extends BaseRepositoryImpl<PmSlabScrapLenBt>
        implements PmSlabScrapLenBtRepository {
    @Resource
    private PmSlabScrapLenBtMapper pmSlabScrapLenBtMapper;

    @Override
    public List<PmSlabScrapLenBt> selectList(PmSlabScrapLenBt pmSlabScrapLenBt) {
        return pmSlabScrapLenBtMapper.selectList(pmSlabScrapLenBt);
    }

    @Override
    public PmSlabScrapLenBt selectByPrimary(Long psslbId) {
        PmSlabScrapLenBt pmSlabScrapLenBt = new PmSlabScrapLenBt();
        pmSlabScrapLenBt.setPsslbId(psslbId);
        List<PmSlabScrapLenBt> pmSlabScrapLenBts = pmSlabScrapLenBtMapper.selectList(pmSlabScrapLenBt);
        if (CollectionUtils.isEmpty(pmSlabScrapLenBts)) {
            return null;
        }
        return pmSlabScrapLenBts.get(0);
    }

    @Override
    public PmSlabScrapLenBt selectCanScrapLen(Long tenantId, BigDecimal remainSlabLen, BigDecimal moldThick,
            BigDecimal moldWidth, String mscLineNo) {
        List<PmSlabScrapLenBt> pmSlabScrapLenBts = this.selectByCondition(Condition.builder(PmSlabScrapLenBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmSlabScrapLenBt.FIELD_TENANT_ID, tenantId)
                        .andGreaterThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_THICK_TO, moldThick)
                        .andLessThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_THICK_FROM, moldThick)
                        .andGreaterThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_WIDTH_TO, moldWidth)
                        .andLessThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_WIDTH_FROM, moldWidth)
                        .andGreaterThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_MAX_LEN, remainSlabLen)
                        .andLessThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_MIN_LEN, remainSlabLen)
                        .andEqualTo(PmSlabScrapLenBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(mscLineNo))).build());
        if(pmSlabScrapLenBts == null || pmSlabScrapLenBts.isEmpty()) {
            return null;
        }
        return pmSlabScrapLenBts.get(0);
    }
    
    @Override
    public List<PmSlabScrapLenBt> selectCanScrapLen(Long tenantId, BigDecimal moldThick,
            BigDecimal moldWidth, String mscLineNo) {
        List<PmSlabScrapLenBt> pmSlabScrapLenBts =  this.selectByCondition(Condition.builder(PmSlabScrapLenBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmSlabScrapLenBt.FIELD_TENANT_ID, tenantId)
                        .andGreaterThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_THICK_TO, moldThick)
                        .andLessThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_THICK_FROM, moldThick)
                        .andGreaterThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_WIDTH_TO, moldWidth)
                        .andLessThanOrEqualTo(PmSlabScrapLenBt.FIELD_SLAB_WIDTH_FROM, moldWidth)
                        .andEqualTo(PmSlabScrapLenBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(mscLineNo))).build());
        if(pmSlabScrapLenBts == null) {
            return new ArrayList<PmSlabScrapLenBt>();
        }
        return pmSlabScrapLenBts;
    }
}
