package com.tofly.xrtygis.custom.service.zsdata.impl;

import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.entity.vo.CostVo;
import com.tofly.xrtygis.custom.entity.vo.MarkWaterVo;
import com.tofly.xrtygis.custom.mapper.DataMonthPlanMapper;
import com.tofly.xrtygis.custom.service.zsdata.DataMonthPlanService;
import com.tofly.xrtygis.custom.util.CommonMethod;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FlashWJL
 * @date 2021/5/15 15:51
 */
@Service
@AllArgsConstructor
public class DataMonthPlanServiceImpl implements DataMonthPlanService {

    private final DataMonthPlanMapper dataMonthPlanMapper;

    /**
     * 制水材料分析
     *
     * @param scid  厂
     * @param years 年月yyyy-MM
     * @return map
     */
    @Override
    public Map<String, Object> makeWater(String scid, String years) {

        Map<String, Object> resultMap = new HashMap<>();
        //月度财务
        MarkWaterVo actual;
        //月度计划
        MarkWaterVo plan;
        //月度累计
        MarkWaterVo pile;
        //月度信息
        List<Map> info;

        switch (scid) {
            case "1":
                actual = dataMonthPlanMapper.getOneMonthActual(years);

                resultMap.put("actual", actual);

                plan = dataMonthPlanMapper.getOneMonthPlan(years);
                resultMap.put("plan", plan);

                pile = dataMonthPlanMapper.getOneMonthPile(years);
                resultMap.put("pile", pile);

                info = dataMonthPlanMapper.getOneInfo(years);

                resultMap.put("info", info);

                resultMap.put("completionrate", CommonMethod.calCompletionRate(actual, plan));
                break;
            case "2":
                actual = dataMonthPlanMapper.getTwoMonthActual(years);
                resultMap.put("actual", actual);


                plan = dataMonthPlanMapper.getTwoMonthPlan(years);
                resultMap.put("plan", plan);


                pile = dataMonthPlanMapper.getTwoMonthPile(years);
                resultMap.put("pile", pile);


                info = dataMonthPlanMapper.getTwoInfo(years);
                resultMap.put("info", info);

                resultMap.put("completionrate", CommonMethod.calCompletionRate(actual, plan));
                break;
            default:
                throw new ToflyDeniedException("CODE ERROR");
        }

        return resultMap;
    }

    /**
     * 成本核算
     *
     * @param scid  厂
     * @param years 年月
     * @return map
     */
    @Override
    public Map<String, Object> costCalculate(String scid, String years) {
        Map<String, Object> resultMap = new HashMap<>(5);
        //月度财务
        CostVo pile;
        //月度计划
        CostVo plan;

        switch (scid) {
            case "0":
                resultMap.put("actual", dataMonthPlanMapper.viewSumMonthActual(years));

                plan = dataMonthPlanMapper.viewSumMonthPlan(years);
                resultMap.put("plan", plan);

                pile = dataMonthPlanMapper.viewSumMonthPile(years);

                resultMap.put("pile", pile);

                resultMap.put("info", dataMonthPlanMapper.viewSumInfo(years));

                resultMap.put("completionrate", CommonMethod.calCompletionRate(pile, plan));

                break;

            case "1":

                resultMap.put("actual", dataMonthPlanMapper.viewOneMonthActual(years));

                plan = dataMonthPlanMapper.viewOneMonthPlan(years);

                resultMap.put("plan", plan);

                pile = dataMonthPlanMapper.viewOneMonthPile(years);

                resultMap.put("pile", pile);

                resultMap.put("info", dataMonthPlanMapper.viewOneInfo(years));

                resultMap.put("completionrate", CommonMethod.calCompletionRate(pile, plan));
                break;
            case "2":

                resultMap.put("actual", dataMonthPlanMapper.viewTwoMonthActual(years));

                plan = dataMonthPlanMapper.viewTwoMonthPlan(years);
                resultMap.put("plan", plan);

                pile = dataMonthPlanMapper.viewTwoMonthPile(years);

                resultMap.put("pile", pile);

                resultMap.put("info", dataMonthPlanMapper.viewTwoInfo(years));

                resultMap.put("completionrate", CommonMethod.calCompletionRate(pile, plan));
                break;
            default:
                throw new ToflyDeniedException("CODE ERROR");
        }
        return resultMap;
    }


}
