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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.entity.dto.CostbgIndexDto;
import com.tofly.xrtygis.custom.entity.dto.CostbgWaterDto;
import com.tofly.xrtygis.custom.entity.dto.ElectricityBillDto;
import com.tofly.xrtygis.custom.entity.dto.NewElectricityBillDto;
import com.tofly.xrtygis.custom.mapper.PlanManagementMapper;
import com.tofly.xrtygis.custom.mapper.WaterMakingPlanMapper;
import com.tofly.xrtygis.custom.service.WaterMakingPlanService;
import com.tofly.xrtygis.custom.util.CommonMethod;
import com.tofly.xrtygis.entity.*;
import com.tofly.xrtygis.service.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author 20126
 */
@Service
@AllArgsConstructor
public class WaterMakingPlanServiceImpl implements WaterMakingPlanService {

    private final CostbgpriceService costbgpriceService;
    private final CostbgindexyearService costbgindexyearService;
    private final CostbgindexService costbgindexService;
    private final CostbgfixedyearService costbgfixedyearService;
    private final CostbgfixedService costbgfixedService;
    private final CostbgwaterService costbgwaterService;
    private final CostbgwateryearService costbgwateryearService;
    private final CostbgunitService costbgunitService;
    private final CostbgunityearService costbgunityearService;

    private final WaterMakingPlanMapper waterMakingPlanMapper;
    private final PlanManagementMapper planManagementMapper;

    /**
     * 添加预算单价信息
     *
     * @param costbgprice 预算单价
     * @return true
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertPricePlan(Costbgprice costbgprice) {
        if (null == costbgprice.getYear()) {
            throw new ToflyDeniedException("年份不能为空");
        }
        //先添加单价信息
        costbgpriceService.save(costbgprice);
        //单单价变动处理
        priceChangeHandle(costbgprice);
        costbgfixedyearService.update(Wrappers.<Costbgfixedyear>lambdaUpdate()
                .eq(Costbgfixedyear::getYear, costbgprice.getYear())
                .set(Costbgfixedyear::getOverTime, new Date())
                .set(Costbgfixedyear::getState, "1"));

        return calculateWaterPlan(costbgprice.getYear());
    }

    /**
     * 查询制水费用
     *
     * @param year 年份
     * @return list
     */
    @Override

