提交 b877594a 作者: panda

服务订单接口-添加远程请求工具类

上级 81d62438
......@@ -122,7 +122,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
......
package com.mmc.oms.feign.config;
//import com.mmc.oms.feign.hystrix.UserAppApiHystrix;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
//
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
///**
// * @author: zj
// * @Date: 2023/5/18 18:21
......@@ -13,8 +12,8 @@ package com.mmc.oms.feign.config;
//@Configuration
//public class FeignConfiguration {
//
// @Bean(name = "userAppApiHystrix")
// public UserAppApiHystrix userAppApi(){
// return new UserAppApiHystrix();
// @Bean(name = "workServiceHystrix")
// public WorkServiceHystrix workServiceHystrixApi() {
// return new WorkServiceHystrix();
// }
//}
package com.mmc.oms.feign.service;
public interface WorkServiceUrl {
String feignQueryWorkServiceListById = "backstage/work/feignQueryWorkServiceListById";
}
package com.mmc.oms.model.dto.mall;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.mmc.oms.common.publicinterface.Create;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
......@@ -55,6 +57,9 @@ public class ServiceOrderTaskDTO implements Serializable {
@ApiModelProperty(value = "用户名称")
private String userName;
@ApiModelProperty(value = "用户昵称")
private String nickName;
@ApiModelProperty(value = "用户id")
private Integer userId;
......@@ -64,10 +69,12 @@ public class ServiceOrderTaskDTO implements Serializable {
@ApiModelProperty(value = "预计服务开始时间")
@NotNull(message = "新增服务-预计服务开始时间不能为空", groups = {Create.class})
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@ApiModelProperty(value = "预计服务结束时间")
@NotNull(message = "新增服务-预计服务结束时间不能为空", groups = {Create.class})
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
@ApiModelProperty(value = "省份",example = "广东")
......@@ -96,6 +103,16 @@ public class ServiceOrderTaskDTO implements Serializable {
@ApiModelProperty(value = "图片")
private List<String> images;
@ApiModelProperty(value = "行业服务-封面图")
private String coverPlan;
@ApiModelProperty(value = "行业服务-分享卡片")
private String shareCard;
@ApiModelProperty(value = "行业服务-视频")
private String video;
@ApiModelProperty(value = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createdTime;
}
package com.mmc.oms.service.mall.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mmc.oms.client.PmsClient;
......@@ -15,6 +16,7 @@ import com.mmc.oms.entity.mall.ServiceOrderProvinceCityDO;
import com.mmc.oms.entity.mall.ServiceOrderTaskDO;
import com.mmc.oms.entity.mall.ServiceOrderTaskImageDO;
import com.mmc.oms.feign.UserAppApi;
import com.mmc.oms.feign.service.WorkServiceUrl;
import com.mmc.oms.model.dto.mall.ServiceOrderTaskDTO;
import com.mmc.oms.model.dto.service.ServiceDTO;
import com.mmc.oms.model.dto.user.UserAccountSimpleDTO;
......@@ -22,6 +24,7 @@ import com.mmc.oms.model.qo.mall.ServiceOrderTaskQO;
import com.mmc.oms.model.vo.mall.ServiceOrderTaskVO;
import com.mmc.oms.service.mall.ServiceOrderTaskService;
import com.mmc.oms.util.BeanCopyUtils;
import com.mmc.oms.util.HttpUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -52,6 +55,8 @@ public class ServiceOrderTaskServiceImpl
@Autowired private UserClient userClient;
@Autowired private HttpUtil httpUtil;
@Override
@Transactional(rollbackFor = Exception.class)
public ResultBody insertOrderTask(ServiceOrderTaskVO param, Integer userAccountId,HttpServletRequest request) {
......@@ -118,14 +123,27 @@ public class ServiceOrderTaskServiceImpl
List<Integer> userIds = orderTaskList.stream().map(ServiceOrderTaskDO::getUserAccountId).collect(Collectors.toList());
Map<Integer, UserAccountSimpleDTO> userMap = getUserDTOMap(request, userIds);
List<Integer> inspectionIds = orderTaskList.stream().map(ServiceOrderTaskDO::getInspectionId).collect(Collectors.toList());
Map<Integer, ServiceDTO> serviceDTOMap = getServiceDTOMap(inspectionIds);
List<ServiceOrderTaskDTO> pageList = new ArrayList<>();
for (ServiceOrderTaskDO item : orderTaskList) {
ServiceOrderTaskDTO orderTaskDTO = getServiceOrderTaskDTO(provinceCityMap, imageMap, item,userMap);
ServiceOrderTaskDTO orderTaskDTO = getServiceOrderTaskDTO(provinceCityMap, imageMap, item,userMap,serviceDTOMap);
pageList.add(orderTaskDTO);
}
return PageResult.buildPage(pageNo, param.getPageSize(), count, pageList);
}
private Map<Integer, ServiceDTO> getServiceDTOMap(List<Integer> inspectionIds) {
String jsonStr = httpUtil.sendPost(WorkServiceUrl.feignQueryWorkServiceListById, null, JSONObject.toJSONString(inspectionIds));
List<ServiceDTO> serviceDTOS = JSONObject.parseArray(jsonStr, ServiceDTO.class);
Map<Integer, ServiceDTO> serviceDTOMap = new HashMap<>();
if(CollectionUtils.isNotEmpty(serviceDTOS)){
serviceDTOMap = serviceDTOS.stream().collect(Collectors.toMap(ServiceDTO::getId, d -> d, (k1, k2) -> k1));
}
return serviceDTOMap;
}
@Override
public ResultBody<ServiceOrderTaskDTO> queryById(Integer id,HttpServletRequest request) {
ServiceOrderTaskDO order = serviceOrderTaskDao.selectById(id);
......@@ -135,7 +153,8 @@ public class ServiceOrderTaskServiceImpl
Map<Integer, ServiceOrderProvinceCityDO> provinceCityMap = getOrderProvinceCityDOMapById(Arrays.asList(id));
Map<Integer, ServiceOrderTaskImageDO> imageMap = getImageListMapById(Arrays.asList(id));
Map<Integer, UserAccountSimpleDTO> userMap = getUserDTOMap(request, Arrays.asList(order.getUserAccountId()));
ServiceOrderTaskDTO serviceOrderTaskDTO = getServiceOrderTaskDTO(provinceCityMap, imageMap, order,userMap);
Map<Integer, ServiceDTO> serviceDTOMap = getServiceDTOMap(Arrays.asList(order.getInspectionId()));
ServiceOrderTaskDTO serviceOrderTaskDTO = getServiceOrderTaskDTO(provinceCityMap, imageMap, order,userMap,serviceDTOMap);
return ResultBody.success(serviceOrderTaskDTO);
}
......@@ -148,7 +167,7 @@ public class ServiceOrderTaskServiceImpl
return userMap;
}
private ServiceOrderTaskDTO getServiceOrderTaskDTO(Map<Integer, ServiceOrderProvinceCityDO> provinceCityMap, Map<Integer, ServiceOrderTaskImageDO> imageMap, ServiceOrderTaskDO item,Map<Integer, UserAccountSimpleDTO> userMap) {
private ServiceOrderTaskDTO getServiceOrderTaskDTO(Map<Integer, ServiceOrderProvinceCityDO> provinceCityMap, Map<Integer, ServiceOrderTaskImageDO> imageMap, ServiceOrderTaskDO item,Map<Integer, UserAccountSimpleDTO> userMap,Map<Integer, ServiceDTO> serviceDTOMap) {
ServiceOrderTaskDTO orderTaskDTO = BeanCopyUtils.properties(item, new ServiceOrderTaskDTO());
// 订单省市信息
BeanCopyUtils.properties(provinceCityMap.get(item.getId()), orderTaskDTO);
......@@ -157,6 +176,9 @@ public class ServiceOrderTaskServiceImpl
BeanCopyUtils.properties(userAccountSimpleDTO, orderTaskDTO);
orderTaskDTO.setUserId(userAccountSimpleDTO==null?null:userAccountSimpleDTO.getId());
ServiceOrderTaskImageDO serviceOrderTaskImageDO = imageMap.get(item.getId());
// 行业服务信息
ServiceDTO serviceDTO = serviceDTOMap.get(item.getInspectionId());
BeanCopyUtils.properties(serviceDTO, orderTaskDTO);
List<String> images = null;
if (serviceOrderTaskImageDO!=null &&serviceOrderTaskImageDO.getPicture() !=null) {
String pictureStr = serviceOrderTaskImageDO.getPicture();
......
package com.mmc.oms.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
@Component
public class HttpUtil {
@Value("${pms.url}")
private String pmsAppUri;
/**
*
* @param url 接口地址
* @param headers 消息头
* @param jsonParam 消息体
* @return
*/
public String sendPost(String url, Map<String, String> headers, String jsonParam) {
String response = null;
try {
CloseableHttpClient httpclient = null;
CloseableHttpResponse httpresponse = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(pmsAppUri+url);
StringEntity stringentity = new StringEntity(jsonParam, ContentType.create("application/json", "UTF-8"));
httppost.setEntity(stringentity);
// 循环添加header
if(headers!=null) {
Iterator headerIterator = headers.entrySet().iterator();
while (headerIterator.hasNext()) {
Map.Entry<String, String> elem = (Map.Entry<String, String>) headerIterator.next();
httppost.addHeader(elem.getKey(), elem.getValue());
}
}
//发post请求
httpresponse = httpclient.execute(httppost);
//utf-8参数防止中文乱码
response = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
} finally {
if (httpclient != null) {
httpclient.close();
}
if (httpresponse != null) {
httpresponse.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
/**
* get请求
*
* @param url
* @param paramMap
* @param headerMap
* @return
*/
public JSONObject sendGet(String url, Map<String, String> paramMap, Map<String, String> headerMap) {
JSONObject jsonObject = null;
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
/*
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
*/
URIBuilder uriBuilder = new URIBuilder(pmsAppUri+url);
if(paramMap!=null) {
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
uriBuilder.addParameter(entry.getKey(), entry.getValue());
}
}
//拼接请求头参数
HttpGet httpGet = new HttpGet(uriBuilder.build());
if(headerMap!=null) {
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
// 使用Apache提供的工具类进行转换成字符串
if (entity != null) {
// 将返回结果处理成String对象
String result = EntityUtils.toString(entity, "UTF-8");
// 转换Json对象
jsonObject = JSON.parseObject(result);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
System.err.println("释放连接出错");
e.printStackTrace();
}
}
}
return jsonObject;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论