package com.cyksj.service.corp.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.corp.CorpFollowConfigMapper;
import com.cyksj.mapper.corp.CorpGroupChatMapper;
import com.cyksj.model.entity.CorpFollowConfig;
import com.cyksj.model.entity.CorpGroupChat;
import com.cyksj.model.request.corp.CorpGroupConfig;
import com.cyksj.redis.RedisService;
import com.cyksj.service.corp.CropChatService;
import com.cyksj.service.corp.WxCorpOps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *项目名: netflix
 *文件名: CropChatServiceImpl
 *创建者: JavaZou
 *创建时间:2023/6/26 16:26
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CropChatServiceImpl implements CropChatService {
	private final WxCorpOps wxCorpOps;

	private final CorpFollowConfigMapper corpFollowConfigMapper;

	private final CorpGroupChatMapper corpGroupChatMapper;

	private final RedisService redisService;

	@Override
	public void getCorpChat(String corpId, String configId) throws Exception {
		wxCorpOps.getCorpChatGroups(corpId,configId);
	}

	@Override
	public void setGroupsConfig(List<String> chatIds) throws Exception {
		String body = wxCorpOps.setGroupsConfig(chatIds);
		//记录 企业客户群的配置id
		System.out.println(body);
	}

	@Override
	public String getGroupsDetail(String chatId) throws Exception {
		return wxCorpOps.getGroupsDetail(chatId);
	}

	@Override
	public String getCropWxChatGroupByGoodsId(Long goodsId) throws Exception {
		String configId = redisService.getStr(RedisService.key.CORP_CHAT_GROUP_CODE.getName() + goodsId);
		if (StrUtil.isBlank(configId)) {
			CorpGroupChat corpGroupChat = corpGroupChatMapper.selectOne(Wrappers.lambdaQuery(CorpGroupChat.class)
					.eq(CorpGroupChat::getGoodsId, goodsId).last("limit 1"));
			if (corpGroupChat == null) {
				throw BusinessRuntimeException.getInstance("");
			}
			configId = corpGroupChat.getConfigId();
		}
		redisService.setNx(RedisService.key.CORP_CHAT_GROUP_CODE.getName() + goodsId, configId, RedisService.key.CORP_CHAT_GROUP_CODE.getTimeout());
		return wxCorpOps.getCorpChatGroups(null, configId);
	}

	@Override
	public void updateGroupsConfig(CorpGroupConfig config) throws Exception {
		wxCorpOps.updateGroupsConfig(config);
	}

	@Override
	public String getCorpServiceCode(Long goodsId) {
		String qrCode = redisService.getStr(RedisService.key.CORP_SERVICE_QR_CODE.getName() + goodsId);
		if (StrUtil.isBlank(qrCode)) {
			CorpFollowConfig corpFollowConfig = corpFollowConfigMapper.selectOne(Wrappers.lambdaQuery(CorpFollowConfig.class)
					.eq(CorpFollowConfig::getGoodsId, goodsId).last("limit 1"));
			if (corpFollowConfig == null) {
				throw BusinessRuntimeException.getInstance("");
			}
			qrCode = corpFollowConfig.getQrCode();
		}
		redisService.setNx(RedisService.key.CORP_SERVICE_QR_CODE.getName() + goodsId, qrCode, RedisService.key.CORP_SERVICE_QR_CODE.getTimeout());
		return qrCode;
	}
}
