package xiaoqu.home.open.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db1.UserProper;
import xiaoqu.home.open.model.db1.Watch;
import xiaoqu.home.open.model.db1.WatchOffLog;
import xiaoqu.home.open.model.db2.TaskInfo;
import xiaoqu.home.open.model.db2.TaskScenes;
import xiaoqu.home.open.model.db2.TaskUser;
import xiaoqu.home.open.model.db2.TodayDynamic;
import xiaoqu.home.open.mqtt.MqttPushClient;
import xiaoqu.home.open.publicBean.AttendanceStatusMessage;
import xiaoqu.home.open.publicBean.MqttBaseInfo;
import xiaoqu.home.open.publicBean.TaskMagnifierMessage;
import xiaoqu.home.open.publicBean.TodayDynamicMessage;
import xiaoqu.home.open.publicBean.WatchBindMessage;
import xiaoqu.home.open.publicBean.WatchShockAndVoiceMessage;
import xiaoqu.home.open.publicBean.WatchTaskInspectScenes;
import xiaoqu.home.open.publicBean.WatchTaskMessage;
import xiaoqu.home.open.publicBean.WatchUnBindMessage;
import xiaoqu.home.open.service.RedisService4;
import xiaoqu.home.open.service.RedisService5;
import xiaoqu.home.open.service.db1.UserProperService;
import xiaoqu.home.open.service.db1.UserService;
import xiaoqu.home.open.service.db1.WatchOffLogService;
import xiaoqu.home.open.service.db1.WatchService;
import xiaoqu.home.open.service.db2.TaskInfoService;
import xiaoqu.home.open.service.db2.TaskScenesService;
import xiaoqu.home.open.service.db2.TaskUserService;
import xiaoqu.home.open.service.db2.TodayDynamicService;
import xiaoqu.home.open.utils.WrapperUtils;

@Component
@Slf4j(topic = "task")
@EnableAsync
public class ScheduledTask {

	@Autowired
	private RedisService5 redisService5;
	@Autowired
	private RedisService4 redisService4;

	@Autowired
	private TaskInfoService taskInfoService;
	@Autowired
	private TaskUserService taskUserService;
	@Autowired
	private UserService userService;
	@Autowired
	private UserProperService userProperService;
	@Autowired
	private TodayDynamicService todayDynamicService;
	@Autowired
	private TaskScenesService taskScenesService;
	@Autowired
	private WatchService watchService;
	@Autowired
	private WatchOffLogService watchOffLogService;

	@Autowired
	private MqttPushClient mqttPushClient;

	//新的主题发布
	@Scheduled(fixedRate = 298000)
	public void mqttMessageSendNew() {
		Set<String> topicList = redisService4.setMembers("MQTTTOPICNEW");
		for (String topic : topicList) {
			if (topic.contains("86")) {
				QueryWrapper<Watch> wrapperWatch = WrapperUtils.getWrapper(false);
				wrapperWatch.eq("imei", topic);
				Watch watch = watchService.getOne(wrapperWatch);
				WatchShockAndVoiceMessage watchShockAndVoiceMessage = new WatchShockAndVoiceMessage();
				watchShockAndVoiceMessage.setMessageType(8);
				watchShockAndVoiceMessage.setShockState(watch.getStockStatus());
				watchShockAndVoiceMessage.setVoiceState(watch.getVoiceStatus());
				watchShockAndVoiceMessage.setVoiceValue(watch.getVoiceValue());
				watchShockAndVoiceMessage.setImei(watch.getImei());
				watchShockAndVoiceMessage.setBlueWorkState(watch.getBlueWorkState());
				mqttPushClient.publish(1, true, topic, JSON.toJSONString(watchShockAndVoiceMessage));
			} else {
				mqttPushClient.publish(1, false, topic, "ping");
			}
		}
	}
	// 手表震动参数和语音参数设置
	@Scheduled(cron = "0/20 * * * * ?")
	public void mqttMessageSend() {
		Set<String> topicList = redisService4.setMembers("MQTTTOPIC");
		for (String topic : topicList) {
			if (topic.contains("86")) {
				QueryWrapper<Watch> wrapperWatch = WrapperUtils.getWrapper(false);
				wrapperWatch.eq("imei", topic);
				Watch watch = watchService.getOne(wrapperWatch);
				WatchShockAndVoiceMessage watchShockAndVoiceMessage = new WatchShockAndVoiceMessage();
				watchShockAndVoiceMessage.setMessageType(8);
				watchShockAndVoiceMessage.setShockState(watch.getStockStatus());
				watchShockAndVoiceMessage.setVoiceState(watch.getVoiceStatus());
				watchShockAndVoiceMessage.setVoiceValue(watch.getVoiceValue());
				watchShockAndVoiceMessage.setImei(watch.getImei());
				watchShockAndVoiceMessage.setBlueWorkState(watch.getBlueWorkState());
				mqttPushClient.publish(1, true, topic, JSON.toJSONString(watchShockAndVoiceMessage));
			} else {
				mqttPushClient.publish(1, false, topic, "ping");
			}
		}
	}

