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

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 com.xisc.pm.domain.entity.PmSpeStAddvBt;
import com.xisc.pm.domain.repository.PmSpeStAddvBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmSpeStAddvBtMapper;

import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.mybatis.pagehelper.domain.Sort.Direction;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 特殊钢种余量增加表(PmSpeStAddvBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-25 08:50:16
 */
@Component
public class PmSpeStAddvBtRepositoryImpl extends BaseRepositoryImpl<PmSpeStAddvBt> implements PmSpeStAddvBtRepository {
    @Resource
    private PmSpeStAddvBtMapper pmSpeStAddvBtMapper;

    @Override
    public List<PmSpeStAddvBt> selectList(PmSpeStAddvBt pmSpeStAddvBt) {
        return pmSpeStAddvBtMapper.selectList(pmSpeStAddvBt);
    }

    @Override
    public PmSpeStAddvBt selectByPrimary(Long pssabId) {
        PmSpeStAddvBt pmSpeStAddvBt = new PmSpeStAddvBt();
        pmSpeStAddvBt.setPssabId(pssabId);
        List<PmSpeStAddvBt> pmSpeStAddvBts = pmSpeStAddvBtMapper.selectList(pmSpeStAddvBt);
        if (CollectionUtils.isEmpty(pmSpeStAddvBts)) {
            return null;
        }
        return pmSpeStAddvBts.get(0);
    }

    @Override
    public List<PmSpeStAddvBt> selectSlabAddv(Long tenantId , String mscLineNo, String stNo, String sgSign, BigDecimal moldThick,
            BigDecimal taim) {
        List<String> sgSginList = new ArrayList<String>();
        sgSginList.add(sgSign);
        sgSginList.add(PmPlateConstants.ALL);
        return PageHelper.doSort(new Sort(Direction.DESC ,PmSpeStAddvBt.FIELD_SG_SIGN) ,()->this.selectByCondition(Condition.builder(PmSpeStAddvBt.class).andWhere(Sqls.custom()
                .andGreaterThanOrEqualTo(PmSpeStAddvBt.FIELD_PLATE_THICK_TO, taim)
                .andLessThanOrEqualTo(PmSpeStAddvBt.FIELD_PLATE_THICK_FROM, taim)
                .andGreaterThanOrEqualTo(PmSpeStAddvBt.FIELD_SLAB_THICK_TO, moldThick)
                .andLessThanOrEqualTo(PmSpeStAddvBt.FIELD_SLAB_THICK_FROM, moldThick)
                .andEqualTo(PmSpeStAddvBt.FIELD_ST_NO, stNo)
                .andIn(PmSpeStAddvBt.FIELD_SG_SIGN, sgSginList)
                .andEqualTo(PmSpeStAddvBt.FIELD_MSC_LINE_NO, mscLineNo)
                .andEqualTo(PmSpeStAddvBt.FIELD_TENANT_ID, tenantId)).build()));
    }
}
