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

import cn.hutool.core.map.MapWrapper;
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.DischargeEnergyIndex;
import com.nantian.cloud.jljc.entity.DischargeEnergyProduct;
import com.nantian.cloud.jljc.entity.DischargeEnergyStock;
import com.nantian.cloud.jljc.mapper.DischargeEnergyProductMapper;
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.service.DischargeEnergyStockService;
import com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockVo;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * 两高项目存量表服务接口实现
 *
 * @author gj
 * @description
 * @since 2022-02-15 09:40:50
 */
@Service("dischargeEnergyStockService")
public class DischargeEnergyStockServiceImpl extends ServiceImpl<DischargeEnergyStockMapper, DischargeEnergyStock> implements DischargeEnergyStockService {

    @Autowired
    private  DischargeEnergyStockService dischargeEnergyStockService;
    @Autowired
    private DischargeEnergyProductService dischargeEnergyProductService;
    @Autowired
    private DischargeEnergyIndexService dischargeEnergyIndexService;

    @Override
    public IPage<DischargeEnergyStockVo> getPageData(Page page, DischargeEnergyStock dischargeEnergyStock) {
        IPage<DischargeEnergyStockVo> pageData = baseMapper.getPageData(page, dischargeEnergyStock, new DataScope());
        return pageData;
    }

    @Override
    public List<DischargeEnergyStockVo> getData(DischargeEnergyStock dischargeEnergyStock) {
//        if ("530000".equals(dischargeEnergyStock.getItemLocation())) {
//            dischargeEnergyStock.setItemLocation("53");
//        } else if ("00".equals(dischargeEnergyStock.getItemLocation().substring(4))) {
//            dischargeEnergyStock.setItemLocation(dischargeEnergyStock.getItemLocation().substring(0, 4));
//        } else {
//
//        }
        List<DischargeEnergyStockVo> cityData = baseMapper.getCityData(dischargeEnergyStock, new DataScope());
        List<DischargeEnergyStockVo> list = new ArrayList<>();
        DischargeEnergyStockVo obj = new DischargeEnergyStockVo();
        obj.setEnterpriseName(cityData.get(0).getMeaning2());
        obj.setEnergyRawValue(cityData.get(0).getRawSum());
        obj.setEnergyConsumptionLastDl(cityData.get(0).getTceDl());
        obj.setEnergyConsumptionLastDj(cityData.get(0).getTceDj());
        obj.setEnergyConsumptionLastTwoDl(cityData.get(0).getTcetowDl());
        obj.setEnergyConsumptionLastTwoDj(cityData.get(0).getTcetowDj());
        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 {
                DischargeEnergyStockVo de = new DischargeEnergyStockVo();
                cityData.get(j-1).setIndex(j+"");
                list.add(cityData.get(j-1));
//                de.setMeaning2(cityData.get(j).getMeaning2());
                de.setEnterpriseName(cityData.get(j).getMeaning2());
                de.setEnergyRawValue(cityData.get(j).getRawSum());
                de.setEnergyConsumptionLastDl(cityData.get(j).getTceDl());
                de.setEnergyConsumptionLastDj(cityData.get(j).getTceDj());
                de.setEnergyConsumptionLastTwoDl(cityData.get(j).getTcetowDl());
                de.setEnergyConsumptionLastTwoDj(cityData.get(j).getTcetowDj());
                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<Map<String, Object>> getNumAndTceData(DischargeEnergyStock dischargeEnergyStock) {
        List<Map<String, Object>> data =  baseMapper.getNumAndTceData(dischargeEnergyStock, new DataScope());
        return data;
    }

    @Override
    public List<Map<String, Object>> getindustryNumAndTce(DischargeEnergyStock dischargeEnergyStock) {
        if ((dischargeEnergyStock.getItemLocation()!=null)){
            if ("530000".equals(dischargeEnergyStock.getItemLocation())) {
                dischargeEnergyStock.setItemLocation("53");
            } else if ("00".equals(dischargeEnergyStock.getItemLocation().substring(4))) {
                dischargeEnergyStock.setItemLocation(dischargeEnergyStock.getItemLocation().substring(0, 4));
            } else {

            }
        }


        List<Map<String, Object>> data =  baseMapper.getindustryNumAndTce(dischargeEnergyStock, new DataScope());
        return data;
    }

    @Override
    public Boolean addStock(String s) {
        JSONObject json = JSONObject.parseObject(s);
        String Product = json.getString("dischargeEnergyProduct");
        String Index = json.getString("dischargeEnergyIndex");
        DischargeEnergyStock dischargeEnergyStock = JSONObject.parseObject(json.getString("dischargeEnergyStock"),DischargeEnergyStock.class);
        List<DischargeEnergyProduct> productList = (List<DischargeEnergyProduct>) JSONArray.parseArray(Product,DischargeEnergyProduct.class);
        List<DischargeEnergyIndex> indexList = (List<DischargeEnergyIndex>) JSONArray.parseArray(Index,DischargeEnergyIndex.class);
//        int sid = baseMapper.addStock(dischargeEnergyStock);
        //保存两高项目
        dischargeEnergyStock.setDelFlag("1");
        dischargeEnergyStockService.save(dischargeEnergyStock);
        //保存产品
        productList.forEach(r->{
            r.setItemId(dischargeEnergyStock.getId());
            r.setItemType("1");
            r.setDelFlag("1");
        });
//        List<DischargeEnergyProduct> uniqueList = productList.stream().collect(
//                collectingAndThen(
//                        toCollection(() -> new TreeSet<>(comparing(n->n.getProduct()))), ArrayList::new)
//        );
        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 IPage<DischargeEnergyStockVo> getStockProductIndex(Page page, DischargeEnergyStock dischargeEnergyStock) {
        return baseMapper.getStockProductIndex(page,dischargeEnergyStock, new DataScope());
    }
}