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.NewWater;
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 org.springframework.web.multipart.MultipartFile;

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);
    }

    @PostMapping("/importData")
    public Result importData(HttpServletRequest httpServletRequest, @RequestParam(name = "file") MultipartFile file,
                             @RequestParam(name = "farmId") String farmId,
                             @RequestParam(name = "unitId") String unitId,
                             @RequestParam(name = "farmName") String farmName) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("farmId", farmId);
        map.put("unitId", unitId);
        map.put("farmName", farmName);
        return iEnergyDataService.importData(httpServletRequest, map, file);
    }
    //为了展示给别人好看一点
    @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("/getDeviceStatus")
    public void getDeviceStatus(@RequestBody Map<String, List<EnergyEnvDevice>> paramsMap) {
        List<EnergyEnvDevice> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (EnergyEnvDevice envDevice : result) {
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("device_code", envDevice.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                device.setDeviceStatus(envDevice.getDeviceStatus());
            }
            deviceService.updateById(device);
        }
    }


 /*   @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);
        }
    }
    @RequestMapping("/getData9")
    public void getData9(@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(23);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 23).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData10")
    public void getData10(@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(36);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 36).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData11")
    public void getData11(@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(37);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 37).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData12")
    public void getData12(@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(38);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 38).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData13")
    public void getData13(@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(27);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 27).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData14")
    public void getData14(@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(40);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 40).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData15")
    public void getData15(@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(41);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 41).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData16")
    public void getData16(@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(39);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 39).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData17")
    public void getData17(@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(42);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 42).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData18")
    public void getData18(@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(24);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 24).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
            }
            iEnergyDataService.save(energyData);
        }
    }*/

    @RequestMapping("/getData19")
    public void getData19(@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());
                energyData.setUnitName(device.getUnitName());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData20")
    public void getData20(@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());
                energyData.setUnitName(device.getUnitName());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData21")
    public void getData21(@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(30);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 30).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
                energyData.setUnitName(device.getUnitName());
            }
            iEnergyDataService.save(energyData);
        }
    }
    @RequestMapping("/getData22")
    public void getData22(@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(31);
            energyData.setCreateDate(new Date());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", 31).eq("device_code", jsonObject.getDeviceCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
                energyData.setUnitName(device.getUnitName());
            }
            iEnergyDataService.save(energyData);
        }
    }

    @RequestMapping("/getData30")
    public void getData30(@RequestBody Map<String, List<NewWater>> paramsMap) {
        List<NewWater> result = (List) paramsMap.get("result");
        System.out.println("接受到的数据格式:" + result);
        for (NewWater jsonObject : result) {
            EnergyData energyData = new EnergyData();
            energyData.setFeedValue("0.00");
            energyData.setWaterValue(jsonObject.getWaterValue());
            energyData.setElectricityValue("0.00");
            energyData.setGasValue("0.00");
            energyData.setCreateDate(jsonObject.getCreateDate());
            QueryWrapper<EnergyEnvDevice> queryWrapper = new QueryWrapper<>();
            if (jsonObject.getFarmId() == 41) {
                queryWrapper.eq("farm_id", 28);
                energyData.setFarmId(28);
            } else if (jsonObject.getFarmId() == 42) {
                queryWrapper.eq("farm_id", 29);
                energyData.setFarmId(29);
            } else if (jsonObject.getFarmId() == 43) {
                queryWrapper.eq("farm_id", 30);
                energyData.setFarmId(30);
            } else if (jsonObject.getFarmId() == 44) {
                queryWrapper.eq("farm_id", 31);
                energyData.setFarmId(31);
            }
            queryWrapper.eq("device_code", jsonObject.getChipCode());
            EnergyEnvDevice device = deviceService.getOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(device)) {
                energyData.setUnitId(device.getUnitId());
                energyData.setUnitName(device.getUnitName());
            }
            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();
    }
}
