package com.xueyi.system.version.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.common.core.constant.digitalman.MessageConstants;
import com.xueyi.common.mqtt.connection.MqttTemplate;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import com.xueyi.message.api.transfer.domain.vo.DmDeployScheduleVo;
import com.xueyi.system.digitalmans.manager.IDmManDeviceManager;
import com.xueyi.system.version.domain.dto.DmApkVersionDto;
import com.xueyi.system.version.domain.dto.DmDeployHistoryDto;
import com.xueyi.system.version.domain.dto.DmReleaseManagerDto;
import com.xueyi.system.version.domain.query.DmApkVersionQuery;
import com.xueyi.system.version.domain.query.DmReleaseManagerQuery;
import com.xueyi.system.version.manager.IDmApkVersionManager;
import com.xueyi.system.version.manager.IDmDeployHistoryManager;
import com.xueyi.system.version.manager.IDmReleaseManagerManager;
import com.xueyi.system.version.service.IDmReleaseManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 发布管理管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class DmReleaseManagerServiceImpl extends BaseServiceImpl<DmReleaseManagerQuery, DmReleaseManagerDto, IDmReleaseManagerManager> implements IDmReleaseManagerService {

    @Autowired
    IDmDeployHistoryManager deployHistoryManager;

    @Autowired
    IDmManDeviceManager dmManDeviceManager;
    @Autowired
    IDmApkVersionManager apkVersionManager;
    @Autowired
    MqttTemplate mqttTemplate;
    /**
     * 查询发布管理对象列表 | 数据权限
     *
     * @param releaseManager 发布管理对象
     * @return 发布管理对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmReleaseManagerMapper"})
    public List<DmReleaseManagerDto> selectListScope(DmReleaseManagerQuery releaseManager) {
        return baseManager.selectList(releaseManager);
    }

    @Override
    public List<DmDeployHistoryDto> selectHistoryByDevId(Serializable id) {
        return deployHistoryManager.selectHistoryByDevId(id);
    }

    @Override
    public Integer remoteDeployByDevId(List<String> idList, String apkVersion) {
        // 对idList进行遍历
        int cnt = 0;
        for (String dId : idList) {

            // 1.dId表示设备dId，根据设备id查询dm_man_device表并获取dto
            DmReleaseManagerQuery query = new DmReleaseManagerQuery();
            query.setDeviceId(dId);
            DmReleaseManagerDto dmReleaseManagerDto = baseManager.selectList(query).get(0);
            if (dmReleaseManagerDto == null) {
                return 0;
            }
            // 根据apkVersion获取DmApkVersionDto
            DmApkVersionQuery versionQuery = new DmApkVersionQuery();
            versionQuery.setApkVersion(apkVersion);
            DmApkVersionDto dmApkVersionDto = apkVersionManager.selectList(versionQuery).get(0);
            JSONObject jo = new JSONObject();
            jo.put("versionName", apkVersion);
            jo.put("path", dmApkVersionDto.getPath());
            jo.put("setupTime", dmApkVersionDto.getReleaseTime());
            jo.put("md5", "");
            // 2.向Mqtt发送消息
            mqttTemplate.sendToMqtt("/prod/digital_man/" + dId + "/notify/apk_update", jo.toJSONString());
            // 3. 更新setup_status字段并更新表
            dmReleaseManagerDto.setSetupStatus(MessageConstants.SYS_DICT_DATA_RELEASE_START_DOWNLOAD);
            baseManager.update(dmReleaseManagerDto);
            cnt++;
        }

        return cnt;
    }

    @Override
    public Integer syncDeployStatusByDevId(DmDeployScheduleVo vo) {
        // 1.获取vo中的devId，并根据devId获取DmReleaseManagerDto
        DmReleaseManagerQuery query = new DmReleaseManagerQuery();
        query.setDeviceId(vo.getDevId());
        DmReleaseManagerDto dmReleaseManagerDto = baseManager.selectList(query).get(0);
        // 2. 更新setup_status字段并更新表
        dmReleaseManagerDto.setSetupStatus(vo.getType());

        return baseManager.update(dmReleaseManagerDto);
    }
}