no message

This commit is contained in:
2025-09-23 12:59:10 +08:00
parent c969b650f5
commit d15216828c
7 changed files with 342 additions and 37 deletions

View File

@ -8,46 +8,18 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
/**
* 异步配置类
*
* @author sczx
*/
@Configuration @Configuration
@EnableAsync @EnableAsync
public class AsyncConfig { public class AsyncConfig {
/**
* 异步任务执行器
*/
@Bean("taskExecutor") @Bean("taskExecutor")
public Executor taskExecutor() { public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数
executor.setCorePoolSize(10); executor.setCorePoolSize(10);
// 最大线程数
executor.setMaxPoolSize(20); executor.setMaxPoolSize(20);
// 队列容量
executor.setQueueCapacity(500); executor.setQueueCapacity(500);
executor.setThreadNamePrefix("data-sync-async-");
// 线程空闲时间
executor.setKeepAliveSeconds(60);
// 线程名前缀
executor.setThreadNamePrefix("async-task-");
// 拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
// 等待时间
executor.setAwaitTerminationSeconds(60);
executor.initialize(); executor.initialize();
return executor; return executor;
} }

View File

@ -12,12 +12,20 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import static com.sczx.sync.common.enums.ApiErrorCode.NOT_PERMISSION; import static com.sczx.sync.common.enums.ApiErrorCode.NOT_PERMISSION;
@ -30,36 +38,157 @@ import static com.sczx.sync.common.enums.ApiErrorCode.NOT_PERMISSION;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/b2c") @RequestMapping("/b2c")
@EnableAsync
public class ReceiveBatteryController { public class ReceiveBatteryController {
@Autowired @Autowired
private ReceiveService receiveService; private ReceiveService receiveService;
@Autowired
@Lazy
@Qualifier("taskExecutor")
private Executor taskExecutor;
@ApiOperation(value = "接收租电数据接口") @ApiOperation(value = "接收租电数据接口")
@PostMapping("/batterymeal") @PostMapping("/batterymeal")
public Map<String, String> batteryMeal(@RequestBody String receiveMealReq) { public CompletableFuture<ResponseEntity<Map<String, String>>> batteryMeal(@RequestBody String receive) {
return receiveService.recceiveMeals(receiveMealReq); return CompletableFuture
.supplyAsync(() -> {
try {
return receiveService.recceiveMeals(receive);
} catch (Exception e) {
log.error("处理电池类型数据时发生异常", e);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "处理失败: " + e.getMessage());
return errorResponse;
}
}, taskExecutor)
.thenApply(result -> {
log.info("电池类型数据处理完成,返回结果: {}", result);
return ResponseEntity.ok(result);
})
.exceptionally(throwable -> {
log.error("异步处理电池类型数据时发生异常", throwable);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "系统异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
});
} }
@ApiOperation(value = "接收用户数据接口") @ApiOperation(value = "接收用户数据接口")
@PostMapping("/userinfo") @PostMapping("/userinfo")
public Map<String, String> userInfo(@RequestBody String receiveMealReq){ public CompletableFuture<ResponseEntity<Map<String, String>>> userInfo(@RequestBody String receive){
return receiveService.recceiveUserInfo(receiveMealReq); return CompletableFuture
.supplyAsync(() -> {
try {
return receiveService.recceiveUserInfo(receive);
} catch (Exception e) {
log.error("处理电池类型数据时发生异常", e);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "处理失败: " + e.getMessage());
return errorResponse;
}
}, taskExecutor)
.thenApply(result -> {
log.info("电池类型数据处理完成,返回结果: {}", result);
return ResponseEntity.ok(result);
})
.exceptionally(throwable -> {
log.error("异步处理电池类型数据时发生异常", throwable);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "系统异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
});
} }
@ApiOperation(value = "接收运营商数据接口") @ApiOperation(value = "接收运营商数据接口")
@PostMapping("/companyinfo") @PostMapping("/companyinfo")
public Map<String, String> companyInfo(@RequestBody String receiveMealReq){ public CompletableFuture<ResponseEntity<Map<String, String>>> companyInfo(@RequestBody String receive){
return receiveService.recceiveCompanyInfo(receiveMealReq); return CompletableFuture
.supplyAsync(() -> {
try {
return receiveService.recceiveCompanyInfo(receive);
} catch (Exception e) {
log.error("处理电池类型数据时发生异常", e);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "处理失败: " + e.getMessage());
return errorResponse;
}
}, taskExecutor)
.thenApply(result -> {
log.info("电池类型数据处理完成,返回结果: {}", result);
return ResponseEntity.ok(result);
})
.exceptionally(throwable -> {
log.error("异步处理电池类型数据时发生异常", throwable);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "系统异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
});
} }
@ApiOperation(value = "接收门店数据接口") @ApiOperation(value = "接收门店数据接口")
@PostMapping("/storeinfo") @PostMapping("/storeinfo")
public Map<String, String> storeInfo(@RequestBody String receiveMealReq){ public CompletableFuture<ResponseEntity<Map<String, String>>> storeInfo(@RequestBody String receive){
return receiveService.recceiveStoreInfo(receiveMealReq); return CompletableFuture
.supplyAsync(() -> {
try {
return receiveService.recceiveStoreInfo(receive);
} catch (Exception e) {
log.error("处理电池类型数据时发生异常", e);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "处理失败: " + e.getMessage());
return errorResponse;
}
}, taskExecutor)
.thenApply(result -> {
log.info("电池类型数据处理完成,返回结果: {}", result);
return ResponseEntity.ok(result);
})
.exceptionally(throwable -> {
log.error("异步处理电池类型数据时发生异常", throwable);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "系统异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
});
} }
@ApiOperation(value = "接收电池类型数据接口")
@PostMapping("/categoryinfo")
public CompletableFuture<ResponseEntity<Map<String, String>>> categoryInfo(@RequestBody String receive) {
return CompletableFuture
.supplyAsync(() -> {
try {
return receiveService.recceiveCategoryInfo(receive);
} catch (Exception e) {
log.error("处理电池类型数据时发生异常", e);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "处理失败: " + e.getMessage());
return errorResponse;
}
}, taskExecutor)
.thenApply(result -> {
log.info("电池类型数据处理完成,返回结果: {}", result);
return ResponseEntity.ok(result);
})
.exceptionally(throwable -> {
log.error("异步处理电池类型数据时发生异常", throwable);
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("code", "9999");
errorResponse.put("msg", "系统异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
});
}
} }

