package com.cyksj.service.corp.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.IoKit;
import com.cyksj.common.util.J11HttpC;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.config.corp.WeChatCorpFactory;
import com.cyksj.config.corp.YHLXWxCorpConfig;
import com.cyksj.dto.CorpOauth2UserInfo;
import com.cyksj.dto.RedisKey;
import com.cyksj.model.entity.WxCorpApp;
import com.cyksj.model.request.corp.CorpFollowContact;
import com.cyksj.model.request.corp.CorpGroupConfig;
import com.cyksj.model.request.corp.CorpMsgTemplate;
import com.cyksj.model.request.corp.CorpXmlMessage;
import com.cyksj.model.request.corp.link.WxCpLinkAdd;
import com.cyksj.model.request.corp.link.WxCpLinkAddResp;
import com.cyksj.model.request.corp.link.WxCpLinkUpd;
import com.cyksj.model.request.corp.message.WxCorpAgentMessage;
import com.cyksj.model.response.OffiaccountJsTicket;
import com.cyksj.model.response.UserExternalContactList;
import com.cyksj.model.response.UserWithExternalPermission;
import com.cyksj.model.response.WechatAccessToken;
import com.cyksj.model.response.corp.*;
import com.cyksj.redis.RedisService;
import com.cyksj.service.corp.WxCorpOps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/*
 *项目名: netflix
 *文件名: WxCorpOpsImpl
 *创建者: JavaZou
 *创建时间:2022/10/13 16:46
 */
@Service
@Slf4j
public class WxCorpOpsImpl implements WxCorpOps {
	@Autowired
	private YHLXWxCorpConfig yhlXWxCorpConfig;

	@Autowired
	private RedisService redisService;

