package com.goafanti.common.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.error.BusinessException;


@Component
public class WeChatUtils {


	@Value(value = "${wx.appId}")
	private String appId;
	@Value(value = "${wx.appSecret}")
	private String appSecret;
	@Value(value = "${wx.state}")
	private String wxState;
	@Autowired
	private RedisUtil redisUtil;

	//待审核通知
	private static final String sptz="j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I";
	//审核结通知
	private static final String jgtz="pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI";
	private static  String send_url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";




	/**
	 *
	 * @param openid
	 * @param type 状态 0驳回 1发起 2同意
	 * @param date
	 */
	public Integer addNotice(String openid ,Integer type,Integer id, Date date, String aname,String remarks) {
		Map<String, Object> map=new HashMap<String, Object>();
		String accessToken=getAccessToken();
		String url=send_url.replace("ACCESS_TOKEN", accessToken);
		map.put("access_token", accessToken);
		map.put("touser", openid);
		map.put("page", "pages/egressDetails/index?id="+id);
		map.put("miniprogram_state", wxState);
		if (type==1) {
			map.put("template_id", sptz);
			map.put("data",getData(type,date, aname,remarks));
		}else {
			map.put("template_id", jgtz);
			map.put("data",getData(type,date, aname,remarks));
		}
		JSONObject res=HttpUtils.httpPost(url, map);
		if (res.get("errcode")!=null) {
			return res.getInteger("errcode");
		}else {
			return 1;
		}

	}
	private Map<String, Object> getData(Integer type,Date date, String aname, String remarks) {
		Map<String, Object> we=new HashMap<String, Object>();
		String strDate=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
		if (type==1) {
			Map<String, Object> v1=new HashMap<String, Object>();
			v1.put("value",strDate);
			we.put("time5", v1);
			Map<String, Object> v2=new HashMap<String, Object>();
			v2.put("value", aname);
			we.put("name3", v2);
			Map<String, Object> v3=new HashMap<String, Object>();
			v3.put("value", remarks);
			we.put("thing10", v3);
		}else {
			String thing2="";
			if (type==0){
				thing2="驳回";
			}else if (type==2){
				thing2="通过";
			}
			Map<String, Object> v1=new HashMap<String, Object>();
			v1.put("value",strDate);
			we.put("date3", v1);
			Map<String, Object> v2=new HashMap<String, Object>();
			v2.put("value", thing2);
			we.put("thing2", v2);
			Map<String, Object> v3=new HashMap<String, Object>();
			v3.put("value", remarks);
			we.put("thing6", v3);
		}
		return we;
	}




	public String getAccessToken() {
		Date date = new Date();
		if (redisUtil.presence("access_token")||redisUtil.presence("expires_in")) {
			String access_token=redisUtil.getString("access_token");
			String expires_in=redisUtil.getString("expires_in");
			if (date.getTime() < Long.valueOf(expires_in)) {
				LoggerUtils.debug(getClass(), "redis中获取accestoken");
				return access_token;
			} else {
				LoggerUtils.debug(getClass(), "accestoken超时重新获取");
				redisUtil.deleteString("access_token");
				redisUtil.deleteString("expires_in");
				return setSessionAccessToken(date);
			}

		} else {
			return setSessionAccessToken(date);
		}


	}



	private String setSessionAccessToken(Date date) {
		String url = String.format(
				"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
				appSecret);
		JSONObject js = HttpUtils.httpGet(url);
		String errcode =js.getString("errcode");
		if (errcode!=null) {
			throw new  BusinessException("获取AccessToken失败！");
		}else {
			LoggerUtils.debug(AsyncUtils.class,"获取并存入AccessToken成功,accesstoken="+js.getString("access_token"));
		}
		String access_token = js.getString("access_token");
		Long expires_in = js.getLong("expires_in");
		//返回时间为秒需要计算换成毫秒
		expires_in=expires_in*1000;
		expires_in+=date.getTime();
		redisUtil.setString("access_token", access_token);
		redisUtil.setString("expires_in", expires_in.toString());
		return access_token;
	}

}
