Quellcode durchsuchen

原始数据导入

lchao vor 7 Monaten
Ursprung
Commit
b147d0fcd7

+ 2 - 0
songhua-admin/src/main/java/com/songhua/SongHuaApplication.java

@@ -3,6 +3,7 @@ package com.songhua;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 /**
@@ -12,6 +13,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
  */
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
 @EnableScheduling
+@EnableAsync
 public class SongHuaApplication {
     public static void main(String[] args) {
         // System.setProperty("spring.devtools.restart.enabled", "false");

+ 29 - 8
songhua-admin/src/main/java/com/songhua/web/controller/system/PzRawDataController.java

@@ -13,10 +13,13 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.context.request.async.DeferredResult;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 /**
  * 原始数据导入Controller
@@ -45,17 +48,35 @@ public class PzRawDataController extends BaseController {
     /**
      * 导入原始数据列表
      */
-    @PostMapping("/importData")
-    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
-        ExcelUtil<PzRawDataVo> util = new ExcelUtil<PzRawDataVo>(PzRawDataVo.class);
-        List<PzRawDataVo> userList = util.importExcel(file.getInputStream());
-        String operName = getUsername();
-        String message = pzRawDataService.importData(userList, updateSupport, operName);
-        return success(message);
+//    @PostMapping("/importData")
+//    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
+//        ExcelUtil<PzRawDataVo> util = new ExcelUtil<PzRawDataVo>(PzRawDataVo.class);
+//        List<PzRawDataVo> userList = util.importExcel(file.getInputStream());
+//        String operName = getUsername();
+//        String message = pzRawDataService.importData(userList, updateSupport, operName);
+//        return success(message);
+//    }
+
+    @Log(title = "原始数据导入", businessType = BusinessType.DELETE)
+    @PostMapping("/importTicket")
+    public DeferredResult<AjaxResult> importTicket(MultipartFile file) {
+        DeferredResult<AjaxResult> deferredResult = new DeferredResult<>();
+
+        CompletableFuture.runAsync(() -> {
+            try {
+                pzRawDataService.importTicket(file);
+                deferredResult.setResult(AjaxResult.success());
+            } catch (IOException e) {
+                deferredResult.setResult(AjaxResult.error("文件处理失败: " + e.getMessage()));
+            }
+        });
+
+        return deferredResult;
     }
 
+
     /**
-     * 导入模板
+     * 下载导入模板
      */
     @ApiOperation(value = "导入模板", notes = "导入模板")
     @PostMapping("/importTemplate")

+ 30 - 1
songhua-system/src/main/java/com/songhua/system/domain/vo/TicketStatisticsImpVO.java

@@ -17,12 +17,41 @@ public class TicketStatisticsImpVO {
 
     private String ticketName;
 
+    //微信张数
     private String vxNumber;
-
+    //微信金额
     private String vxMoney;
+    //支付宝张数
+    private String zfbNumber;
+    //支付宝金额
+    private String zfbMoney;
+
+    private String xjNumber;
+
+    private String xjMoney;
+
+    private String ysfNumber;
+
+    private String ysfMoney;
+
+    private String zfbxcxNumber;
+
+    private String zfbxcxMoney;
+
+    private String vxxcxNumber;
+
+    private String vxxcxMoney;
+
+    private String lysNumber;
+
+    private String lysMoney;
 
+    private String otaNumber;
 
+    private String otaMoney;
 
+    private String lhdsNumber;
 
+    private String lhdsMoney;
 
 }

+ 4 - 2
songhua-system/src/main/java/com/songhua/system/mapper/PzRawDataMapper.java

@@ -2,9 +2,9 @@ package com.songhua.system.mapper;
 
 import java.util.List;
 
-import com.songhua.common.core.domain.entity.SysUser;
 import com.songhua.system.domain.PzRawData;
 import com.songhua.system.domain.PzRawDataVo;
+import com.songhua.system.domain.vo.TicketStatisticsImpVO;
 
 /**
  * 原始数据导入Mapper接口
@@ -64,6 +64,8 @@ public interface PzRawDataMapper
      */
     public int deletePzRawDataByIds(Long[] ids);
 
-    public PzRawDataVo checkIfDataExists(String ticketName);
+    public Long checkIfDataExists(String ticketName);
+
+    public int deleteByTicketName();
 
 }

+ 7 - 9
songhua-system/src/main/java/com/songhua/system/service/IPzRawDataService.java

@@ -2,8 +2,11 @@ package com.songhua.system.service;
 
 import com.songhua.system.domain.PzRawData;
 import com.songhua.system.domain.PzRawDataVo;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.List;
+import java.util.concurrent.CompletableFuture;
 
 /**
  * 原始数据导入Service接口
@@ -65,17 +68,12 @@ public interface IPzRawDataService {
      */
     public int deletePzRawDataById(Long id);
 
-    PzRawDataVo checkIfDataExists(String ticketName);
+    Long checkIfDataExists(String ticketName);
+
+    void export(PzRawData pzRawData);
 
     /**
      * 导入用户数据
-     *
-     * @param userList        用户数据列表
-     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
-     * @param operName        操作用户
-     * @return 结果
      */
-    public String importData(List<PzRawDataVo> userList, Boolean isUpdateSupport, String operName);
-
-    void export(PzRawData pzRawData);
+    CompletableFuture<String> importTicket(MultipartFile file) throws IOException;
 }

