
package org.tio.sitexxx.im.server.timetask;

import cn.hutool.core.collection.CollectionUtil;
import org.tio.sitexxx.im.server.handler.wx.WxChatApi;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.model.main.WxGroupUser;
import org.tio.sitexxx.service.service.base.UserService;
import org.tio.sitexxx.service.service.chat.ChatIndexService;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.wx.SysMsgVo;
import org.tio.utils.jfinal.P;
import org.tio.utils.quartz.AbstractJobWithLog;

import java.util.List;

/**
 * 群聊禁言定时任务处理
 * @author xufei
 * 2020年7月3日 下午2:56:29
 */
public class GroupForbiddenJob extends AbstractJobWithLog {

	private static Logger log = LoggerFactory.getLogger(GroupForbiddenJob.class);

	private static boolean isRunning = false;

	@Override
	public void run(JobExecutionContext context) {
		boolean allowExecute = P.getBoolean("quartz.open.flag", false);
		if (allowExecute && isStart()) {
			try {
				List<WxGroupUser> groupUsers = WxGroupUser.dao.find("select * from wx_group_user where forbiddenflag = ? and cancelforbiddentime < now()",
				        Const.Forbiddenflag.DURATION);
				if (CollectionUtil.isNotEmpty(groupUsers)) {
					for (WxGroupUser groupUser : groupUsers) {
						User user = UserService.ME.getById(groupUser.getUid());
						if (user == null) {
							continue;
						}
						try {
							WxGroupUser updateGroupUser = new WxGroupUser();
							updateGroupUser.setId(groupUser.getId());
							updateGroupUser.setForbiddenflag(Const.Forbiddenflag.NO);
							updateGroupUser.setForbiddenduration(0);
							updateGroupUser.setCancelforbiddentime(null);
							boolean update = updateGroupUser.update();
							if (!update) {
								log.error("自动解除禁言异常");
								continue;
							}
							ChatIndexService.clearGroupUserCache(groupUser.getId());
							ChatIndexService.clearGroupUserListCache(groupUser.getGroupid());
							SysMsgVo sysMsgVo = new SysMsgVo(user.getNick(), Const.MsgTemplate.CANCEL_FORBIDDEN, user.getNick(),
									Const.MsgTemplateKey.GROUP_CANCEL_FORBIDDEN);
							WxChatApi.sendGroupMsgEach(sysMsgVo.toText(), Const.ContentType.TEXT, user.getId(),
									groupUser.getGroupid(), Const.YesOrNo.YES, sysMsgVo);
						} catch (Exception e) {
							log.error(e.getMessage(), e);
						}
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				end();
			}
		}
	}

	/**
	 * 判断计划任务是否可以开始:true：可以运用；false：不可以
	 * @return
	 * @author xufei
	 */
	private static boolean isStart() {
		boolean ret;
		synchronized (GroupForbiddenJob.class) {
			ret = !isRunning && (isRunning = true);
		}
		return ret;
	}

	/**
	 * 计划任务结束处理
	 * @author xufei
	 */
	private static void end() {
		synchronized (GroupForbiddenJob.class) {
			isRunning = false;
		}
	}
}