View File

@ -0,0 +1,24 @@
package com.sczx.sync.dto;
import lombok.Data;
@Data
public class BatteryCategory {
private Integer id;
private String category_name;
private String product;
private String icon;
private Integer is_delete;
private String voltage;
private String ah;
private String max_voltage;
private String max_charge;
private Integer batch;
private Integer cell_id;
private Integer charge_id;
private Integer operator_id;
private Integer protect_id;
private Integer supplier_id;
}

View File

@ -0,0 +1,31 @@
package com.sczx.sync.mapper;
import com.sczx.sync.dto.BatteryCategory;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface BatteryCategoryMapper {
/**
* 根据ID查询电池类别
* @param id 主键ID
* @return 电池类别对象
*/
BatteryCategory selectById(@Param("id") Integer id);
/**
* 插入电池类别
* @param batteryCategory 电池类别对象
* @return 影响行数
*/
int insert(BatteryCategory batteryCategory);
int update(BatteryCategory batteryCategory);
}

View File

@ -11,4 +11,6 @@ public interface ReceiveService {
Map<String, String> recceiveCompanyInfo(String request); Map<String, String> recceiveCompanyInfo(String request);
Map<String, String> recceiveStoreInfo(String request); Map<String, String> recceiveStoreInfo(String request);
Map<String, String> recceiveCategoryInfo(String request);
} }

View File