	@Override
	public CorpExternalContactInfo getExternalContact(String corpId, String userId) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token=" + getAccessToken(corpId) + "&external_userid=" + userId;
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户详情错误: " + res.body());
		}
		log.info("企业微信客户详情:{}", res.body());
		return Jsons.parseObject(res.body(), CorpExternalContactInfo.class);
	}

	@Override
	public String getAccessToken(String corpId) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		String accessToken = redisService.getStr(RedisKey.CORP_ACCESS_TOKEN_KEY + corpId);
		if (StringUtils.isNotBlank(accessToken)) {
			return accessToken;
		}
		return access_token(corpId);
	}
	@Override
	public String getMsgAccessToken(String corpId) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		String accessToken = redisService.getStr(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId);
		if (StringUtils.isNotBlank(accessToken)) {
			return accessToken;
		}
		return getKfAccessToken(corpId);
	}

	@Override
	public CorpUserExternalTagGroupList getCorpTagList(String corpId, List<String> tagIds, List<String> groupIds) throws Exception {
		JSONObject params = new JSONObject();
		if (!tagIds.isEmpty()) {
			params.putOpt("tag_id", tagIds);
		}
		if (!groupIds.isEmpty()) {
			params.putOpt("group_id", groupIds);
		}

		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=" + getAccessToken(corpId);
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信获取企业标签库错误: " + res.body());
		}

		return Jsons.parseObject(res.body(), CorpUserExternalTagGroupList.class);
	}

	@Override
	public List<String> getFollowUserList(String corpId) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list?access_token=" + getAccessToken(corpId);
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户联系功能的成员列表错误:" + res.body());
		}

		return Jsons.parseObject(res.body(), UserWithExternalPermission.class).getFollowers();
	}

	@Override
	public CorpUserInfo getFollowUser(String corpId, String userId) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + getAccessToken(corpId) + "&userid=" + userId;
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户联系功能的成员列表错误:" + res.body());
		}

		return Jsons.parseObject(res.body(), CorpUserInfo.class);
	}

	@Override
	public List<String> listExternalContacts(String corpId, String userId) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token=" + getAccessToken(corpId) + "&userid=" + userId;
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户列表错误:" + res.body());
		}

		return Jsons.parseObject(res.body(), UserExternalContactList.class).getExternalUserId();
	}

	@Override
	public String createUrl(String corpId, String state) {
		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
		return "https://open.weixin.qq.com/connect/oauth2/authorize" +
				"?appid=" + wxCorpApp.getCorpId() +
				"&redirect_uri=" + URLEncoder.encode(wxCorpApp.getDomain() + "/manage/corp/redirect", IoKit.Charsets.UTF_8.getCharset()) +
				"&response_type=code" +
				"&scope=snsapi_base" +
				"&state=" + state +
				"#wechat_redirect";
	}

	@Override
	public CorpOauth2UserInfo getUserInfo(String corpId, String code) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo" +
				"?access_token=" + getAccessToken(corpId) +
				"&code=" + code;

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.cacheURI()
						.send(HttpResponse.BodyHandlers.ofString());

		log.info("企业微信根据code获取成员信息 res:{}", res.body());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信根据code获取成员信息失败: " + res.body());
		}

		return Jsons.parseObject(res.body(), CorpOauth2UserInfo.class);
	}

	@Override
	public String getJsTicket(String corpId) throws Exception {
		String jsTicket = redisService.getStr(RedisKey.CORP_JS_TICKET_KEY + corpId);
		if (StrUtil.isBlank(jsTicket)) {
			return js_ticket(corpId);
		}
		return jsTicket;
	}

	@Override
	public String getAgentJsTicket(String corpId, String agentId) throws Exception {
		String ticket = redisService.getStr(RedisKey.CORP_AGENT_JS_TICKET_KEY + corpId);
		if (StrUtil.isBlank(ticket)) {
			return agentJsTicket(corpId);
		}
		return ticket;
	}

	@Override
	public String getCorpChatGroups(String corpId, String configId) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get_join_way?access_token=" + getAccessToken(corpId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.putOpt("config_id", configId);
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.cacheURI()
						.body(HttpRequest.BodyPublishers.ofString(jsonObject.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofByteArray());
		String body = IoKit.toString(res.body());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取access_token失败: " + body);
		}
		JSONObject re = Jsons.parseObject(body, JSONObject.class);
		Integer errcode = re.getInt("errcode");
		if (null != errcode && 0 != errcode) {
			throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + re.getStr("errmsg") + "[" + errcode + "]");
		}
		JSONObject codeRe = Jsons.parseObject(re.getStr("join_way"), JSONObject.class);
		String qr_code = codeRe.getStr("qr_code");
		return qr_code;
	}

	@Override
	public String setGroupsConfig(List<String> chatIds) throws Exception {
		String corpId = yhlXWxCorpConfig.getCorpId();
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/add_join_way?access_token=" + getAccessToken(corpId);
		JSONObject jsonObject = new JSONObject();
		//scene	是	场景。
		//1 - 群的小程序插件
		//2 - 群的二维码插件
		jsonObject.putOpt("scene", 2);
		jsonObject.putOpt("chat_id_list", chatIds);
		jsonObject.putOpt("room_base_name", "Ai银河课堂 MJ SD教程分享");
		//auto_create_room当群满了后，是否自动新建群。0-否；1-是。 默认为1
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.cacheURI()
						.body(HttpRequest.BodyPublishers.ofString(jsonObject.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofByteArray());
		String body = IoKit.toString(res.body());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取access_token失败: " + body);
		}
		log.info("配置企业微信群返回信息body:{}", body);
		return body;
	}

	@Override
	public String getGroupsDetail(String chatId) throws Exception {
		String corpId = yhlXWxCorpConfig.getCorpId();
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=" + getAccessToken(corpId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.putOpt("chat_id", chatId);
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.cacheURI()
						.body(HttpRequest.BodyPublishers.ofString(jsonObject.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofByteArray());
		String body = IoKit.toString(res.body());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取access_token失败: " + body);
		}
		return body;
	}

	@Override
	public String corpFollowContactConfig(CorpFollowContact corpFollowContact) throws Exception {
		//配置
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=" + getAccessToken(null);
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.cacheURI()
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(corpFollowContact), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofByteArray());
		String body = IoKit.toString(res.body());
		JSONObject jsonObject = Jsons.parseObject(body, JSONObject.class);
		if (jsonObject.getInt("errcode") != 0) {
			throw BusinessRuntimeException.getInstance("配置客服活码失败");
		}
		return body;
	}

	@Override
	public void updateCorpFollowContactConfig(CorpFollowContact corpFollowContact) throws Exception {
		//配置
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token=" + getAccessToken(null);
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.cacheURI()
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(corpFollowContact), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofByteArray());
		String body = IoKit.toString(res.body());
		JSONObject jsonObject = Jsons.parseObject(body, JSONObject.class);
		if (jsonObject.getInt("errcode") != 0) {
			throw BusinessRuntimeException.getInstance("更新客服活码失败");
		}
	}

	@Override
	public void sendAgentMessage(String corpId, WxCorpAgentMessage agentMessage) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + getAccessToken(corpId);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(agentMessage), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("发送应用消息: " + res.body());
		}

		JSONObject response = Jsons.parseObject(res.body(), JSONObject.class);
		if (response.getInt("errcode") != 0) {
			throw BusinessRuntimeException.getInstance("发送应用消息: " + response.getStr("errmsg"));
		}
	}

	@Override
	public CorpGroupChatListDetail getGroupChatList(String corpId, String cursor, Integer limit) throws Exception {
		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=%s", getAccessToken(corpId));
		JSONObject params = new JSONObject();
		params.putOpt("cursor", cursor);
		params.putOpt("limit", limit);
		HttpResponse<String> response = J11HttpC.custom()
				.ofPost()
				.headers(J11HttpC.ReqType.raw_json)
				.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
				.url(url)
				.send(HttpResponse.BodyHandlers.ofString());
		CorpGroupChatListDetail chatListDetail = Jsons.parseObject(response.body(), CorpGroupChatListDetail.class);
		if (response.statusCode() != 200 || chatListDetail.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户群列表错误:" + chatListDetail.getErrmsg());
		}
		return chatListDetail;
	}

	@Override
	public CorpGroupChatDetail getCorpGroupChatDetail(String corpId, CorpXmlMessage message) throws Exception {
		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=%s", getAccessToken(corpId));
		JSONObject params = new JSONObject();
		params.putOpt("chat_id", message.getChatId());
		//是否需要返回群成员的名字group_chat.member_list.name。0-不返回；1-返回。默认不返回
		params.putOpt("need_name", 1);
		HttpResponse<String> response = J11HttpC.custom()
				.ofPost()
				.headers(J11HttpC.ReqType.raw_json)
				.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
				.url(url)
				.send(HttpResponse.BodyHandlers.ofString());
		JSONObject jsonObject = Jsons.parseObject(response.body(), JSONObject.class);
		if (response.statusCode() != 200 || jsonObject.getInt("errcode") != 0) {
			throw BusinessRuntimeException.getInstance("企业微信获取客户群详情错误:" + jsonObject.get("errmsg"));
		}
		return Jsons.parseObject(jsonObject.get("group_chat"), CorpGroupChatDetail.class);
	}

	@Override
	public CorpUserExternalTagGroupInfo addCorpTag(CorpUserExternalTagGroupInfo tagGroup, String corpId) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token=" + getAccessToken(corpId);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(tagGroup.getTagGroup()), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信添加企业客户标签错误: " + res.body());
		}

		CorpUserExternalTagGroupInfo corpUserExternalTagGroupInfo = Jsons.parseObject(res.body(), CorpUserExternalTagGroupInfo.class);
		if (corpUserExternalTagGroupInfo.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("新增标签错误:{0}", corpUserExternalTagGroupInfo.getErrmsg());
		}

		return corpUserExternalTagGroupInfo;
	}

	@Override
	public List<CorpUserExternalTagGroupInfo.TagGroup> getAllCorpTagByGroupsId(String groupId, String corpId) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		//获取该标签组下的所有标签
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=" + getAccessToken(corpId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.putOpt("group_id", List.of(groupId));
		HttpResponse<String> res = J11HttpC.custom()
				.ofPost()
				.url(url)
				.headers(J11HttpC.ReqType.raw_json)
				.body(HttpRequest.BodyPublishers.ofString(jsonObject.toString(), IoKit.Charsets.UTF_8.getCharset()))
				.send(HttpResponse.BodyHandlers.ofString());
		JSONObject re = Jsons.parseObject(res.body(), JSONObject.class);
		List<CorpUserExternalTagGroupInfo.TagGroup> tagGroups = Jsons.parseList(re.get("tag_group"), CorpUserExternalTagGroupInfo.TagGroup.class);
		if (re.getInt("errcode") != 0) {
			throw BusinessRuntimeException.getInstance("获取标签组下所有标签错误");
		}
		return tagGroups;
	}

	@Override
	public void updateGroupsConfig(CorpGroupConfig config) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/update_join_way?access_token=" + getAccessToken(null);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(config), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("更新企业微信客户群配置失败: " + res.body());
		}
	}

	@Override
	public CorpBaseResult editCorpTag(JSONObject params) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?access_token=" + getAccessToken(null);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信编辑企业客户标签错误: " + res.body());
		}

		CorpBaseResult corpBaseResult = Jsons.parseObject(res.body(), CorpBaseResult.class);
		if (corpBaseResult.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("修改标签错误:{0}", corpBaseResult.getErrmsg());
		}
		return corpBaseResult;
	}

	@Override
	public CorpBaseResult delCorpTag(String[] tagIds, String[] groupIds) throws Exception {
		JSONObject params = new JSONObject();
		if (tagIds != null && tagIds.length > 0) {
			params.putOpt("tag_id", tagIds);
		}
		if (groupIds != null && groupIds.length > 0) {
			params.putOpt("group_id", groupIds);
		}
		//https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token=ACCESS_TOKEN

		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token=" + getAccessToken(null);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信删除企业客户标签错误: " + res.body());
		}

		CorpUserExternalTagGroupInfo corpUserExternalTagGroupInfo = Jsons.parseObject(res.body(), CorpUserExternalTagGroupInfo.class);
		if (corpUserExternalTagGroupInfo.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("企业微信删除企业客户标签错误:{0}", corpUserExternalTagGroupInfo.getErrmsg());
		}

		return Jsons.parseObject(res.body(), CorpBaseResult.class);
	}

	@Override
	public CorpBaseResult markTag(String externalUserid, String userid, String[] tags, String[] removeTags) throws Exception {
		JSONObject params = new JSONObject();
		params.putOpt("external_userid", externalUserid);
		params.putOpt("userid", userid);
		if (tags != null && tags.length > 0) {
			params.putOpt("add_tag", tags);
		}
		if (removeTags != null && removeTags.length > 0) {
			params.putOpt("remove_tag", removeTags);
		}
		//https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token=ACCESS_TOKEN

		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token=" + getAccessToken(null);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信编辑企业客户标签错误: " + res.body());
		}

		return Jsons.parseObject(res.body(), CorpBaseResult.class);
	}

	@Override
	public CorpMsgTemplateAddResult addMsgTemplate(String corpId, CorpMsgTemplate msgTemplate) throws Exception {
		corpId = Optional.ofNullable(corpId).orElse(yhlXWxCorpConfig.getCorpId());
		String accessToken = getAccessToken(corpId);
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template?access_token=" + accessToken;
		log.info("发送的内容为: {}",Jsons.toJson(msgTemplate));
		log.info("企业微信群发消息 corpId:{},userCount:{}", corpId, msgTemplate.getExternalUserid().size());
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(msgTemplate), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("企业微信创建企业群发错误: " + res.body());
		}
		log.info("企业微信创建群发 result:{}",res.body());

		return Jsons.parseObject(res.body(),CorpMsgTemplateAddResult.class);
	}

	@Override
	public String getMediaIdByFileName(String msgType, String url, String corpId) {
		//临时解决
		if ("miniprogram".equals(msgType)) {
			msgType = "image";
		}
		String[] split = url.split("/");
		String filename = split[split.length - 1];
		String mediaId = redisService.getStr(String.format(RedisService.key.CORP_WECLOME_MEDIA.getName(), corpId) + filename.split("\\.")[0]);

		if (StringUtils.isBlank(mediaId)) {
			File file = new File("./" + filename);
			HttpUtil.downloadFile(url, file);

			Map<String, Object> paramMap = new HashMap<>();
			//文件上传只需将参数中的键指定（默认file），值设为文件对象即可，对于使用者来说，文件上传与普通表单提交并无区别
			paramMap.put("media", FileUtil.file(file.getAbsolutePath()));

			try {
				String result = HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + msgType + "&access_token=" + getAccessToken(corpId), paramMap);

				CorpMediaUploadResult corpMediaUploadResult = JSONUtil.toBean(result, CorpMediaUploadResult.class);
				if (StringUtils.isBlank(corpMediaUploadResult.getMediaId())) {
					throw BusinessRuntimeException.getInstance("企微上传素材错误。回现素材为空.");
				}
				FileUtil.del(file);
				long time = (corpMediaUploadResult.getCreatedAt() + 60 * 60 * 24 * 3 - 60 * 60) - System.currentTimeMillis() / 1000;
				redisService.set(String.format(RedisService.key.CORP_WECLOME_MEDIA.getName(), corpId) + filename, corpMediaUploadResult.getMediaId(), time);
				return corpMediaUploadResult.getMediaId();
			} catch (Exception e) {
				FileUtil.del(file);
				throw BusinessRuntimeException.getInstance("上传临时素材出错,errorMsg:" + StringUtil.getErrorText(e));
			}
		}

		return mediaId;
	}

	@Override
	public CorpBaseResult remindBulkTask(String msgid, String corpId) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remind_groupmsg_send?access_token=" + getAccessToken(corpId);
		log.info("提醒客服群发消息msgId:{}", msgid);
		JSONObject json = new JSONObject();
		json.putOpt("msgid", msgid);
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(json.toString(), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("提醒客服群发消息错误: " + res.body());
		}
		log.info("提醒客服群发消息 result:{}", res.body());
		return Jsons.parseObject(res.body(), CorpBaseResult.class);
	}

	public String access_token(String corpId) throws Exception {
		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
		if (corpId == null) {
			throw BusinessRuntimeException.getInstance("{0}企业微信不存在", corpId);
		}
		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", wxCorpApp.getCorpId(), wxCorpApp.getCorpSecret());
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.cacheURI()
						.send(HttpResponse.BodyHandlers.ofByteArray());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取access_token失败: " + IoKit.toString(res.body()));
		}

		WechatAccessToken token = Jsons.parseObject(res.body(), WechatAccessToken.class);
		if (null == token) {
			throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + IoKit.toString(res.body()));
		}

		final Integer errcode = token.getErrcode();
		if (null != errcode && 0 != errcode) {
			throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + token.getErrmsg() + "[" + errcode + "]");
		}
		//存入redis
		String accessToken = token.getAccessToken();
		// 设置过期时间 比微信官方给的时间提前300秒过期
		redisService.set(RedisKey.CORP_ACCESS_TOKEN_KEY + corpId, accessToken, token.getExpiresIn() - 300L);
		return accessToken;
	}

	public String getKfAccessToken(String corpId) throws Exception {
		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
		if (StringUtils.isBlank(wxCorpApp.getMsgSecret())) {
			throw BusinessRuntimeException.getInstance("该企业会话存档权限尚未开通.请联系管理员配置api secret");
		}
		String accessToken = redisService.getStr(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId);
		if(StringUtils.isBlank(accessToken)){
			String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" +
					"?corpid=" + corpId +"&corpsecret=" + wxCorpApp.getMsgSecret();

			HttpResponse<String> res =
					J11HttpC.custom()
							.ofGet()
							.url(url)
							.send(HttpResponse.BodyHandlers.ofString());

			if (200 != res.statusCode()) {
				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + res.body());
			}
			JSONObject result = new JSONObject(res.body());

			final Integer errcode = result.getInt("errcode");
			if (null != errcode && 0 != errcode) {
				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + result.getStr("errmsg") + "[" + errcode + "]");
			}
			accessToken = result.getStr("access_token");
			redisService.set(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId, accessToken, result.getInt("expires_in") - 300L);
			return accessToken;
		}
		return accessToken;
	}

	public String js_ticket(String corpId) throws Exception {
		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s", this.getAccessToken(corpId));
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.cacheURI()
						.send(HttpResponse.BodyHandlers.ofByteArray());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + IoKit.toString(res.body()));
		}

		OffiaccountJsTicket ticket = Jsons.parseObject(res.body(), OffiaccountJsTicket.class);
		if (null == ticket) {
			throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + IoKit.toString(res.body()));
		}

		final Integer errcode = ticket.getErrcode();
		if (null != errcode && 0 != errcode) {
			throw BusinessRuntimeException.getInstance("获取js_ticket失败: " + ticket.getErrmsg() + "[" + errcode + "]");
		}
		String jsTicket = ticket.getTicket();
		redisService.set(RedisKey.CORP_JS_TICKET_KEY + corpId, jsTicket, ticket.getExpiresIn() - 300L);
		return jsTicket;
	}

	private String agentJsTicket(String corpId) throws Exception {
		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=%s&type=agent_config", this.getAccessToken(corpId));
		HttpResponse<byte[]> res =
				J11HttpC.custom()
						.ofGet()
						.url(url)
						.cacheURI()
						.send(HttpResponse.BodyHandlers.ofByteArray());

		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + IoKit.toString(res.body()));
		}

		OffiaccountJsTicket ticket = Jsons.parseObject(res.body(), OffiaccountJsTicket.class);
		if (null == ticket) {
			throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + IoKit.toString(res.body()));
		}
		final Integer errcode = ticket.getErrcode();
		if (null != errcode && 0 != errcode) {
			throw BusinessRuntimeException.getInstance("获取agent_js_ticket失败: " + ticket.getErrmsg() + "[" + errcode + "]");
		}
		String jsTicket = ticket.getTicket();
		redisService.set(RedisKey.CORP_AGENT_JS_TICKET_KEY + corpId, jsTicket, ticket.getExpiresIn() - 300L);
		return jsTicket;
	}


	@Override
	public WxCpLinkAddResp customerAcquisitionCreateLink(String corpId, WxCpLinkAdd wxCpLinkAdd) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/create_link?access_token=" + getAccessToken(corpId);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(wxCpLinkAdd), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("创建获客助手链接: " + res.body());
		}

		WxCpLinkAddResp resp = Jsons.parseObject(res.body(), WxCpLinkAddResp.class);
		if (resp.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("创建获客助手链接: " + resp.getErrmsg());
		}
		return resp;
	}


	@Override
	public WxCpLinkAddResp customerAcquisitionUpdateLink(String corpId, WxCpLinkUpd wxCpLinkUpd) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/update_link?access_token=" + getAccessToken(corpId);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(wxCpLinkUpd), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("编辑获客助手链接: " + res.body());
		}

		WxCpLinkAddResp resp = Jsons.parseObject(res.body(), WxCpLinkAddResp.class);
		if (resp.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("编辑获客助手链接: " + resp.getErrmsg());
		}
		return resp;
	}


	@Override
	public WxCpLinkAddResp customerAcquisitionDeleteLink(String corpId, WxCpLinkUpd wxCpLinkUpd) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/delete_link?access_token=" + getAccessToken(corpId);

		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(wxCpLinkUpd), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("删除获客助手链接: " + res.body());
		}

		WxCpLinkAddResp resp = Jsons.parseObject(res.body(), WxCpLinkAddResp.class);
		if (resp.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("删除获客助手链接: " + resp.getErrmsg());
		}
		return resp;
	}

	@Override
	public List<String> getPermitUserList(String corpId, String type) throws Exception {
		String url = "https://qyapi.weixin.qq.com/cgi-bin/msgaudit/get_permit_user_list?access_token=" + getMsgAccessToken(corpId);

		JSONObject params = new JSONObject();
		if (StringUtils.isNotBlank(type)) {
			params.putOpt("type",type);
		}
		HttpResponse<String> res =
				J11HttpC.custom()
						.ofPost()
						.url(url)
						.headers(J11HttpC.ReqType.raw_json)
						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(params), IoKit.Charsets.UTF_8.getCharset()))
						.send(HttpResponse.BodyHandlers.ofString());
		if (200 != res.statusCode()) {
			throw BusinessRuntimeException.getInstance("获取会话内容存档开启成员列表: " + res.body());
		}

		CorpBaseResult resp = Jsons.parseObject(res.body(), CorpBaseResult.class);
		if (resp.getErrcode() != 0) {
			throw BusinessRuntimeException.getInstance("获取会话内容存档开启成员列表: " + resp.getErrmsg());
		}
		return JSONUtil.parseObj(res.body()).getJSONArray("ids").toList(String.class);
	}
}
