package cn.hmsoft.weixin.service;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.service.FrameService;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.weixin.constants.WxAppType;
import cn.hmsoft.weixin.data.dao.WxDefineDao;
import cn.hmsoft.weixin.data.model.WxDefine;
import cn.hmsoft.weixin.data.model.WxMaterial;
import cn.hmsoft.weixin.data.model.WxMenuDefine;
import cn.hmsoft.weixin.data.model.WxMsgDefine;
import cn.hmsoft.weixin.data.model.WxMsgProcessor;
import cn.hmsoft.weixin.data.model.WxMsgReply;
import cn.hmsoft.weixin.data.model.WxParam;
import cn.hmsoft.weixin.data.model.WxTag;
import cn.hmsoft.weixin.data.model.WxTemplate;
import cn.hmsoft.weixin.data.model.WxUser;
import cn.hmsoft.weixin.data.model.WxUserLog;
import cn.hmsoft.weixin.data.model.WxUserTag;

@Service
public class WxDefineService extends FrameService {

	@Autowired
	private WxDefineDao daoDefine;

	public void addDefine(WxDefine wx, FrameOptr optr) {
		FrameAssertUtil.checkParam(wx.getApp_id(), wx.getWx_name(), wx.getWx_type());
		if (WxAppType.WeChatPublic.equals(wx.getWx_type()))
			FrameAssertUtil.isNotEmpty(wx.getWx_code(), "当类型为公众号时，微信号不允许为空！");
		String sql = "select * from wx_define where app_id=?";
		FrameAssertUtil.isNull(this.daoDefine.findBySql(sql, wx.getApp_id()), "该微信公众号已经存在，请检查");
		this.daoDefine.insert(wx);
	}

	public void editDefine(WxDefine wx, FrameOptr optr) {
		FrameAssertUtil.checkParam(wx.getWx_id(), wx.getApp_id(), wx.getWx_name(), wx.getWx_type());
		if (WxAppType.WeChatPublic.equals(wx.getWx_type()))
			FrameAssertUtil.isNotEmpty(wx.getWx_code(), "当类型为公众号时，微信号不允许为空！");
		String sql = "select * from wx_define where app_id=? and wx_id!=?";
		FrameAssertUtil.isNull(this.daoDefine.findBySql(sql, wx.getApp_id(), wx.getWx_id()), "该微信公众号已经存在，请检查");
		this.daoDefine.update(wx);
	}

	public void deleteDefine(int wx_id, FrameOptr optr) {
		WxDefine wx = this.daoDefine.find(wx_id);
		if (wx != null) {
			if (wx.getApp_token_expired() != null && wx.getApp_token_expired().isAfter(LocalDateTime.now()))
				throw new BusinessException("该公众号正在被使用，禁止删除！");
		}
		// 删除管理表
		this.daoDefine.delete(WxDefine.class,"wx_id",wx_id);
		this.daoDefine.delete(WxMaterial.class,"wx_id",wx_id);
		this.daoDefine.delete(WxMenuDefine.class,"wx_id",wx_id);
		this.daoDefine.delete(WxMsgDefine.class,"wx_id",wx_id);
		this.daoDefine.delete(WxMsgProcessor.class,"wx_id",wx_id);
		this.daoDefine.delete(WxMsgReply.class,"wx_id",wx_id);
		this.daoDefine.delete(WxParam.class,"wx_id",wx_id);
		this.daoDefine.delete(WxTag.class,"wx_id",wx_id);
		this.daoDefine.delete(WxTemplate.class,"wx_id",wx_id);
		this.daoDefine.delete(WxUser.class,"wx_id",wx_id);
		this.daoDefine.delete(WxUserLog.class,"wx_id",wx_id);
		this.daoDefine.delete(WxUserTag.class,"wx_id",wx_id);
	}
}