	// 手表震动参数和语音参数设置
	@Scheduled(cron = "0/1 * * * * ?")
	public void WatchShockAndVoiceMessage() {
		String watchShockAndVoiceMessage = redisService4.rightPop("WatchShockAndVoiceMessage");
		while (watchShockAndVoiceMessage != null) {
			log.info("手表震动参数和语音参数设置=" + watchShockAndVoiceMessage);
			WatchShockAndVoiceMessage watchBindMessage = JSON.parseObject(watchShockAndVoiceMessage, WatchShockAndVoiceMessage.class);
			watchBindMessage.setMessageType(8);
			String topic = watchBindMessage.getImei();// 发布主题
			mqttPushClient.publish(1, true, topic, JSON.toJSONString(watchBindMessage));
			watchShockAndVoiceMessage = redisService4.rightPop("WatchShockAndVoiceMessage");
		}
	}

	/****************
	 * 考勤自动下班消息通知
	 */
	@Scheduled(cron = "0/1 * * * * ?")
	public void AttendanceStatusMessage() {
		String taskMagnifierMessageJSON = redisService4.leftPop("AttendanceStatusMessage");
		while (taskMagnifierMessageJSON != null) {
			log.info("考勤自动下班消息=" + taskMagnifierMessageJSON);
			AttendanceStatusMessage taskMagnifierMessage = JSON.parseObject(taskMagnifierMessageJSON, AttendanceStatusMessage.class);
			QueryWrapper<Watch> wrapperWatch = WrapperUtils.getWrapper(false);
			wrapperWatch.eq("user_id", taskMagnifierMessage.getUserId());
			List<Watch> watchList = watchService.list(wrapperWatch);
			if (watchList != null) {
				for (Watch watch : watchList) {
					String topic = watch.getImei();// 发布主题
					mqttPushClient.publish(1, false, topic, taskMagnifierMessageJSON);
				}
			} else {
				log.info("下班提醒蓝牙关闭未找到手表信息userId=" + taskMagnifierMessage.getUserId());
			}
			taskMagnifierMessageJSON = redisService4.leftPop("AttendanceStatusMessage");
		}
	}

	/****************
	 * 放大镜消息通知
	 */
	@Scheduled(cron = "0/1 * * * * ?")
	public void TaskMagnifierMessage() {
		String taskMagnifierMessageJSON = redisService4.leftPop("TaskMagnifierMessage");
		while (taskMagnifierMessageJSON != null) {
			log.info("放大镜消息=" + taskMagnifierMessageJSON);
			TaskMagnifierMessage taskMagnifierMessage = JSON.parseObject(taskMagnifierMessageJSON, TaskMagnifierMessage.class);
			String topic = taskMagnifierMessage.getMobile() + "_" + taskMagnifierMessage.getDistrictId();// 发布主题
			mqttPushClient.publish(1, false, topic, taskMagnifierMessageJSON);
			taskMagnifierMessageJSON = redisService4.leftPop("TaskMagnifierMessage");
		}
	}

