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

import com.xisc.pm.domain.entity.PmIngotCutLenBt;
import com.xisc.pm.domain.repository.PmIngotCutLenBtRepository;
import com.xisc.pm.infra.mapper.PmIngotCutLenBtMapper;
import io.choerodon.core.exception.CommonException;
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;

/**
 * 钢锭切缝量基准表(PmIngotCutLenBt)资源库
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-09-05 15:18:17
 */
@Component
public class PmIngotCutLenBtRepositoryImpl extends BaseRepositoryImpl<PmIngotCutLenBt>
        implements PmIngotCutLenBtRepository {
    @Resource
    private PmIngotCutLenBtMapper pmIngotCutLenBtMapper;

    @Override
    public List<PmIngotCutLenBt> selectList(PmIngotCutLenBt pmIngotCutLenBt) {
        return pmIngotCutLenBtMapper.selectList(pmIngotCutLenBt);
    }

    @Override
    public PmIngotCutLenBt selectByPrimary(Long piclbId) {
        PmIngotCutLenBt pmIngotCutLenBt = new PmIngotCutLenBt();
        pmIngotCutLenBt.setPiclbId(piclbId);
        List<PmIngotCutLenBt> pmIngotCutLenBts = pmIngotCutLenBtMapper.selectList(pmIngotCutLenBt);
        if (CollectionUtils.isEmpty(pmIngotCutLenBts)) {
            return null;
        }
        return pmIngotCutLenBts.get(0);
    }

    @Override
    public PmIngotCutLenBt selectByOrderThick(BigDecimal orderThick, Long tenantId) {
        List<PmIngotCutLenBt> pmIngotCutLenBts = this.selectByCondition(Condition.builder(PmIngotCutLenBt.class)
                .andWhere(Sqls.custom().andGreaterThanOrEqualTo(PmIngotCutLenBt.FIELD_PLATE_THICK_TO, orderThick)
                        .andLessThanOrEqualTo(PmIngotCutLenBt.FIELD_PLATE_THICK_FROM, orderThick)
                        .andEqualTo(PmIngotCutLenBt.FIELD_TENANT_ID, tenantId))
                .build());
        if(pmIngotCutLenBts == null || pmIngotCutLenBts.isEmpty()) {
            // 未匹配到钢锭切缝量基准表数据，轴合同厚度【{0}】
            throw new CommonException("pm.pm_ingot_cut_len_bt_001", orderThick.toPlainString());
        }
        return pmIngotCutLenBts.get(0);
    }

}
