package com.huimv.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.BaseFarm;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.EnergyEnvDevice;
import com.huimv.admin.entity.vo.WaterAndElectricity;
import com.huimv.admin.service.IBaseFarmService;
import com.huimv.admin.service.IEnergyDataService;
import com.huimv.admin.service.IEnergyEnvDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 耗能数据 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-14
 */
@RestController
@RequestMapping("/energy-data")
@CrossOrigin
public class EnergyDataController {

    @Autowired
    private IEnergyDataService iEnergyDataService;
    @Autowired
    private IEnergyEnvDeviceService deviceService;
    @Autowired
    private IBaseFarmService farmService;

    @RequestMapping("/listCount")
    public Result listCount(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listCount(httpServletRequest, paramsMap);
    }

    @RequestMapping("/listData")
    public Result listData(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listData(httpServletRequest, paramsMap);
    }

    @RequestMapping("/listRank")
    public Result listRank(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listRank(httpServletRequest, paramsMap);
    }

    //大屏
    @RequestMapping("/listDataScreen")
    public Result listDataScreen(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listDataScreen(httpServletRequest, paramsMap);
    }

    @RequestMapping("/listRankScreen")
    public Result listRankScreen(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listRankScreen(httpServletRequest, paramsMap);
    }

    @RequestMapping("/listDataAll")
    public Result listDataAll(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listDataAll(httpServletRequest, paramsMap);
    }

    //为了展示给别人好看一点
    @RequestMapping("/listDataWaterAndElc")
    public Result listDataAll2(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return iEnergyDataService.listDataAll2(httpServletRequest, paramsMap);
    }
    //给别人的接口，展示水电的实时数据
    @RequestMapping("/listDataTail")
    public Result listDataTail(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return iEnergyDataService.listDataTail(httpServletRequest, paramsMap);
    }

    @RequestMapping("/getData")
    public void getData(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(26);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 26).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }

    @RequestMapping("/getData2")
    public void getData2(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(29);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 29).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData3")
    public void getData3(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(22);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 22).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData4")
    public void getData4(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(28);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 28).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData5")
    public void getData5(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(21);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 21).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData6")
    public void getData6(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(33);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 33).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData7")
    public void getData7(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(34);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 34).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData8")
    public void getData8(@RequestBody Map<String, List<WaterAndElectricity>> paramsMap) {
        List<WaterAndElectricity> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (WaterAndElectricity jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWater());
            energyData.setElectricityValue(jsonObject.getElectricity());
            energyData.setGasValue("0.00");
            energyData.setFarmId(35);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 35).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody EnergyData energyData) {
        iEnergyDataService.save(energyData);
        return Result.SUCCESS();
    }

    @PostMapping("/listAll")
    public Result listAll(@RequestBody Map<String, String> paramsMap) {
        return iEnergyDataService.listAll(paramsMap);
    }

    @PostMapping("/removeById")
    public Result listAll(@RequestBody EnergyData energyData) {
        iEnergyDataService.removeById(energyData.getId());
        return Result.SUCCESS();
    }
}
