From bd874cbd64348c73bcc6c9e13659df24d6ce7920 Mon Sep 17 00:00:00 2001 From: x_z Date: Mon, 14 Nov 2022 11:24:02 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81=202.=E8=B0=83?= =?UTF-8?q?=E6=95=B4redis=E7=9A=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../glxp/udidl/admin/config/RedisConfig.java | 43 +++++++++ .../device/UdiUpdateController.java | 38 ++++++++ .../udidl/admin/req/UpdateUdiRequest.java | 21 +++++ .../service/dataSync/DeviceUpdateService.java | 92 +++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 src/main/java/com/glxp/udidl/admin/config/RedisConfig.java create mode 100644 src/main/java/com/glxp/udidl/admin/controller/device/UdiUpdateController.java create mode 100644 src/main/java/com/glxp/udidl/admin/req/UpdateUdiRequest.java create mode 100644 src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceUpdateService.java diff --git a/src/main/java/com/glxp/udidl/admin/config/RedisConfig.java b/src/main/java/com/glxp/udidl/admin/config/RedisConfig.java new file mode 100644 index 0000000..87d0b80 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/config/RedisConfig.java @@ -0,0 +1,43 @@ +package com.glxp.udidl.admin.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; + +/** + * Redis配置类 + */ +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(LettuceConnectionFactory cf) { + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setConnectionFactory(cf); + redisTemplate.setValueSerializer(RedisSerializer.json()); + redisTemplate.setHashValueSerializer(RedisSerializer.string()); + redisTemplate.setKeySerializer(RedisSerializer.string()); + redisTemplate.setHashKeySerializer(RedisSerializer.string()); + redisTemplate.afterPropertiesSet(); + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class); + ObjectMapper objectMapper = new ObjectMapper() + .registerModule(new ParameterNamesModule()) + .registerModule(new Jdk8Module()) + .registerModule(new JavaTimeModule()); + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + serializer.setObjectMapper(objectMapper); + redisTemplate.setValueSerializer(serializer); + return redisTemplate; + } + +} diff --git a/src/main/java/com/glxp/udidl/admin/controller/device/UdiUpdateController.java b/src/main/java/com/glxp/udidl/admin/controller/device/UdiUpdateController.java new file mode 100644 index 0000000..79999d0 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/controller/device/UdiUpdateController.java @@ -0,0 +1,38 @@ +package com.glxp.udidl.admin.controller.device; + +import cn.hutool.core.util.StrUtil; +import com.glxp.udidl.admin.annotation.AuthRuleAnnotation; +import com.glxp.udidl.admin.entity.udid.Contactlist; +import com.glxp.udidl.admin.enums.ResultEnum; +import com.glxp.udidl.admin.req.UpdateUdiRequest; +import com.glxp.udidl.admin.res.BaseResponse; +import com.glxp.udidl.admin.service.dataSync.DeviceUpdateService; +import com.glxp.udidl.admin.util.ResultVOUtils; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * 数据更新接口 + */ +@RestController +public class UdiUpdateController { + + @Resource + private DeviceUpdateService deviceUpdateService; + + @ApiOperation(value = "更新医疗器械信息接口", response = Contactlist.class) + @AuthRuleAnnotation("udi/update/updateUdiData") + @GetMapping("udi/update/updateUdiData") + public BaseResponse updateData(UpdateUdiRequest updateUdiRequest) { + if (StrUtil.isBlank(updateUdiRequest.getStartDate()) || StrUtil.isBlank(updateUdiRequest.getEndDate())) { + return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "未指定更新时间"); + } + deviceUpdateService.updateUdiData(updateUdiRequest); + return ResultVOUtils.success(); + } + + +} diff --git a/src/main/java/com/glxp/udidl/admin/req/UpdateUdiRequest.java b/src/main/java/com/glxp/udidl/admin/req/UpdateUdiRequest.java new file mode 100644 index 0000000..4c4bc0d --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/req/UpdateUdiRequest.java @@ -0,0 +1,21 @@ +package com.glxp.udidl.admin.req; + +import lombok.Data; + +/** + * 更新医疗器械信息接口请求参数 + */ +@Data +public class UpdateUdiRequest { + + /** + * 开始时间 + */ + private String startDate; + + /** + * 结束时间 + */ + private String endDate; + +} diff --git a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceUpdateService.java b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceUpdateService.java new file mode 100644 index 0000000..f1f82cf --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceUpdateService.java @@ -0,0 +1,92 @@ +package com.glxp.udidl.admin.service.dataSync; + +import cn.hutool.core.date.DateField; +import cn.hutool.core.date.DatePattern; +import cn.hutool.core.date.DateTime; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSONObject; +import com.glxp.udidl.admin.constant.Constant; +import com.glxp.udidl.admin.entity.info.CompanyEntity; +import com.glxp.udidl.admin.entity.udid.JobLog; +import com.glxp.udidl.admin.entity.udid.TokenEntity; +import com.glxp.udidl.admin.req.UpdateUdiRequest; +import com.glxp.udidl.admin.req.udid.TokenRequest; +import com.glxp.udidl.admin.service.info.CompanyService; +import com.glxp.udidl.admin.service.inout.DeviceService; +import com.glxp.udidl.admin.service.inout.ProductInfoService; +import com.glxp.udidl.admin.service.udi.JobLogService; +import com.glxp.udidl.admin.util.HttpClient; +import com.glxp.udidl.admin.util.RedisUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; + +@Slf4j +@Service +@Transactional(rollbackFor = Exception.class) +public class DeviceUpdateService { + + private String token; + private String url = "sharing/get"; + + @Resource + private RedisUtil redisUtil; + @Resource + private CompanyService companyService; + @Resource + private JobLogService jobLogService; //日志 + @Resource + private DeviceSaveService deviceSaveService; + @Resource + private DeviceService deviceService; + @Resource + private ProductInfoService productInfoService; + + @Async + public void updateUdiData(UpdateUdiRequest updateUdiRequest) { + log.info("开始更新数据,更新时间段为: {} - {}", updateUdiRequest.getStartDate(), updateUdiRequest.getEndDate()); + //解析更新参数 + DateTime startDate = DateUtil.parse(updateUdiRequest.getStartDate(), DatePattern.SIMPLE_MONTH_PATTERN); + DateTime endDate = DateUtil.parse(updateUdiRequest.getEndDate(), DatePattern.SIMPLE_MONTH_PATTERN); + + List dateRanges = DateUtil.rangeToList(startDate, endDate, DateField.MONTH); + + JobLog jobLog = new JobLog(); + jobLog.setCreateTime(new Date()); + jobLog.setStatus(0); + jobLog.setType(Constant.LOG_TYPE_INFO); + jobLog.setDownloadType(Constant.DOWNLOAD_TYPE_MANUAL); + jobLogService.insert(jobLog); + + + } + + + /** + * 获取当前token + * + * @return + */ + public String getToken() { + token = redisUtil.get("UDI_UPDATE_TOKEN"); + if (StrUtil.isEmpty(token)) { + //查询下载数据使用的appid + CompanyEntity companyEntity = companyService.findByTaskType(Constant.TASK_TYPE_UPDATE); + TokenRequest tokenRequest = new TokenRequest(); + tokenRequest.setAppId(companyEntity.getAppId()); + tokenRequest.setAppSecret(companyEntity.getAppSecret()); + tokenRequest.setTyshxydm(companyEntity.getTyshxydm()); + String response = HttpClient.post("token/get", tokenRequest); + TokenEntity tokenEntity = JSONObject.parseObject(response, TokenEntity.class); + token = tokenEntity.getAccessToken(); + redisUtil.setEx("UDI_UPDATE_TOKEN", token, 1 * 60 * 60 * 6); + } + return token; + } +}