修改下载逻辑,记录上一次下载状态,新增继续下载

master
anthonyywj2 3 years ago
parent 94759bc144
commit 41702743c9

@ -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")

@ -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;
}
}

@ -53,4 +53,7 @@ public class DeviceRequest {
@ApiModelProperty(value = "下载方式: auto自动 manual手动")
private String downloadType;
}

@ -0,0 +1,8 @@
package com.glxp.udidl.admin.req.udid;
import lombok.Data;
@Data
public class JobRequest {
private String jobId;
}

@ -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<DataSetResult.DataSet> 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<String> getKeyList(DataSetResult.DataSet ds) {
return ds.getDeviceInfo().stream().map(item -> item.getDeviceRecordKey()).collect(Collectors.toList());
}

@ -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());
}
}

@ -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);

@ -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);

@ -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<ProductClassify>().eq("code", code).eq("qxlb", qxlb));
List<ProductClassify> productClassifyList = productClassifyMapper.selectList(new QueryWrapper<ProductClassify>().eq("code", code).eq("qxlb", qxlb));
if (CollUtil.isNotEmpty(productClassifyList)) {
productClassifyList.get(0);
}
return new ProductClassify();
}
}

Loading…
Cancel
Save