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

import cn.hutool.core.collection.CollectionUtil;
import org.quartz.JobExecutionContext;
import org.tio.sitexxx.im.server.handler.wx.call.WxCallUtils;
import org.tio.sitexxx.service.model.main.WxCallItem;
import org.tio.utils.quartz.AbstractJobWithLog;

import java.util.List;

/**
 * 音视频通话，响应超时检查任务
 * @author tanyaowu
 */
public class WxCallRespTimeoutJob extends AbstractJobWithLog {

	@Override
	public void run(JobExecutionContext context) {

		check();
	}

	private static final String sql = "select * from wx_call_item where (status = ? or status = ?) and calltime < date_sub(now(), interval 40 second)";
	private static final String sqlexp = "select * from wx_call_item where hanguptype = ? and calltime < date_sub(now(), interval 24 HOUR)";

	public static void check() {
		List<WxCallItem> list = WxCallItem.dao.find(sql, WxCallItem.Status.BEGIN_CALLING,WxCallItem.Status.TCP_CONNECTED);
		if (CollectionUtil.isNotEmpty(list)) {
			for (WxCallItem wxCallItem : list) {
				WxCallUtils.endCall(null, wxCallItem.getId(), null, WxCallItem.Hanguptype.RESP_TIMEOUT);
			}	
		}
		List<WxCallItem> listexp = WxCallItem.dao.find(sqlexp, WxCallItem.Hanguptype.NOT_HANGUP);
		if (CollectionUtil.isNotEmpty(listexp)) {
			for (WxCallItem wxCallItem : listexp) {
				WxCallUtils.endCall(null, wxCallItem.getId(), null, WxCallItem.Hanguptype.TCP_DROPPED);
			}
		}
	}
}
