From 41702743c93f3456fe28f125b303d0f977c48ebe Mon Sep 17 00:00:00 2001 From: anthonyywj2 <353682448@qq.com> Date: Sat, 13 Aug 2022 11:50:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8B=E8=BD=BD=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E8=AE=B0=E5=BD=95=E4=B8=8A=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E7=8A=B6=E6=80=81=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/UdidlDeviceController.java | 48 ++++++++++- .../glxp/udidl/admin/entity/udid/JobLog.java | 15 ++++ .../udidl/admin/req/udid/DeviceRequest.java | 3 + .../glxp/udidl/admin/req/udid/JobRequest.java | 8 ++ .../dataSync/DeviceDownloadService.java | 79 ++++++++++++++++++- .../service/dataSync/DeviceSyncService.java | 30 ++----- .../admin/service/udi/JobLogService.java | 3 + .../service/udi/impl/JobLogServiceImpl.java | 5 ++ .../udi/impl/ProductClassifyServiceImpl.java | 8 +- 9 files changed, 169 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/glxp/udidl/admin/req/udid/JobRequest.java diff --git a/src/main/java/com/glxp/udidl/admin/controller/device/UdidlDeviceController.java b/src/main/java/com/glxp/udidl/admin/controller/device/UdidlDeviceController.java index a0a36e0..ef15719 100644 --- a/src/main/java/com/glxp/udidl/admin/controller/device/UdidlDeviceController.java +++ b/src/main/java/com/glxp/udidl/admin/controller/device/UdidlDeviceController.java @@ -1,13 +1,21 @@ package com.glxp.udidl.admin.controller.device; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.glxp.udidl.admin.annotation.AuthRuleAnnotation; +import com.glxp.udidl.admin.constant.Constant; import com.glxp.udidl.admin.entity.udid.DeviceEntity; +import com.glxp.udidl.admin.entity.udid.JobLog; import com.glxp.udidl.admin.req.ProductInfoFilterRequest; import com.glxp.udidl.admin.req.udid.DeviceRequest; +import com.glxp.udidl.admin.req.udid.JobRequest; import com.glxp.udidl.admin.res.BaseResponse; import com.glxp.udidl.admin.res.udid.DataSetSingleHistoryResult; +import com.glxp.udidl.admin.service.dataSync.DeviceDownloadService; +import com.glxp.udidl.admin.service.dataSync.DeviceSaveService; import com.glxp.udidl.admin.service.dataSync.DeviceSyncService; import com.glxp.udidl.admin.service.inout.ProductInfoService; +import com.glxp.udidl.admin.service.udi.JobLogService; import com.glxp.udidl.admin.util.ResultVOUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -17,10 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Date; @@ -35,6 +40,10 @@ public class UdidlDeviceController { DeviceSyncService deviceSyncService; @Resource ProductInfoService productInfoService; + @Resource + JobLogService jobLogService; + @Resource + DeviceDownloadService deviceDownloadService; @ApiOperation(value = "按日期段下载医疗器械信息") @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", required = true, dataType = "Date"), @@ -57,6 +66,37 @@ public class UdidlDeviceController { return ResultVOUtils.success("后台已开始下载!"); } + + @ApiOperation(value = "继续下载") + @ApiImplicitParam(name = "jobId", value = "任务ID", required = true, dataType = "String") + @AuthRuleAnnotation("udidl/device/goOnDlDi") + @GetMapping("udidl/device/goOnDlDi") + public BaseResponse goOnDlDi(JobRequest jobRequest) { + JobLog jobLog = jobLogService.findById(jobRequest.getJobId()); + if (jobLog != null) { + if (StrUtil.isNotEmpty(jobLog.getLastUploadRequest())) { + DeviceRequest deviceRequest = JSONUtil.toBean(jobLog.getLastUploadRequest(), DeviceRequest.class); + if (deviceRequest != null) { + logger.info("继续上一次下载"); + boolean b = deviceDownloadService.getDataAndSave(deviceRequest, jobLog); + if (b) { + jobLog.setStatus(1); + jobLog.setType(Constant.LOG_TYPE_SUCCESS); + jobLog.setUpdateTime(new Date()); + jobLogService.update(jobLog); + } else { + jobLog.setStatus(1); + jobLog.setType(Constant.LOG_TYPE_ERROR); + jobLog.setUpdateTime(new Date()); + jobLogService.update(jobLog); + } + } + } + } + return ResultVOUtils.success("后台已开始下载!"); + } + + //todo 补全返回参数说明 @ApiOperation(value = "下载指定标识的设备信息", response = DataSetSingleHistoryResult.class) @ApiImplicitParam(name = "deviceId", value = "设备ID", required = true, dataType = "String") diff --git a/src/main/java/com/glxp/udidl/admin/entity/udid/JobLog.java b/src/main/java/com/glxp/udidl/admin/entity/udid/JobLog.java index bdf5fc3..1178147 100644 --- a/src/main/java/com/glxp/udidl/admin/entity/udid/JobLog.java +++ b/src/main/java/com/glxp/udidl/admin/entity/udid/JobLog.java @@ -57,4 +57,19 @@ public class JobLog { @ApiModelProperty(value = "状态 0:处理中 1.已完成") private Integer status; + + + private String lastUploadRequest; + + public Integer getInsertCount() { + if (insertCount == null) + return 0; + return insertCount; + } + + public Integer getRealCount() { + if (realCount == null) + return 0; + return realCount; + } } diff --git a/src/main/java/com/glxp/udidl/admin/req/udid/DeviceRequest.java b/src/main/java/com/glxp/udidl/admin/req/udid/DeviceRequest.java index 8aafb89..171f1d0 100644 --- a/src/main/java/com/glxp/udidl/admin/req/udid/DeviceRequest.java +++ b/src/main/java/com/glxp/udidl/admin/req/udid/DeviceRequest.java @@ -53,4 +53,7 @@ public class DeviceRequest { @ApiModelProperty(value = "下载方式: auto:自动 manual:手动") private String downloadType; + + + } diff --git a/src/main/java/com/glxp/udidl/admin/req/udid/JobRequest.java b/src/main/java/com/glxp/udidl/admin/req/udid/JobRequest.java new file mode 100644 index 0000000..132bf41 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/req/udid/JobRequest.java @@ -0,0 +1,8 @@ +package com.glxp.udidl.admin.req.udid; + +import lombok.Data; + +@Data +public class JobRequest { + private String jobId; +} diff --git a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java index 925d1fb..d81a29f 100644 --- a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java +++ b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceDownloadService.java @@ -2,6 +2,7 @@ package com.glxp.udidl.admin.service.dataSync; import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.glxp.udidl.admin.constant.Constant; @@ -235,7 +236,7 @@ public class DeviceDownloadService { /** * 按天/按月下载 * - * @param 日期:如果是天,就是单日时间,如果是月份,就是一个月份 yyyy-MM + * @param :如果是天,就是单日时间,如果是月份,就是一个月份 yyyy-MM * @return */ public DataSetTotalResult getData(DeviceRequest deviceRequest) { @@ -282,6 +283,82 @@ public class DeviceDownloadService { } + public boolean getDataAndSave(DeviceRequest deviceRequest, JobLog jobLog) { + DataSetTotalResult results = new DataSetTotalResult(); + int page = 1; + if (StrUtil.isNotEmpty(deviceRequest.getCurrentPageNumber())) { + page = Integer.parseInt(deviceRequest.getCurrentPageNumber()); + } + boolean hasData = true; //是否还有数据,如无退出 + + while (hasData) { + DataSetResult dataSetResult = getData(deviceRequest, page); + if (dataSetResult.getReturnCode() != 1 && reCount == 0) { + token = ""; + reCount++; + try { + dataSetResult = getData(deviceRequest, page); + } catch (Exception e) { + jobLog.setStatus(1); + jobLog.setType(Constant.LOG_TYPE_ERROR); + jobLog.setMsg("下载失败:" + e.getMessage()); + jobLog.setLastUploadRequest(JSONUtil.toJsonStr(deviceRequest)); + jobLogService.update(jobLog); + return false; + } + } + if (dataSetResult.getReturnCode() != 1) { + results.setCode(-1); + String msg = results.getMsg(); + msg = msg + JSON.toJSONString(dataSetResult) + ";"; + results.setMsg(msg); + jobLog.setStatus(1); + jobLog.setMsg("下载失败:" + msg); + jobLog.setType(Constant.LOG_TYPE_ERROR); + jobLog.setStatus(1); + jobLog.setLastUploadRequest(JSONUtil.toJsonStr(deviceRequest)); + jobLogService.update(jobLog); + return false; + } else { + if (null != dataSetResult.getDataSet()) { + if (dataSetResult.getDataSet().getDeviceInfo() != null && dataSetResult.getDataSet().getDeviceInfo().size() > 0) { + List dataSets = new ArrayList<>(); + dataSets.add(dataSetResult.getDataSet()); + try { + int insertCount = deviceSaveService.DeviceSave(dataSets, jobLog.getDownloadDate()); + jobLog.setRealCount(jobLog.getRealCount() + dataSetResult.getDataSet().getDeviceInfo().size()); + jobLog.setInsertCount(jobLog.getInsertCount() + insertCount); + jobLog.setTotalCount(dataSetResult.getTotalRecordCount()); + jobLog.setMsg("下载中:" + "总条数:" + dataSetResult.getTotalRecordCount() + ";下载条数:" + jobLog.getInsertCount() + ";插入条数:" + jobLog.getInsertCount()); + jobLog.setLastUploadRequest(JSONUtil.toJsonStr(deviceRequest)); + jobLogService.update(jobLog); + } catch (Exception e) { + log.error("插入数据失败", e); + jobLog.setStatus(1); + jobLog.setType(Constant.LOG_TYPE_ERROR); + jobLog.setMsg("数据写入失败" + e.getMessage()); + jobLog.setUpdateTime(new Date()); + jobLog.setInsertCount(0); + jobLog.setLastUploadRequest(JSONUtil.toJsonStr(deviceRequest)); + jobLogService.update(jobLog); + return false; + } + } else { + hasData = false; + break; + } + } else { + hasData = false; + break; + } + } + page++; + + } + return true; + } + + public List getKeyList(DataSetResult.DataSet ds) { return ds.getDeviceInfo().stream().map(item -> item.getDeviceRecordKey()).collect(Collectors.toList()); } diff --git a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceSyncService.java b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceSyncService.java index 2c2d207..0ed9683 100644 --- a/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceSyncService.java +++ b/src/main/java/com/glxp/udidl/admin/service/dataSync/DeviceSyncService.java @@ -381,7 +381,6 @@ public class DeviceSyncService { if (deviceRequest.getRangeValue().contains("--")) { String[] dateArr = deviceRequest.getRangeValue().split("--"); int compareResult = DateUtil.compareDate(dateArr[0], dateArr[1], "yyyy-MM-dd"); - if (compareResult == -1) { long between = cn.hutool.core.date.DateUtil.between(cn.hutool.core.date.DateUtil.parseDate(dateArr[0]), cn.hutool.core.date.DateUtil.parseDate(dateArr[1]), DateUnit.DAY); between += 1; //包括自身 @@ -419,41 +418,24 @@ public class DeviceSyncService { jobLog.setCreateTime(new Date()); jobLog.setStatus(0); jobLogService.insertLog(jobLog); - - + log.info(jobLog.getId() + "----"); //开始下载数据 - DataSetTotalResult data = deviceDownloadService.getData(deviceRequest); - if (data.getCode() != 1) { - log.error(date + " 数据下载失败"); - jobLog.setStatus(1); - jobLog.setType(Constant.LOG_TYPE_ERROR); - jobLog.setMsg(data.getMsg()); - jobLog.setUpdateTime(new Date()); - jobLogService.update(jobLog); - continue; - } + boolean b = deviceDownloadService.getDataAndSave(deviceRequest, jobLog); - jobLog.setRealCount(data.realRecordCount()); - jobLog.setTotalCount(data.getTotalRecordCount()); - try { - int insertCount = deviceSaveService.DeviceSave(data.getData(), DateUtil.parseDate(date)); + if (b) { jobLog.setStatus(1); jobLog.setType(Constant.LOG_TYPE_SUCCESS); - jobLog.setMsg("下载成功,总条数:" + data.getTotalRecordCount() + " ;插入数据:" + insertCount + " 条"); jobLog.setUpdateTime(new Date()); - jobLog.setInsertCount(insertCount); jobLogService.update(jobLog); - } catch (Exception e) { - log.error("插入数据失败", e); + } else { jobLog.setStatus(1); jobLog.setType(Constant.LOG_TYPE_ERROR); - jobLog.setMsg("下载成功,数据插入失败"); jobLog.setUpdateTime(new Date()); - jobLog.setInsertCount(0); jobLogService.update(jobLog); + continue; } - log.info(date + " 的数据下载完成"); } + log.info("本次任务下载结束时间:" + DateUtil.getDateTime()); } } diff --git a/src/main/java/com/glxp/udidl/admin/service/udi/JobLogService.java b/src/main/java/com/glxp/udidl/admin/service/udi/JobLogService.java index 40ac70f..0b79447 100644 --- a/src/main/java/com/glxp/udidl/admin/service/udi/JobLogService.java +++ b/src/main/java/com/glxp/udidl/admin/service/udi/JobLogService.java @@ -7,6 +7,9 @@ import com.glxp.udidl.admin.res.BaseResponse; public interface JobLogService { BaseResponse getList(JobLogFilterRequest param); + + JobLog findById(String jobId); + int insert(JobLog jobLog); int update(JobLog jobLog); diff --git a/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java b/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java index 583b664..5cf70e6 100644 --- a/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java +++ b/src/main/java/com/glxp/udidl/admin/service/udi/impl/JobLogServiceImpl.java @@ -36,6 +36,11 @@ public class JobLogServiceImpl implements JobLogService { return ResultVOUtils.success(pageSimpleResponse); } + @Override + public JobLog findById(String jobId) { + return jobLogMapper.selectById(jobId); + } + @Transactional(rollbackFor = Exception.class) public int insert(JobLog jobLog) { return jobLogMapper.insert(jobLog); diff --git a/src/main/java/com/glxp/udidl/admin/service/udi/impl/ProductClassifyServiceImpl.java b/src/main/java/com/glxp/udidl/admin/service/udi/impl/ProductClassifyServiceImpl.java index 6b6ff8b..25afa79 100644 --- a/src/main/java/com/glxp/udidl/admin/service/udi/impl/ProductClassifyServiceImpl.java +++ b/src/main/java/com/glxp/udidl/admin/service/udi/impl/ProductClassifyServiceImpl.java @@ -1,5 +1,6 @@ package com.glxp.udidl.admin.service.udi.impl; +import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.glxp.udidl.admin.dao.udi.ProductClassifyMapper; import com.glxp.udidl.admin.entity.udi.ProductClassify; @@ -7,6 +8,7 @@ import com.glxp.udidl.admin.service.udi.ProductClassifyService; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.List; @Service public class ProductClassifyServiceImpl implements ProductClassifyService { @@ -17,7 +19,11 @@ public class ProductClassifyServiceImpl implements ProductClassifyService { @Override public ProductClassify findByCode(String code, String qxlb) { - return productClassifyMapper.selectOne(new QueryWrapper().eq("code", code).eq("qxlb", qxlb)); + List productClassifyList = productClassifyMapper.selectList(new QueryWrapper().eq("code", code).eq("qxlb", qxlb)); + if (CollUtil.isNotEmpty(productClassifyList)) { + productClassifyList.get(0); + } + return new ProductClassify(); } }