package com.mortals.xhx.daemon.task;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.message.impl.MessageProducer;
import com.mortals.xhx.common.code.LogTypeEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders;
import com.mortals.xhx.common.model.MessageHeader;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.model.DeviceLogQuery;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.queue.DefaultTbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsgHeaders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.mortals.xhx.common.key.Constant.MESSAGETYPE_NOTIFY_RESTART_APP;

/**
 * 设备定时重启
 */
@Slf4j
@Service("DeviceRestartTask")
public class DeviceRestartTaskImpl implements ITaskExcuteService {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("设备定时重启任务");
        String restartProduct = GlobalSysInfo.getParamValue(Constant.PARAM_RESTART_PRODUCT_LIST, "ckp,hjq");
        StrUtil.split(restartProduct, ",").stream().forEach(productCode -> {
            List<DeviceEntity> deviceEntities = deviceService.find(new DeviceQuery().productCode(productCode));
            for (DeviceEntity device : deviceEntities) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
                header.put(MessageHeader.MESSAGETYPE, MESSAGETYPE_NOTIFY_RESTART_APP);
                header.put(MessageHeader.DEVICECODE, device.getDeviceCode());
                header.put(MessageHeader.TIMESTAMP, DateUtils.getCurrStrDateTime());
                TbQueueMsg queueMsg = new DefaultTbQueueMsg(IdUtil.fastUUID(), "W10=", header);
                messageProducer.sendMsg(QueueKey.DEFAULT_EXCHANGE, Constant.DOWN_TOPIC + device.getDeviceCode(), JSON.toJSONString(queueMsg));

                DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
                deviceLogEntity.initAttrValue();
                deviceLogEntity.setTraceID(IdUtil.fastSimpleUUID());
                deviceLogEntity.setSiteId(device.getSiteId());
                deviceLogEntity.setDeviceId(device.getId());
                deviceLogEntity.setDeviceName(device.getDeviceName());
                deviceLogEntity.setDeviceCode(device.getDeviceCode());
                deviceLogEntity.setMessageHead(MESSAGETYPE_NOTIFY_RESTART_APP);
                deviceLogEntity.setContent("W10=");
                deviceLogEntity.setLogType(LogTypeEnum.下发服务.getValue());
                deviceLogEntity.setCreateUserId(1L);
                deviceLogEntity.setCreateTime(new Date());
                deviceLogService.save(deviceLogEntity, null);
            }
        });
        //定时删除日志 7天前
        DeviceLogQuery contidion = new DeviceLogQuery();
        DateUtils.addCurrDate(-7);
        contidion.setCreateTimeEnd(DateUtils.getDateTimeStr(DateUtils.addCurrDate(-7),DateUtils.P_yyyy_MM_dd_HH_mm_ss));
        deviceLogService.getDao().delete(contidion);
    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }


    public static void main(String[] args) {
        System.out.println(DateUtils.getDateTimeStr(DateUtils.addCurrDate(-7),DateUtils.P_yyyy_MM_dd_HH_mm_ss));
    }


}