	/****************
	 * 手表
	 */
	@Scheduled(cron = "0/1 * * * * ?")
//	@Async("scheduledExecutorService")
	public void WatchConnectList() {
		String watchOnOff = redisService4.rightPop("WatchConnectList");
		while (watchOnOff != null) {
			log.info("手表上下线=" + watchOnOff);
			JSONObject connectObject = JSON.parseObject(watchOnOff);
			String clientid = connectObject.getString("clientid");
			if (clientid.length()!=15 && clientid.length()!=31) {
				break;
			}
			QueryWrapper<Watch> wrapperWatch = WrapperUtils.getWrapper(false);
			wrapperWatch.eq("imei", clientid);
			Watch watch = watchService.getOne(wrapperWatch);
			if (watch != null) {
				Long connected_at = connectObject.getLong("connected_at");
				Long disconnected_at = connectObject.getLong("disconnected_at");
				WatchOffLog watchOffLog = new WatchOffLog();
				watchOffLog.setImei(clientid);
				if (connected_at != null) {
					watch.setStatus(1);
					watch.setOnTime(new Date(connected_at));
					watchOffLog.setLogType(1);
					watchOffLog.setCreateTime(new Date(connected_at));
				}
				if (disconnected_at != null) {
					watch.setStatus(0);
					watch.setOffTime(new Date(disconnected_at));
					watchOffLog.setLogType(0);
					watchOffLog.setCreateTime(new Date(disconnected_at));
				}
				watchOffLogService.save(watchOffLog);
				watchService.updateById(watch);
			} else {
				watch = new Watch();
				watch.setCreateTime(new Date());
				watch.setImei(clientid);
				watch.setStatus(1);
				watchService.save(watch);
			}
			watchOnOff = redisService4.leftPop("WatchConnectList");
		}
	}

	/****************
	 * 手表
	 */
	@Scheduled(cron = "0/2 * * * * ?")
	public void WatchSetParamsInit() {
		String WatchSetParamsInit = redisService5.leftPop("WatchSetParamsInit");
		if (WatchSetParamsInit != null) {
			log.info("手表初始化参数=" + WatchSetParamsInit);
			mqttPushClient.publish(1, true, "WatchSetParamsInit", WatchSetParamsInit);
		}
		String WatchShockSetParamsInit = redisService5.leftPop("WatchShockSetParamsInit");
		if (WatchShockSetParamsInit != null) {
			log.info("手表震动初始化参数=" + WatchShockSetParamsInit);
			mqttPushClient.publish(1, true, "WatchShockSetParamsInit", WatchShockSetParamsInit);
		}

	}

	// 下发手机解绑消息到手表
	@Scheduled(cron = "0/2 * * * * ?")
	@Async("scheduledExecutorService")
	public void MqttWatchBandMessage() {
		String mqttWatchBandMessage = redisService4.leftPop("MqttWatchBandMessage");
		while (mqttWatchBandMessage != null) {
			log.info("手表信息解绑信息=" + mqttWatchBandMessage);
			MqttBaseInfo watchTaskMessage = JSON.parseObject(mqttWatchBandMessage, MqttBaseInfo.class);
			if (watchTaskMessage.getMessageType() == 2) {
				WatchBindMessage watchBindMessage = JSON.parseObject(mqttWatchBandMessage, WatchBindMessage.class);
				String topic = watchBindMessage.getImei();// 发布主题
				mqttPushClient.publish(1, false, topic, mqttWatchBandMessage);
			}
			if (watchTaskMessage.getMessageType() == 3) {
				WatchUnBindMessage watchBindMessage = JSON.parseObject(mqttWatchBandMessage, WatchUnBindMessage.class);
				String topic = watchBindMessage.getImei();// 发布主题
				mqttPushClient.publish(1, false, topic, mqttWatchBandMessage);
			}
			mqttWatchBandMessage = redisService4.leftPop("MqttWatchBandMessage");
		}
	}

