package com.nantian.cloud.jljc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.DischargeEnergyConstruction;
import com.nantian.cloud.jljc.entity.DischargeEnergyIndex;
import com.nantian.cloud.jljc.entity.DischargeEnergyProduct;
import com.nantian.cloud.jljc.entity.DischargeEnergyStock;
import com.nantian.cloud.jljc.mapper.DischargeEnergyConstructionMapper;
import com.nantian.cloud.jljc.mapper.DischargeEnergyStockMapper;
import com.nantian.cloud.jljc.service.DischargeEnergyIndexService;
import com.nantian.cloud.jljc.service.DischargeEnergyProductService;
import com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyConstructionVo;
import com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockVo;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.nantian.cloud.jljc.service.DischargeEnergyConstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 两高项目在建表服务接口实现
 *
 * @author gj
 * @description
 * @since 2022-02-17 09:31:49
 */
@Service
public class DischargeEnergyConstructionServiceImpl extends ServiceImpl<DischargeEnergyConstructionMapper, DischargeEnergyConstruction> implements DischargeEnergyConstructionService {
    @Autowired
    private DischargeEnergyConstructionService dischargeEnergyConstructionService;
    @Autowired
    private DischargeEnergyProductService dischargeEnergyProductService;
    @Autowired
    private DischargeEnergyIndexService dischargeEnergyIndexService;

    @Override
    public IPage<DischargeEnergyConstruction> getPageData(Page page, DischargeEnergyConstruction dischargeEnergyConstruction) {
        return baseMapper.getPageData(page, dischargeEnergyConstruction, new DataScope());
    }

    @Override
    public List<DischargeEnergyConstructionVo> getData(DischargeEnergyConstruction dischargeEnergyConstruction) {
        List<DischargeEnergyConstructionVo> cityData = baseMapper.getCityData(dischargeEnergyConstruction, new DataScope());
        List<DischargeEnergyConstructionVo> list = new ArrayList<>();
        DischargeEnergyConstructionVo obj = new DischargeEnergyConstructionVo();
        obj.setMeaning2(cityData.get(0).getMeaning2());
        obj.setEnergyRawValue(cityData.get(0).getRawSum());
        obj.setEnergyDesignDl(cityData.get(0).getEnergyDesignDlSum());
        obj.setEnergyDesignDj(cityData.get(0).getEnergyDesignDjSum());
        obj.setEnergyNewPlanDl(cityData.get(0).getEnergyNewPlanDlSum());
        obj.setEnergyNewPlanDj(cityData.get(0).getEnergyNewPlanDjSum());
        list.add(obj);
        for (int j = 1; j < cityData.size(); j++) {
            if (cityData.get(j).getMeaning2().equals(cityData.get(j - 1).getMeaning2())) {
                cityData.get(j - 1).setIndex(j + "");
                list.add(cityData.get(j - 1));
            } else {
                DischargeEnergyConstructionVo de = new DischargeEnergyConstructionVo();
                cityData.get(j - 1).setIndex(j + "");
                list.add(cityData.get(j - 1));

                de.setMeaning2(cityData.get(j).getMeaning2());
                de.setEnergyRawValue(cityData.get(j).getRawSum());
                de.setEnergyDesignDl(cityData.get(j).getEnergyDesignDlSum());
                de.setEnergyDesignDj(cityData.get(j).getEnergyDesignDjSum());
                de.setEnergyNewPlanDl(cityData.get(j).getEnergyNewPlanDlSum());
                de.setEnergyNewPlanDj(cityData.get(j).getEnergyNewPlanDjSum());
                de.setIndex("");
                list.add(de);

            }
        }
        String index = cityData.size()+"";
        cityData.get(cityData.size() - 1).setIndex(index);
        list.add(cityData.get(cityData.size() - 1));
        return list;
    }

    @Override
    public List<DischargeEnergyConstructionVo> addConstruction(String s) {
        JSONObject json = JSONObject.parseObject(s);
        String Product = json.getString("dischargeEnergyProduct");
        String Index = json.getString("dischargeEnergyIndex");
        DischargeEnergyConstruction dischargeEnergyConstruction = JSONObject.parseObject(json.getString("dischargeEnergyStock"),DischargeEnergyConstruction.class);
        List<DischargeEnergyProduct> productList = (List<DischargeEnergyProduct>) JSONArray.parseArray(Product,DischargeEnergyProduct.class);
        List<DischargeEnergyIndex> indexList = (List<DischargeEnergyIndex>) JSONArray.parseArray(Index,DischargeEnergyIndex.class);
        //保存两高项目
        dischargeEnergyConstruction.setDelFlag("1");
        dischargeEnergyConstructionService.save(dischargeEnergyConstruction);
        //保存产品
        productList.forEach(r->{
            r.setItemId(dischargeEnergyConstruction.getId());
            r.setItemType("2");
            r.setDelFlag("1");
        });
        dischargeEnergyProductService.saveBatch(productList);
        //保存产品指标
        productList.forEach(r->{
            indexList.forEach(e->{
                if (Objects.equals(r.getProductNum(),e.getProductNum())){
                    e.setProductId(r.getId());
                    e.setDelFlag("1");
                }
            });
        });
        dischargeEnergyIndexService.saveBatch(indexList);

        return null;
    }

    @Override
    public List<DischargeEnergyConstructionVo> getConstructionProductIndex(Page page, DischargeEnergyConstruction dischargeEnergyConstruction) {
        return baseMapper.getConstructionProductIndex(page,dischargeEnergyConstruction, new DataScope());
    }
}