CarConditionService.java 15.6 KB
package cn.com.glsx.stoassistant.service.cardition;

import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.scrm.system.model.wechatapplets.LoginUserInfoResponse;
import cn.com.glsx.stoassistant.config.GSCServicePoolConfig;
import cn.com.glsx.stoassistant.constans.GSCServiceConstants;
import cn.com.glsx.stoassistant.request.carcondition.CarConditionAddRequest;
import cn.com.glsx.stoassistant.request.carcondition.CarConditionDetailRequest;
import cn.com.glsx.stoassistant.request.carcondition.CarConditionEditRequest;
import cn.com.glsx.stoassistant.request.carcondition.CarConditionPadPageRequest;
import cn.com.glsx.stoassistant.response.carcondition.CarConditionPadDetailVO;
import cn.com.glsx.stoassistant.response.carcondition.CarConditionPageVO;
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.oreframework.boot.autoconfigure.rop.util.RopThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class CarConditionService {

    @Autowired
    private GSCServicePoolConfig gscServicePoolConfig;

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

            // 构建请求参数
            Map<String, Object> params = new HashMap<>();
            params.put("entryStatus", request.getEntryStatus());
            params.put("pageSize", request.getPageSize());
            params.put("currentPage", request.getCurrentPage());
            params.put("operatorId", request.getOperatorId());

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

            // 调用指定服务的接口
            String result = SpringCloudHttpUtil.post(
                    serviceParam,
                    params,
                    String.class,
                    "/carCondition/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<CarConditionPageVO> 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")), CarConditionPageVO.class));
            return RopResponse.success(pagination);
        } catch (Exception e) {
            log.error("车况分页查询失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("车况分页查询失败:" + e.getMessage(), e);
        }
    }

    public RopResponse<CarConditionPadDetailVO> detail(CarConditionDetailRequest request) {
        try {
            LoginUserInfoResponse loginUser = RopThreadLocal.getLoginUser(LoginUserInfoResponse.class);

            // 获取指定服务的配置
            GSCServicePoolConfig.GSCServiceParam serviceParam = gscServicePoolConfig.getServiceParam(GSCServiceConstants.GSC_VCP_PAD_FACADE);

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

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

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

            log.info("调用服务:{} 返回结果:{}", 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("调用服务:{},车况主键id:{},响应结果:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE,request.getId(), data);
            CarConditionPadDetailVO response = buildDetailResp(data);
            return RopResponse.success(response);
        } catch (Exception e) {
            log.error("获取车况详情失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("获取车况详情失败:" + e.getMessage(), e);
        }
    }

    /**
     * 构建车况详情响应
     *
     * @param data
     * @return
     */
    private static CarConditionPadDetailVO buildDetailResp(Map<String, Object> data) {
        CarConditionPadDetailVO response = new CarConditionPadDetailVO();
        response.setId((Integer) data.get("id"));
        response.setCarConditionCode((String) data.get("carConditionCode"));
        response.setOwnerName((String) data.get("ownerName"));
        response.setSellerUserId((String) data.get("sellerUserId"));
        response.setSellerName((String) data.get("sellerName"));
        response.setDrivingLicenseImgUrl((String) data.get("drivingLicenseImgUrl"));
        response.setNameplateImgUrl((String) data.get("nameplateImgUrl"));
        response.setLeftFrontImgUrl((String) data.get("leftFrontImgUrl"));
        response.setRightFrontImgUrl((String) data.get("rightFrontImgUrl"));
        response.setMileageImgUrl((String) data.get("mileageImgUrl"));
        response.setCenterConsoleImgUrl((String) data.get("centerConsoleImgUrl"));
        response.setDriverSeatImgUrl((String) data.get("driverSeatImgUrl"));
        response.setRearSeatImgUrl((String) data.get("rearSeatImgUrl"));
        response.setOtherDetailImgUrl((String) data.get("otherDetailImgUrl"));
        response.setConditionDescription((String) data.get("conditionDescription"));
        response.setEntryStatus((Integer) data.get("entryStatus"));
        return response;
    }

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

            // 构建请求参数
            Map<String, Object> params = buildAddParams(request);

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

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

            log.info("调用服务:{} 返回结果:{}", 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("添加车况成功,车况id:{}", data.get("id"));
            return RopResponse.success((Integer) data.get("id"));
        } catch (Exception e) {
            log.error("添加车况失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("添加车况失败:" + e.getMessage(), e);
        }
    }

    private static Map<String, Object> buildAddParams(CarConditionAddRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("ownerName", request.getOwnerName());
        params.put("licensePlate", request.getLicensePlate());
        params.put("vin", request.getVin());
        params.put("registrationDate", request.getRegistrationDate());
        params.put("sellerUserId", request.getSellerUserId());
        params.put("sellerName", request.getSellerName());
        params.put("sellerMerchantId", request.getSellerMerchantId());
        params.put("sellerMerchantName", request.getSellerMerchantName());
        params.put("conversionUserId", request.getConversionUserId());
        params.put("conversionName", request.getConversionName());
        params.put("conversionMerchantId", request.getConversionMerchantId());
        params.put("conversionMerchantName", request.getConversionMerchantName());
        params.put("drivingLicenseImgUrl", request.getDrivingLicenseImgUrl());
        params.put("nameplateImgUrl", request.getNameplateImgUrl());
        params.put("leftFrontImgUrl", request.getLeftFrontImgUrl());
        params.put("rightFrontImgUrl", request.getRightFrontImgUrl());
        params.put("mileageImgUrl", request.getMileageImgUrl());
        params.put("centerConsoleImgUrl", request.getCenterConsoleImgUrl());
        params.put("driverSeatImgUrl", request.getDriverSeatImgUrl());
        params.put("rearSeatImgUrl", request.getRearSeatImgUrl());
        params.put("otherDetailImgUrl", request.getOtherDetailImgUrl());
        params.put("conditionDescription", request.getConditionDescription());
        params.put("entryStatus", request.getEntryStatus());
        params.put("source", request.getSource());
        params.put("openId", request.getOpenId());
        params.put("phone", request.getPhone());
        params.put("operator", request.getOperator());
        params.put("operatorId", request.getOperatorId());
        params.put("merchantId", request.getMerchantId());
        params.put("merchantName", request.getMerchantName());
        return params;
    }

    private static Map<String, Object> buildEditParams(CarConditionEditRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", request.getId());
        params.put("ownerName", request.getOwnerName());
        params.put("licensePlate", request.getLicensePlate());
        params.put("vin", request.getVin());
        params.put("registrationDate", request.getRegistrationDate());
        params.put("sellerUserId", request.getSellerUserId());
        params.put("sellerName", request.getSellerName());
        params.put("sellerMerchantId", request.getSellerMerchantId());
        params.put("sellerMerchantName", request.getSellerMerchantName());
        params.put("conversionUserId", request.getConversionUserId());
        params.put("conversionName", request.getConversionName());
        params.put("conversionMerchantId", request.getConversionMerchantId());
        params.put("conversionMerchantName", request.getConversionMerchantName());
        params.put("drivingLicenseImgUrl", request.getDrivingLicenseImgUrl());
        params.put("nameplateImgUrl", request.getNameplateImgUrl());
        params.put("leftFrontImgUrl", request.getLeftFrontImgUrl());
        params.put("rightFrontImgUrl", request.getRightFrontImgUrl());
        params.put("mileageImgUrl", request.getMileageImgUrl());
        params.put("centerConsoleImgUrl", request.getCenterConsoleImgUrl());
        params.put("driverSeatImgUrl", request.getDriverSeatImgUrl());
        params.put("rearSeatImgUrl", request.getRearSeatImgUrl());
        params.put("otherDetailImgUrl", request.getOtherDetailImgUrl());
        params.put("conditionDescription", request.getConditionDescription());
        params.put("entryStatus", request.getEntryStatus());
        params.put("source", request.getSource());
        params.put("openId", request.getOpenId());
        params.put("operator", request.getOperator());
        params.put("operatorId", request.getOperatorId());
        params.put("merchantId", request.getMerchantId());
        params.put("merchantName", request.getMerchantName());
        return params;
    }

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

            // 构建请求参数
            Map<String, Object> params = buildEditParams(request);

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

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

            log.info("调用服务:{} 返回结果:{}", 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("编辑车况成功,车况id:{}", data.get("id"));
            return RopResponse.success((Integer) data.get("id"));
        } catch (Exception e) {
            log.error("编辑车况成功失败,服务:{}", GSCServiceConstants.GSC_VCP_PAD_FACADE, e);
            throw new RuntimeException("编辑车况成功失败:" + e.getMessage(), e);
        }
    }

}