+ 145 - 26
songhua-system/src/main/java/com/songhua/system/service/impl/PzRawDataServiceImpl.java

@@ -2,27 +2,38 @@ package com.songhua.system.service.impl;
 
 import com.songhua.common.exception.ServiceException;
 import com.songhua.common.utils.DateUtils;
-import com.songhua.common.utils.StringUtils;
 import com.songhua.system.domain.PzRawData;
 import com.songhua.system.domain.PzRawDataVo;
 import com.songhua.system.domain.vo.ShhTicketRulesReqVO;
+import com.songhua.system.domain.vo.TicketStatisticsImpVO;
 import com.songhua.system.enums.TicketRulesEnum;
 import com.songhua.system.mapper.PzRawDataMapper;
 import com.songhua.system.service.IPzRawDataService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.NumberToTextConverter;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.nio.file.Paths;
+import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
 
 /**
  * 原始数据导入Service业务层处理
@@ -73,7 +84,6 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
 
     @Override
     public int insertPzRawDataVo(PzRawDataVo pzRawDataVo) {
-        pzRawDataVo.setCreateTime(DateUtils.getNowDate());
         return pzRawDataMapper.insertPzRawDataVo(pzRawDataVo);
     }
 
@@ -116,42 +126,151 @@ public class PzRawDataServiceImpl implements IPzRawDataService {
     }
 
     @Override
-    public PzRawDataVo checkIfDataExists(String ticketName) {
-        PzRawDataVo pzRawDataVo = pzRawDataMapper.checkIfDataExists(ticketName);
+    public Long checkIfDataExists(String ticketName) {
+        return pzRawDataMapper.checkIfDataExists(ticketName);
+    }
+
+    private List<PzRawDataVo> mapToPzRawDataVo(TicketStatisticsImpVO ticket) {
+        List<PzRawDataVo> pzRawDataVoList = new ArrayList<>();
+
+        // 微信
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getVxNumber(), ticket.getVxMoney(), "1"));
+
+        // 支付宝
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getZfbNumber(), ticket.getZfbMoney(), "2"));
+
+        // 现金
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getXjNumber(), ticket.getXjMoney(), "3"));
+
+        // 云闪付
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getYsfNumber(), ticket.getYsfMoney(), "4"));
+
+        // 支付宝小程序余额支付
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getZfbxcxNumber(), ticket.getZfbxcxMoney(), "5"));
+
+        // 微信小程序余额支付
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getVxxcxNumber(), ticket.getVxxcxMoney(), "6"));
+
+        // ota余额支付
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getOtaNumber(), ticket.getOtaMoney(), "7"));
+
+        // 灵活代收
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getLhdsNumber(), ticket.getLhdsMoney(), "8"));
+
+        // 旅行社余额支付
+        pzRawDataVoList.add(createPzRawDataVo(ticket.getTicketName(), ticket.getLysNumber(), ticket.getLysMoney(), "9"));
+
+        return pzRawDataVoList;
+    }
+
+    private PzRawDataVo createPzRawDataVo(String ticketName, String number, String money, String payType) {
+        PzRawDataVo pzRawDataVo = new PzRawDataVo();
+        pzRawDataVo.setTicketId(checkIfDataExists(ticketName));
+        pzRawDataVo.setNumber(Integer.parseInt(number));
+        pzRawDataVo.setMoney(new BigDecimal(money));
+        pzRawDataVo.setPayType(payType);
+        pzRawDataVo.setCreateTime(new Date()); // 假设当前时间为创建时间
         return pzRawDataVo;
     }
 
+    private String getCellValue(Cell cell) {
+        String cellValue = "";
+        switch (cell.getCellType()) {
+            case _NONE: // 未知类型,用于表示初始化前的状态或缺少具体类型。仅供内部使用。
+                break;
+            case NUMERIC: // 数字类型,用于Excel日期格式化
+                if (HSSFDateUtil.isCellDateFormatted(cell)) {
+                    Date d = cell.getDateCellValue();
+                    DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US);
+                    DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+                    try {
+                        cellValue = sdf.format(df.parse(d.toString()));
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                } else {
+                    cellValue = NumberToTextConverter.toText(cell.getNumericCellValue());
+                }
+                break;
+            case STRING: // 字符串类型
+                cellValue = cell.getStringCellValue();
+                break;
+            case FORMULA: // 公式类型
+                cellValue = String.valueOf(cell.getStringCellValue());
+                break;
+            case BLANK: // 空白的单元格
+                break;
+            case BOOLEAN: // 布尔类型
+                break;
+            case ERROR: // 错误类型
+                break;
+            default:
+        }
+        return cellValue;
+    }
+
     /**
-     * 导入用户数据
-     *
-     * @param userList        用户数据列表
-     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
-     * @param operName        操作用户
-     * @return 结果
+     * 导入
      */
     @Override
