package com.fastbee.deviceData.api.devlink.service;

import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataMiaoqing;
import com.fastbee.deviceData.mapper.DeviceRealtimedataMiaoqingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * devLink苗情设备数据服务
 */
@Service
public class ZhanLianMiaoQingDataService extends ZhanLianBaseService {
    @Autowired
    private ZhanLianAuthorizationService authorizationService;
    @Autowired
    private DeviceRealtimedataMiaoqingMapper deviceRealtimedataMiaoqingMapper;
    public DeviceRealtimedataMiaoqing setData(Map<String, String> propertiesMap){
        DeviceRealtimedataMiaoqing miaoqing = DeviceRealtimedataMiaoqing.builder()
                .deviceId(propertiesMap.get("deviceId"))
                .airTemp(toBigDecimalValue(propertiesMap.get("airTemp")))
                .mppTemp(toBigDecimalValue(propertiesMap.get("mppTemp")))
                .batteryCap(Long.parseLong(propertiesMap.get("batteryCap")))
                .batteryVolt(toBigDecimalValue(propertiesMap.get("batteryVolt")))
                .batteryCur(toBigDecimalValue(propertiesMap.get("batteryCur")))
                .batteryPower(toBigDecimalValue(propertiesMap.get("batteryPower")))
                .loadVolt(toBigDecimalValue(propertiesMap.get("loadVolt")))
                .loadCur(toBigDecimalValue(propertiesMap.get("loadCur")))
                .loadPower(toBigDecimalValue(propertiesMap.get("loadPower")))
                .photVolt(toBigDecimalValue(propertiesMap.get("photVolt")))
                .photCur(toBigDecimalValue(propertiesMap.get("photCur")))
                .photPower(toBigDecimalValue(propertiesMap.get("photPower")))
                .cumChargeDay(toBigDecimalValue(propertiesMap.get("cum_charge_day")))
                .cumChargeMon(toBigDecimalValue(propertiesMap.get("cum_charge_mon")))
                .cumChargeYear(toBigDecimalValue(propertiesMap.get("cum_charge_year")))
                .cumChargeAll(toBigDecimalValue(propertiesMap.get("cum_charge_all")))
                .cumEleDay(toBigDecimalValue(propertiesMap.get("cum_ele_day")))
                .cumEleMon(toBigDecimalValue(propertiesMap.get("cum_ele_mon")))
                .cumEleYear(toBigDecimalValue(propertiesMap.get("cum_ele_year")))
                .cumEleAll(toBigDecimalValue(propertiesMap.get("cum_ele_all")))
                .shoudong(toBigDecimalValue(propertiesMap.get("shoudong")))
                .control(toBigDecimalValue(propertiesMap.get("control")))
                .build();

        miaoqing.setSaveTime(DateUtils.getNowDate());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(propertiesMap.get("realTime"));
            miaoqing.setRealTime(date);
        } catch (ParseException e) {
            throw new ServiceException("时间格式转换失败！");
        }
        System.err.println("苗情设备光伏板实时数据："+miaoqing);
        return miaoqing;
    }

    public static void main(String[] args) {
        ZhanLianMiaoQingDataService devLinkMiaoQingDataService = new ZhanLianMiaoQingDataService();
        Map<String, String> metDeviceRealData = devLinkMiaoQingDataService.getDeviceRealTimeData("3275");
        devLinkMiaoQingDataService.setData(metDeviceRealData);
    }
}