    public Map<String, Object> viewExpense(String year) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("info", waterMakingPlanMapper.viewExpense(year));
        result.put("sum", planManagementMapper.getExpenseSum(year));
        return result;
    }

    /**
     * 修改计划单价
     *
     * @param price 计划单价信息
     * @return true
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePricePlan(Costbgprice price) {
        if (null == price.getId()) {
            throw new ToflyDeniedException("id不能为空");
        }
        costbgpriceService.save(price);
        Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, price.getYear()));
        Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                .eq(Costbgfixedyear::getYear, price.getYear()));
        Costbgindexyear indexYear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, price.getYear()));

        if (null != waterYear) {

            Long yearId = null;

            if (fixedYear == null) {
                Costbgfixedyear year = new Costbgfixedyear();
                year.setYear(price.getYear());
                year.setState("0");
                costbgfixedyearService.save(year);
                yearId = year.getId();
            }

            for (Costbgwater water : costbgwaterService.list(Wrappers.<Costbgwater>lambdaQuery()
                    .eq(Costbgwater::getYearId, waterYear.getId()).orderByAsc(Costbgwater::getMonth))) {

                Costbgindex index = new Costbgindex();
                if (indexYear != null) {
                    index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getMonth, water.getMonth()).eq(Costbgindex::getYearId, indexYear.getId()));
                }
                Costbgfixed fixedPlan = new Costbgfixed();
                //不为空取基本电费
                if (null != fixedYear) {
                    fixedPlan = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery().eq(Costbgfixed::getYearId, fixedYear.getId()).eq(Costbgfixed::getMouth, water.getMonth()));
                }

                Costbgfixed fixed = CommonMethod.newCalFixedPlan(CommonMethod.calExpense(index, price, water, fixedPlan.getJbdfOne(), fixedPlan.getJbdfTwo()), water);
                //不为空修改单变信息，为空添加
                if (null != fixedYear) {
                    fixed.setId(fixedPlan.getId());
                    costbgfixedService.updateById(fixed);
                } else {
                    fixed.setYearId(yearId);
                    costbgfixedService.save(fixed);
                }

            }
        }
        return calculateWaterPlan(price.getYear());
    }


    /**
     * 查看单变计划
     *
     * @param id id
     * @return list
     */
    @Override
    public Map<String, Object> viewFixedPlan(Long id) {
        Map<String, Object> result = new HashMap<>(2);
        result.put("info", planManagementMapper.getFixedInfo(id));
        result.put("sum", planManagementMapper.getFixedSum(id));
        return result;
    }

    /**
     * 填报电费
     *
     * @param dto dto
     * @return true
     */
    @Override
    public boolean insertElectricityBill(NewElectricityBillDto ndto) {

        if (null == ndto || null == ndto.getEbd()) {
            throw new ToflyDeniedException("不允许填报为空");
        }
        String year = ndto.getYear();

        Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                .eq(Costbgfixedyear::getYear, year));

        Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, year));

        Costbgindexyear indexYear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, year));

        List<Costbgprice> list = costbgpriceService.list(Wrappers.<Costbgprice>lambdaQuery()
                .eq(Costbgprice::getYear, year).orderByDesc(Costbgprice::getCreateTime));

        Costbgprice price = null;

        if (!list.isEmpty()) {
            price = list.get(0);
        }

        Costbgfixed costbgfixed;

        for (ElectricityBillDto dto : ndto.getEbd()) {
            String month = dto.getMonth();
            Double jbdfOne = dto.getJbdfOne();
            Double jbdfTwo = dto.getJbdfTwo();
            //不存在首先创建该年计划
            if (null == fixedYear) {
                Costbgfixedyear costbgfixedyear = new Costbgfixedyear();

                costbgfixedyear.setState("0");
                costbgfixedyear.setYear(year);
                costbgfixedyearService.save(costbgfixedyear);

                costbgfixed = new Costbgfixed();


                if (indexYear != null && "1".equals(indexYear.getState()) && price != null) {
                    Costbgindex index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getMonth, month).eq(Costbgindex::getYearId, indexYear.getId()));

                    index.setJbdlOne(CommonMethod.divide(CommonMethod.multiply(dto.getJbdfOne(), 10000D), price.getJbdldjOne()));
                    index.setJbdlTwo(CommonMethod.divide(CommonMethod.multiply(dto.getJbdfTwo(), 10000D), price.getJbdldjTwo()));
                    index.setJbdlTotal((null == index.getJbdlOne() ? 0 : index.getJbdlOne()) + (null == index.getJbdlTwo() ? 0 : index.getJbdlTwo()));
                    costbgindexService.updateById(index);
                }

                if (waterYear != null && indexYear != null && price != null) {

                    Costbgwater water = costbgwaterService.getOne(Wrappers.<Costbgwater>lambdaQuery()
                            .eq(Costbgwater::getMonth, month).eq(Costbgwater::getYearId, waterYear.getId()));

                    Costbgindex index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getYearId, indexYear.getId()).eq(Costbgindex::getMonth, month));

                    costbgfixed = CommonMethod.newCalFixedPlan(CommonMethod.calExpense(index, price, water, dto.getJbdfOne(), dto.getJbdfTwo()), water);

                }

                costbgfixed.setYearId(costbgfixedyear.getId());
                costbgfixed.setMouth(month);
                costbgfixed.setJbdfOne(dto.getJbdfOne());
                costbgfixed.setJbdfTwo(dto.getJbdfTwo());
                costbgfixed.setJbdfTotal(CommonMethod.add(costbgfixed.getJbdfOne(), costbgfixed.getJbdfTwo()));

                costbgfixedService.save(costbgfixed);
            } else {
                costbgfixed = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery().eq(Costbgfixed::getMouth, month));
                if (indexYear != null && price != null) {
                    Costbgindex index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getMonth, month));

                    index.setJbdlOne(CommonMethod.divide(CommonMethod.multiply(dto.getJbdfOne(), 10000D), price.getJbdldjOne()));
                    index.setJbdlTwo(CommonMethod.divide(CommonMethod.multiply(dto.getJbdfTwo(), 10000D), price.getJbdldjTwo()));
                    index.setJbdlTotal((null == index.getJbdlOne() ? 0 : index.getJbdlOne()) + (null == index.getJbdlTwo() ? 0 : index.getJbdlTwo()));

                    costbgindexService.updateById(index);
                }

                if (waterYear != null && indexYear != null && price != null) {

                    Costbgwater water = costbgwaterService.getOne(Wrappers.<Costbgwater>lambdaQuery()
                            .eq(Costbgwater::getMonth, month));

                    Costbgindex index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getMonth, month));

                    Costbgfixed fixed = CommonMethod.newCalFixedPlan(CommonMethod.calExpense(index, price, water, dto.getJbdfOne(), dto.getJbdfTwo()), water);

                    fixed.setId(costbgfixed.getId());

                    costbgfixedService.updateById(fixed);

                } else {
                    costbgfixedService.update(Wrappers.<Costbgfixed>lambdaUpdate()
                            .eq(Costbgfixed::getId, costbgfixed.getId())
                            .set(Costbgfixed::getJbdfOne, dto.getJbdfOne())
                            .set(Costbgfixed::getJbdfTwo, dto.getJbdfTwo())
                            .set(Costbgfixed::getJbdfTotal, dto.getJbdfTwo() + dto.getJbdfOne()));
                }
            }
        }
        return calculateWaterPlan(year);
    }

    /**
     * 查看电费信息
     *
     * @param year 年份
     * @return page
     */
    @Override
    public Map<String, Object> viewElectricityBill(String year) {
        Map<String, Object> result = new HashMap<>();
        result.put("info", waterMakingPlanMapper.viewElectricityBill(year));
        result.put("sum", planManagementMapper.getElectricityBillSum(year));
        return result;
    }

    /**
     * 添加水量计划
     *
     * @param dto dto
     * @return true of false
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertWaterPlan(CostbgWaterDto dto) {
        if (null == dto) {
            throw new ToflyDeniedException("传入数据不能为空");
        } else if (null == dto.getYear() || "".equals(dto.getYear())) {
            throw new ToflyDeniedException("年份不能为空");
        } else if (null == dto.getCostbgwater() || dto.getCostbgwater().size() == 0) {
            throw new ToflyDeniedException("填报信息不能为空");
        }

        List<Costbgwater> waterList = dto.getCostbgwater();
        //查询对应年份是否存在计划
        Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, dto.getYear()));

        Costbgunityear unitYear = costbgunityearService.getOne(Wrappers.<Costbgunityear>lambdaQuery()
                .eq(Costbgunityear::getYear, dto.getYear()));

        Costbgindexyear indexYear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, dto.getYear()));

        Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                .eq(Costbgfixedyear::getYear, dto.getYear()));
        //水量计划不存在时提示
        if (null == waterYear) {
            throw new ToflyDeniedException(dto.getYear() + "年份计划不存在，请创建计划");
        }

        if ("1".equals(waterYear.getState())) {
            throw new ToflyDeniedException("该年份计划已经填报");
        }
        //遍历水量计划信息
        for (Costbgwater costbgwater : waterList) {
            //计算水量计划相关信息
            Costbgwater water = CommonMethod.calWaterPlan(costbgwater);
            //设置年份id
            water.setYearId(waterYear.getId());
            costbgwaterService.save(water);
            //当单耗和指标计划都不为空时
            headerWaterPlan(unitYear, indexYear, fixedYear, water);

        }
        if (null != indexYear && "0".equals(indexYear.getState())) {
            indexYear.setState("1");
            indexYear.setOverTime(new Date());
            costbgindexyearService.updateById(indexYear);
        }
        if (null != fixedYear && "0".equals(fixedYear.getState())) {
            fixedYear.setState("1");
            fixedYear.setOverTime(new Date());
            costbgfixedyearService.updateById(fixedYear);
        }
        calculateWaterPlan(dto.getYear());

        return costbgwateryearService.update(Wrappers.<Costbgwateryear>lambdaUpdate()
                .eq(Costbgwateryear::getYear, dto.getYear())
                .set(Costbgwateryear::getState, "1")
                .set(Costbgwateryear::getOverTime, new Date()));
    }

    /**
     * 修改水量计划信息
     *
     * @param dto dto
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWaterPlan(CostbgWaterDto dto) {
        if (null == dto) {
            throw new ToflyDeniedException("传入数据不能为空");
        } else if (null == dto.getYear() || "".equals(dto.getYear())) {
            throw new ToflyDeniedException("年份不能为空");
        } else if (null == dto.getCostbgwater() || dto.getCostbgwater().size() == 0) {
            throw new ToflyDeniedException("填报信息不能为空");
        }
        List<Costbgwater> waterList = dto.getCostbgwater();

        Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, dto.getYear()));

        Costbgunityear unitYear = costbgunityearService.getOne(Wrappers.<Costbgunityear>lambdaQuery()
                .eq(Costbgunityear::getYear, dto.getYear()));

        Costbgindexyear indexYear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, dto.getYear()));

        Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                .eq(Costbgfixedyear::getYear, dto.getYear()));

        List<Costbgprice> list = costbgpriceService.list(Wrappers.<Costbgprice>lambdaQuery()
                .eq(Costbgprice::getYear, dto.getYear()).orderByDesc(Costbgprice::getCreateTime));

        Costbgprice price = null;

        if (list != null && list.size() > 0) {
            price = list.get(0);
        }


        if (null != waterYear && "1".equals(waterYear.getState())) {

            for (Costbgwater costbgwater : waterList) {
                Costbgwater water = CommonMethod.calWaterPlan(costbgwater);
                costbgwaterService.updateById(water);

                //计划不为空查询单耗信息
                Costbgunit unit;
                Costbgindex index = null;

                if (null != unitYear && "1".equals(unitYear.getState())) {

                    unit = costbgunitService.getOne(Wrappers.<Costbgunit>lambdaQuery()
                            .eq(Costbgunit::getYearId, unitYear.getId()).eq(Costbgunit::getMonth, water.getMonth()));

                    //设置值，并修改单耗
                    unit.setDhTotal(CommonMethod.newCalUnitPlan(unit, water));
                    if (null != indexYear && "1".equals(indexYear.getState())) {

                        index = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                                .eq(Costbgindex::getMonth, water.getMonth()).eq(Costbgindex::getYearId, indexYear.getId()));

                        Costbgindex indexPlan = CommonMethod.newCalIndexPlan(water, unit);
                        indexPlan.setId(index.getId());
                        costbgindexService.updateById(indexPlan);
                        unit.setOtherOneDh(CommonMethod.divide(index.getOtherOne(), CommonMethod.multiply(water.getZslOne(), 10D)));
                        unit.setOtherTwoDh(CommonMethod.divide(index.getOtherTwo(), CommonMethod.multiply(water.getZslTwo(), 10D)));
                    }
                    costbgunitService.updateById(unit);
                }

                Costbgfixed fee = null;

                Costbgfixed fixedPlan = new Costbgfixed();
                if (null != price && null != index) {
                    if (fixedYear != null) {
                        fixedPlan = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery()
                                .eq(Costbgfixed::getMouth, water.getMonth()).eq(Costbgfixed::getYearId, fixedYear.getId()));
                    }
                    fee = CommonMethod.calExpense(index, price, water, fixedPlan.getJbdfOne(), fixedPlan.getJbdfTwo());
                }

                if (fee != null && fixedYear != null) {
                    Costbgfixed costbgfixed = CommonMethod.newCalFixedPlan(fee, water);
                    costbgfixed.setId(fixedPlan.getId());
                    costbgfixedService.updateById(costbgfixed);
                }

            }
            if (null != indexYear && "0".equals(indexYear.getState())) {
                indexYear.setState("1");
                indexYear.setOverTime(new Date());
                costbgindexyearService.updateById(indexYear);
            }
            if (null != fixedYear && "0".equals(fixedYear.getState())) {
                fixedYear.setState("1");
                fixedYear.setOverTime(new Date());
                costbgfixedyearService.updateById(fixedYear);
            }
        }
        return calculateWaterPlan(dto.getYear());
    }

    /**
     * 修改指标信息
     *
     * @param dto dto
     * @return true
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateIndexPlan(CostbgIndexDto dto) {
        List<Costbgindex> costbgindices = dto.getCostbgindices();
        if (costbgindices == null) {
            throw new ToflyDeniedException("指标信息不能为空");
        }

        for (Costbgindex info : costbgindices) {
            if (info != null) {
                //指标信息
                Costbgindex index = costbgindexService.getById(info.getId());
                if (index != null) {
                    //年份
                    Costbgindexyear one = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                            .eq(Costbgindexyear::getId, index.getYearId()));
                    if (one == null) {
                        continue;
                    }
                    if (one.getYear() == null) {
                        throw new ToflyDeniedException("年份信息未填报完整");
                    }
                    String year = one.getYear();
                    //水量年份信息
                    Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                            .eq(Costbgwateryear::getYear, year).ne(Costbgwateryear::getState, "0"));
                    //单变年份信息
                    Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                            .eq(Costbgfixedyear::getYear, year).ne(Costbgfixedyear::getState, "0"));
                    //查询单耗计划
                    Costbgunityear unitYear = costbgunityearService.getOne(Wrappers.<Costbgunityear>lambdaQuery()
                            .eq(Costbgunityear::getYear, year).ne(Costbgunityear::getState, "0"));

                    //年份信息不为空，查询单价与单变信息
                    if (fixedYear != null) {
                        Costbgfixed fixed = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery()
                                .eq(Costbgfixed::getYearId, fixedYear.getId()).eq(Costbgfixed::getMouth, index.getMonth()));

                        List<Costbgprice> price = costbgpriceService.list(Wrappers.<Costbgprice>lambdaQuery()
                                .eq(Costbgprice::getYear, year).orderByDesc(Costbgprice::getCreateTime));

                        //有该年份单价时，计算基本电量
                        if (price != null && !price.isEmpty()) {
                            Costbgprice pri = price.get(0);
                            Double jbdlOne = null;
                            Double jbdlTwo = null;
                            if (pri.getJbdldjOne() != null && fixed.getJbdfOne() != null) {
                                jbdlOne = CommonMethod.divide(CommonMethod.multiply(fixed.getJbdfOne(), 10000D), pri.getJbdldjOne());
                                info.setJbdlOne(jbdlOne);
                            }
                            if (pri.getJbdldjTwo() != null && fixed.getJbdfTwo() != null) {
                                jbdlTwo = CommonMethod.divide(CommonMethod.multiply(fixed.getJbdfTwo(), 10000D), pri.getJbdldjTwo());
                                info.setJbdlTwo(jbdlTwo);
                            }
                            if (jbdlOne != null || jbdlTwo != null) {
                                info.setJbdlTotal(CommonMethod.add(null == jbdlOne ? 0 : jbdlOne, null == jbdlTwo ? 0 : jbdlTwo));
                            }
                        }

                        if (null != waterYear) {
                            Costbgwater water = costbgwaterService.getOne(Wrappers.<Costbgwater>lambdaQuery()
                                    .eq(Costbgwater::getYearId, waterYear.getId()).eq(Costbgwater::getMonth, index.getMonth()));
                            if (unitYear != null) {
                                Costbgunit unit = costbgunitService.getOne(Wrappers.<Costbgunit>lambdaQuery()
                                        .eq(Costbgunit::getYearId, unitYear.getId())
                                        .eq(Costbgunit::getMonth, index.getMonth()));
                                CommonMethod.newCalIndexPlan(index, water, unit);
                            }
                            if (price != null && !price.isEmpty()) {
                                Costbgfixed costbgfixed = CommonMethod.calFixedPlan(fixed, index, price.get(0), water);
                                costbgfixedService.updateById(costbgfixed);
                            }

                        }

                    }

                }
            }
            costbgindexService.updateById(info);
        }
        return calculateWaterPlan(dto.getYear());
    }

    /**
     * 获取用量信息，用于费用计算
     *
     * @param year 年份
     * @return list
     */
    @Override
    public List<Map> getDosage(String year) {

        Costbgindexyear indexyear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, year));

        Costbgwateryear wateryear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, year));

        if (null == indexyear && null == wateryear) {
            throw new ToflyDeniedException("请先完善" + year + "年份计划");
        }

        return waterMakingPlanMapper.getDosage(year);
    }

    @Override
    public boolean calculateWaterPlan(String year) {

        //水量信息
        List<Costbgwater> waterList = costbgwaterService.list(Wrappers.<Costbgwater>lambdaQuery()
                .like(Costbgwater::getMonth, year).orderByAsc(Costbgwater::getMonth));
        //查询单变信息
        List<Costbgfixed> fixedList = costbgfixedService.list(Wrappers.<Costbgfixed>lambdaQuery()
                .like(Costbgfixed::getMouth, year).orderByAsc(Costbgfixed::getMouth));
        //查询单耗计划
        List<Costbgunit> unitList = costbgunitService.list(Wrappers.<Costbgunit>lambdaQuery()
                .like(Costbgunit::getMonth, year).orderByAsc(Costbgunit::getMonth));

        List<Costbgindex> indexList = costbgindexService.list(Wrappers.<Costbgindex>lambdaQuery()
                .like(Costbgindex::getMonth, year).orderByAsc(Costbgindex::getMonth));
        //单价信息
        List<Costbgprice> priceList = costbgpriceService.list(Wrappers.<Costbgprice>lambdaQuery()
                .eq(Costbgprice::getYear, year).orderByDesc(Costbgprice::getCreateTime));
        Costbgwater water = null;
        Costbgfixed fixed = null;
        Costbgprice price = null;
        Costbgindex index = null;
        Costbgunit unit = null;

        if (!priceList.isEmpty()) {
            price = priceList.get(0);
        }

        boolean flag = true;

        for (int i = 0; i < 12; i++) {

            if (!waterList.isEmpty()) {
                water = waterList.get(i);
            }
            if (!unitList.isEmpty()) {
                unit = unitList.get(i);
            }
            if (!indexList.isEmpty()) {
                index = indexList.get(i);
            }
            if (!fixedList.isEmpty()) {
                fixed = fixedList.get(i);
            }

            Costbgwater costbgwater = CommonMethod.newCalTbgWater(water);
            if (costbgwater != null) {
                costbgwaterService.updateById(water);
            }
            Costbgindex costbgindex = CommonMethod.newCalIndexPlan(index, costbgwater, unit);
            if (costbgindex != null) {
                costbgindexService.updateById(costbgindex);
                if (unit != null && costbgwater != null) {
                    unit.setOtherdh(CommonMethod.divide(costbgindex.getOther(), CommonMethod.multiply(costbgwater.getZslOne(), 10D)));
                }
            }
            if (unit != null) {
                unit.setDhTotal(CommonMethod.newCalUnitPlan(unit, water));
                costbgunitService.updateById(unit);
            }
            Costbgfixed costbgfixed = CommonMethod.calFixedPlan(fixed, costbgindex, price, costbgwater);
            if (costbgfixed != null) {
                costbgfixedService.updateById(costbgfixed);
                flag = false;
            }
        }

        if (!flag) {
            costbgfixedyearService.update(Wrappers.<Costbgfixedyear>lambdaUpdate()
                    .eq(Costbgfixedyear::getYear, year)
                    .set(Costbgfixedyear::getOverTime, new Date())
                    .set(Costbgfixedyear::getState, 1));
        }

        return true;
    }



    /* ****************************************************************************************** */

    /**
     * 单价变动处理
     *
     * @param costbgprice 单价预算信息
     */
    private void priceChangeHandle(Costbgprice costbgprice) {
        //查询水量计划是否填报
        Costbgwateryear waterYear = costbgwateryearService.getOne(Wrappers.<Costbgwateryear>lambdaQuery()
                .eq(Costbgwateryear::getYear, costbgprice.getYear()));
        //查询预算指标是否填报
        Costbgindexyear indexYear = costbgindexyearService.getOne(Wrappers.<Costbgindexyear>lambdaQuery()
                .eq(Costbgindexyear::getYear, costbgprice.getYear()));

        //查询单变预算年计划是否存在
        Costbgfixedyear fixedYear = costbgfixedyearService.getOne(Wrappers.<Costbgfixedyear>lambdaQuery()
                .eq(Costbgfixedyear::getYear, costbgprice.getYear()));

        Long fixedYearId = null;
        //当查询出的年份计划为空时，创建年份计划
        if (null == fixedYear && ("1".equals(waterYear.getState()) || "1".equals(indexYear.getState()))) {
            Costbgfixedyear year = new Costbgfixedyear();
            year.setYear(costbgprice.getYear());
            year.setState("0");
            costbgfixedyearService.save(year);
            fixedYearId = year.getId();
        }
        //指标年份计划不为空且不是未填报
        if (null != indexYear && !"0".equals(indexYear.getState())) {
            //查出计划指标详情
            List<Costbgindex> indexList = costbgindexService.list(Wrappers.<Costbgindex>lambdaQuery()
                    .eq(Costbgindex::getYearId, indexYear.getId()));
            for (Costbgindex index : indexList) {
                Costbgfixed fixed = null;
                if (null != fixedYear && !"0".equals(fixedYear.getState())) {
                    fixed = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery()
                            .eq(Costbgfixed::getYearId, fixedYear.getId())
                            .eq(Costbgfixed::getMouth, index.getMonth()));
                    //当基本电费不为空，
                    if (fixed != null && null != fixed.getJbdfTwo() && null != fixed.getJbdfTwo()) {
                        index.setJbdlOne(CommonMethod.divide(CommonMethod.multiply(fixed.getJbdfOne(), 10000D), costbgprice.getJbdldjOne()));
                        index.setJbdlTwo(CommonMethod.divide(CommonMethod.multiply(fixed.getJbdfTwo(), 10000D), costbgprice.getJbdldjTwo()));
                        index.setJbdlTotal((null == index.getJbdlOne() ? 0 : index.getJbdlOne()) + (null == index.getJbdlTwo() ? 0 : index.getJbdlTwo()));
                        costbgindexService.updateById(index);
                    }
                }
                //水量计划不为空且不是未填报参与运算
                if (null != waterYear && !"0".equals(waterYear.getState())) {
                    Costbgwater water = costbgwaterService.getOne(Wrappers.<Costbgwater>lambdaQuery()
                            .eq(Costbgwater::getMonth, index.getMonth())
                            .eq(Costbgwater::getYearId, waterYear.getId()));
                    if (null != fixed) {
                        fixed = CommonMethod.newCalFixedPlan(CommonMethod.calExpense(index, costbgprice, water, fixed.getJbdfOne(), fixed.getJbdfTwo()), water);
                    } else {
                        fixed = CommonMethod.newCalFixedPlan(CommonMethod.calExpense(index, costbgprice, water, null, null), water);
                    }
                } else {
                    if (null != fixed) {
                        fixed = CommonMethod.calExpense(index, costbgprice, null, fixed.getJbdfOne(), fixed.getJbdfTwo());
                    } else {
                        fixed = CommonMethod.calExpense(index, costbgprice, null, null, null);
                    }

                }

                //单变计划为空或未填报时直接添加新记录
                if (null == fixedYear) {
                    fixed.setYearId(fixedYearId);
                    costbgfixedService.save(fixed);
                    //不为空是未填报时修改
                } else if ("0".equals(fixedYear.getState())) {
                    fixed.setId(fixedYear.getId());
                    costbgfixedService.updateById(fixed);
                    //不为空且不是未填报时，查不出对应记录，新增单变信息
                } else if (!"0".equals(fixedYear.getState())) {

                    Costbgfixed costbgfixed = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery()
                            .select(Costbgfixed::getId)
                            .eq(Costbgfixed::getMouth, index.getMonth())
                            .eq(Costbgfixed::getYearId, fixedYear.getId()));
                    if (null != costbgfixed) {
                        fixed.setId(costbgfixed.getId());
                        costbgfixedService.updateById(fixed);
                    } else {
                        fixed.setYearId(fixedYear.getId());
                        costbgfixedService.save(fixed);
                    }
                }
            }
        }
    }

    private void headerWaterPlan(Costbgunityear unitYear, Costbgindexyear indexYear, Costbgfixedyear fixedYear, Costbgwater water) {
        if (null != unitYear && null != indexYear) {
            if (!"0".equals(unitYear.getState())) {
                Costbgunit unit = costbgunitService.getOne(Wrappers.<Costbgunit>lambdaQuery()
                        .eq(Costbgunit::getYearId, unitYear.getId())
                        .eq(Costbgunit::getMonth, water.getMonth()));
                Costbgindex index = CommonMethod.newCalIndexPlan(water, unit);
                //当指标计划创建但是未填报时添加数据
                if ("0".equals(unitYear.getState())) {
                    index.setYearId(indexYear.getId());
                    index.setMonth(water.getMonth());
                    costbgindexService.save(index);
                } else {
                    Costbgindex costbgindex = costbgindexService.getOne(Wrappers.<Costbgindex>lambdaQuery()
                            .eq(Costbgindex::getYearId, indexYear.getId()).eq(Costbgindex::getMonth, water.getMonth()));
                    if (null != costbgindex) {
                        index.setId(costbgindex.getId());
                        //修改指标
                        costbgindexService.updateById(index);
                    }

                }
            }
        }
        if (null != fixedYear) {
            Costbgfixed fixed = costbgfixedService.getOne(Wrappers.<Costbgfixed>lambdaQuery()
                    .eq(Costbgfixed::getYearId, fixedYear.getId()).eq(Costbgfixed::getMouth, water.getMonth()));

            if (fixed != null) {
                Costbgfixed fixedPlan = CommonMethod.newCalFixedPlan(fixed, water);

                if (!"0".equals(fixedYear.getState())) {
                    fixedPlan.setId(fixed.getId());
                    costbgfixedService.updateById(fixedPlan);
                } else {
                    fixedPlan.setYearId(fixedYear.getId());
                    costbgfixedService.save(fixedPlan);
                }
            }

        }
    }
}
