package cn.hmsoft.hmplatform.wxtoken.service;

import java.time.LocalDateTime;

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

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.helper.BeanHelper;
import cn.hmsoft.hmplatform.wxtoken.data.dao.PWxDefineDao;
import cn.hmsoft.hmplatform.wxtoken.data.dao.PWxDeployDao;
import cn.hmsoft.hmplatform.wxtoken.data.model.PWxDefine;
import cn.hmsoft.hmplatform.wxtoken.data.model.PWxDeploy;
import cn.hmsoft.hmplatform.wxtoken.data.model.PWxDeployLog;
import cn.hmsoft.hmplatform.wxtoken.data.model.PWxTokenNotice;
import cn.hmsoft.hmplatform.wxtoken.util.WeixinTokenModuleUtil;
import cn.hmsoft.log.LogHelper;

@Service
public class PlatformWeixinTokenService {

	@Autowired
	private PWxDefineDao daoDefine;
	@Autowired
	private PWxDeployDao daoDeploy;

	public void fetchToken(PWxDefine w, String internet_addr, String deploy_addr, String request_addr) {
		PWxDefine wx = this.daoDefine.findWxDefine(w.getApp_id());
		
		WeixinTokenModuleUtil util = WeixinTokenModuleUtil.instance(false);
		if (wx == null) {
			// 如果是新的，自动添加
			wx = BeanHelper.copy(PWxDefine.class, w);
			wx.setApp_accesstoken(null);
			wx.setApp_jsapi(null);
			wx.setApp_token_expired(null);
			wx.setApp_create_time(LocalDateTime.now());
			this.daoDefine.insert(wx);
		} else {
			wx.setApp_secrect(w.getApp_secrect());
			wx.setWx_name(w.getWx_name());
			wx.setApp_name(w.getWx_name());
			wx.setApp_update_time(LocalDateTime.now());
			this.daoDefine.update(wx);
		}
		PWxDeploy deploy = this.daoDeploy.findDeploy(wx.getToken_id(), internet_addr, deploy_addr);
		if (deploy == null) {
			deploy = new PWxDeploy();
			deploy.setDeploy_addr(deploy_addr);
			deploy.setInternet_addr(internet_addr);
			deploy.setToken_id(wx.getToken_id());
			deploy.setRequest_time(LocalDateTime.now());
			this.daoDefine.insert(deploy);
		} else {
			deploy.setRequest_time(LocalDateTime.now());
			this.daoDefine.update(deploy);
		}

		PWxDeployLog log = new PWxDeployLog();
		log.setApp_accesstoken(wx.getApp_accesstoken());
		log.setApp_accesstokenkey(wx.getApp_accesstokenkey());
		log.setApp_token_expired(wx.getApp_token_expired());
		log.setDeploy_addr(deploy.getDeploy_addr());
		log.setInternet_addr(deploy.getInternet_addr());
		log.setRequest_addr(request_addr);
		log.setRequest_time(LocalDateTime.now());
		log.setToken_id(wx.getToken_id());
		this.daoDefine.insert(log);
		// 加入到线程中
		final PWxDefine ww = BeanHelper.copy(PWxDefine.class, wx);
		new Thread(() -> {
			// 等待写入服务器
			try {
				Thread.sleep(500);
			}catch(Exception e) {
				
			}
			util.addWxDefine(ww);
		}).start();

	}

	/********************************
	 * 通知记录
	 * 
	 * @param deploy
	 * @param ajax
	 */
	public void logTokenNotice(PWxDefine wx, PWxDeploy deploy, Ajax ajax) {
		PWxTokenNotice notice = new PWxTokenNotice();
		notice.setApp_accesstoken(wx.getApp_accesstoken());
		notice.setApp_accesstokenkey(wx.getApp_accesstokenkey());
		notice.setApp_token_expired(wx.getApp_token_expired());
		notice.setNotice_addr(deploy.getInternet_addr());
		notice.setNotice_status((ajax != null && ajax.isSuccess()) ? FrameStatus.Active.toString() : FrameStatus.InActive.toString());
		notice.setToken_id(wx.getToken_id());
		notice.setNotice_time(LocalDateTime.now());
		try {
			this.daoDefine.insert(notice);
			this.daoDeploy.noticeDeploy(wx.getToken_id(), deploy.getInternet_addr());
		} catch (Exception e) {
			LogHelper.error(e);

		}
	}
}
