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

import cn.hutool.json.JSONObject;
import com.xy.annotate.Factory;
import com.xy.device.EnumAppUpdateStatus;
import com.xy.device.EnumMqttCmdTempletTask;
import com.xy.dto.ActivityInfoDto;
import com.xy.dto.DeviceVersionUpDto;
import com.xy.dto.MqttCmdDto;
import com.xy.service.ActivityInfoService;
import com.xy.service.DeviceVersionUpServiceImpl;
import com.xy.service.factory.cmd.CmdService;
import com.xy.utils.R;
import com.xy.utils.SysDictUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

/**
 * 任务指令 接口实现
 */
@Factory(type = "task")
@AllArgsConstructor
public class TaskCmdServiceImpl implements CmdService {

    private ActivityInfoService activityInfoService;

    private DeviceVersionUpServiceImpl deviceVersionUpService;

    @Override
    public R senCommand(List<MqttCmdDto> mqttDtos) {
        for (MqttCmdDto mqttDto : mqttDtos) {
            JSONObject data = mqttDto.getPater().getData();
            String type = data.getStr("type");
            //终止活动
            if (type.equals(EnumMqttCmdTempletTask.ABORT.getCode())) {
                activityInfoService.abort(new ActivityInfoDto.Abort().setDeviceId(mqttDto.getDeviceId()));
            }
            //版本升级
            if (Type.APP.getType().equals(type)) {
                String task = data.getStr("task");
                if (Task.UPDATE.getTask().equals(task)) {
                    Integer value = SysDictUtils.getValue(EnumAppUpdateStatus.Code.CODE.getCode(), EnumAppUpdateStatus.CMD.getCode(), Integer.class);
                    DeviceVersionUpDto.SaveOrUpdate saveOrUpdate = new DeviceVersionUpDto.SaveOrUpdate()
                            .setDeviceId(mqttDto.getDeviceId())
                            .setDeviceVersion(data.getStr("version"))
                            .setAccumSendNum(1);
                    saveOrUpdate.setCmdValue(mqttDto.getPater().getData().toString());
                    saveOrUpdate.setStatus(value);
                    saveOrUpdate.setAppKey(data.getStr("appKey"));
                    deviceVersionUpService.saveOrUpdate(saveOrUpdate);
                }
            }
        }
        return R.ok();
    }

    @Override
    public R resultBack(MqttCmdDto.ResultBack resultBack) {
        DeviceVersionUpDto.DeviceVersionUpResultBack deviceVersionUpResultBack = resultBack.getDeviceVersionUpResultBack();
        //版本升级
        if (deviceVersionUpResultBack != null) {
            DeviceVersionUpDto.SaveOrUpdate saveOrUpdate = new DeviceVersionUpDto.SaveOrUpdate()
                    .setDeviceId(deviceVersionUpResultBack.getDeviceId())
                    .setDeviceVersion(deviceVersionUpResultBack.getDeviceVersion());
            saveOrUpdate.setStatus(deviceVersionUpResultBack.getStatus());
            saveOrUpdate.setProgress(deviceVersionUpResultBack.getProgress());
            saveOrUpdate.setMsg(deviceVersionUpResultBack.getMsg());
            deviceVersionUpService.saveOrUpdate(saveOrUpdate);
        }
        return R.ok();
    }

    @Getter
    @AllArgsConstructor
    public enum Type {

        APP("app", "机器app指令"),

        ;

        private String type;

        private String msg;
    }

    @Getter
    @AllArgsConstructor
    public enum Task {

        UPDATE("update", "机器app升级指令"),

        ;

        private String task;

        private String msg;
    }
}
