贩卖机图片上传
This commit is contained in:
1 parent
81f46a161d
commit
c9f0aeb0ec
4 files changed
+131
-2
No files matched your search
+90
@@ -1,10 +1,16 @@
|
||||
package cn.iocoder.yudao.module.product.service.device;
|
||||
|
||||
import cn.iocoder.yudao.module.product.service.device.bo.DispenseRequestBo;
|
||||
import cn.iocoder.yudao.module.product.service.device.bo.DispenseResponseBo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.device.vo.*;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.device.DeviceDO;
|
||||
@@ -13,6 +19,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.device.DeviceMapper;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
||||
@@ -24,16 +31,62 @@ import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class DeviceServiceImpl implements DeviceService {
|
||||
|
||||
@Resource
|
||||
private DeviceMapper deviceMapper;
|
||||
|
||||
// TOKEN
|
||||
@Value("${xr.cabinet.token}")
|
||||
private String token;
|
||||
|
||||
//API_URL
|
||||
@Value("${xr.cabinet.api_url}")
|
||||
private String api_url;
|
||||
|
||||
@Override
|
||||
public Long createDevice(DeviceSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
DeviceDO device = BeanUtils.toBean(createReqVO, DeviceDO.class);
|
||||
deviceMapper.insert(device);
|
||||
DispenseRequestBo request = new DispenseRequestBo();
|
||||
request.setDeviceId(createReqVO.getDeviceId());
|
||||
request.setMessage(createReqVO.getProductUrl()+","+createReqVO.getFileUrl());
|
||||
// 设置请求头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", token);
|
||||
|
||||
// 创建请求实体
|
||||
HttpEntity<DispenseRequestBo> requestEntity = new HttpEntity<>(request, headers);
|
||||
|
||||
// 创建RestTemplate
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
try {
|
||||
// 发送请求并解析响应
|
||||
ResponseEntity<DispenseResponseBo> response = restTemplate.exchange(
|
||||
api_url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
DispenseResponseBo.class
|
||||
);
|
||||
|
||||
DispenseResponseBo body = response.getBody();
|
||||
if (body != null) {
|
||||
System.out.println("响应结果: " + body);
|
||||
if (body.getCode() == 0) {
|
||||
System.out.println("上传成功!详情: " + body.getData());
|
||||
} else {
|
||||
System.out.println("操作失败!原因: " + body.getMsg());
|
||||
log.error("设备--{}:[图片上传失败]:原因: {}",createReqVO.getDeviceId(), body.getMsg());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("图片上传请求异常: " + e.getMessage());
|
||||
log.error("设备--{}:[图片上传异常]:原因: {}",createReqVO.getDeviceId(), e.getMessage());
|
||||
}
|
||||
// 返回
|
||||
return device.getId();
|
||||
}
|
||||
@@ -44,6 +97,43 @@ public class DeviceServiceImpl implements DeviceService {
|
||||
validateDeviceExists(updateReqVO.getId());
|
||||
// 更新
|
||||
DeviceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDO.class);
|
||||
DispenseRequestBo request = new DispenseRequestBo();
|
||||
request.setDeviceId(updateReqVO.getDeviceId());
|
||||
request.setMessage(updateReqVO.getProductUrl()+","+updateReqVO.getFileUrl());
|
||||
// 设置请求头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", token);
|
||||
|
||||
// 创建请求实体
|
||||
HttpEntity<DispenseRequestBo> requestEntity = new HttpEntity<>(request, headers);
|
||||
|
||||
// 创建RestTemplate
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
try {
|
||||
// 发送请求并解析响应
|
||||
ResponseEntity<DispenseResponseBo> response = restTemplate.exchange(
|
||||
api_url,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
DispenseResponseBo.class
|
||||
);
|
||||
|
||||
DispenseResponseBo body = response.getBody();
|
||||
if (body != null) {
|
||||
System.out.println("响应结果: " + body);
|
||||
if (body.getCode() == 0) {
|
||||
System.out.println("上传成功!详情: " + body.getData());
|
||||
} else {
|
||||
System.out.println("操作失败!原因: " + body.getMsg());
|
||||
log.error("设备--{}:[图片上传失败]:原因: {}",updateReqVO.getDeviceId(), body.getMsg());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("图片上传请求异常: " + e.getMessage());
|
||||
log.error("设备--{}:[图片上传异常]:原因: {}",updateReqVO.getDeviceId(), e.getMessage());
|
||||
}
|
||||
deviceMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package cn.iocoder.yudao.module.product.service.device.bo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 货道机发货请求
|
||||
*/
|
||||
@Data
|
||||
public class DispenseRequestBo {
|
||||
//设备IMEI
|
||||
private String deviceId;
|
||||
|
||||
//参数格式:T,C&N,C&N,C&N
|
||||
// T:通道类型(1:货道,2:货道带调货检测:0x30:柜门:0x31:柜门带反馈)
|
||||
// C:货道或柜门编号,多个货道同时出货逗号分隔 =(1--80)指定货道出货
|
||||
// N:出货数量
|
||||
private String message;
|
||||
|
||||
public DispenseRequestBo(String deviceId, String message) {
|
||||
this.deviceId = deviceId;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public DispenseRequestBo() {
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.yudao.module.product.service.device.bo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 货道机发货请求
|
||||
*/
|
||||
@Data
|
||||
public class DispenseResponseBo {
|
||||
private int code;
|
||||
private String msg;
|
||||
private String data;
|
||||
}
|
||||
@@ -3,8 +3,8 @@ spring:
|
||||
name: yudao-server
|
||||
|
||||
profiles:
|
||||
# active: prod
|
||||
active: local
|
||||
active: prod
|
||||
# active: local
|
||||
|
||||
main:
|
||||
allow-circular-references: true # 允许循环依赖,因为项目是三层架构,无法避免这个情况。
|
||||
|
||||
Reference in new issue
Block a user