From abaf6d1697983cbc51abc354ca072d8fc8343472 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Mon, 30 May 2022 13:51:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bweb/config/RedisConfig.java | 19 +++- .../msg/RedisKeyExpirationListener.java | 34 ++++++ .../HighGasOilPriceTaskController.java | 106 ++++++++++++++++-- .../java/com/hai/msg/entity/MsgTopic.java | 3 +- .../service/HighGasOilPriceTaskService.java | 6 + .../impl/HighGasOilPriceTaskServiceImpl.java | 13 ++- 6 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 hai-bweb/src/main/java/com/bweb/config/msg/RedisKeyExpirationListener.java diff --git a/hai-bweb/src/main/java/com/bweb/config/RedisConfig.java b/hai-bweb/src/main/java/com/bweb/config/RedisConfig.java index 0c5f530e..003edb36 100644 --- a/hai-bweb/src/main/java/com/bweb/config/RedisConfig.java +++ b/hai-bweb/src/main/java/com/bweb/config/RedisConfig.java @@ -3,12 +3,16 @@ package com.bweb.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; +import com.hai.msg.entity.MsgTopic; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.*; +import org.springframework.data.redis.listener.PatternTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -17,6 +21,19 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; @EnableCaching //开启注解 public class RedisConfig extends CachingConfigurerSupport { + + @Bean(destroyMethod = "destroy") + public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory, + MessageListener driverLBSMsgListener) { + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(factory); + + //可以添加多个 messageListener + container.addMessageListener(driverLBSMsgListener, new PatternTopic(MsgTopic.oilPriceTask.getName())); + + return container; + } + /** * retemplate相关配置 * @param factory @@ -106,4 +123,4 @@ public class RedisConfig extends CachingConfigurerSupport { public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { return redisTemplate.opsForZSet(); } -} \ No newline at end of file +} diff --git a/hai-bweb/src/main/java/com/bweb/config/msg/RedisKeyExpirationListener.java b/hai-bweb/src/main/java/com/bweb/config/msg/RedisKeyExpirationListener.java new file mode 100644 index 00000000..1adf852e --- /dev/null +++ b/hai-bweb/src/main/java/com/bweb/config/msg/RedisKeyExpirationListener.java @@ -0,0 +1,34 @@ +package com.bweb.config.msg; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.redis.connection.Message; +import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; +import org.springframework.stereotype.Component; + +/** + * @author hurui + * @version 1.0 + * @ClassName RedisKeyExpirationListener + * @description: TODO + * @date 2021/8/19 10:00 + */ +@Component +public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener { + + private static Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class); + + public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { + super(listenerContainer); + } + + public void onMessage(Message message, byte[] pattern) { + try { + System.out.println(message); + } catch (Exception e) { + logger.error("redis过期事件异常:",e); + } + } +} diff --git a/hai-bweb/src/main/java/com/bweb/controller/HighGasOilPriceTaskController.java b/hai-bweb/src/main/java/com/bweb/controller/HighGasOilPriceTaskController.java index 440a5301..9063a63c 100644 --- a/hai-bweb/src/main/java/com/bweb/controller/HighGasOilPriceTaskController.java +++ b/hai-bweb/src/main/java/com/bweb/controller/HighGasOilPriceTaskController.java @@ -6,9 +6,18 @@ import com.github.pagehelper.PageInfo; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; +import com.hai.common.security.UserCenter; import com.hai.common.utils.ResponseMsgUtil; +import com.hai.entity.HighGasOilPriceTask; +import com.hai.entity.HighMerchantStore; +import com.hai.entity.SecDictionary; +import com.hai.entity.SecRegion; +import com.hai.enum_type.GasTaskPriceTypeEnum; import com.hai.model.ResponseData; +import com.hai.model.UserInfoModel; +import com.hai.service.CommonService; import com.hai.service.HighGasOilPriceTaskService; +import com.hai.service.HighMerchantStoreService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; @@ -18,6 +27,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.HashMap; +import java.util.List; import java.util.Map; @Controller @@ -30,19 +40,95 @@ public class HighGasOilPriceTaskController { @Resource private HighGasOilPriceTaskService gasOilPriceTaskService; - @RequestMapping(value="/addTask",method = RequestMethod.POST) + @Resource + private HighMerchantStoreService merchantStoreService; + + @Resource + private CommonService commonService; + + @Resource + private UserCenter userCenter; + + @RequestMapping(value="/batchAddTask",method = RequestMethod.POST) @ResponseBody - @ApiOperation(value = "增加任务") - public ResponseData addTask(@RequestBody JSONObject body) { + @ApiOperation(value = "批量增加任务") + public ResponseData batchAddTask(@RequestBody List taskList) { try { - if (body.getLong("regionId") == null - || body.getInteger("oilNo") == null - || body.getBigDecimal("price") == null - ) { + UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class); + if (userInfoModel == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, ""); + } + if (taskList == null || taskList.size() == 0) { log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } + for (HighGasOilPriceTask task : taskList) { + if (task.getPriceType() == null + || task.getPrice() == null + || task.getOilNo() == null + || task.getExecutionType() == null + ) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + // 执行方式 1. 立刻执行 2. 定时执行 + if (task.getExecutionType().equals(2) && task.getStartTime() == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置执行时间"); + } + + // 价格类型 1. 国标价 2. 油站价 3. 优惠幅度 + if (task.getPriceType().equals(GasTaskPriceTypeEnum.type1.getStatus())) { + if (task.getRegionId() == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置区域"); + } + // 加油站 + SecRegion region = commonService.getRegionsById(task.getRegionId()); + if (region == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到区域"); + } + task.setRegionId(region.getRegionId()); + task.setRegionName(region.getRegionName()); + } + + // 价格类型 1. 国标价 2. 油站价 3. 优惠幅度 + if (task.getPriceType().equals(GasTaskPriceTypeEnum.type2.getStatus()) + || task.getPriceType().equals(GasTaskPriceTypeEnum.type3.getStatus())) { + + if (task.getMerStoreId() == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置加油站"); + } + // 加油站 + HighMerchantStore store = merchantStoreService.getDetailById(task.getMerStoreId()); + if (store == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到加油站"); + } + task.setMerStoreId(store.getId()); + task.setMerStoreName(store.getStoreName()); + task.setMerStoreAddress(store.getAddress()); + } + + // 查询油品 + SecDictionary oil = commonService.mappingSysCode("GAS_OIL_TYPE", task.getOilNo().toString()); + if (oil == null) { + log.error("HighGasDiscountOilPriceController -> updateOilPriceOfficial() error!",""); + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + task.setOilType(Integer.parseInt(oil.getExt1())); + task.setOilTypeName(oil.getExt2()); + task.setOilNoName(oil.getCodeName()); + task.setOpUserId(userInfoModel.getSecUser().getId()); + task.setOpUserName(userInfoModel.getSecUser().getUserName()); + } + + gasOilPriceTaskService.batchAddTask(taskList); + return ResponseMsgUtil.success("操作成功"); } catch (Exception e) { @@ -103,6 +189,12 @@ public class HighGasOilPriceTaskController { Map param = new HashMap<>(); param.put("regionId", regionId); param.put("regionName", regionName); + param.put("merStoreId", status); + param.put("merStoreName", status); + param.put("oilType", status); + param.put("oilNo", status); + param.put("priceType", status); + param.put("executionType", status); param.put("status", status); PageHelper.startPage(pageNum,pageSize); diff --git a/hai-service/src/main/java/com/hai/msg/entity/MsgTopic.java b/hai-service/src/main/java/com/hai/msg/entity/MsgTopic.java index a2ebb3dd..65af6191 100644 --- a/hai-service/src/main/java/com/hai/msg/entity/MsgTopic.java +++ b/hai-service/src/main/java/com/hai/msg/entity/MsgTopic.java @@ -3,7 +3,8 @@ package com.hai.msg.entity; public enum MsgTopic { // 门店账户业务员 - MerStoreAccount("mer-store-account"); + MerStoreAccount("mer-store-account"), + oilPriceTask("OIL-PRICE-TASK"); private String name; diff --git a/hai-service/src/main/java/com/hai/service/HighGasOilPriceTaskService.java b/hai-service/src/main/java/com/hai/service/HighGasOilPriceTaskService.java index dd528cb1..3d6e7a69 100644 --- a/hai-service/src/main/java/com/hai/service/HighGasOilPriceTaskService.java +++ b/hai-service/src/main/java/com/hai/service/HighGasOilPriceTaskService.java @@ -17,6 +17,12 @@ public interface HighGasOilPriceTaskService { */ void editData(HighGasOilPriceTask gasOilPriceTask); + /** + * 批量增加价格任务 + * @param taskList + */ + void batchAddTask(List taskList); + /** * 增加价格任务 * @param gasOilPriceTask diff --git a/hai-service/src/main/java/com/hai/service/impl/HighGasOilPriceTaskServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighGasOilPriceTaskServiceImpl.java index e01f6f1b..3ad0fbc1 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighGasOilPriceTaskServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighGasOilPriceTaskServiceImpl.java @@ -17,6 +17,8 @@ import com.hai.service.HighGasOilPriceTaskService; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; @@ -48,14 +50,21 @@ public class HighGasOilPriceTaskServiceImpl implements HighGasOilPriceTaskServic } @Override + @Transactional(propagation= Propagation.REQUIRED) + public void batchAddTask(List taskList) { + for (HighGasOilPriceTask task : taskList) { + addTask(task); + } + } + + @Override + @Transactional(propagation= Propagation.REQUIRED) public void addTask(HighGasOilPriceTask gasOilPriceTask) { editData(gasOilPriceTask); - // 立刻执行 if (gasOilPriceTask.getExecutionType().equals(GasTaskExecutionTypeEnum.type1.getStatus())) { businessHandle(gasOilPriceTask); } - } @Override