|
@@ -1,20 +1,46 @@
|
|
package com.ywt.mg.services;
|
|
package com.ywt.mg.services;
|
|
|
|
|
|
|
|
+import com.ywt.gapi.ResultCode;
|
|
|
|
+import com.ywt.gapi.base.file.ReadFileRequest;
|
|
|
|
+import com.ywt.gapi.base.file.ReadFileResponse;
|
|
|
|
+import com.ywt.mg.configs.Constants;
|
|
|
|
+import com.ywt.mg.core.PagedList;
|
|
|
|
+import com.ywt.mg.core.SqlHelper;
|
|
import com.ywt.mg.core.utils.Checker;
|
|
import com.ywt.mg.core.utils.Checker;
|
|
|
|
+import com.ywt.mg.core.utils.CollectionUtil;
|
|
|
|
+import com.ywt.mg.core.utils.FormatUtil;
|
|
|
|
+import com.ywt.mg.core.utils.StringHelper;
|
|
|
|
+import com.ywt.mg.core.utils.serializers.JsonSerializer;
|
|
import com.ywt.mg.domain.entities.*;
|
|
import com.ywt.mg.domain.entities.*;
|
|
import com.ywt.mg.domain.models.ConstantDef;
|
|
import com.ywt.mg.domain.models.ConstantDef;
|
|
|
|
+import com.ywt.mg.domain.models.ExcelDataMap;
|
|
|
|
+import com.ywt.mg.domain.models.ExcelStyleMap;
|
|
|
|
+import com.ywt.mg.domain.models.enums.DownloadRecordStatusEnum;
|
|
|
|
+import com.ywt.mg.domain.models.enums.TosEnum;
|
|
|
|
+import com.ywt.mg.domain.models.pojo.ExcelCollectPojo;
|
|
import com.ywt.mg.domain.ywtDrugEntities.Pharmacy;
|
|
import com.ywt.mg.domain.ywtDrugEntities.Pharmacy;
|
|
import com.ywt.mg.domain.ywtDrugEntities.PharmacyRepository;
|
|
import com.ywt.mg.domain.ywtDrugEntities.PharmacyRepository;
|
|
|
|
+import com.ywt.mg.params.downloadRecord.DownloadFileRequest;
|
|
|
|
+import com.ywt.mg.params.downloadRecord.QueryDownloadRecordListRequest;
|
|
import com.ywt.mg.params.offline.DownloadOfflineListNewRequest;
|
|
import com.ywt.mg.params.offline.DownloadOfflineListNewRequest;
|
|
|
|
+import com.ywt.mg.web.BaseResponse;
|
|
|
|
+import com.ywt.mg.web.DataResponse;
|
|
|
|
+import com.ywt.mg.web.PageDataResponse;
|
|
|
|
+import com.ywt.mg.web.common.ExcelDownloadSrv;
|
|
|
|
+import com.ywt.mg.web.common.FileSrv;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import sun.rmi.runtime.Log;
|
|
|
|
+import sun.security.provider.MD5;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 下载记录相关方法
|
|
* 下载记录相关方法
|
|
@@ -32,17 +58,26 @@ public class DownloadRecordService {
|
|
@Autowired
|
|
@Autowired
|
|
private AuthService authService;
|
|
private AuthService authService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private SqlHelper sqlHelper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private FileSrv fileSrv;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ExcelDownloadSrv excelDownloadSrv;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 得到(重复点击情况下的下载记录)或插入一条新的下载记录
|
|
* 得到(重复点击情况下的下载记录)或插入一条新的下载记录
|
|
*
|
|
*
|
|
* @param id 表ID,需要生成
|
|
* @param id 表ID,需要生成
|
|
- * @param name 名称(格式: 公司后台-一级菜单-二级菜单)
|
|
|
|
- * @param paramMd5 下载参数的md5
|
|
|
|
|
|
+ * @param name 名称(前端路径)(格式: 公司后台-一级菜单-二级菜单)
|
|
|
|
+ * @param fileName 文件名称
|
|
* @param paramJson 下载参数的json
|
|
* @param paramJson 下载参数的json
|
|
* @param paramUrl 下载参数url
|
|
* @param paramUrl 下载参数url
|
|
* @return 下载记录
|
|
* @return 下载记录
|
|
*/
|
|
*/
|
|
- public DownloadRecord getOrInsertDownloadRecord(int id, String name, String paramMd5, String paramUrl) {
|
|
|
|
|
|
+ public DownloadRecord getOrInsertDownloadRecord(int id, String name, String fileName, String paramUrl, String paramJson) {
|
|
int pharmacyId = 0, hospitalId = 0, currentAdminId = 0;
|
|
int pharmacyId = 0, hospitalId = 0, currentAdminId = 0;
|
|
String pharmacyName = "", hospitalName = "", currentAdminName = "";
|
|
String pharmacyName = "", hospitalName = "", currentAdminName = "";
|
|
MGAdmins currentAdmin = authService.getCurrentAdmin();
|
|
MGAdmins currentAdmin = authService.getCurrentAdmin();
|
|
@@ -50,16 +85,6 @@ public class DownloadRecordService {
|
|
currentAdminId = currentAdmin.getId();
|
|
currentAdminId = currentAdmin.getId();
|
|
currentAdminName = currentAdmin.getAccount();
|
|
currentAdminName = currentAdmin.getAccount();
|
|
}
|
|
}
|
|
- DownloadRecord record = downloadRecordRepository.findCreatingLeastOne(currentAdminId, paramMd5, paramUrl);
|
|
|
|
- if (!Checker.isNone(record)) {
|
|
|
|
- return record;
|
|
|
|
- }
|
|
|
|
- record = new DownloadRecord();
|
|
|
|
- // todo: 需要判断是否有同样的下载
|
|
|
|
- record.setId(id);
|
|
|
|
- record.setName(name);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
Pharmacy pharmacy = authService.getCurrentPharmacy();
|
|
Pharmacy pharmacy = authService.getCurrentPharmacy();
|
|
if (!Checker.isNone(pharmacy)) {
|
|
if (!Checker.isNone(pharmacy)) {
|
|
pharmacyId = pharmacy.getId();
|
|
pharmacyId = pharmacy.getId();
|
|
@@ -70,6 +95,21 @@ public class DownloadRecordService {
|
|
hospitalId = hospital.getId();
|
|
hospitalId = hospital.getId();
|
|
hospitalName = hospital.getName();
|
|
hospitalName = hospital.getName();
|
|
}
|
|
}
|
|
|
|
+ Map map = new HashMap();
|
|
|
|
+ map.put("param", paramJson);
|
|
|
|
+ map.put("adminId", currentAdminId);
|
|
|
|
+ map.put("hospitalId", hospitalId);
|
|
|
|
+ map.put("pharmacyId", pharmacyId);
|
|
|
|
+ String paramMd5 = StringHelper.md5(JsonSerializer.toJson(map));
|
|
|
|
+ // todo: 判断是否有同样的下载
|
|
|
|
+ DownloadRecord record = downloadRecordRepository.findCreatingLeastOne(currentAdminId, paramMd5, paramUrl);
|
|
|
|
+ if (!Checker.isNone(record)) {
|
|
|
|
+ return record;
|
|
|
|
+ }
|
|
|
|
+ record = new DownloadRecord();
|
|
|
|
+ record.setId(id);
|
|
|
|
+ record.setName(name);
|
|
|
|
+ record.setFileName(fileName);
|
|
record.setPharmacyId(pharmacyId);
|
|
record.setPharmacyId(pharmacyId);
|
|
record.setPharmacyName(pharmacyName);
|
|
record.setPharmacyName(pharmacyName);
|
|
record.setHospitalId(hospitalId);
|
|
record.setHospitalId(hospitalId);
|
|
@@ -78,11 +118,140 @@ public class DownloadRecordService {
|
|
record.setAdminName(currentAdminName);
|
|
record.setAdminName(currentAdminName);
|
|
record.setCreateTime(new Date());
|
|
record.setCreateTime(new Date());
|
|
record.setParamMd5(paramMd5);
|
|
record.setParamMd5(paramMd5);
|
|
|
|
+ record.setParamJson(paramJson);
|
|
record.setParamUrl(paramUrl);
|
|
record.setParamUrl(paramUrl);
|
|
- record.setStatus(0);
|
|
|
|
|
|
+ record.setStatus(DownloadRecordStatusEnum.Default.getValue());
|
|
record.setUpdateTime(new Date());
|
|
record.setUpdateTime(new Date());
|
|
downloadRecordRepository.saveAndFlush(record);
|
|
downloadRecordRepository.saveAndFlush(record);
|
|
return record;
|
|
return record;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void saveUrlAndStatus(int id, String url, int status) {
|
|
|
|
+ DownloadRecord record = downloadRecordRepository.findOne(id);
|
|
|
|
+ if (!Checker.isNone(record)) {
|
|
|
|
+ record.setUrl(url);
|
|
|
|
+ record.setStatus(status);
|
|
|
|
+ if (status == DownloadRecordStatusEnum.SUCCESS.getValue()) {
|
|
|
|
+ Date finishTime = new Date();
|
|
|
|
+ record.setFinishTime(finishTime);
|
|
|
|
+ try {
|
|
|
|
+ int diff = (int) (finishTime.getTime() - record.getCreateTime().getTime());
|
|
|
|
+ record.setDifftime(diff);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("DownloadRecordService # saveUrlAndStatus 时间戳处理失败", e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ downloadRecordRepository.saveAndFlush(record);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public BaseResponse queryDownloadRecordList(QueryDownloadRecordListRequest request) throws Exception {
|
|
|
|
+ PageDataResponse<Object> pageDataResponse = new PageDataResponse<>();
|
|
|
|
+ PagedList<DownloadRecord> downloadRecordPagedList = queryDownloadRecordListCommon(request);
|
|
|
|
+ //做转换处理
|
|
|
|
+ if (!Checker.isNone(downloadRecordPagedList.getItems())) {
|
|
|
|
+ List<Map<String, Object>> datas = CollectionUtil.toList(downloadRecordPagedList.getItems(), p -> {
|
|
|
|
+ Map<String, Object> item = new HashMap<>(16);
|
|
|
|
+
|
|
|
|
+ item.put("id", p.getId());
|
|
|
|
+ item.put("name", p.getName());
|
|
|
|
+ item.put("createTime", FormatUtil.createTimeFormatDetail(p.getCreateTime()));
|
|
|
|
+ item.put("updateTime", FormatUtil.createTimeFormatDetail(p.getUpdateTime()));
|
|
|
|
+ item.put("status", p.getStatus());
|
|
|
|
+ item.put("statusStr", DownloadRecordStatusEnum.getDisplayName(p.getStatus()));
|
|
|
|
+ return item;
|
|
|
|
+ });
|
|
|
|
+ pageDataResponse.setData(datas);
|
|
|
|
+ } else {
|
|
|
|
+ pageDataResponse.setData(downloadRecordPagedList.getItems());
|
|
|
|
+ }
|
|
|
|
+ int count = downloadRecordPagedList.getTotal();
|
|
|
|
+ pageDataResponse.setPageIndex(request.getPageIndex());
|
|
|
|
+ pageDataResponse.setPageSize(request.getPageSize());
|
|
|
|
+ pageDataResponse.setTotalCount(count);
|
|
|
|
+ return pageDataResponse.succeed("处理成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private PagedList<DownloadRecord> queryDownloadRecordListCommon(QueryDownloadRecordListRequest request) throws ParseException {
|
|
|
|
+ List<Object> paramList = new ArrayList<>();
|
|
|
|
+ String whereSql = " ( 1=1 ) ";
|
|
|
|
+
|
|
|
|
+ int hospitalId = request.getHospitalId();
|
|
|
|
+ if (hospitalId > 0) {
|
|
|
|
+ whereSql += " and ( hospital_id = ?)";
|
|
|
|
+ paramList.add(hospitalId);
|
|
|
|
+ }
|
|
|
|
+ int adminId = request.getAdminId();
|
|
|
|
+ if (adminId > 0) {
|
|
|
|
+ whereSql += " and ( admin_id = ?)";
|
|
|
|
+ paramList.add(hospitalId);
|
|
|
|
+ }
|
|
|
|
+ int pageIndex = request.getPageIndex();
|
|
|
|
+ int pageSize = request.getPageSize();
|
|
|
|
+ return sqlHelper.getPagedList(pageIndex, pageSize, "*", whereSql, "create_time desc", DownloadRecord.class, paramList.toArray());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void downloadFile(DownloadFileRequest request, HttpServletResponse httpServletResponse) {
|
|
|
|
+ int downloadRecordId = request.getDownloadRecordId();
|
|
|
|
+ try {
|
|
|
|
+ if (downloadRecordId <= 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ DownloadRecord record = downloadRecordRepository.findOne(downloadRecordId);
|
|
|
|
+ if (!Checker.isNone(record)) {
|
|
|
|
+ String fileName = record.getFileName() + ".xlsx";
|
|
|
|
+ httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), Constants.CHARSET_NAME));
|
|
|
|
+ httpServletResponse.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
|
|
+ httpServletResponse.setHeader("Pragma", "no-cache");
|
|
|
|
+ httpServletResponse.setHeader("Cache-Control", "no-cache");
|
|
|
|
+ httpServletResponse.setDateHeader("Expires", 0);
|
|
|
|
+
|
|
|
|
+ OutputStream out = httpServletResponse.getOutputStream();
|
|
|
|
+ String url = Checker.getStringValue(record.getUrl());
|
|
|
|
+ if (!Checker.isNone(url)) {
|
|
|
|
+ byte[] b = fileSrv.readFile(url);
|
|
|
|
+ if (!Checker.isNone(b)) {
|
|
|
|
+ if (!Checker.isNone(b)) {
|
|
|
|
+ out.write(b);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ out.flush();
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error("DownloadRecordService#downloadFile(), downloadRecordId={}", downloadRecordId, e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建文件 并且上传到 OSS,最后保存到本地数据库
|
|
|
|
+ *
|
|
|
|
+ * @param fileName 文件名称
|
|
|
|
+ * @param downloadRecordId 对应 downloadRecord 表ID
|
|
|
|
+ * @param map 数据map
|
|
|
|
+ * @param itemList 统计map
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public void createFileAndUploadOssAndSaveToDataBase(String fileName, int downloadRecordId, ExcelDataMap map, List<ExcelCollectPojo> itemList) throws Exception {
|
|
|
|
+ fileName = fileName + FormatUtil.formatDate(new Date(), FormatUtil.FORMAT_DATE_SECOND_NUMBER);
|
|
|
|
+ String preLocalPath = ConstantDef.LOCAL_DOWNLOAD_FILE_PATH;
|
|
|
|
+ // 写到服务器上
|
|
|
|
+ String localPath = preLocalPath + fileName + ".xlsx";
|
|
|
|
+ ExcelStyleMap excelStyleMap = new ExcelStyleMap(new String[]{});
|
|
|
|
+ // 生成本地文件
|
|
|
|
+ excelDownloadSrv.generateAndReturnExcelFileWithNoStyleAndCollectLocal(fileName, localPath, map, excelStyleMap, itemList);
|
|
|
|
+ String ossPath = String.format(ConstantDef.BIG_DATA_EXCEL_FILE_PATH, FormatUtil.formatDate(new Date(), "yyyy-MM"));
|
|
|
|
+ // 本地文件上传到oss
|
|
|
|
+ DataResponse dataResponse = fileSrv.localFileUploadToOss(localPath, fileName, ossPath);
|
|
|
|
+ if (dataResponse.getCode() == BaseResponse.SUCCEED) {
|
|
|
|
+ // 上传成功,将上传状态和地址保存到数据库
|
|
|
|
+ saveUrlAndStatus(downloadRecordId, dataResponse.getData().toString(), DownloadRecordStatusEnum.SUCCESS.getValue());
|
|
|
|
+ } else {
|
|
|
|
+ // 上传失败,将上传状态保存到数据库
|
|
|
|
+ saveUrlAndStatus(downloadRecordId, "", DownloadRecordStatusEnum.FAILURE.getValue());
|
|
|
|
+ }
|
|
|
|
+ // 删除文件
|
|
|
|
+ fileSrv.deleteFile(localPath);
|
|
|
|
+ }
|
|
}
|
|
}
|