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

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

import javax.annotation.Resource;

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.PmPlateTrimBt;
import com.xisc.pm.domain.repository.PmPlateTrimBtRepository;
import com.xisc.pm.infra.mapper.PmPlateTrimBtMapper;

/**
 * 钢板切边量基准表(PmPlateTrimBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-24 10:37:37
 */
@Component
public class PmPlateTrimBtRepositoryImpl extends BaseRepositoryImpl<PmPlateTrimBt> implements PmPlateTrimBtRepository {
    @Resource
    private PmPlateTrimBtMapper pmPlateTrimBtMapper;

    @Override
    public List<PmPlateTrimBt> selectList(PmPlateTrimBt pmPlateTrimBt) {
        return pmPlateTrimBtMapper.selectList(pmPlateTrimBt);
    }

    @Override
    public PmPlateTrimBt selectByPrimary(Long pptbId) {
        PmPlateTrimBt pmPlateTrimBt = new PmPlateTrimBt();
        pmPlateTrimBt.setPptbId(pptbId);
        List<PmPlateTrimBt> pmPlateTrimBts = pmPlateTrimBtMapper.selectList(pmPlateTrimBt);
        if (CollectionUtils.isEmpty(pmPlateTrimBts)) {
            return null;
        }
        return pmPlateTrimBts.get(0);
    }
    
    @Override
    public List<PmPlateTrimBt> selectMaxTrimWidth(Long tenantId, String prodSideMark, BigDecimal orderThick, BigDecimal szWidth,BigDecimal modeWidth,
             String mscLineNo) {
        BigDecimal ratio = BigDecimal.ZERO;
        if(szWidth != null && modeWidth != null && modeWidth.compareTo(BigDecimal.ZERO) != 0) {
            ratio = szWidth.divide(modeWidth , BaseConstants.Digital.TWO , RoundingMode.HALF_UP);
        }
        return this.selectByCondition(Condition.builder(PmPlateTrimBt.class)
                .andWhere(Sqls.custom()
                        .andEqualTo(PmPlateTrimBt.FIELD_MSC_LINE_NO, mscLineNo)
                        .andEqualTo(PmPlateTrimBt.FIELD_WITH_SIDE_FLAG, prodSideMark)
                        .andGreaterThanOrEqualTo(PmPlateTrimBt.FIELD_PLATE_THICK_TO, orderThick)
                        .andLessThanOrEqualTo(PmPlateTrimBt.FIELD_PLATE_THICK_FROM, orderThick)
                        .andGreaterThanOrEqualTo(PmPlateTrimBt.FIELD_COMBINE_PLATE_WIDTH_TO, szWidth)
                        .andLessThanOrEqualTo(PmPlateTrimBt.FIELD_COMBINE_PLATE_WIDTH_FROM, szWidth)
                        .andGreaterThanOrEqualTo(PmPlateTrimBt.FIELD_WIDEN_TO, ratio)
                        .andLessThanOrEqualTo(PmPlateTrimBt.FIELD_WIDEN_FROM, ratio)
                        .andEqualTo(PmPlateTrimBt.FIELD_TENANT_ID, tenantId))
                .build());
    }
}