@ -3,6 +3,7 @@ package com.sczx.sync.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.common.utils.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sczx.sync.dto.BatteryCategory;
import com.sczx.sync.dto.NoticeReq; import com.sczx.sync.dto.NoticeReq;
import com.sczx.sync.entity.UserInfo; import com.sczx.sync.entity.UserInfo;
import com.sczx.sync.mapper.*; import com.sczx.sync.mapper.*;
@ -38,6 +39,8 @@ public class ReceiveServiceImpl implements ReceiveService {
private CompanyStoreMapper companyStoreMapper; private CompanyStoreMapper companyStoreMapper;
@Autowired @Autowired
private NoticeMapper noticeMapper; private NoticeMapper noticeMapper;
@Autowired
private BatteryCategoryMapper batteryCategoryMapper;
@Value("${battery-platform.appid}") @Value("${battery-platform.appid}")
private String appId; private String appId;
@ -428,4 +431,27 @@ public class ReceiveServiceImpl implements ReceiveService {
return map; return map;
} }
@Override
public Map<String, String> recceiveCategoryInfo(String request) {
log.info("接收电池类型数据:{}", JSON.toJSONString(request));
SyncRequest<BatteryCategory> syncRequest = JSON.parseObject(request, SyncRequest.class);
Map<String, String> map = new HashMap<>();
if (!syncRequest.getAppid().equals(appId) || !syncRequest.getAccessToken().equals(accessToken)) {
map.put("msg", "没有权限");
map.put("code", "500");
return map;
}
BatteryCategory batteryCategory = JSON.parseObject(String.valueOf(syncRequest.getData()), BatteryCategory.class);
BatteryCategory Bc = batteryCategoryMapper.selectById(batteryCategory.getId());
if (Bc != null){
batteryCategoryMapper.update(batteryCategory);
}else{
batteryCategoryMapper.insert(batteryCategory);
}
map.put("msg", "成功");
map.put("code", "200");
return map;
}
} }

View File

@ -0,0 +1,121 @@
<?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.sczx.sync.mapper.BatteryCategoryMapper">
<!-- 结果映射 -->
<resultMap id="BatteryCategoryMap" type="com.sczx.sync.dto.BatteryCategory">
<id property="id" column="id"/>
<result property="category_name" column="category_name"/>
<result property="product" column="product"/>
<result property="icon" column="icon"/>
<result property="is_delete" column="is_delete"/>
<result property="voltage" column="voltage"/>
<result property="ah" column="ah"/>
<result property="max_voltage" column="max_voltage"/>
<result property="max_charge" column="max_charge"/>
<result property="batch" column="batch"/>
<result property="cell_id" column="cell_id"/>
<result property="charge_id" column="charge_id"/>
<result property="operator_id" column="operator_id"/>
<result property="protect_id" column="protect_id"/>
<result property="supplier_id" column="supplier_id"/>
</resultMap>
<!-- 表名 -->
<sql id="table_name">zc_battery_category</sql>
<!-- 所有字段 -->
<sql id="all_columns">
id, category_name, product, icon, is_delete, voltage, ah, max_voltage,
max_charge, batch, cell_id, charge_id, operator_id, protect_id, supplier_id
</sql>
<!-- 更新电池类别 -->
<update id="update" parameterType="com.sczx.sync.dto.BatteryCategory">
UPDATE
<include refid="table_name"/>
<set>
<if test="category_name != null and category_name != ''">
category_name = #{category_name},
</if>
<if test="product != null and product != ''">
product = #{product},
</if>
<if test="icon != null and icon != ''">
icon = #{icon},
</if>
<if test="is_delete != null">
is_delete = #{is_delete},
</if>
<if test="voltage != null and voltage != ''">
voltage = #{voltage},
</if>
<if test="ah != null and ah != ''">
ah = #{ah},
</if>
<if test="max_voltage != null and max_voltage != ''">
max_voltage = #{max_voltage},
</if>
<if test="max_charge != null and max_charge != ''">
max_charge = #{max_charge},
</if>
<if test="batch != null">
batch = #{batch},
</if>
<if test="cell_id != null">
cell_id = #{cell_id},
</if>
<if test="charge_id != null">
charge_id = #{charge_id},
</if>
<if test="operator_id != null">
operator_id = #{operator_id},
</if>
<if test="protect_id != null">
protect_id = #{protect_id},
</if>
<if test="supplier_id != null">
supplier_id = #{supplier_id},
</if>
</set>
WHERE id = #{id}
</update>
<!-- 根据ID查询电池类别 -->
<select id="selectById" resultMap="BatteryCategoryMap">
SELECT
<include refid="all_columns"/>
FROM
<include refid="table_name"/>
WHERE id = #{id}
</select>
<!-- 插入电池类别 -->
<insert id="insert" parameterType="com.sczx.sync.dto.BatteryCategory">
INSERT INTO
<include refid="table_name"/>
(
<include refid="all_columns"/>
)
VALUES (
#{id},
#{category_name},
#{product},
#{icon},
#{is_delete},
#{voltage},
#{ah},
#{max_voltage},
#{max_charge},
#{batch},
#{cell_id},
#{charge_id},
#{operator_id},
#{protect_id},
#{supplier_id}
)
</insert>
</mapper>