|
@@ -0,0 +1,277 @@
|
|
|
|
+package cn.lttc.modules.lttcsamplescan.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
+import cn.lttc.modules.lttcsamplescan.entity.SampleScan;
|
|
|
|
+import cn.lttc.modules.lttcsamplescan.entity.SampleScanDetail;
|
|
|
|
+import cn.lttc.modules.lttcsamplescan.entity.SampleScanFile;
|
|
|
|
+import cn.lttc.modules.lttcsamplescan.mapper.SampleScanMapper;
|
|
|
|
+import cn.lttc.modules.lttcsamplescan.service.ISampleScanService;
|
|
|
|
+import cn.lttc.modules.utils.HttpUtils;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: 保存
|
|
|
|
+ * @Author: GDW
|
|
|
|
+ * @Date: 2024-01-24
|
|
|
|
+ * @Version: V1.0
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class SampleScanServiceImpl implements ISampleScanService {
|
|
|
|
+
|
|
|
|
+ @Value("${file.upload-path}")
|
|
|
|
+ private String uploadDir;
|
|
|
|
+ @Resource
|
|
|
|
+ private SampleScanMapper sampleScanMapper;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public synchronized String saveData(MultipartHttpServletRequest mulReq) {
|
|
|
|
+ //日期处理开始
|
|
|
|
+ // 获取当前年份
|
|
|
|
+ int currentYear = LocalDate.now().getYear();
|
|
|
|
+ int currentMonth = LocalDate.now().getMonthValue();
|
|
|
|
+ int monthLength = Integer.toString(currentMonth).length();
|
|
|
|
+ //两位字符串月
|
|
|
|
+ String currentMonthStr;
|
|
|
|
+ if (monthLength == 1){
|
|
|
|
+ currentMonthStr = "0" + currentMonth;
|
|
|
|
+ } else {
|
|
|
|
+ currentMonthStr = String.valueOf(currentMonth);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int dayOfMonth = LocalDate.now().getDayOfMonth();
|
|
|
|
+ int dayLength = Integer.toString(dayOfMonth).length();
|
|
|
|
+ //两位字符串 day
|
|
|
|
+ String dayStr;
|
|
|
|
+ if (dayLength == 1){
|
|
|
|
+ dayStr = "0" + dayOfMonth;
|
|
|
|
+ } else {
|
|
|
|
+ dayStr = String.valueOf(dayOfMonth);
|
|
|
|
+ }
|
|
|
|
+ //日期处理结束
|
|
|
|
+
|
|
|
|
+ //处理主表数据与明细表数据
|
|
|
|
+ String mainJson = mulReq.getParameter("main");
|
|
|
|
+ String detailJson = mulReq.getParameter("detail");
|
|
|
|
+ //将JSON字符串转换为Java对象
|
|
|
|
+ SampleScan sampleScan = JSON.parseObject(mainJson, SampleScan.class);
|
|
|
|
+ List<SampleScanDetail> sampleScanDetails = JSON.parseArray(detailJson, SampleScanDetail.class);
|
|
|
|
+
|
|
|
|
+ //查询数据库中最大的编号
|
|
|
|
+ String maxCode = sampleScanMapper.getMaxCode();
|
|
|
|
+ if (maxCode != null && !maxCode.isEmpty()) {
|
|
|
|
+ // 截取字符串的前面部分和后四位 (前八位为 年 + 月 + 日 后四位为流水号)
|
|
|
|
+ String frontPart = maxCode.substring(0, maxCode.length() - 4);
|
|
|
|
+ String lastFourCharacters = maxCode.substring(maxCode.length() - 4);
|
|
|
|
+ //获取当前的年月日
|
|
|
|
+ String now = currentYear + currentMonthStr + dayStr;
|
|
|
|
+ //如果日期相同
|
|
|
|
+ //则流水号加一
|
|
|
|
+ if (frontPart.equals(now)) {
|
|
|
|
+ StringBuilder newCode = new StringBuilder(String.valueOf((Integer.parseInt(lastFourCharacters) + 1)));
|
|
|
|
+ for (int i = newCode.length(); i < 4; i++) {
|
|
|
|
+ newCode.insert(0, "0");
|
|
|
|
+ }
|
|
|
|
+ sampleScan.setBillNO(now + newCode.toString());
|
|
|
|
+ } else {
|
|
|
|
+ //若不相同则将当前日期拼接上0001
|
|
|
|
+ String newCode = now + "0001";
|
|
|
|
+ sampleScan.setBillNO(newCode);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ String now = currentYear + currentMonthStr + dayStr;
|
|
|
|
+ //若不相同则将当前日期拼接上0001
|
|
|
|
+ String newCode = now + "0001";
|
|
|
|
+ sampleScan.setBillNO(newCode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //设置其他属性
|
|
|
|
+ sampleScan.setBillDate(new Date());
|
|
|
|
+ sampleScan.setCreateTime(new Date());
|
|
|
|
+ String loginId = StpUtil.getLoginIdAsString();
|
|
|
|
+ sampleScan.setEditTime(new Date());
|
|
|
|
+ sampleScan.setEditUser(loginId);
|
|
|
|
+
|
|
|
|
+ //图片列表
|
|
|
|
+ List<MultipartFile> imgList = new ArrayList<>();
|
|
|
|
+ //图片分类
|
|
|
|
+ ArrayList<MultipartFile> mainImgList = new ArrayList<>();
|
|
|
|
+ HashMap<String, ArrayList<MultipartFile>> detailsImgMapList = new HashMap<>();
|
|
|
|
+ // 使用 Iterator 遍历文件参数
|
|
|
|
+ Iterator<String> fileNames = mulReq.getFileNames();
|
|
|
|
+ while (fileNames.hasNext()) {
|
|
|
|
+ String paramName = fileNames.next();
|
|
|
|
+ //name是public开头的为单据头所属图片
|
|
|
|
+ if (paramName.startsWith("public")) {
|
|
|
|
+ mainImgList.add(mulReq.getFile(paramName));
|
|
|
|
+ imgList.add(mulReq.getFile(paramName));
|
|
|
|
+ } else if (paramName.startsWith("detail")) {
|
|
|
|
+ //name是detail开头的为单据详情所属图片
|
|
|
|
+ String substring = paramName.substring(7, 9);
|
|
|
|
+ if (!detailsImgMapList.containsKey(substring)) {
|
|
|
|
+ detailsImgMapList.put(substring, new ArrayList<>());
|
|
|
|
+ }
|
|
|
|
+ detailsImgMapList.get(substring).add(mulReq.getFile(paramName));
|
|
|
|
+ imgList.add(mulReq.getFile(paramName));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //将单据头的图片链接拼接起来
|
|
|
|
+ StringBuilder mainFileList = new StringBuilder();
|
|
|
|
+ if (mainImgList.size() > 0){
|
|
|
|
+ for (int i = 0; i < mainImgList.size(); i++) {
|
|
|
|
+ String originalFilename = mainImgList.get(i).getOriginalFilename();
|
|
|
|
+ if (i == mainImgList.size() - 1){
|
|
|
|
+ mainFileList.append("https://xyxt.lttc.cn/samplescan/api/getimg/").append(currentYear).append("/").append(currentMonthStr).append("/").append(originalFilename);
|
|
|
|
+ } else {
|
|
|
|
+ mainFileList.append("https://xyxt.lttc.cn/samplescan/api/getimg/").append(currentYear).append("/").append(currentMonthStr).append("/").append(originalFilename).append(";");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ sampleScan.setFileSavePath(mainFileList.toString());
|
|
|
|
+ }
|
|
|
|
+ sampleScan.setFileSavePath(mainFileList.toString());
|
|
|
|
+
|
|
|
|
+ //循环处理单据体
|
|
|
|
+ if (sampleScanDetails!= null && sampleScanDetails.size() > 0){
|
|
|
|
+ sampleScanDetails.forEach(sampleScanDetail -> {
|
|
|
|
+ sampleScanDetail.setUuid(UUID.randomUUID().toString());
|
|
|
|
+ sampleScanDetail.setBillNO(sampleScan.getBillNO());
|
|
|
|
+ //文件名列表
|
|
|
|
+ ArrayList<MultipartFile> multipartFiles = detailsImgMapList.get(sampleScanDetail.getId());
|
|
|
|
+ StringBuilder detailFileList = new StringBuilder();
|
|
|
|
+ if (multipartFiles != null && multipartFiles.size() > 0){
|
|
|
|
+ for (int i = 0; i < multipartFiles.size(); i++) {
|
|
|
|
+ String originalFilename = multipartFiles.get(i).getOriginalFilename();
|
|
|
|
+ if (i == multipartFiles.size() - 1){
|
|
|
|
+ detailFileList.append("https://xyxt.lttc.cn/samplescan/api/getimg/").append(currentYear).append("/").append(currentMonthStr).append("/").append(originalFilename);
|
|
|
|
+ } else {
|
|
|
|
+ detailFileList.append("https://xyxt.lttc.cn/samplescan/api/getimg/").append(currentYear).append("/").append(currentMonthStr).append("/").append(originalFilename).append(";");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ sampleScanDetail.setFileSavePath(detailFileList.toString());
|
|
|
|
+ }
|
|
|
|
+ //图片路径
|
|
|
|
+ sampleScanDetail.setFileSavePath(detailFileList.toString());
|
|
|
|
+ //创建时间
|
|
|
|
+ sampleScanDetail.setCreateTime(new Date());
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ //调用保存方法
|
|
|
|
+ saveFileAndData(sampleScan, sampleScanDetails, currentYear, currentMonthStr, imgList);
|
|
|
|
+
|
|
|
|
+ return sampleScan.getUid();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ List<SampleScanFile> saveFile(int currentYearInt, String currentMonthStr, List<MultipartFile> resultList) {
|
|
|
|
+ // 指定目标目录
|
|
|
|
+// String uploadDir = System.getProperty("user.dir") + "/uploads/" + currentYear + "/" + currentMonth + "/";
|
|
|
|
+ //相对路径
|
|
|
|
+ String relativePath = currentYearInt + "/" + currentMonthStr + "/";
|
|
|
|
+ //绝对路径
|
|
|
|
+ String ap = uploadDir + "/" + relativePath;
|
|
|
|
+ // 确保目录存在,如果不存在则创建
|
|
|
|
+ File directory = new File(ap);
|
|
|
|
+ if (!directory.exists()) {
|
|
|
|
+ directory.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ ArrayList<SampleScanFile> sampleScanFiles = new ArrayList<>();
|
|
|
|
+ String filePath = null;
|
|
|
|
+ for (MultipartFile multipartFile : resultList) {
|
|
|
|
+ // 获取文件名
|
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
|
+
|
|
|
|
+ //获取扩展名
|
|
|
|
+ int dotIndex = fileName.lastIndexOf('.');
|
|
|
|
+ String expandedName = (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
|
|
|
|
+ //构造sampleScanFile对象
|
|
|
|
+ SampleScanFile sampleScanFile = new SampleScanFile();
|
|
|
|
+ sampleScanFile.setFileName(fileName);
|
|
|
|
+ sampleScanFile.setFileType(expandedName);
|
|
|
|
+ sampleScanFile.setFileSavePath(currentYearInt + "\\" + currentMonthStr + "\\" + fileName);
|
|
|
|
+ sampleScanFile.setUuid(UUID.randomUUID().toString());
|
|
|
|
+ sampleScanFile.setCreateTime(new Date());
|
|
|
|
+ sampleScanFile.setUploadTime(new Date());
|
|
|
|
+ String loginId = StpUtil.getLoginIdAsString();
|
|
|
|
+ sampleScanFile.setUploadUser(loginId);
|
|
|
|
+
|
|
|
|
+ sampleScanFiles.add(sampleScanFile);
|
|
|
|
+
|
|
|
|
+ // 文件保存路径
|
|
|
|
+ filePath = ap + fileName;
|
|
|
|
+ try {
|
|
|
|
+ // 使用 FileCopyUtils 工具保存文件
|
|
|
|
+ FileCopyUtils.copy(multipartFile.getBytes(), new File(filePath));
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return sampleScanFiles;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void saveFileAndData(SampleScan sampleScan, List<SampleScanDetail> sampleScanDetails, int currentYearInt, String currentMonthStr, List<MultipartFile> resultList) {
|
|
|
|
+
|
|
|
|
+ if (resultList!= null && resultList.size() > 0) {
|
|
|
|
+ List<SampleScanFile> sampleScanFiles = saveFile(currentYearInt, currentMonthStr, resultList);
|
|
|
|
+ if (sampleScanFiles!=null && sampleScanFiles.size() > 0) {
|
|
|
|
+ sampleScanMapper.saveImgData(sampleScanFiles);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //储存到数据库
|
|
|
|
+ sampleScanMapper.addMainData(sampleScan);
|
|
|
|
+
|
|
|
|
+ if (sampleScanDetails !=null && sampleScanDetails.size() > 0) {
|
|
|
|
+ sampleScanMapper.addDetailData(sampleScanDetails);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseEntity<byte[]> getImgService(String year, String month, String filename) {
|
|
|
|
+ String filePath = uploadDir + "/" + year + "/" + month + "/" + filename;
|
|
|
|
+
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ try {
|
|
|
|
+ inputStream = HttpUtils.getLocalhostInputStream(filePath);
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //将输入流转为byte[]
|
|
|
|
+ byte[] bytesByStream = getBytesByStream(inputStream);
|
|
|
|
+ final HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setContentType(MediaType.IMAGE_PNG);
|
|
|
|
+
|
|
|
|
+ return new ResponseEntity<>(bytesByStream, headers, HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public byte[] getBytesByStream(InputStream inputStream) {
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
+
|
|
|
|
+ int b;
|
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
+ try {
|
|
|
|
+ while ((b = inputStream.read(bytes)) != -1) {
|
|
|
|
+
|
|
|
|
+ byteArrayOutputStream.write(bytes, 0, b);
|
|
|
|
+ }
|
|
|
|
+ return byteArrayOutputStream.toByteArray();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+}
|