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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.api.dto.PmCutPlaceDTO;
import com.xisc.pm.domain.entity.PmCutPlaceBt;
import com.xisc.pm.domain.entity.PmProContProdTtr;
import com.xisc.pm.domain.repository.PmCutPlaceBtRepository;
import com.xisc.pm.infra.constant.PmPlateConstants;
import com.xisc.pm.infra.enums.FormPlateMode;
import com.xisc.pm.infra.mapper.PmCutPlaceBtMapper;

import io.choerodon.core.exception.CommonException;

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

/**
 * 粗切位置决定基准表(PmCutPlaceBt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-24 08:40:39
 */
@Component
public class PmCutPlaceBtRepositoryImpl extends BaseRepositoryImpl<PmCutPlaceBt> implements PmCutPlaceBtRepository {
    @Resource
    private PmCutPlaceBtMapper pmCutPlaceBtMapper;

    @Override
    public List<PmCutPlaceBt> selectList(PmCutPlaceBt pmCutPlaceBt) {
        return pmCutPlaceBtMapper.selectList(pmCutPlaceBt);
    }

    @Override
    public PmCutPlaceBt selectByPrimary(Long pcpbId) {
        PmCutPlaceBt pmCutPlaceBt = new PmCutPlaceBt();
        pmCutPlaceBt.setPcpbId(pcpbId);
        List<PmCutPlaceBt> pmCutPlaceBts = pmCutPlaceBtMapper.selectList(pmCutPlaceBt);
        if (CollectionUtils.isEmpty(pmCutPlaceBts)) {
            return null;
        }
        return pmCutPlaceBts.get(0);
    }
    
    @Override
    public PmCutPlaceDTO queryCutPlace(FormPlateMode formPlateMode , String rollLineNo ,PmProContProdTtr axisPmProContProdTtr , Long tenantId) {
        // 读取粗切位置决定基准表
        List<PmCutPlaceBt> pmCutPlaceBts = this
                .selectByCondition(Condition.builder(PmCutPlaceBt.class)
                        .andWhere(Sqls.custom().andEqualTo(PmCutPlaceBt.FIELD_FORM_PLATE_MODE_FIRST, formPlateMode.getType())
                                .andEqualTo(PmCutPlaceBt.FIELD_ROLL_LINE_NO, rollLineNo)
                                .andGreaterThanOrEqualTo(PmCutPlaceBt.FIELD_PLATE_THICK_TO, axisPmProContProdTtr.getOrderThick())
                                .andLessThanOrEqualTo(PmCutPlaceBt.FIELD_PLATE_THICK_FROM, axisPmProContProdTtr.getOrderThick())
                                .andEqualTo(PmCutPlaceBt.FIELD_TENANT_ID, tenantId))
                        .build());
        if (pmCutPlaceBts == null || pmCutPlaceBts.isEmpty()) {
            throw new CommonException("pm.pm_pro_cont_018", rollLineNo, formPlateMode.getType() , axisPmProContProdTtr.getOrderThick().toPlainString());
        }
        PmCutPlaceBt cutPlaceBt = pmCutPlaceBts.get(0);
        PmCutPlaceDTO pmCutPlaceDTO = new PmCutPlaceDTO();
        if (StringUtils.isNotBlank(axisPmProContProdTtr.getBacklogQMark())
                && axisPmProContProdTtr.getBacklogQMark().charAt(0) == PmPlateConstants.BACKLOG_Q_MARK_FIRST_CHAR) {
            pmCutPlaceDTO.setType(PmPlateConstants.FUR_DEAL_TYPE_Q);
        } else if (StringUtils.isNotBlank(axisPmProContProdTtr.getBacklogTMark())
                && axisPmProContProdTtr.getBacklogTMark().charAt(0) == PmPlateConstants.BACKLOG_T_MARK_FIRST_CHAR) {
            pmCutPlaceDTO.setType(PmPlateConstants.FUR_DEAL_TYPE_Q);
        } else if (StringUtils.isNotBlank(axisPmProContProdTtr.getBacklogAMark())
                && axisPmProContProdTtr.getBacklogAMark().charAt(0) == PmPlateConstants.BACKLOG_A_MARK_FIRST_CHAR) {
            pmCutPlaceDTO.setType(PmPlateConstants.FUR_DEAL_TYPE_A);
        } else if (StringUtils.isNotBlank(axisPmProContProdTtr.getBacklogNMark())
                && axisPmProContProdTtr.getBacklogNMark().charAt(0) == PmPlateConstants.BACKLOG_N_MARK_FIRST_CHAR) {
            pmCutPlaceDTO.setType(PmPlateConstants.FUR_DEAL_TYPE_A);
        } else {
            pmCutPlaceDTO.setType(PmPlateConstants.FUR_DEAL_TYPE_G);
        }
        // 粗切长度上下限
        switch (pmCutPlaceDTO.getType()) {
            case PmPlateConstants.FUR_DEAL_TYPE_Q:
                pmCutPlaceDTO.setCutMax(cutPlaceBt.getQtCutLenTo());
                pmCutPlaceDTO.setCutMin(cutPlaceBt.getQtCutLenFrom());
                break;
            case PmPlateConstants.FUR_DEAL_TYPE_A:
                pmCutPlaceDTO.setCutMax(cutPlaceBt.getUnqtCutLenTo());
                pmCutPlaceDTO.setCutMin(cutPlaceBt.getUnqtCutLenFrom());
                break;
            default:
                pmCutPlaceDTO.setCutMax(cutPlaceBt.getNormalCutLenTo());
                pmCutPlaceDTO.setCutMin(cutPlaceBt.getNormalCutLenFrom());
                break;
        }
        return pmCutPlaceDTO;
    }
}
