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

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.PmMinThickTolBt;
import com.xisc.pm.domain.repository.PmMinThickTolBtRepository;
import com.xisc.pm.infra.mapper.PmMinThickTolBtMapper;

import javax.annotation.Resource;

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

/**
 * 最小轧制厚度公差范围基准表(PmMinThickTolBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-23 11:11:38
 */
@Component
public class PmMinThickTolBtRepositoryImpl extends BaseRepositoryImpl<PmMinThickTolBt>
        implements PmMinThickTolBtRepository {
    @Resource
    private PmMinThickTolBtMapper pmMinThickTolBtMapper;

    @Override
    public List<PmMinThickTolBt> selectList(PmMinThickTolBt pmMinThickTolBt) {
        return pmMinThickTolBtMapper.selectList(pmMinThickTolBt);
    }

    @Override
    public PmMinThickTolBt selectByPrimary(Long pmttbId) {
        PmMinThickTolBt pmMinThickTolBt = new PmMinThickTolBt();
        pmMinThickTolBt.setPmttbId(pmttbId);
        List<PmMinThickTolBt> pmMinThickTolBts = pmMinThickTolBtMapper.selectList(pmMinThickTolBt);
        if (CollectionUtils.isEmpty(pmMinThickTolBts)) {
            return null;
        }
        return pmMinThickTolBts.get(0);
    }

    @Override
    public PmMinThickTolBt selectByRollLineNoAndAxisOrderThick(Long tennantId, String rollLineNo,
            BigDecimal orderThick) {
        List<PmMinThickTolBt> pmMinThickTolBts = this.selectByCondition(Condition.builder(PmMinThickTolBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmMinThickTolBt.FIELD_ROLL_LINE_NO, rollLineNo)
                        .andGreaterThanOrEqualTo(PmMinThickTolBt.FIELD_PLATE_THICK_TO, orderThick)
                        .andLessThanOrEqualTo(PmMinThickTolBt.FIELD_PLATE_THICK_FROM, orderThick)
                        .andEqualTo(PmMinThickTolBt.FIELD_TENANT_ID, tennantId))
                .orderByAsc(PmMinThickTolBt.FIELD_MIN_THICK_TOL).build());
        if (pmMinThickTolBts == null || pmMinThickTolBts.isEmpty()) {
            return null;
        }
        return pmMinThickTolBts.get(0);
    }

}