	// 下发任务消息到手表
	@Scheduled(cron = "0/2 * * * * ?")
	@Async("scheduledExecutorService")
	public void MqttWatchTaskMessage() {
		String mqttTaskStateMessage = redisService4.leftPop("MqttWatchTaskMessage");
		while (mqttTaskStateMessage != null) {
//			log.info("新任务提醒MqttWatchTaskMessage=" + mqttTaskStateMessage);
			WatchTaskMessage watchTaskMessage = JSON.parseObject(mqttTaskStateMessage, WatchTaskMessage.class);
			Long userId = watchTaskMessage.getUserId();
			User user = userService.getById(userId);
			QueryWrapper<Watch> wrapperWatch = WrapperUtils.getWrapper(false);
			wrapperWatch.eq("user_id", userId);
			List<Watch> watchList = watchService.list(wrapperWatch);
			if (watchList != null && watchList.size() > 0) {
				if (watchList.size() == 1) {
					Watch watch = watchList.get(0);
//					if (watch.getStatus() == 0) {// 离线了就不推了重新回到队列
//						redisService4.rPush("MqttWatchTaskMessage", mqttTaskStateMessage);
////						log.info(user.getName()+"="+watch.getImei()+"手表离线,推送手表端新消息="+mqttTaskStateMessage+"失败");
//						break;
//					}
					if (user.getDistrictId() != null && watchTaskMessage.getDistrictId().intValue() == user.getDistrictId()) {// 当前站点一致才发送
						String topic = watch.getImei();// 发布主题
						mqttPushClient.publish(1, false, topic, JSON.toJSONString(watchTaskMessage));
						log.info(user.getName() + "=" + watch.getImei() + "推送手表端新消息=" + mqttTaskStateMessage);
					}
				} else {
//					boolean flag = false;
					for (Watch watch : watchList) {
//						if (watch.getStatus() == 0) {// 离线了就不推了重新回到队列
////							log.info(user.getName()+"="+watch.getImei()+"手表离线,推送手表端新消息="+mqttTaskStateMessage+"失败");
//							continue;
//						}
						if (user.getDistrictId() != null && watchTaskMessage.getDistrictId().intValue() == user.getDistrictId()) {// 当前站点一致才发送
							String topic = watch.getImei();// 发布主题
							mqttPushClient.publish(1, false, topic, JSON.toJSONString(watchTaskMessage));
							log.info(user.getName() + "=" + watch.getImei() + "推送手表端新消息=" + mqttTaskStateMessage);
//							flag = true;
						}
					}
//					if (!flag) {
//						redisService4.rPush("MqttWatchTaskMessage", mqttTaskStateMessage);
//					}
				}

			} else {
				log.info("新任务消息提醒未找到手表信息userId=" + userId + user.getName());
			}
			mqttTaskStateMessage = redisService4.leftPop("MqttWatchTaskMessage");
		}
	}

	// 插入任务通知消息
	@Scheduled(cron = "0/2 * * * * ?")
	@Async("scheduledExecutorService")
	public void WatchTaskUserList() {
		String taskUserId = redisService4.leftPop("WatchTaskUserList");
		while (taskUserId != null) {
			log.info("新消息提醒taskUserId=" + taskUserId);
			WatchTaskMessage outputBean = new WatchTaskMessage();
			TaskUser bean = taskUserService.getById(taskUserId);
			TaskInfo taskInfo = taskInfoService.getById(bean.getTaskId());
			BeanUtils.copyProperties(taskInfo, outputBean);
			outputBean.setUserId(bean.getUserId());
			outputBean.setDistrictId(taskInfo.getDistrictId());
			outputBean.setSendTime(DateUtil.format(taskInfo.getSendTime(), "yyyy-MM-dd HH:mm:ss"));
			if (taskInfo.getTaskPosition().equals("")) {
				outputBean.setTaskPositions(new ArrayList<String>());
			} else {
				List<String> list = new ArrayList<String>();
				QueryWrapper<TaskScenes> wrapperTaskScenes = WrapperUtils.getWrapper(false);
				wrapperTaskScenes.eq("task_id", taskInfo.getId());
				wrapperTaskScenes.orderByAsc("order_by");
				List<TaskScenes> taskScenesList = taskScenesService.list(wrapperTaskScenes);
				for (TaskScenes taskScenes : taskScenesList) {
					String tempString = taskScenes.getSuperiorGridName().replace("->", "");
					list.add(tempString + taskScenes.getName());
				}
				outputBean.setTaskPositions(list);
			}
			if (taskInfo.getTaskType() == 5) {// 巡检任务
				List<WatchTaskInspectScenes> taskInspectScenes = new ArrayList<WatchTaskInspectScenes>();
				QueryWrapper<TaskScenes> wrapperTaskScenes = WrapperUtils.getWrapper(false);
				wrapperTaskScenes.eq("task_id", taskInfo.getId());
				wrapperTaskScenes.orderByAsc("order_by");
				List<TaskScenes> taskScenesList = taskScenesService.list(wrapperTaskScenes);
				for (TaskScenes taskScenes : taskScenesList) {
					WatchTaskInspectScenes watchTaskInspectScenes = new WatchTaskInspectScenes();
					BeanUtils.copyProperties(taskScenes, watchTaskInspectScenes);
					String tempString = taskScenes.getSuperiorGridName().replace("->", "");
					watchTaskInspectScenes.setName(tempString + taskScenes.getName());
					taskInspectScenes.add(watchTaskInspectScenes);
				}
				outputBean.setTaskInspectScenes(taskInspectScenes);
				outputBean.setExecuteTimeStart(DateUtil.format(taskInfo.getCreateTime(), "MM.dd-HH:mm"));
				outputBean.setExecuteTimeEnd(DateUtil.format(taskInfo.getPreFinishTime(), "MM.dd-HH:mm"));
			}
			outputBean.setMessageType(1);
			redisService4.lPush("MqttWatchTaskMessage", JSON.toJSONString(outputBean));
			taskUserId = redisService4.leftPop("WatchTaskUserList");
		}

	}

