旧系统,修改成异步方式

This commit is contained in:
dengjun 2024-02-02 17:09:06 +08:00
parent a3ae356a1f
commit 8f70e34cb5
9 changed files with 245 additions and 86 deletions

View File

@ -6,6 +6,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;
/** /**
* @Author limng * @Author limng
@ -15,6 +16,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableCreateCacheAnnotation @EnableCreateCacheAnnotation
//开启Feign支持 //开启Feign支持
@EnableFeignClients @EnableFeignClients
@EnableAsync
public class NuzarCustomerApplication implements CommandLineRunner { public class NuzarCustomerApplication implements CommandLineRunner {
/** /**

View File

@ -24,7 +24,6 @@ import com.haitonggauto.rtosc.common.dto.Result;
import com.haitonggauto.rtosc.common.enums.ErrorType; import com.haitonggauto.rtosc.common.enums.ErrorType;
import com.haitonggauto.rtosc.common.handler.BaseHandler; import com.haitonggauto.rtosc.common.handler.BaseHandler;
import com.haitonggauto.rtosc.common.utils.*; import com.haitonggauto.rtosc.common.utils.*;
import com.haitonggauto.rtosc.config.SyncConfig;
import com.haitonggauto.rtosc.dto.*; import com.haitonggauto.rtosc.dto.*;
import com.haitonggauto.rtosc.excel.*; import com.haitonggauto.rtosc.excel.*;
import com.haitonggauto.rtosc.query.CargoQuery; import com.haitonggauto.rtosc.query.CargoQuery;
@ -35,10 +34,7 @@ import com.haitonggauto.rtosc.handler.excel.ReadExcelListener;
import com.haitonggauto.rtosc.handler.mapper.PoMapper; import com.haitonggauto.rtosc.handler.mapper.PoMapper;
import com.haitonggauto.rtosc.repository.enums.AuditEnum; import com.haitonggauto.rtosc.repository.enums.AuditEnum;
import com.haitonggauto.rtosc.repository.query.PrintQuery; import com.haitonggauto.rtosc.repository.query.PrintQuery;
import com.haitonggauto.rtosc.repository.service.CustomerExportInCargoService; import com.haitonggauto.rtosc.repository.service.*;
import com.haitonggauto.rtosc.repository.service.CustomerExportInService;
import com.haitonggauto.rtosc.repository.service.CustomerExportInTimesService;
import com.haitonggauto.rtosc.repository.service.CustomerExportLoadService;
import com.haitonggauto.rtosc.service.CustomerService; import com.haitonggauto.rtosc.service.CustomerService;
import com.nuzar.common.security5.common.util.SecurityUtils; import com.nuzar.common.security5.common.util.SecurityUtils;
import com.nuzar.rtops.log.dto.LogRecordDTO; import com.nuzar.rtops.log.dto.LogRecordDTO;
@ -46,6 +42,7 @@ import com.nuzar.rtops.log.service.EsLogApprovalUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -107,9 +104,6 @@ public class ExportInHandler implements BaseHandler {
@Resource @Resource
private NuzarPubApi pubApi; private NuzarPubApi pubApi;
@Resource
private SyncConfig syncConfig;
@ApiOperation("船名航次模糊匹配") @ApiOperation("船名航次模糊匹配")
@PostMapping("/shipVoyage") @PostMapping("/shipVoyage")
public Result<List<ShipVoyageVo>> getExportInShipNameList( public Result<List<ShipVoyageVo>> getExportInShipNameList(
@ -1223,7 +1217,7 @@ public class ExportInHandler implements BaseHandler {
List<CustomerExportIn> list = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, check.getIds()).list(); List<CustomerExportIn> list = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, check.getIds()).list();
if (check.getCheckStatus() == AuditEnum.AUDIT_PASS) { if (check.getCheckStatus() == AuditEnum.AUDIT_PASS) {
this.syncAddExportInToOld(list); customerService.syncAddExportInToOld(list);
} }
// 记录日志 // 记录日志
@ -1281,7 +1275,7 @@ public class ExportInHandler implements BaseHandler {
.eq(CustomerExportIn::getVoyageId, check.getVoyageId()).list(); .eq(CustomerExportIn::getVoyageId, check.getVoyageId()).list();
if (check.getCheckStatus() == AuditEnum.AUDIT_PASS) { if (check.getCheckStatus() == AuditEnum.AUDIT_PASS) {
this.syncAddExportInToOld(list); customerService.syncAddExportInToOld(list);
} }
// 记录日志 // 记录日志
@ -1336,7 +1330,7 @@ public class ExportInHandler implements BaseHandler {
boolean row = customerExportInService.updateBatchById(exportIns); boolean row = customerExportInService.updateBatchById(exportIns);
if (row) { if (row) {
this.syncDelExportInToOld(check.getIds()); customerService.syncDelExportInToOld(check.getIds());
return ResultUtil.success("success"); return ResultUtil.success("success");
} }
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "修改失败ID不存在"); return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "修改失败ID不存在");
@ -3519,79 +3513,4 @@ public class ExportInHandler implements BaseHandler {
return ResultUtil.success(rst, String.valueOf(page.getTotal())); return ResultUtil.success(rst, String.valueOf(page.getTotal()));
} }
private void formatData(List<CustomerExportIn> ins) {
if (CollectionUtils.isEmpty(ins)) {
return;
}
ins.stream().forEach(item -> {
});
}
private void syncAddExportInToOld(List<CustomerExportIn> list) throws Exception {
if (!syncConfig.getSync() || CollectionUtils.isEmpty(list)) { // 没有打开同步或列表为空真直接返回
return;
}
for (CustomerExportIn item : list) {
OldExportInAddReq req = PoMapper.instance.exportIn2OldExportInAddReq(item);
req.setVlsNm(item.getShipName()+"/"+item.getShipEnName());
if (item.getLength() != null) {
req.setLength(req.getLength().setScale(3, RoundingMode.HALF_UP));
}
if (item.getWidth() != null) {
req.setWidth(item.getWidth().setScale(3, RoundingMode.HALF_UP));
}
if (item.getHeight() != null) {
req.setHeight(item.getHeight().setScale(3, RoundingMode.HALF_UP));
}
if (item.getWeight() != null) {
req.setWeight(item.getWeight().setScale(3, RoundingMode.HALF_UP));
}
if (StringUtils.equals("备件", item.getCartType())) {
req.setCarNum(0);
req.setPartNum(item.getQuantity());
req.setPartOpMode(item.getOperateType());
} else {
req.setCarNum(item.getQuantity());
req.setPartNum(0);
req.setCarOpMode(item.getOperateType());
}
if (item.getEachWeight() != null) {
req.setMnfWeight(item.getEachWeight().setScale(3, RoundingMode.HALF_UP));
}
if (item.getEachVolume() != null) {
req.setMnfVolume(item.getEachVolume().setScale(3, RoundingMode.HALF_UP));
}
System.err.println(JSONObject.toJSONString(req));
String post = OkHttpUtils.post(syncConfig.getUrl() + "/execInPortPlanAdd", OkHttpUtils.buildJsonRequestBody(JSONObject.toJSONString(req)), null);
JSONObject rst = JSONObject.parseObject(post);
if (StringUtils.equals("0", rst.getString("success"))) {
throw new RuntimeException(rst.getString("errmsg"));
}
}
}
private void syncDelExportInToOld(List<Long> list) throws Exception {
if (!syncConfig.getSync() || CollectionUtils.isEmpty(list)) { // 没有打开同步或列表为空真直接返回
return;
}
for (Long id : list) {
Map<String, String> params = new HashMap<>();
params.put("nsId", String.valueOf(id));
String post = OkHttpUtils.post(syncConfig.getUrl() + "/execInPortPlanDel", OkHttpUtils.buildFormRequestBody(params), null);
JSONObject rst = JSONObject.parseObject(post);
if (StringUtils.equals("0", rst.getString("success"))) {
throw new RuntimeException(rst.getString("errmsg"));
}
}
}
} }

View File

@ -173,4 +173,8 @@ public interface CustomerService {
* @param jobs * @param jobs
*/ */
void updateImportTake(Boolean imp, CustomerImportTake importTake, List<CustomerImportTakeCargo> cargos, List<CustomerImportTakeJob> jobs); void updateImportTake(Boolean imp, CustomerImportTake importTake, List<CustomerImportTakeCargo> cargos, List<CustomerImportTakeJob> jobs);
void syncAddExportInToOld(List<CustomerExportIn> list);
void syncDelExportInToOld(List<Long> list);
} }

