package com.xy.service.factory.cmd.impl;

import cn.hutool.json.JSONObject;
import com.xy.annotate.Factory;
import com.xy.device.EnumMqttCmdTempletSets;
import com.xy.dto.DeviceSetRecordsDto;
import com.xy.dto.DeviceTempSetDto;
import com.xy.dto.MqttCmdDto;
import com.xy.dto.PaterDto;
import com.xy.entity.DeviceTempSet;
import com.xy.service.DeviceSetRecordsServiceImpl;
import com.xy.service.DeviceTempSetServiceImpl;
import com.xy.service.factory.cmd.CmdService;
import com.xy.utils.R;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * 设置指令 接口实现
 */
@Factory(type = "sets")
@AllArgsConstructor
public class SetsCmdServiceImpl implements CmdService {

    private DeviceTempSetServiceImpl deviceTempSetService;
    private DeviceSetRecordsServiceImpl deviceSetRecordsService;

    @Override
    public R senCommand(List<MqttCmdDto> mqttDtos) {
        for (MqttCmdDto mqttDto : mqttDtos) {
            PaterDto pater = mqttDto.getPater();
            JSONObject data = pater.getData();
            String type = data.getStr("type");
            //温度设置指令
            if (type.equals(EnumMqttCmdTempletSets.TEMPERATURE.getCode())) {
                DeviceTempSetDto.Save save = new DeviceTempSetDto.Save()
                        .setDeviceId(mqttDto.getDeviceId())
                        .setWorkMode(data.getStr("workModel"))
                        .setTempValue(data.getInt("target"))
                        .setTempValue1(data.getInt("target1"))
                        .setTempValue2(data.getInt("target2"))
                        .setTempValue3(data.getInt("target3"))
                        .setJob1StartTime(data.getInt("start1"))
                        .setJob1EndTime(data.getInt("end1"))
                        .setJob2StartTime(data.getInt("start2"))
                        .setJob2EndTime(data.getInt("end2"))
                        .setJob3StartTime(data.getInt("start3"))
                        .setJob3EndTime(data.getInt("end3"));
                String warningLow = data.getStr("warningLow");
                String warningHigh = data.getStr("warningHigh");
                if (!warningLow.equals("$warningLow$")) {
                    save.setWarningLow(Integer.valueOf(warningLow));
                }
                if (!warningHigh.equals("$warningHigh$")) {
                    save.setWarningHigh(Integer.valueOf(warningHigh));
                }
                deviceTempSetService.save(save);
            } else {
                DeviceSetRecordsDto.Save save = new DeviceSetRecordsDto.Save();
                save.setType(type)
                        .setDeviceId(mqttDto.getDeviceId())
                        .setContent(data.toString())
                        ;
                deviceSetRecordsService.save(save);
            }
        }
        return R.ok();
    }

    @Override
    public R resultBack(MqttCmdDto.ResultBack resultBack) {
        DeviceTempSetDto.DeviceTempResultBack deviceTempResultBack = resultBack.getDeviceTempResultBack();
        //温度设置指令
        if (resultBack.getCmdType().equals(EnumMqttCmdTempletSets.TEMPERATURE.getCode())) {
            DeviceTempSetDto.Vo deviceTempSetVo = deviceTempSetService.getLastOne(new DeviceTempSetDto.GetLastOneDto().setDeviceId(deviceTempResultBack.getDeviceId())).getData();
            DeviceTempSet deviceTempSet = new DeviceTempSet()
                    .setId(deviceTempSetVo.getId())
                    .setStatus(deviceTempResultBack.getDeviceTempSetStatusEnum().getCode());
            deviceTempSetService.updateById(deviceTempSet);
        }
        return R.ok();
    }
}