	// 插入手机端通知消息
	@Scheduled(cron = "0/2 * * * * ?")
	@Async("scheduledExecutorService")
	public void TodayDynamicMessage() {
		String taskId = redisService4.leftPop("IntellectualTaskIdList");

		while (taskId != null) {
			List<TodayDynamic> todayDynamicList = new ArrayList<TodayDynamic>();
			// 代理和物业需要推送，保洁不用
			// 1：直接查代理
			TaskInfo taskInfo = taskInfoService.getById(taskId);
			Long districtId = taskInfo.getDistrictId();
			TodayDynamic todayDynamic = new TodayDynamic();
			todayDynamic.setDistrictId(districtId);
			todayDynamic.setCreateTime(new Date());
			todayDynamic.setName(taskInfo.getName());
			todayDynamic.setTaskId(Long.parseLong(taskId));
			todayDynamic.setNo(taskInfo.getNo());
			todayDynamic.setTaskPosition(taskInfo.getTaskPosition());
			todayDynamic.setStatus(taskInfo.getStatus());
//			任务状态(0:已生成;1:已匹配;2:待抢单;3:已接单;4:作业中;5:按时完成;6:超时完成;7:人工取消;8:系统取消;9:未完成)
			if (taskInfo.getStatus().intValue() == 1) {
				todayDynamic.setStatusName("待派发");
			}
			if (taskInfo.getStatus().intValue() == 2) {
				todayDynamic.setStatusName("待抢单");
			}
			if (taskInfo.getStatus().intValue() == 3) {
				todayDynamic.setStatusName("已接单");
			}
			if (taskInfo.getStatus().intValue() == 4) {
				todayDynamic.setStatusName("作业中");
			}
			if (taskInfo.getStatus().intValue() == 5) {
				todayDynamic.setStatusName("按时完成");
			}
			if (taskInfo.getStatus().intValue() == 6) {
				todayDynamic.setStatusName("超时完成");
			}
			QueryWrapper<UserProper> queryWrapper1 = WrapperUtils.getWrapper(false);
			queryWrapper1.eq("district_id", districtId);
			queryWrapper1.eq("type", 2);
			List<UserProper> agentUserProper = userProperService.list(queryWrapper1);
			if (agentUserProper.size() > 0) {
				for (UserProper userProper : agentUserProper) {
					TodayDynamic todayDynamicT = new TodayDynamic();
					BeanUtils.copyProperties(todayDynamic, todayDynamicT);
					todayDynamicT.setUserId(userProper.getUserId());
					todayDynamicT.setMessageType(0);
					redisService4.lPush("MqttTaskStateMessage", JSON.toJSONString(todayDynamicT));
					todayDynamicList.add(todayDynamicT);
				}
			}
			// 定义一个物业人员ID集合
			Set<Long> userIds = new HashSet<Long>();
			// 2：物业管理员
			QueryWrapper<User> queryUser = WrapperUtils.getWrapper(false);
			queryUser.eq("role_type", 0);// 管理员
			queryUser.eq("property_id", taskInfo.getPropertyId());
			List<User> userAdminList = userService.list(queryUser);
			if (userAdminList.size() > 0) {
				for (User user : userAdminList) {
					TodayDynamic todayDynamicT = new TodayDynamic();
					BeanUtils.copyProperties(todayDynamic, todayDynamicT);
					todayDynamicT.setUserId(user.getId());
					todayDynamicT.setMessageType(1);
					userIds.add(user.getId());
					redisService4.lPush("MqttTaskStateMessage", JSON.toJSONString(todayDynamicT));
					todayDynamicList.add(todayDynamicT);
				}
			}
			// 3：任务对应的普通管理员
			// 自己负责的
			QueryWrapper<TaskUser> query2 = WrapperUtils.getWrapper(false);
			query2.eq("task_id", taskId);
			List<TaskUser> taskUserList = taskUserService.list(query2);
			if (taskUserList.size() > 0) {
				for (TaskUser taskUser : taskUserList) {
					// 判断是用户上报的需要指派的就通知一下指派员
					if ((taskInfo.getTaskType().intValue() == 3 || taskInfo.getTaskType().intValue() == 4) && taskInfo.getStatus() == 1) {
						String report_manage_user = taskInfo.getReportManageUser();
						String[] report_manage_users = report_manage_user.split(",");
						if (report_manage_users.length > 0) {
							for (String userName : report_manage_users) {
								QueryWrapper<User> queryU = WrapperUtils.getWrapper(false);
								queryU.eq("name", userName);
								queryU.last(" limit 1");
								User temUser = userService.getOne(queryU);
								if (temUser != null) {
									TodayDynamic todayDynamicT = new TodayDynamic();
									BeanUtils.copyProperties(todayDynamic, todayDynamicT);
									if (userIds.contains(temUser.getId())) {
										continue;// 已经加过了就不在加了
									}
									todayDynamicT.setUserId(temUser.getId());
									todayDynamicT.setMessageType(1);
									userIds.add(temUser.getId());
								}
							}
						}
						continue;
					}
					// 组长先不考虑了
//					if ((taskInfo.getTaskType().intValue() == 3 || taskInfo.getTaskType().intValue() == 4) && taskInfo.getStatus() == 2) {
//						String report_manage_user = taskInfo.getUploadManageUser();
//						String[] report_manage_users = report_manage_user.split(",");
//						if (report_manage_users.length > 0) {
//							for (String userName : report_manage_users) {
//								QueryWrapper<User> queryU = WrapperUtils.getWrapper(false);
//								queryU.eq("name", userName);
//								queryU.last(" limit 1");
//								User temUser = userService.getOne(queryU);
//								if (temUser != null) {
//									TodayDynamic todayDynamicT = new TodayDynamic();
//									BeanUtils.copyProperties(todayDynamic, todayDynamicT);
//									if (userIds.contains(temUser.getId())) {
//										continue;// 已经加过了就不在加了
//									}
//									todayDynamicT.setUserId(temUser.getId());
//									userIds.add(taskUser.getId());
//								}
//							}
//						}
//						continue;
//					}
					TodayDynamic todayDynamicT = new TodayDynamic();
					BeanUtils.copyProperties(todayDynamic, todayDynamicT);
					if (userIds.contains(taskUser.getUserId())) {
						continue;// 已经加过了就不在加了
					}
					todayDynamicT.setUserId(taskUser.getId());
					todayDynamicT.setMessageType(1);
					userIds.add(taskUser.getUserId());

					redisService4.lPush("MqttTaskStateMessage", JSON.toJSONString(todayDynamicT));
					todayDynamicList.add(todayDynamicT);
				}
			}

			todayDynamicService.saveBatch(todayDynamicList);
			log.info("任务ID=" + taskId + "推送消息人ID集合" + JSON.toJSONString(userIds));
			taskId = redisService4.leftPop("IntellectualTaskIdList");
		}
	}