View File

@ -1,21 +1,32 @@
package com.haitonggauto.rtosc.service.impl; package com.haitonggauto.rtosc.service.impl;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haitonggauto.rtosc.common.context.UserContext;
import com.haitonggauto.rtosc.common.dto.LoginUser;
import com.haitonggauto.rtosc.common.utils.OkHttpUtils;
import com.haitonggauto.rtosc.config.SyncConfig;
import com.haitonggauto.rtosc.dto.OldExportInAddReq;
import com.haitonggauto.rtosc.handler.mapper.PoMapper;
import com.haitonggauto.rtosc.repository.entity.*; import com.haitonggauto.rtosc.repository.entity.*;
import com.haitonggauto.rtosc.repository.enums.SequenceTypeEnum; import com.haitonggauto.rtosc.repository.enums.SequenceTypeEnum;
import com.haitonggauto.rtosc.repository.service.*; import com.haitonggauto.rtosc.repository.service.*;
import com.haitonggauto.rtosc.service.CustomerService; import com.haitonggauto.rtosc.service.CustomerService;
import com.nuzar.cloud.annotation.echo.EchoResult; import com.nuzar.cloud.annotation.echo.EchoResult;
import com.nuzar.cloud.mapper.base.EchoEntity; import com.nuzar.cloud.mapper.base.EchoEntity;
import com.nuzar.common.security5.common.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils; import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.math.RoundingMode;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -76,6 +87,12 @@ public class CustomerServiceImpl implements CustomerService {
@Resource @Resource
private CustomerImportTakeJobService importTakeJobService; private CustomerImportTakeJobService importTakeJobService;
@Resource
private CustomerExportInSyncLogService customerExportInSyncLogService;
@Resource
private SyncConfig syncConfig;
@Override @Override
@EchoResult @EchoResult
public <T extends EchoEntity> T wrapperEntity(T entity) { public <T extends EchoEntity> T wrapperEntity(T entity) {
@ -766,4 +783,104 @@ public class CustomerServiceImpl implements CustomerService {
importTakeJobService.saveBatch(insert); importTakeJobService.saveBatch(insert);
} }
} }
@Override
@Async
public void syncAddExportInToOld(List<CustomerExportIn> list) {
if (!syncConfig.getSync() || CollectionUtils.isEmpty(list)) { // 没有打开同步或列表为空真直接返回
return;
}
// 异步时变量会丢失
LoginUser tmpUser = new LoginUser();
tmpUser.setUserId(SecurityUtils.getUserId());
tmpUser.setRoleId(0L);
tmpUser.setAdmin(true);
UserContext.setUser(tmpUser);
for (CustomerExportIn item : list) {
OldExportInAddReq req = PoMapper.instance.exportIn2OldExportInAddReq(item);
req.setVlsNm(item.getShipName()+"/"+item.getShipEnName());
if (item.getLength() != null) {
req.setLength(req.getLength().setScale(3, RoundingMode.HALF_UP));
}
if (item.getWidth() != null) {
req.setWidth(item.getWidth().setScale(3, RoundingMode.HALF_UP));
}
if (item.getHeight() != null) {
req.setHeight(item.getHeight().setScale(3, RoundingMode.HALF_UP));
}
if (item.getWeight() != null) {
req.setWeight(item.getWeight().setScale(3, RoundingMode.HALF_UP));
}
if (StringUtils.equals("备件", item.getCartType())) {
req.setCarNum(0);
req.setPartNum(item.getQuantity());
req.setPartOpMode(item.getOperateType());
} else {
req.setCarNum(item.getQuantity());
req.setPartNum(0);
req.setCarOpMode(item.getOperateType());
}
if (item.getEachWeight() != null) {
req.setMnfWeight(item.getEachWeight().setScale(3, RoundingMode.HALF_UP));
}
if (item.getEachVolume() != null) {
req.setMnfVolume(item.getEachVolume().setScale(3, RoundingMode.HALF_UP));
}
try {
String post = OkHttpUtils.post(syncConfig.getUrl() + "/execInPortPlanAdd", OkHttpUtils.buildJsonRequestBody(JSONObject.toJSONString(req)), null);
JSONObject rst = JSONObject.parseObject(post);
System.err.println(rst.toJSONString());
if (StringUtils.equals("0", rst.getString("success"))) {
throw new RuntimeException("请求参数:" + JSONObject.toJSONString(req) + ", 错误信息:" + rst.getString("errmsg"));
}
} catch (Exception e) {
CustomerExportInSyncLog log = new CustomerExportInSyncLog();
log.setBatchNo(StringUtils.joinWith(",", list.stream().map(p -> p.getBatchNo()).collect(Collectors.toList())));
log.setRequestStr(JSONObject.toJSONString(req));
log.setResponseStr(e.getMessage());
customerExportInSyncLogService.save(log);
}
}
}
@Override
@Async
public void syncDelExportInToOld(List<Long> list) {
if (!syncConfig.getSync() || CollectionUtils.isEmpty(list)) { // 没有打开同步或列表为空真直接返回
return;
}
// 异步时变量会丢失
LoginUser tmpUser = new LoginUser();
tmpUser.setUserId(SecurityUtils.getUserId());
tmpUser.setRoleId(0L);
tmpUser.setAdmin(true);
UserContext.setUser(tmpUser);
for (Long id : list) {
Map<String, String> params = new HashMap<>();
params.put("nsId", String.valueOf(id));
try {
String post = OkHttpUtils.post(syncConfig.getUrl() + "/execInPortPlanDel", OkHttpUtils.buildFormRequestBody(params), null);
JSONObject rst = JSONObject.parseObject(post);
if (StringUtils.equals("0", rst.getString("success"))) {
throw new RuntimeException(rst.getString("errmsg"));
}
} catch (Exception e) {
CustomerExportInSyncLog log = new CustomerExportInSyncLog();
log.setBatchNo("");
log.setRequestStr(JSONObject.toJSONString(params));
log.setResponseStr(e.getMessage());
customerExportInSyncLogService.save(log);
}
}
}
} }

