package com.jwipc.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.db.DbUtil;
import com.jwipc.order.entity.ShortagePlan;
import com.jwipc.order.mapper.ShortagePlanMapper;
import com.jwipc.order.model.po.ShortagePlanReleasePO;
import com.jwipc.order.model.po.ShortagePlanPO;
import com.jwipc.order.model.query.BasicPlanQuery;
import com.jwipc.order.service.IShortagePlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.baomidou.dynamic.datasource.support.DdConstants.SLAVE;

/**
 * <p>
 * 欠料标准预测计划 服务实现类
 * </p>
 *
 * @author swq
 * @since 2024-04-28
 */
@Service
public class ShortagePlanServiceImpl extends ServiceImpl<ShortagePlanMapper, ShortagePlan> implements IShortagePlanService {
    @Override
    public List<ShortagePlanPO> selectListByPage(Page<ShortagePlan> dbPage, BasicPlanQuery query) {
        if(dbPage == null){
            return baseMapper.selectListByPage(query);
        }
        return baseMapper.selectListByPage(dbPage,query);
    }
    @Override
    public long findMaxVersion(){
        return baseMapper.findMaxVersion();
    }

    /**
     * 根据当前版本查询上个版本
     *
     * @param version 当前版本
     * @return 上个版本
     */
    @Override
    public Long findPreviousVersion(Long version) {
        return this.baseMapper.findPreviousVersion(version);
    }

    /**
     * 获取待发布的计划数据
     *
     * @param toPage 分页条件
     * @param query  查询条件
     * @return 结果
     */
    @Override
    @DS(SLAVE)
    public List<ShortagePlanReleasePO> findWaitReleaseData(Page<ShortagePlanReleasePO> toPage, BasicPlanQuery query) {
        if (StringUtils.isNotEmpty(query.getWorkOrderNumber())) {
            query.setWorkOrderNumber(DbUtil.toRegexp(query.getWorkOrderNumber()));
        }
        //先去掉空格
        if (StringUtils.isNotEmpty(query.getProduct())) {
            query.setProduct(query.getProduct().trim());
        }
        if (StringUtils.isNotEmpty(query.getProduct()) && query.getProduct().indexOf(" ") > 0) {
            query.setProduct(query.getProduct().replaceAll(" ", "|"));
        }
        if (StringUtils.isNotEmpty(query.getProduct()) && query.getProduct().indexOf(",") > 0) {
            query.setProduct(query.getProduct().replace(",", "|"));
        }

        if(null == toPage){
            return baseMapper.findWaitReleaseData(query);
        }
        return baseMapper.findWaitReleaseData(toPage, query);
    }

    /**
     * 获取计划待发布总量，已发布总量和未发布总量
     *
     * @param query 查询条件
     * @return 结果
     */
    @Override
    public ShortagePlanReleasePO findPlanSituation(BasicPlanQuery query) {
        return baseMapper.findPlanSituation(query);
    }

    /**
     * 获取最新的流水号
     *
     * @param excludePlanId 排除的计划
     */
    @Override
    public String findNewSerialNumber( Long excludePlanId){
        return baseMapper.findNewSerialNumber(excludePlanId);
    }
}
