LiveCodeService.java 5.8 KB
package cn.com.glsx.stoassistant.service;

import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import cn.com.glsx.stoassistant.constans.GSCServiceConstants;
import cn.com.glsx.stoassistant.request.GroupAllocRequest;
import cn.com.glsx.stoassistant.request.LiveCodeRequest;
import cn.com.glsx.stoassistant.response.GroupAllocResponse;
import cn.com.glsx.stoassistant.response.LiveCodeResponse;
import cn.com.glsx.stoassistant.utils.SpringCloudHttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 活码服务
 */
@Slf4j
@Service
public class LiveCodeService {

    @Autowired
    private GSCServicePoolConfig gscServicePoolConfig;

    /**
     * 获取群活码
     */
    public LiveCodeResponse getLiveCode(LiveCodeRequest request) {
        // 默认调用 gsc-vcp-pad-facade 服务
        return getLiveCode(request, GSCServiceConstants.GSC_VCP_PAD_FACADE);
    }

    /**
     * 获取群活码(支持指定服务)
     *
     * @param request     请求参数
     * @param serviceName 服务名称(如 gsc-vcp-pad-facade、gsc-vcp-web-facade)
     * @return 活码响应
     */
    public LiveCodeResponse getLiveCode(LiveCodeRequest request, String serviceName) {
        try {
            // 获取指定服务的配置
            GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(serviceName);

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            params.put("reportUuidId", request.getReportUuidId());

            log.info("调用服务:{},请求参数:{}", serviceName, params);

            // 调用指定服务的接口
            String result = SpringCloudHttpUtil.post(
                    serviceParam,
                    params,
                    String.class,
                    "/liveCode/generate",
                    null
            );

            log.info("调用服务:{} 返回结果:{}", serviceName, result);

            // 解析返回结果
            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            if (resultMap == null) {
                throw new RuntimeException("调用服务失败:返回结果为空");
            }

            Integer code = (Integer) resultMap.get("code");
            if (code == null || code != 0) {
                String msg = (String) resultMap.get("msg");
                throw new RuntimeException("调用服务失败:" + msg);
            }

            Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
            if (data == null) {
                throw new RuntimeException("调用服务失败:data为空");
            }

            LiveCodeResponse response = new LiveCodeResponse();
            response.setH5Url((String) data.get("h5Url"));
            response.setQrCodeUrl((String) data.get("qrCodeUrl"));

            return response;
        } catch (Exception e) {
            log.error("获取群活码失败,服务:{}", serviceName, e);
            throw new RuntimeException("获取群活码失败:" + e.getMessage(), e);
        }
    }

    /**
     * 扫码分配空闲企微群
     */
    public GroupAllocResponse allocateGroup(GroupAllocRequest request) {
        return allocateGroup(request, GSCServiceConstants.GSC_VCP_PAD_FACADE);
    }

    /**
     * 扫码分配空闲企微群(支持指定服务)
     */
    public GroupAllocResponse allocateGroup(GroupAllocRequest request, String serviceName) {
        try {
            GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(serviceName);

            Map<String, Object> params = new HashMap<>();
            params.put("liveCode", request.getLiveCode());
            params.put("openid", request.getOpenid());
            params.put("signature", request.getSignature());
            params.put("exp", request.getExp());
            params.put("nonce", request.getNonce());
            params.put("saUserId", request.getSaUserId());
            params.put("carConditionCode", request.getCarConditionCode());

            log.info("调用服务:{},扫码分配群,请求参数:{}", serviceName, params);

            String result = SpringCloudHttpUtil.post(
                    serviceParam,
                    params,
                    String.class,
                    "/liveCode/allocate",
                    null
            );

            log.info("调用服务:{} 扫码分配群返回结果:{}", serviceName, result);

            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
            if (resultMap == null) {
                throw new RuntimeException("调用服务失败:返回结果为空");
            }

            Integer code = (Integer) resultMap.get("code");
            if (code == null || code != 0) {
                String msg = (String) resultMap.get("msg");
                throw new RuntimeException("调用服务失败:" + msg);
            }

            Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
            if (data == null) {
                throw new RuntimeException("调用服务失败:data为空");
            }

            GroupAllocResponse response = new GroupAllocResponse();
            response.setQrCodeUrl((String) data.get("qrCodeUrl"));
            response.setGroupName((String) data.get("groupName"));
            response.setLockExpireTime((String) data.get("lockExpireTime"));

            return response;
        } catch (Exception e) {
            log.error("扫码分配群失败,服务:{}", serviceName, e);
            throw new RuntimeException("扫码分配群失败:" + e.getMessage(), e);
        }
    }
}