-    public String importData(List<PzRawDataVo> userList, Boolean isUpdateSupport, String operName) {
-        if (StringUtils.isNull(userList) || userList.size() == 0) {
-            throw new ServiceException("导入用户数据不能为空!");
-        }
+//    @Async
+    public CompletableFuture<String> importTicket(MultipartFile file) throws IOException {
+        List<TicketStatisticsImpVO> list = new ArrayList<>();
+        int successNum = 0;
+        int failureNum = 0;
         StringBuilder successMsg = new StringBuilder();
         StringBuilder failureMsg = new StringBuilder();
-        for (PzRawDataVo data : userList) {
-            try {
-                // 检查数据是否已存在
-                PzRawDataVo isExist = checkIfDataExists(data.getTicketName());
-                data.setTicketId(isExist.getId());
-                insertPzRawDataVo(data);
-                String msg = "【成功】" + data.toString() + " 导入成功:";
-                failureMsg.append(msg).append("\n");
-            } catch (Exception e) {
-                String msg = "【失败】" + data.toString() + " 导入失败:" + e.getMessage();
-                failureMsg.append(msg).append("\n");
+        // 获取文件输入流
+        InputStream inputStream = file.getInputStream();
+        // 读取整个excel
+        XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
+        // 获取第一个sheet页
+        XSSFSheet sheetAt = sheets.getSheetAt(0);
+        try {
+            // 从第三行开始循环获取每一行数据
+            for (int i = 2; i <= sheetAt.getPhysicalNumberOfRows(); i++) {
+                Row row = sheetAt.getRow(i);
+                list.add(new TicketStatisticsImpVO(getCellValue(row.getCell(0)), getCellValue(row.getCell(1)), getCellValue(row.getCell(2)), getCellValue(row.getCell(3)),
+                        getCellValue(row.getCell(4)), getCellValue(row.getCell(5)), getCellValue(row.getCell(6)),
+                        getCellValue(row.getCell(7)), getCellValue(row.getCell(8)), getCellValue(row.getCell(9)),
+                        getCellValue(row.getCell(10)), getCellValue(row.getCell(11)), getCellValue(row.getCell(12)),
+                        getCellValue(row.getCell(13)), getCellValue(row.getCell(14)), getCellValue(row.getCell(15)),
+                        getCellValue(row.getCell(16)), getCellValue(row.getCell(17)), getCellValue(row.getCell(18))));
             }
+
+        } catch (Exception e) {
+            failureNum++;
+        } finally {
+            IOUtils.closeQuietly(inputStream);
         }
-        return successMsg.toString();
+//             将 TicketStatisticsImpVO 映射到 PzRawDataVo 并插入数据库
+        list = list.stream().filter(item -> !item.getTicketName().contains("总计")).collect(Collectors.toList());
+        if (list.size() > 0) {
+            pzRawDataMapper.deleteByTicketName();
+            for (TicketStatisticsImpVO ticket : list) {
+                List<PzRawDataVo> pzRawDataVoList = mapToPzRawDataVo(ticket);
+                if (pzRawDataVoList == null || pzRawDataVoList.isEmpty()) {
+                    throw new ServiceException("导入失败:数据映射失败。");
+                }
+                for (PzRawDataVo pzRawDataVo : pzRawDataVoList) {
+                    try {
+                        insertPzRawDataVo(pzRawDataVo);
+                        successNum++;
+                    } catch (Exception e) {
+                        failureNum++;
+                        failureMsg.append("导入失败:").append(e.getMessage()).append("\n");
+                    }
+                }
+            }
+        }
+        successMsg.append("导入成功:" + successNum + " 条数据,失败:" + failureNum + " 条数据。");
+        if (failureNum > 0) {
+            successMsg.append("\n").append(failureMsg);
+        }
+        return CompletableFuture.completedFuture(successMsg.toString());
     }
 
+
     @Override
     public void export(PzRawData pzRawData) {
         List<ShhTicketRulesReqVO> list = calculateTotalPrices(pzRawDataMapper.selectPzRawDataList(pzRawData));

+ 5 - 1
songhua-system/src/main/resources/mapper/system/PzRawDataMapper.xml

@@ -119,11 +119,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </foreach>
     </delete>
 
-    <select id="checkIfDataExists" parameterType="PzRawDataVo" resultMap="PzRawDataVoResult">
+    <select id="checkIfDataExists" resultType="java.lang.Long">
         SELECT
             id
         FROM
             pz_ticket_type_management  where ticket_name = #{ticketName}
     </select>
 
+    <delete id="deleteByTicketName" parameterType="Long">
+        delete from pz_raw_data
+    </delete>
+
 </mapper>

+ 17 - 4
songhua-ui/src/views/system/data/index.vue

@@ -171,9 +171,18 @@
         <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
         <div slot="tip" class="el-upload__tip text-center">
           <span>仅允许导入xls、xlsx格式文件。</span>
-          <el-link :underline="false" style="font-size:12px;vertical-align: baseline;" type="primary"
-                   @click="importTemplate">下载模板
-          </el-link>
+<!--          <el-link :underline="false" style="font-size:12px;vertical-align: baseline;" type="primary"-->
+<!--                   @click="importTemplate">下载模板-->
+<!--          </el-link>-->
+        </div>
+        <div v-if="upload.showProgressMessage" class="progress-message">
+          <el-alert
+            title="文件导入中,请稍候..."
+            type="info"
+            :closable="false"
+            center
+            show-icon>
+          </el-alert>
         </div>
       </el-upload>
       <div slot="footer" class="dialog-footer">
@@ -237,7 +246,7 @@ export default {
         // 设置上传的请求头部
         headers: {Authorization: "Bearer " + getToken()},
         // 上传的地址
-        url: process.env.VUE_APP_BASE_API + "/system/data/importData"
+        url: process.env.VUE_APP_BASE_API + "/system/data/importTicket"
       },
       // 表单校验
       rules: {
@@ -355,11 +364,15 @@ export default {
     // 文件上传中处理
     handleFileUploadProgress(event, file, fileList) {
       this.upload.isUploading = true;
+      // 显示请稍候提示
+      this.upload.showProgressMessage = true;
     },
     // 文件上传成功处理
     handleFileSuccess(response, file, fileList) {
       this.upload.open = false;
       this.upload.isUploading = false;
+      // 关闭请稍候提示
+      this.upload.showProgressMessage = false;
       this.$refs.upload.clearFiles();
       this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
       this.getList();

+ 1 - 1
songhua-ui/src/views/system/user/index.vue

@@ -673,4 +673,4 @@ export default {
     }
   }
 };
-</script>
+</script>