package com.fastbee.data.controller.userRecharge.zhanLian;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.common.core.domain.AjaxResultPro;
import com.fastbee.rechargecard.domain.NgWaterPumpUsageRecords;
import com.fastbee.rechargecard.mapper.NgWaterPumpUsageRecordsMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mijiupro
 */
@RestController
@RequestMapping("/flow/device")
@Api(tags = "流量计设备实时数据")
public class ZhanLianFlowDeviceDataController {

    @Autowired
    private ZhanLianBaseService1 zhanLianBaseService;

    @Autowired
    private NgWaterPumpUsageRecordsMapper ngWaterPumpUsageRecordsMapper;
    @GetMapping("/realtimeData")
    public AjaxResultPro realtimeData(@Param("deviceNumber") String deviceNumber) {
        System.err.println(deviceNumber);
        // try {
        //     Map<String, String> deviceRealTimeData = zhanLianBaseService.getDeviceRealTimeData(deviceNumber);
        //     return AjaxResultPro.success(deviceRealTimeData);
        // } catch (Exception e) {
        //     HashMap<String, String> deviceData = new HashMap<>();
        //     deviceData.put("realTime", "2024-12-21 23:24:52");
        //     deviceData.put("sumFlow_l", "0");
        //     deviceData.put("sumFlow_F", "0");
        //     deviceData.put("deviceId", "3852");
        //     deviceData.put("inFlow", "0");
        //     deviceData.put("status", "2");
        //     return AjaxResultPro.success(deviceData);
        // }

        NgWaterPumpUsageRecords ngWaterPumpUsageRecords1 = new NgWaterPumpUsageRecords();
        ngWaterPumpUsageRecords1.setDeviceNumber(deviceNumber);
        List<NgWaterPumpUsageRecords> ngWaterPumpUsageRecords = ngWaterPumpUsageRecordsMapper.selectNgWaterPumpUsageRecordsList(ngWaterPumpUsageRecords1);
        System.err.println(ngWaterPumpUsageRecords);
        //根据pump_start_time排序时间由近到远
        if(!ngWaterPumpUsageRecords.isEmpty()){
            ngWaterPumpUsageRecords.sort(Comparator.comparing(NgWaterPumpUsageRecords::getPumpStartTime).reversed());
            HashMap<String, String> deviceData = new HashMap<>();
            NgWaterPumpUsageRecords data = ngWaterPumpUsageRecords.get(0);
            String messageContent = data.getMessageContent();
            JSONObject jsonObject = JSONUtil.parseObj(messageContent);
            String data1 = jsonObject.getStr("data");
            JSONObject jsonObject1 = JSONUtil.parseObj(data1);

            //获取瞬时流量
            String inFlow = jsonObject1.getStr("insFlow");

            float in = Float.parseFloat(inFlow);


            if(in!=0){
                //精确到小数点后两位
                inFlow= String.format("%.2f", in/100);
            }
            //获取累计水量
            String sumFlow = jsonObject1.getStr("sumFlow");
            //解析成整型除以100
            float sum = Float.parseFloat(sumFlow);
            if(sum!=0){
                //精确到小数点后两位
               sumFlow= String.format("%.2f", sum/100);
            }

            deviceData.put("realTime", "2024-12-21 23:24:52");
                deviceData.put("sumFlow_l", sumFlow.split("\\.")[0]);
                deviceData.put("sumFlow_F", sumFlow.split("\\.")[1]);
                deviceData.put("deviceId", "3852");
                deviceData.put("inFlow",inFlow);
                deviceData.put("status", "2");

            return AjaxResultPro.success(deviceData);
        }else {
            HashMap<String, String> deviceData = new HashMap<>();
                deviceData.put("realTime", "2024-12-21 23:24:52");
                deviceData.put("sumFlow_l", "0");
                deviceData.put("sumFlow_F", "0");
                deviceData.put("deviceId", "3852");
                deviceData.put("inFlow", "0");
                deviceData.put("status", "2");
                return AjaxResultPro.success(deviceData);
        }



    }

    @GetMapping("/zhanlian/realtimeData")
    public AjaxResultPro getDeviceData(@Param("deviceNumber") String deviceNumber){
        try {
            Map<String, String> deviceRealTimeData = zhanLianBaseService.getDeviceRealTimeData(deviceNumber);
            return AjaxResultPro.success(deviceRealTimeData);
        } catch (Exception e) {
            HashMap<String, String> deviceData = new HashMap<>();
            deviceData.put("realTime", "2024-12-21 23:24:52");
            deviceData.put("sumFlow_l", "0");
            deviceData.put("sumFlow_F", "0");
            deviceData.put("deviceId", "3852");
            deviceData.put("inFlow", "0");
            deviceData.put("status", "2");
            return AjaxResultPro.success(deviceData);
        }
    }
}
