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

import com.xisc.pm.domain.entity.PmIngotSpecTrimBt;
import com.xisc.pm.domain.repository.PmIngotSpecTrimBtRepository;
import com.xisc.pm.infra.mapper.PmIngotSpecTrimBtMapper;
import com.xisc.pm.infra.util.Utils;
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 javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 钢锭轧制规格及切边量基准表(PmIgnotSpecTrimBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-24 15:01:54
 */
@Component
public class PmIngotSpecTrimBtRepositoryImpl extends BaseRepositoryImpl<PmIngotSpecTrimBt>
        implements PmIngotSpecTrimBtRepository {
    @Resource
    private PmIngotSpecTrimBtMapper pmIngotSpecTrimBtMapper;

    @Override
    public List<PmIngotSpecTrimBt> selectList(PmIngotSpecTrimBt pmIngotSpecTrimBt) {
        return pmIngotSpecTrimBtMapper.selectList(pmIngotSpecTrimBt);
    }

    @Override
    public PmIngotSpecTrimBt selectByPrimary(Long pistbId) {
        PmIngotSpecTrimBt pmIngotSpecTrimBt = new PmIngotSpecTrimBt();
        pmIngotSpecTrimBt.setPistbId(pistbId);
        List<PmIngotSpecTrimBt> pmIngotSpecTrimBts = pmIngotSpecTrimBtMapper.selectList(pmIngotSpecTrimBt);
        if (CollectionUtils.isEmpty(pmIngotSpecTrimBts)) {
            return null;
        }
        return pmIngotSpecTrimBts.get(0);
    }

    @Override
    public List<PmIngotSpecTrimBt> selectBy(Long tenantId,String rollDirectCode , String prodSideMark, BigDecimal orderThick,
            BigDecimal szWidth, String mscLineNo) {
        List<PmIngotSpecTrimBt> pmIngotSpecTrimBts = this.selectByCondition(Condition.builder(PmIngotSpecTrimBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmIngotSpecTrimBt.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmIngotSpecTrimBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(mscLineNo))
                        .andEqualTo(PmIngotSpecTrimBt.FIELD_ROLL_DIRECT_CODE, rollDirectCode)
                        .andGreaterThanOrEqualTo(PmIngotSpecTrimBt.FIELD_MAX_ROLL_THICK, orderThick)
                        .andLessThanOrEqualTo(PmIngotSpecTrimBt.FIELD_MIN_ROLL_THICK, orderThick)
                        .andGreaterThanOrEqualTo(PmIngotSpecTrimBt.FIELD_MAX_ROLL_WIDTH, szWidth)
                        .andLessThanOrEqualTo(PmIngotSpecTrimBt.FIELD_MIN_ROLL_WIDTH, szWidth)
                        .andEqualTo(PmIngotSpecTrimBt.FIELD_WITH_SIDE_FLAG, prodSideMark))
                .build());
        return pmIngotSpecTrimBts;
    }
    
    @Override
    public PmIngotSpecTrimBt selectBy(Long tenantId, String ingotType, String guaranteeQuality, String mscLineNo) {
        PmIngotSpecTrimBt record = new PmIngotSpecTrimBt();
        record.setRollLineNo(Utils.mscLineNoToRoolLineNo(mscLineNo));
        record.setGuaranteeQuality(guaranteeQuality);
        record.setIngotType(ingotType);
        record.setTenantId(tenantId);
        return this.selectOne(record );
    }
}
