package com.cyksj.web.controller.user;

import cn.hutool.core.util.StrUtil;
import com.cyksj.common.util.Codec;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.corp.CorpWxYlRecordMapper;
import com.cyksj.model.entity.CorpWxYlRecord;
import com.cyksj.model.entity.User;
import com.cyksj.model.request.TempCorpFollowCodeReq;
import com.cyksj.model.response.UserInfoResp;
import com.cyksj.redis.RedisService;
import com.cyksj.service.corp.CorpUserFrontService;
import com.cyksj.web.util.StpUserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 项目名: yhlxj2
 * 文件名: CorpUserFrontController
 * 创建者: JavaZou
 * 创建时间:2025/3/11 16:26
 */
@RestController
@RequestMapping("/applets/user")
@RequiredArgsConstructor
public class CorpUserFrontController {
	private final CorpUserFrontService corpUserFrontService;

	private final RedisService redisService;

	private final CorpWxYlRecordMapper corpWxYlRecordMapper;

	private final UserMapper userMapper;

	/**
	 * 获取用户添加企业微信和下单 情况
	 */
	@GetMapping("/get/situation")
	public Result<UserInfoResp> getUserInfoResp(@RequestParam(defaultValue = "0") Integer type) {
		long userId = StpUserUtil.getLoginIdAsLong();
		UserInfoResp resp = corpUserFrontService.getUserInfoResp(userId, type);
		return GatewayResponse.SUCCESS.newBuilder().toResult(resp);
	}

	/**
	 * 获取添加客服 临时二维码
	 */
	@PostMapping("/post")
	public Result<String> generateTempCorpFollowCode(@RequestBody TempCorpFollowCodeReq tempCorpFollowCodeReq) throws Exception {
		String keyName = RedisService.key.CORP_WX_YL_KEY.getName();
		long userId = StpUserUtil.getLoginIdAsLong();
		keyName += userId;
		if (tempCorpFollowCodeReq.getGoodsId() != null) {
			keyName += ":" + tempCorpFollowCodeReq.getGoodsId();
		}
		String codeUrl = redisService.getStr(keyName);
		if (StrUtil.isEmpty(codeUrl)) {
			tempCorpFollowCodeReq.setUserId(userId);
			User user = userMapper.selectById(userId);
			tempCorpFollowCodeReq.setS(StrUtil.isEmpty(user.getUnionid()) ? 1 : 0);
			codeUrl = corpUserFrontService.generateTempCorpFollowCode(tempCorpFollowCodeReq);
			redisService.setNx(keyName, codeUrl, RedisService.key.CORP_WX_YL_KEY.getTimeout());
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult(codeUrl);
	}

	/**
	 * 引流用户授权跳转 详情
	 * 非微信用户
	 */
	@GetMapping("/get/yl/redirect/detail")
	public Result<String> getYlRedirectDetail(Long ylId) throws Exception {
		CorpWxYlRecord corpWxYlRecord = corpWxYlRecordMapper.selectById(ylId);
		if (corpWxYlRecord == null) {
			return GatewayResponse.SUCCESS.newBuilder().toResult();
		}
		String json = Codec.DoBase64.custom().setData(corpWxYlRecord.getState()).decodeAsText();
		TempCorpFollowCodeReq re = Jsons.parseObject(json, TempCorpFollowCodeReq.class);
		Long userId = re.getUserId();
		User user = userMapper.selectById(userId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(StrUtil.isEmpty(user.getLoginPhone()) ? user.getEmail() : user.getLoginPhone());
	}
}
