/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.visualization.modular.grainOilReserve.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.unis.visualization.modular.grainOilReserve.entity.VO.CityVarietyCapacityChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.CurrentCrkCapacityChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockDetail;
import com.unis.visualization.modular.grainOilReserve.entity.VO.GradeCapacityChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.RotationPlanChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.StockDetail;
import com.unis.visualization.modular.grainOilReserve.entity.VO.StorehouseCapacityChart;
import com.unis.visualization.modular.grainOilReserve.entity.VO.VarietyCapacityChart;
import com.unis.visualization.modular.grainOilReserve.mapper.GrainOilReserveMapper;
import com.unis.visualization.modular.grainOilReserve.param.DepotStockParam;
import com.unis.visualization.modular.grainOilReserve.param.GrainOilReserveParam;
import com.unis.visualization.modular.grainOilReserve.service.GrainOilReserveService;
import org.springframework.stereotype.Service;

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

/**
 * 可视化-粮油储备Service接口实现类
 *
 * @author TS
 * @date  2024/06/18 11:08
 **/
@Service
public class GrainOilReserveServiceImpl  implements GrainOilReserveService {
    @Resource
    private GrainOilReserveMapper grainOilReserveMapper;

    /**
     * 获取仓容统计图表信息
     *
     * @return
     */
    @Override
    public StorehouseCapacityChart getStorehouseChartDatas() {
        //得到总库容 单位：万吨
        StorehouseCapacityChart chart =grainOilReserveMapper.getStorehouseCapacity();
        //总库存 单位：万吨
        StorehouseCapacityChart kcslChart =grainOilReserveMapper.getStorehouseStock();
        BigDecimal  designCapacity = chart.getDesignCapacity();
        Double kcsl = kcslChart.getKcsl();
        chart.setKcsl(kcsl);
        //得到未用仓库容
        BigDecimal actualNoneCapacity =NumberUtil.sub(designCapacity,kcsl);
        chart.setActualNoneCapacity(actualNoneCapacity);
        //得到仓容比例
        BigDecimal capacityRate = NumberUtil.mul(NumberUtil.div(kcsl,designCapacity),100);
        chart.setCapacityRate(capacityRate);
        //得到去年今日库存
        StorehouseCapacityChart yesterYearKcslChart =grainOilReserveMapper.getYesteryearStorehouseStock();
        Double yesterYearKcsl = yesterYearKcslChart.getYesterYearKcsl();
        //在计算同比增长率时，如果分母为0，会产生无穷大的结果。为避免这种情况，可以将上期数据设定一个极小值，例如0.001，以确保计算结果的可靠性
        if (yesterYearKcsl == 0.0){
            yesterYearKcsl = 0.001;
        }
        chart.setYesterYearKcsl(yesterYearKcsl);
        //同比增长率=（当年的指标值-上年同期的值）÷上年同期的值*100%
        Double sub = NumberUtil.sub(kcsl,yesterYearKcsl);
        Double yoyRate = NumberUtil.mul(NumberUtil.div(sub,yesterYearKcsl),100);
        chart.setYoyRate(yoyRate);
        return chart;
    }

    /**
     * 获取品种统计图表信息
     *
     * @return
     */
    @Override
    public List<VarietyCapacityChart> getVarietyChartDatas() {
        return grainOilReserveMapper.getVarietyChartDatas();
    }

    /**
     * 获取等级统计图表信息
     *
     * @return
     */
    @Override
    public List<GradeCapacityChart> getGradeChartDatas() {
        return grainOilReserveMapper.getGradeChartDatas();
    }

    /**
     * 获取各市州品种统计图表信息
     *
     * @return
     */
    @Override
    public List<CityVarietyCapacityChart> getCityVarietyChartDatas() {
        return grainOilReserveMapper.getCityVarietyChartDatas();
    }

    /**
     * 获取当日出入库统计图表信息
     *
     * @param param
     * @return
     */
    @Override
    public List<CurrentCrkCapacityChart> getCurrentCrkChartDatas(GrainOilReserveParam param) {
        return grainOilReserveMapper.getCurrentCrkChartDatas(param.getCrkType());
    }

    /**
     * 获取轮换计划统计图表信息
     *
     * @return
     */
    @Override
    public RotationPlanChart getRotationPalnChartDatas() {
        RotationPlanChart planChart= grainOilReserveMapper.getRotationPalnChartDatas();
        BigDecimal outPlanQuantity = planChart.getOutPlanQuantity();// 计划出库
        BigDecimal inPlanQuantity = planChart.getInPlanQuantity();// 计划入库
        BigDecimal cksl = planChart.getCkslSum()!=null?planChart.getCkslSum():NumberUtil.toBigDecimal(0.0);// 出库数量
        BigDecimal rksl = planChart.getRkslSum()!=null?planChart.getRkslSum():NumberUtil.toBigDecimal(0.0);// 入库数量
        BigDecimal ckRate = NumberUtil.mul(NumberUtil.div(cksl,NumberUtil.mul(outPlanQuantity,1000)),100); //出库比例
        BigDecimal rkRate =NumberUtil.mul( NumberUtil.div(rksl,NumberUtil.mul(inPlanQuantity,1000)),100); //入库比例
        planChart.setCkRate(ckRate);
        planChart.setRkRate(rkRate);
        return planChart;
    }

    /**
     * 获取库区库存分布统计图表信息
     *
     * @param param
     * @return
     */
    @Override
    public List<DeportStockChart> getDepotStockChartDatas(DepotStockParam param) {
        return grainOilReserveMapper.getDepotStockChartDatas(param.getQueryType());
    }

    /**
     * 获取库区详情信息
     *
     * @param orgId
     * @return
     */
    @Override
    public DeportStockDetail getDepotStockDetail(Integer orgId) {
        DeportStockDetail deportStockDetail = grainOilReserveMapper.getDepotStockDetail(orgId);
        //得到仓房容量
        DeportStockDetail storehouse = grainOilReserveMapper.getStorehouseDetail(orgId);
        if (storehouse != null){
            deportStockDetail.setDesignCapacity(storehouse.getDesignCapacity());
            deportStockDetail.setStorehouseCount(storehouse.getStorehouseCount());
        }
        //得到油罐容量
        DeportStockDetail tank = grainOilReserveMapper.getTankDetail(orgId);
        if (tank != null){
            deportStockDetail.setTankCapacity(tank.getTankCapacity());
            deportStockDetail.setTankCount(tank.getTankCount());
        }
        //库存信息
        List<StockDetail> stockDetailList = grainOilReserveMapper.getStockDetailList(orgId);
        deportStockDetail.setStockDetailList(stockDetailList);
        return deportStockDetail;
    }
}