	// 下发通知消息
	@Scheduled(cron = "0/2 * * * * ?")
	@Async("scheduledExecutorService")
	public void MqttTaskStateMessage() {
		String mqttTaskStateMessage = redisService4.leftPop("MqttTaskStateMessage");
		while (mqttTaskStateMessage != null) {
			TodayDynamic todayDynamic = JSON.parseObject(mqttTaskStateMessage, TodayDynamic.class);
			TodayDynamicMessage todayDynamicMessage = new TodayDynamicMessage();
			BeanUtils.copyProperties(todayDynamic, todayDynamicMessage);
			todayDynamicMessage.setMessageType(0);
			todayDynamicMessage.setTaskName(todayDynamic.getName());
			Long userId = todayDynamic.getUserId();
			User user = userService.getById(userId);
			if (user != null) {
				String topic = user.getMobile() + "_" + todayDynamic.getDistrictId();// 发布主题
				String taskTime = DateUtil.format(todayDynamic.getCreateTime(), "HH:mm");
				todayDynamicMessage.setTaskTime(taskTime);
				mqttPushClient.publish(1, false, topic, JSON.toJSONString(todayDynamicMessage));
			}
			mqttTaskStateMessage = redisService4.leftPop("MqttTaskStateMessage");
		}
	}

}
