chengf
2026-02-06 49158a77cb676a15bebe1be5507e18e5a30c1fa5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.jeecg.modules.demo.contract.controller;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.SerializationUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.contract.entity.ExcelDataDTO;
import org.jeecg.modules.demo.contract.entity.SemanticWord;
import org.jeecg.modules.demo.contract.service.ISemanticWordService;
import org.jeecg.modules.demo.copywriting.controller.CopywritingAsyncService;
import org.jeecg.modules.listener.ExcelDataListener;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 适配新Excel列的导入接口控制器
 */
@RestController
@RequestMapping("/api/excel")
@Slf4j
public class ExcelImportController {
 
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISemanticWordService semanticWordService;
    /**
     * Excel文件导入接口(适配大类、品牌等五列)
     * @return 导入结果
     */
    @RequiresPermissions("contract:contract:importContract")
    @PostMapping("/importContract")
    public Result<String> importExcel(@RequestPart SemanticWord importParam, @RequestPart MultipartFile file, HttpServletRequest req) {
        // 1. 文件基础校验
        if (file.isEmpty()) {
            return Result.error("上传的文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            return Result.error("仅支持上传Excel文件(.xlsx/.xls)");
        }
 
        // 2. 解析Excel文件
        ExcelDataListener listener = new ExcelDataListener();
        try {
            EasyExcel.read(file.getInputStream(), ExcelDataDTO.class, listener)
                    .sheet() // 读取第一个sheet(如需指定sheet,可传sheet名称/索引,如.sheet("数据sheet"))
                    .doRead();
 
            // 3. 获取解析后的数据,可执行业务逻辑
            List<ExcelDataDTO> dataList = listener.getDataList();
            dataList.remove(0);
            List<SemanticWord> semanticWords = new ArrayList<>();
            int i = 0;
            for (ExcelDataDTO data : dataList) {
                LambdaQueryWrapper<SemanticWord> sem = new LambdaQueryWrapper<>();
                sem.eq(SemanticWord::getContractId, importParam.getContractId());
                sem.eq(SemanticWord::getWord, data.getLeakWord());
                sem.eq(SemanticWord::getOutWord, data.getSemanticQuestion());
                long count = semanticWordService.count(sem);
                if (count > 0) {
                    i ++;
                } else {
                    SemanticWord copy = new  SemanticWord();
                    BeanUtils.copyProperties(copy, importParam);
                    copy.setContractId(importParam.getContractId());
                    copy.setBrand(data.getBrand());
                    copy.setCategoryOne(data.getCategory());
                    copy.setWord(data.getLeakWord());
                    copy.setOutWord(data.getSemanticQuestion());
                    QueryWrapper qw = new QueryWrapper<SysUser>();
                    qw.eq("realName", data.getCreator());
                    Page result = (Page) sysUserService.queryPageList(req, qw, 2, 1).getResult();
                    if (result.getTotal() != 1) {
                        return Result.error("未查询到用户:" + data.getCreator());
                    }
                    String userId = ((SysUser)(result).getRecords().get(0)).getId();
                    copy.setChanger(userId);
                    semanticWords.add(copy);
                }
            }
            semanticWordService.saveBatch(semanticWords);
 
 
            // 示例:调用Service保存数据(替换为你的实际业务逻辑)
            // excelDataService.saveBatch(dataList);
 
            return Result.ok("Excel导入成功!共导入" + (dataList.size() - i) + "条数据,重复" + i + "条数据");
        } catch (IOException e) {
            log.error("文件读取失败", e);
            return Result.error("文件读取失败:" + e.getMessage());
        } catch (RuntimeException e) {
            log.error("数据解析失败", e);
            return Result.error("数据解析失败:" + e.getMessage());
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
 
 
    @Autowired
    private CopywritingAsyncService copywritingAsyncService;
 
    @RequiresPermissions("contract:contract:batchGenerateCopy")
    @RequestMapping(value = "/batchGenerateCopy", method = RequestMethod.POST)
    public Result<?> batchGenerateCopy(@RequestBody ContractParam cp) {
        if (cp.getContractId().isEmpty()) {
            return Result.error("文案编号不能为空");
        } else if (cp.getFileName().isEmpty()) {
            return Result.error("文件不能为空");
        } else if (cp.getYoushang().isEmpty()) {
            return Result.error("友商不能为空");
        } else if (cp.getWenti().isEmpty()) {
            return Result.error("问题描述不能为空");
        } else if (cp.getCopywritingRequirements().isEmpty()) {
            return Result.error("文案要求不能为空");
        } else if (cp.getAuditor().isEmpty() || cp.getAuditorName().isEmpty()) {
            return Result.error("用户不能为空");
        }
        QueryWrapper<SemanticWord> semanticWordQueryWrapper = new QueryWrapper<>();
        semanticWordQueryWrapper.eq("contract_id", cp.getContractId());
        List<SemanticWord> list = semanticWordService.list(semanticWordQueryWrapper);
        if (list.isEmpty()) {
            return Result.error("该合同不存在语义词");
        } else {
            // 调用异步方法执行实际生成逻辑
            copywritingAsyncService.asyncBatchGenerateCopy(
                    cp.getContractId(), list, cp.getFileName(), cp.getYoushang(), cp.getWenti(),
                    cp.getCopywritingRequirements(),cp.getAuditor(), cp.getAuditorName()
            );
            // 立即返回响应,不等待生成完成
            return Result.OK("开始生成,任务已提交至后台处理");
        }
    }
}