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.order.entity.BasicPlan;
import com.jwipc.order.mapper.BasicPlanMapper;
import com.jwipc.order.model.po.BasicForecastPlanReleasePO;
import com.jwipc.order.model.po.BasicPlanPO;
import com.jwipc.order.model.query.BasicPlanQuery;
import com.jwipc.order.service.IBasicPlanService;
import org.springframework.stereotype.Service;

import java.util.List;

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

/**
 * @Author: swq
 * @Date: 2024/3/12 16:39
 */
@Service
public class BasicPlanServiceImpl extends ServiceImpl<BasicPlanMapper, BasicPlan> implements IBasicPlanService {
    @Override
    public List<BasicPlanPO> selectListByPage(Page<BasicPlan> 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<BasicForecastPlanReleasePO> findWaitReleaseData(Page<BasicForecastPlanReleasePO> toPage, BasicPlanQuery query) {
        if(null == toPage){
            return baseMapper.findWaitReleaseData(query);
        }
        return baseMapper.findWaitReleaseData(toPage, query);
    }

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

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

    @Override
    public void invalidAllPlanData(){
         baseMapper.invalidAllPlanData();
    }

    @Override
    public void updateQuota(String productCode,String supplierCode, String factoryCode, String oriQuota, String adjustmentQuota) {
        if(!oriQuota.equals("")&&oriQuota!=null){
            baseMapper.updateQuota(productCode,supplierCode,factoryCode,oriQuota,adjustmentQuota);
        }
        if(!adjustmentQuota.equals("")&&adjustmentQuota!=null){
            baseMapper.updateQuota(productCode,supplierCode,factoryCode,oriQuota,adjustmentQuota);
        }
    }
}