View File

@ -0,0 +1,38 @@
package com.haitonggauto.rtosc.repository.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.haitonggauto.rtosc.common.db.entity.BaseEntity;
import java.io.Serializable;
import lombok.Data;
/**
* 出口进场基本表
* @TableName customer_export_in_sync_log
*/
@TableName(value ="customer_export_in_sync_log")
@Data
public class CustomerExportInSyncLog extends BaseEntity implements Serializable {
/**
* 受理号
*/
@TableField(value = "batch_no")
private String batchNo;
/**
* 请求参数
*/
@TableField(value = "request_str")
private String requestStr;
/**
* 返回参数
*/
@TableField(value = "response_str")
private String responseStr;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,18 @@
package com.haitonggauto.rtosc.repository.mapper;
import com.haitonggauto.rtosc.repository.entity.CustomerExportInSyncLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author dj
* @description 针对表customer_export_in_sync_log(出口进场基本表)的数据库操作Mapper
* @createDate 2024-02-02 15:56:08
* @Entity com.haitonggauto.rtosc.repository.entity.CustomerExportInSyncLog
*/
public interface CustomerExportInSyncLogMapper extends BaseMapper<CustomerExportInSyncLog> {
}

View File

@ -0,0 +1,13 @@
package com.haitonggauto.rtosc.repository.service;
import com.haitonggauto.rtosc.repository.entity.CustomerExportInSyncLog;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author dj
* @description 针对表customer_export_in_sync_log(出口进场基本表)的数据库操作Service
* @createDate 2024-02-02 15:56:08
*/
public interface CustomerExportInSyncLogService extends IService<CustomerExportInSyncLog> {
}

View File

@ -0,0 +1,22 @@
package com.haitonggauto.rtosc.repository.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haitonggauto.rtosc.repository.entity.CustomerExportInSyncLog;
import com.haitonggauto.rtosc.repository.service.CustomerExportInSyncLogService;
import com.haitonggauto.rtosc.repository.mapper.CustomerExportInSyncLogMapper;
import org.springframework.stereotype.Service;
/**
* @author dj
* @description 针对表customer_export_in_sync_log(出口进场基本表)的数据库操作Service实现
* @createDate 2024-02-02 15:56:08
*/
@Service
public class CustomerExportInSyncLogServiceImpl extends ServiceImpl<CustomerExportInSyncLogMapper, CustomerExportInSyncLog>
implements CustomerExportInSyncLogService{
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.haitonggauto.rtosc.repository.mapper.CustomerExportInSyncLogMapper">
<resultMap id="BaseResultMap" type="com.haitonggauto.rtosc.repository.entity.CustomerExportInSyncLog">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="batchNo" column="batch_no" jdbcType="VARCHAR"/>
<result property="isDel" column="is_del" jdbcType="SMALLINT"/>
<result property="createBy" column="create_by" jdbcType="VARCHAR"/>
<result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
<result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
<result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
<result property="version" column="version" jdbcType="NUMERIC"/>
<result property="requestStr" column="request_str" jdbcType="VARCHAR"/>
<result property="responseStr" column="response_str" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id,batch_no,is_del,
create_by,create_date,update_by,
update_date,version,request_str,
response_str
</sql>
</mapper>