CluesService.java 7 KB
package cn.com.glsx.stoassistant.service.clues;

import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import cn.com.glsx.stoassistant.constans.GSCServiceConstants;
import cn.com.glsx.stoassistant.request.clues.CluesPadPageReq;
import cn.com.glsx.stoassistant.request.clues.CluesStatusCountReq;
import cn.com.glsx.stoassistant.response.clues.CluesPageResp;
import cn.com.glsx.stoassistant.response.clues.CluesStatusCountResp;
import cn.com.glsx.stoassistant.utils.Pagination;
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 CluesService {

    @Autowired
    private GSCServicePoolConfig gscServicePoolConfig;

    public RopResponse<Pagination<CluesPageResp>> page(CluesPadPageReq request) {
        try {
            // 获取指定服务的配置
            GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(GSCServiceConstants.GSC_VCP_PAD_FACADE);

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            Map<String, Object> tParams = new HashMap<>();
            tParams.put("cluesStatus", request.getCluesStatus());
            tParams.put("keyword", request.getKeyword());
            tParams.put("sellerUserId", request.getSellerUserId());
            tParams.put("sellerMerchantId", request.getSellerMerchantId());
            params.put("t", tParams);
            params.put("pageSize", request.getPageSize());
            params.put("currentPage", request.getCurrentPage());
            log.info("二手车线索分页查询调用服务:{},请求参数params:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, params);

            // 调用指定服务的接口
            String result = SpringCloudHttpUtil.post(
                    serviceParam,
                    params,
                    String.class,
                    "/clues/page",
                    null
            );
            log.info("二手车线索分页查询调用服务:{} 返回结果result:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, 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为空");
            }
            log.info("二手车线索分页查询调用服务:{},二手车线索分页查询,响应结果:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, data);
            Pagination<CluesPageResp> pagination = new Pagination<>();
            pagination.setPageNum((Integer) data.get("current"));
            pagination.setPageSize((Integer) data.get("size"));
            pagination.setCount((Integer) data.get("total"));
            pagination.setData(JSON.parseArray(JSON.toJSONString(data.get("records")), CluesPageResp.class));
            return RopResponse.success(pagination);
        } catch (Exception e) {
            log.error("二手车线索分页查询失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("二手车线索分页查询失败:" + e.getMessage(), e);
        }
    }

    public RopResponse<CluesStatusCountResp> statusCount(CluesStatusCountReq request) {
        try {
            // 获取指定服务的配置
            GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(GSCServiceConstants.GSC_VCP_PAD_FACADE);

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            params.put("sellerUserId", request.getSellerUserId());
            params.put("sellerMerchantId", request.getSellerMerchantId());
            params.put("keyword", request.getKeyword());
            log.info("二手车线索状态数量统计调用服务:{},请求参数params:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, params);

            // 调用指定服务的接口
            String result = SpringCloudHttpUtil.post(
                    serviceParam,
                    params,
                    String.class,
                    "/clues/statusCount",
                    null
            );
            log.info("二手车线索状态数量统计调用服务:{} 返回结果result:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, 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为空");
            }
            log.info("二手车线索状态数量统计调用服务:{},二手车线索状态数量统计查询,响应结果:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, data);
            CluesStatusCountResp cluesStatusCountResp = buildStatusCountResp(data);
            return RopResponse.success(cluesStatusCountResp);
        } catch (Exception e) {
            log.error("二手车线索线索状态数量统计查询失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("二手车线索线索状态数量统计失败:" + e.getMessage(), e);
        }
    }

    /**
     * 构建二手车线索状态数量统计
     *
     * @param data
     * @return
     */
    private static CluesStatusCountResp buildStatusCountResp(Map<String, Object> data) {
        CluesStatusCountResp response = new CluesStatusCountResp();
        response.setTotalCount(Long.parseLong(data.get("totalCount").toString()));
        response.setPendingFollowCount(Long.parseLong(data.get("pendingFollowCount").toString()));
        response.setFollowingCount(Long.parseLong(data.get("followingCount").toString()));
        response.setTempLostCount(Long.parseLong(data.get("tempLostCount").toString()));
        response.setSuccessCount(Long.parseLong(data.get("successCount").toString()));
        return response;
    }
}