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

import org.apache.commons.collections.CollectionUtils;
import org.hzero.core.base.BaseConstants;
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.PmAxisBrdTrimBt;
import com.xisc.pm.domain.repository.PmAxisBrdTrimBtRepository;
import com.xisc.pm.infra.mapper.PmAxisBrdTrimBtMapper;
import com.xisc.pm.infra.util.Utils;

import io.choerodon.core.exception.CommonException;

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

/**
 * 纵轧展宽宽度余量基准表(PmAxisBrdTrimBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-09-23 09:16:11
 */
@Component
public class PmAxisBrdTrimBtRepositoryImpl extends BaseRepositoryImpl<PmAxisBrdTrimBt>
        implements PmAxisBrdTrimBtRepository {
    @Resource
    private PmAxisBrdTrimBtMapper pmAxisBrdTrimBtMapper;

    @Override
    public List<PmAxisBrdTrimBt> selectList(PmAxisBrdTrimBt pmAxisBrdTrimBt) {
        return pmAxisBrdTrimBtMapper.selectList(pmAxisBrdTrimBt);
    }

    @Override
    public PmAxisBrdTrimBt selectByPrimary(Long pabtbId) {
        PmAxisBrdTrimBt pmAxisBrdTrimBt = new PmAxisBrdTrimBt();
        pmAxisBrdTrimBt.setPabtbId(pabtbId);
        List<PmAxisBrdTrimBt> pmAxisBrdTrimBts = pmAxisBrdTrimBtMapper.selectList(pmAxisBrdTrimBt);
        if (CollectionUtils.isEmpty(pmAxisBrdTrimBts)) {
            return null;
        }
        return pmAxisBrdTrimBts.get(0);
    }

    @Override
    public PmAxisBrdTrimBt selectPmAxisBrdTrimBt(Long tenantId, String mscLineNo, String withSideFlag) {
        List<PmAxisBrdTrimBt> pmAxisBrdTrimBts = this.selectByCondition(Condition.builder(PmAxisBrdTrimBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmAxisBrdTrimBt.FIELD_TENANT_ID, tenantId)
                        .andEqualTo(PmAxisBrdTrimBt.FIELD_WITH_SIDE_FLAG, withSideFlag)
                        .andEqualTo(PmAxisBrdTrimBt.FIELD_VALID_FLAG, BaseConstants.Flag.YES.longValue())
                        .andEqualTo(PmAxisBrdTrimBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(mscLineNo)))
                .build());
        if(pmAxisBrdTrimBts == null || pmAxisBrdTrimBts.isEmpty()) {
            // 未匹配到纵轧展宽宽度余量基准表，产线号【{0}】，切边标记【{1}】
            throw new CommonException("pm.pm_axis_brd_trim_bt_001", mscLineNo , withSideFlag);
        }
        return pmAxisBrdTrimBts.get(0);
    }
}
