package com.huimv.admin.jinghongtimer;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.ProtData;
import com.huimv.admin.jinghongtimer.Vo.JinghongWater;
import com.huimv.admin.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;
import java.util.Objects;

//@RestController
//@RequestMapping("/test")
@Configuration
@EnableScheduling
public class ProtTimer {

    @Autowired
    private ProtDataMapper protDataMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EnergyDataMapper energyDataMapper;


    @Scheduled(cron = "0 * * * * ? ")
//    @GetMapping("/test3")
//    @Scheduled(cron = "0 0/10 * * * ? ")
    public void action(){
        String url = "http://139.9.172.209:8085/getFlow"; // 更好的做法是从配置中读取这个URL
        try {
            // 使用泛型来指定返回类型
            ResponseEntity<List<JinghongWater>> response = restTemplate.exchange(
                    url,
                    org.springframework.http.HttpMethod.GET,
                    null,
                    new org.springframework.core.ParameterizedTypeReference<List<JinghongWater>>() {}
            );

            for (JinghongWater jinghongWater : Objects.requireNonNull(response.getBody())) {
                System.out.println(jinghongWater);
                ProtData protData = new ProtData();
                protData.setCreateDate(jinghongWater.getCreateTime());
                protData.setFarmId(getFarmId(jinghongWater.getDeviceId()));
                protData.setFlow(jinghongWater.getValueA());

                if ("861213058872158".equals(jinghongWater.getDeviceId())||"865661077850256".equals(jinghongWater.getDeviceId())){
                    protData.setLoctionType(2);
                }else {
                    protData.setLoctionType(1);
                }

                if("865661077850256".equals(jinghongWater.getDeviceId())){
                    protData.setFlow(String.format("%.2f",Double.parseDouble(jinghongWater.getValueA()) * 0.01));
                    QueryWrapper<EnergyData> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("unit_id", 162).orderByDesc("id").last("limit 1");
                    EnergyData energyData = energyDataMapper.selectOne(queryWrapper);
                    EnergyData energyData1 = new EnergyData();
                    String waterValue = energyData.getOriginalWaterValue();
                    if(StringUtils.isBlank(waterValue)){
                        waterValue = jinghongWater.getValueA();
                    }
                    energyData1.setUnitName("潞水基地一层2单元");
                    energyData1.setUnitId(162);
                    energyData1.setFarmId(30);
                    energyData1.setDisplayType(0);
                    energyData1.setCreateDate(new Date());
                    energyData1.setElectricityValue("0");
                    energyData1.setOriginalWaterValue(jinghongWater.getValueA());
                    energyData1.setFeedValue("0");
                    energyData1.setGasValue("0");
                    if (ObjectUtil.isNotEmpty(energyData)) {
                        double v = (Double.parseDouble(jinghongWater.getValueA())- Double.parseDouble(waterValue)) *0.01;
                        if (v < 0) {
                            energyData1.setWaterValue("0.00");
                            energyData1.setOriginalWaterValue(waterValue);
                        } else if (v> 300 ){
                            energyData1.setWaterValue("0.00");
                            energyData1.setOriginalWaterValue(waterValue);
                        }else {
                            energyData1.setWaterValue(String.format("%.2f", v));
                        }
                    } else {
                        energyData1.setWaterValue(String.format("%.2f",Double.parseDouble(jinghongWater.getValueA()) * 0.01));
                    }
                    energyDataMapper.insert(energyData1);
                }

                protData.setCod("0");
                protData.setNh3n("0");
                protData.setPh("0");
                protData.setTn("0");
                protData.setTp("0");
                protDataMapper.insert(protData);
            }

        } catch (RestClientException e) {
            // 处理异常，比如记录日志或抛出更具体的业务异常
            e.printStackTrace();
            throw new RuntimeException("Failed to fetch flows", e);
        }
    }
    private Integer getFarmId(String deivceId){
        switch (deivceId){
            case "861213058861409":
                return 28;
            case "861213058829596":
                return 31;
            case "861213058846764":
                return 29;
            case "861213058846640":
                return 30;
            case "865661077850256":
                return 30;
            default: //"861213058872158":
                return 28;
        }

    }
    private String getDevice(String deivceId){
        switch (deivceId){
            case "861213058861409":
                return "芙江";
            case "861213058829596":
                return "凤行";
            case "861213058846764":
                return "庙贝";
            case "861213058846640":
                return "潞水";
            default: //"861213058872158"
                return "芙江2";
        }


    }
}
