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

import io.choerodon.core.exception.CommonException;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xisc.pm.domain.entity.PmMidSlabAddvBt;
import com.xisc.pm.domain.repository.PmMidSlabAddvBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.mapper.PmMidSlabAddvBtMapper;
import com.xisc.pm.infra.mapper.QdToPoOrderMapper;
import com.xisc.pm.infra.util.Utils;

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;
import java.util.Objects;

/**
 * 低碳钢坯加余量基准表(PmMidSlabAddvBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-09-25 15:18:47
 */
@Component
public class PmMidSlabAddvBtRepositoryImpl extends BaseRepositoryImpl<PmMidSlabAddvBt>
        implements PmMidSlabAddvBtRepository {
    @Resource
    private PmMidSlabAddvBtMapper pmMidSlabAddvBtMapper;

    @Autowired
    private QdToPoOrderMapper qdToPoOrderMapper;

    @Override
    public List<PmMidSlabAddvBt> selectList(PmMidSlabAddvBt pmMidSlabAddvBt) {
        return pmMidSlabAddvBtMapper.selectList(pmMidSlabAddvBt);
    }

    @Override
    public PmMidSlabAddvBt selectByPrimary(Long pmsabId) {
        PmMidSlabAddvBt pmMidSlabAddvBt = new PmMidSlabAddvBt();
        pmMidSlabAddvBt.setPmsabId(pmsabId);
        List<PmMidSlabAddvBt> pmMidSlabAddvBts = pmMidSlabAddvBtMapper.selectList(pmMidSlabAddvBt);
        if (CollectionUtils.isEmpty(pmMidSlabAddvBts)) {
            return null;
        }
        return pmMidSlabAddvBts.get(0);
    }

    @Override
    public BigDecimal queryLenWidthDiff(BigDecimal taim, String orderNo, String mscLineNo, String stNo, String sgSgin, BigDecimal slabThk,
            Long tenantId) {
//        BigDecimal beginRollTemp = qdToPoOrderMapper.queryBeginRollTempByMscLine(orderNo, mscLineNo, tenantId, slabThk);
//        if (Objects.isNull(beginRollTemp)) {
//            //该断面不符合合同设计加热炉工艺要求，请重新选择断面。
//            throw new CommonException("pm.pm_pro_cont_062");
//        }
//        //如果查询出来为空不走这个逻辑
//        if (BigDecimal.valueOf(-1).compareTo(beginRollTemp) == 0) {
//            return null ;
//        }
        List<String> sgSginList = new ArrayList<String>();
        sgSginList.add(sgSgin);
        sgSginList.add(PmPlateConstants.ALL);
        List<PmMidSlabAddvBt> pmMidSlabAddvBts = PageHelper.doSort(new Sort(Direction.DESC, PmMidSlabAddvBt.FIELD_SG_SIGN),()->this.selectByCondition(Condition.builder(PmMidSlabAddvBt.class)
                .andWhere(Sqls.custom().andEqualTo(PmMidSlabAddvBt.FIELD_ST_NO, stNo)
                        .andEqualTo(PmMidSlabAddvBt.FIELD_ROLL_LINE_NO, Utils.mscLineNoToRoolLineNo(mscLineNo))
                        .andLessThanOrEqualTo(PmMidSlabAddvBt.FIELD_PLATE_THICK_FROM, taim)
                        .andGreaterThanOrEqualTo(PmMidSlabAddvBt.FIELD_PLATE_THICK_TO, taim)
//                        .andGreaterThanOrEqualTo(PmMidSlabAddvBt.FIELD_START_ROLL_TEMP2_AIM , beginRollTemp)
                        .andEqualTo(PmMidSlabAddvBt.FIELD_TENANT_ID, tenantId)
                        .andIn(PmMidSlabAddvBt.FIELD_SG_SIGN, sgSginList))
                .build()));
        if(pmMidSlabAddvBts == null || pmMidSlabAddvBts.isEmpty()) {
            return null ;
        }
        return pmMidSlabAddvBts.get(0).getLenWidthDiff();
    }
}
