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

import com.xisc.pm.domain.entity.PmIngotYieldBt;
import com.xisc.pm.domain.repository.PmIngotYieldBtRepository;
import com.xisc.pm.infra.mapper.PmIngotYieldBtMapper;

import io.choerodon.core.exception.CommonException;

import org.apache.commons.collections.CollectionUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 钢锭收得率基准表(PmIngotYieldBt)资源库
 *
 * @author 蒋雨航 yuhang.jiang02@hand-china.com
 * @since 2024-09-05 15:20:29
 */
@Component
public class PmIngotYieldBtRepositoryImpl extends BaseRepositoryImpl<PmIngotYieldBt>
        implements PmIngotYieldBtRepository {
    @Resource
    private PmIngotYieldBtMapper pmIngotYieldBtMapper;

    @Override
    public List<PmIngotYieldBt> selectList(PmIngotYieldBt pmIngotYieldBt) {
        return pmIngotYieldBtMapper.selectList(pmIngotYieldBt);
    }

    @Override
    public PmIngotYieldBt selectByPrimary(Long piybId) {
        PmIngotYieldBt pmIngotYieldBt = new PmIngotYieldBt();
        pmIngotYieldBt.setPiybId(piybId);
        List<PmIngotYieldBt> pmIngotYieldBts = pmIngotYieldBtMapper.selectList(pmIngotYieldBt);
        if (CollectionUtils.isEmpty(pmIngotYieldBts)) {
            return null;
        }
        return pmIngotYieldBts.get(0);
    }

    @Override
    public PmIngotYieldBt selectByIngotType(String ingotType, Long tenantId) {
        PmIngotYieldBt record = new PmIngotYieldBt();
        record.setTenantId(tenantId);
        record.setIngotType(ingotType);
        PmIngotYieldBt pmIngotYieldBt = this.selectOne(record);
        if(pmIngotYieldBt == null) {
            // 通过锭坯型代码【{0}】未找到收得率配置！
            throw new CommonException("pm.pm_ingot_yield_bt_001", ingotType);
        }
        return pmIngotYieldBt;
    }

}
