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

import org.apache.commons.collections.CollectionUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.springframework.stereotype.Component;
import com.xisc.pm.domain.entity.PmIngotTypeBt;
import com.xisc.pm.domain.repository.PmIngotTypeBtRepository;
import com.xisc.pm.infra.mapper.PmIngotTypeBtMapper;

import io.choerodon.core.exception.CommonException;

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

/**
 * 锭坯型基表(PmIngotTypeBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-29 14:54:10
 */
@Component
public class PmIngotTypeBtRepositoryImpl extends BaseRepositoryImpl<PmIngotTypeBt> implements PmIngotTypeBtRepository {
    @Resource
    private PmIngotTypeBtMapper pmIngotTypeBtMapper;

    @Override
    public List<PmIngotTypeBt> selectList(PmIngotTypeBt pmIngotTypeBt) {
        return pmIngotTypeBtMapper.selectList(pmIngotTypeBt);
    }

    @Override
    public PmIngotTypeBt selectByPrimary(Long pitbId) {
        PmIngotTypeBt pmIngotTypeBt = new PmIngotTypeBt();
        pmIngotTypeBt.setPitbId(pitbId);
        List<PmIngotTypeBt> pmIngotTypeBts = pmIngotTypeBtMapper.selectList(pmIngotTypeBt);
        if (CollectionUtils.isEmpty(pmIngotTypeBts)) {
            return null;
        }
        return pmIngotTypeBts.get(0);
    }

    @Override
    public PmIngotTypeBt selectByIngotType(String ingotType, Long tenantId) {
        PmIngotTypeBt record = new PmIngotTypeBt();
        record.setIngotType(ingotType);
        record.setTenantId(tenantId);
        PmIngotTypeBt pmIngotTypeBt = this.selectOne(record);
        
        if(pmIngotTypeBt == null) {
            // 根据锭坯型代码【ingot_type】找到钢锭重出错！
            throw new CommonException("pm.pm_ingot_type_bt_001", ingotType);
        }
        return pmIngotTypeBt;
    }
}
