package com.fastbee.iot.strategy;


import cn.hutool.json.JSONObject;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.deviceData.domain.DeviceRealtimedataMeteorology;
import com.fastbee.deviceData.domain.DeviceRealtimedataMiaoqing;
import com.fastbee.deviceData.domain.DeviceRealtimedataMoisture;
import com.fastbee.deviceData.domain.DeviceRealtimedataWorms;
import com.fastbee.deviceData.mapper.DeviceRealtimedataMoistureMapper;
import com.fastbee.deviceData.mapper.DeviceRealtimedataWormsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class DeviceRealTimeDataStorage {

    @Autowired
    private DeviceRealtimedataMoistureMapper dataMoistureMapper;

    @Autowired
    private DeviceRealtimedataWormsMapper dataWormsMapper;



    public boolean saveData(Long productId,String deviceBrand,JSONObject propertiesMap){

        //墒情设备
        if(productId==142){
            //根据设备品牌映射数据
            if(deviceBrand.equals("展连IOT")){
                int inserted = dataMoistureMapper.insert(setMoistureDataByZhanLian(propertiesMap));
                return inserted > 0;
            }
        }
        //虫情设备
        if(productId==143){
            if(deviceBrand.equals("仁科") ){
                int inserted = dataWormsMapper.insert(setWormsDataByRenKe(propertiesMap));
                return inserted > 0;
            }
        }

        return false;
    }

    /**
     * 仁科虫情设备数据映射
     */
    public DeviceRealtimedataWorms setWormsDataByRenKe(JSONObject propertiesMap){
        DeviceRealtimedataWorms build = DeviceRealtimedataWorms.builder()
                .deviceAddr(propertiesMap.get("deviceId").toString())
                .rain(propertiesMap.get("rain").toString())
                .insecticideTem(propertiesMap.get("insecticideTem").toString())
                .shake(propertiesMap.get("shake").toString())
                .lng(propertiesMap.get("lng").toString())
                .lat(propertiesMap.get("lat").toString())
                .drying(propertiesMap.get("drying").toString())
                .rainFlap(propertiesMap.get("rainFlap").toString())
                .attractWorm(propertiesMap.get("attractWorm").toString())
                .illum(propertiesMap.get("illum").toString())
                .dryingTem(propertiesMap.get("dryingTem").toString())
                .fillLight(propertiesMap.get("fillLight").toString())
                .camera(propertiesMap.get("camera").toString())
                .pestPhotos(propertiesMap.get("pestPhotos").toString())
                .build();
        return build;
    }

    /**
     * 展连IOT墒情设备数据映射
     */
    public DeviceRealtimedataMoisture setMoistureDataByZhanLian(JSONObject propertiesMap){
        DeviceRealtimedataMoisture build = DeviceRealtimedataMoisture.builder()
                .deviceId(propertiesMap.get("deviceId").toString())
                .soilTemp(propertiesMap.getBigDecimal("soilTemp"))
                .soilHumi(propertiesMap.getBigDecimal("soilHumi"))
                .soilEc(propertiesMap.getBigDecimal("soilEc"))
                .soilPh(propertiesMap.getBigDecimal("soilPh"))
                .soilTemp1(propertiesMap.getBigDecimal("soilTemp_1"))
                .soilHumi1(propertiesMap.getBigDecimal("soilHumi_1"))
                .soilTemp2(propertiesMap.getBigDecimal("soilTemp_2"))
                .soilHumi2(propertiesMap.getBigDecimal("soilHumi_2"))
                .soilTemp3(propertiesMap.getBigDecimal("soilTemp_3"))
                .soilHumi3(propertiesMap.getBigDecimal("soilHumi_3"))
                .soilTemp4(propertiesMap.getBigDecimal("soilTemp_4"))
                .soilHumi4(propertiesMap.getBigDecimal("soilHumi_4"))
                .airTemp(propertiesMap.getBigDecimal("airTemp"))
                .mppTemp(propertiesMap.getBigDecimal("mppTemp"))
                .batteryCap(propertiesMap.getBigDecimal("batteryCap"))
                .batteryVolt(propertiesMap.getBigDecimal("batteryVolt"))
                .batteryCur(propertiesMap.getBigDecimal("batteryCur"))
                .batteryPower(propertiesMap.getBigDecimal("batteryPower"))
                .loadVolt(propertiesMap.getBigDecimal("loadVolt"))
                .loadCur(propertiesMap.getBigDecimal("loadCur"))
                .loadPower(propertiesMap.getBigDecimal("loadPower"))
                .photVolt(propertiesMap.getBigDecimal("photVolt"))
                .photCur(propertiesMap.getBigDecimal("photCur"))
                .photPower(propertiesMap.getBigDecimal("photPower"))
                .cumChargeDay(propertiesMap.getBigDecimal("cum_charge_day"))
                .cumChargeMon(propertiesMap.getBigDecimal("cum_charge_mon"))
                .cumChargeYear(propertiesMap.getBigDecimal("cum_charge_year"))
                .cumChargeAll(propertiesMap.getBigDecimal("cum_charge_all"))
                .cumEleDay(propertiesMap.getBigDecimal("cum_ele_day"))
                .cumEleMon(propertiesMap.getBigDecimal("cum_ele_mon"))
                .cumEleYear(propertiesMap.getBigDecimal("cum_ele_year"))
                .cumEleAll(propertiesMap.getBigDecimal("cum_ele_all"))
                .shoudong(propertiesMap.getLong("shoudong"))
                .control(propertiesMap.getLong("control"))
                //TODO set实时时间
                .saveTime(DateUtils.getNowDate())
                .build();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(propertiesMap.get("realTime").toString());
            build.setRealTime(date);
        } catch (ParseException e) {
            throw new ServiceException("时间格式转换失败！");
        }
        System.err.println("墒情设备实时数据："+build);
        return build;

    }

    /**
     * 展连IOT气象设备数据映射
     */
    public DeviceRealtimedataMeteorology setMeteorologyDataByZhanLian(JSONObject propertiesMap) {
        DeviceRealtimedataMeteorology build = DeviceRealtimedataMeteorology.builder()
                .deviceId(propertiesMap.getStr("deviceId"))
                .airTemp(propertiesMap.getBigDecimal("airTemp"))
                .airHumi(propertiesMap.getBigDecimal("airHumi"))
                .windSpeed(propertiesMap.getBigDecimal("windSpeed"))
                .windDir(propertiesMap.getBigDecimal("windDir"))
                .airPre(propertiesMap.getBigDecimal("airPre"))
                .guangzhao(propertiesMap.getBigDecimal("guangzhao"))
                .fushe(propertiesMap.getBigDecimal("fushe"))
                .sumRain(propertiesMap.getBigDecimal("sumRain"))
                .sumRain(propertiesMap.getBigDecimal("sumRain"))
                .insRain((propertiesMap.getBigDecimal("insRain")))
                .dayRain(propertiesMap.getBigDecimal("dayRain"))
                .zhengfa(propertiesMap.getBigDecimal("zhengfa"))
                .soilTemp(propertiesMap.getBigDecimal("soilTemp"))
                .soilHumi(propertiesMap.getBigDecimal("soilHumi"))
                .build();

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

    /**
     * 展连IOT苗情设备数据映射
     */
    public DeviceRealtimedataMiaoqing setMiaoqingDataByZhanLian(JSONObject propertiesMap) {
        DeviceRealtimedataMiaoqing build = DeviceRealtimedataMiaoqing.builder()
                .deviceId(propertiesMap.getStr("deviceId"))
                .airTemp(propertiesMap.getBigDecimal("airTemp"))
                .mppTemp(propertiesMap.getBigDecimal("mppTemp"))
                .batteryCap(propertiesMap.getLong("batteryCap"))
                .batteryVolt(propertiesMap.getBigDecimal("batteryVolt"))
                .batteryCur(propertiesMap.getBigDecimal("batteryCur"))
                .batteryPower(propertiesMap.getBigDecimal("batteryPower"))
                .loadVolt(propertiesMap.getBigDecimal("loadVolt"))
                .loadCur(propertiesMap.getBigDecimal("loadCur"))
                .loadPower(propertiesMap.getBigDecimal("loadPower"))
                .photVolt(propertiesMap.getBigDecimal("photVolt"))
                .photCur(propertiesMap.getBigDecimal("photCur"))
                .photPower(propertiesMap.getBigDecimal("photPower"))
                .cumChargeDay(propertiesMap.getBigDecimal("cum_charge_day"))
                .cumChargeMon(propertiesMap.getBigDecimal("cum_charge_mon"))
                .cumChargeYear(propertiesMap.getBigDecimal("cum_charge_year"))
                .cumChargeAll(propertiesMap.getBigDecimal("cum_charge_all"))
                .cumEleDay(propertiesMap.getBigDecimal("cum_ele_day"))
                .cumEleMon(propertiesMap.getBigDecimal("cum_ele_mon"))
                .cumEleYear(propertiesMap.getBigDecimal("cum_ele_year"))
                .cumEleAll(propertiesMap.getBigDecimal("cum_ele_all"))
                .shoudong(propertiesMap.getBigDecimal("shoudong"))
                .control(propertiesMap.getBigDecimal("control"))
                .build();
        build.setSaveTime(DateUtils.getNowDate());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(propertiesMap.getStr("realTime"));
            build.setRealTime(date);
        } catch (ParseException e) {
            throw new ServiceException("时间格式转换失败！");
        }
        return build;
    }


    



}
