chengf
2025-10-18 eb2268638f7ffe61abfccac5fd378534ec4b052c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package org.jeecg.modules.demo.copywriting.controller;
 
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
 
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.contract.entity.SemanticWord;
import org.jeecg.modules.demo.contract.service.ISemanticWordService;
import org.jeecg.modules.demo.copywriting.entity.Copywriting;
import org.jeecg.modules.demo.copywriting.service.ICopywritingService;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
 
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
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.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 文案
 * @Author: jeecg-boot
 * @Date:   2025-10-13
 * @Version: V1.0
 */
@Tag(name="文案")
@RestController
@RequestMapping("/copywriting/copywriting")
@Slf4j
public class CopywritingController extends JeecgController<Copywriting, ICopywritingService> {
    @Autowired
    private ICopywritingService copywritingService;
     @Autowired
     private ISemanticWordService semanticWordService;
    
    /**
     * 分页列表查询
     *
     * @param copywriting
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "文案-分页列表查询")
    @Operation(summary="文案-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<Copywriting>> queryPageList(Copywriting copywriting,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
 
        QueryWrapper<Copywriting> queryWrapper = QueryGenerator.initQueryWrapper(copywriting, req.getParameterMap());
 
        if (StringUtils.isNotBlank(copywriting.getTitleLike())) {
            queryWrapper.like("title", copywriting.getTitleLike());
        }
        if (StringUtils.isNotBlank(copywriting.getTitleLike()) && StringUtils.isNotBlank(copywriting.getWordLike())) {
            queryWrapper.or();
        }
        if (StringUtils.isNotBlank(copywriting.getWordLike())){
            queryWrapper.exists("SELECT 1 FROM semantic_word WHERE semantic_word.id = copywriting.word_id " +
                            "AND semantic_word.word LIKE '%" + copywriting.getWordLike() + "%'");
 
        }
        Page<Copywriting> page = new Page<Copywriting>(pageNo, pageSize);
        IPage<Copywriting> pageList = copywritingService.page(page, queryWrapper);
        for (Copywriting item : pageList.getRecords()) {
            item.setSemanticWord(semanticWordService.getById(item.getWordId()));
        }
        return Result.OK(pageList);
    }
 
    /**
     *   添加
     *
     * @param copywriting
     * @return
     */
    @AutoLog(value = "文案-添加")
    @Operation(summary="文案-添加")
    @RequiresPermissions("copywriting:copywriting:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Copywriting copywriting) {
        copywritingService.save(copywriting);
 
        return Result.OK("添加成功!");
    }
    
    /**
     *  编辑
     *
     * @param copywriting
     * @return
     */
    @AutoLog(value = "文案-编辑")
    @Operation(summary="文案-编辑")
    @RequiresPermissions("copywriting:copywriting:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody Copywriting copywriting) {
        copywritingService.updateById(copywriting);
        return Result.OK("编辑成功!");
    }
    
    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文案-通过id删除")
    @Operation(summary="文案-通过id删除")
    @RequiresPermissions("copywriting:copywriting:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        copywritingService.removeById(id);
        return Result.OK("删除成功!");
    }
    
    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "文案-批量删除")
    @Operation(summary="文案-批量删除")
    @RequiresPermissions("copywriting:copywriting:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.copywritingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "文案-通过id查询")
    @Operation(summary="文案-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<Copywriting> queryById(@RequestParam(name="id",required=true) String id) {
        Copywriting copywriting = copywritingService.getById(id);
        if(copywriting==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(copywriting);
    }
 
    /**
    * 导出excel
    *
    * @param request
    * @param copywriting
    */
    @RequiresPermissions("copywriting:copywriting:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Copywriting copywriting) {
        return super.exportXls(request, copywriting, Copywriting.class, "文案");
    }
 
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("copywriting:copywriting:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Copywriting.class);
    }
 
 
     @RequiresPermissions("copywriting:copywriting:aiCreateCopyWriting")
     @RequestMapping(value = "/aiCreateCopyWriting", method = RequestMethod.POST)
     private Result<?> aiCreateCopyWriting(
                                           @RequestParam String[] jianli,
                                           @RequestParam String wenanyaoqiu,
                                           @RequestParam String louchu,
                                           @RequestParam String youshang,
                                           @RequestParam String wenti,
                                           @RequestParam String user) {
         // 工作流接口配置(请替换为实际值)
         String path = "D:\\opt\\upFiles\\";
         String workflowUrl = "http://14.103.174.44/v1/workflows/run"; // 目标工作流接口地址
         String authToken = "app-J1Tqytg0ZetcrVTF2fVHHY8B"; // 接口认证Token(解决401问题)
         String userId = user; // 调用者标识(解决user参数缺失问题)
 
         try {
             // 1. 构建请求头(包含认证信息)
             HttpHeaders headers = new HttpHeaders();
             headers.setContentType(MediaType.APPLICATION_JSON);
             headers.set("Authorization", "Bearer " + authToken); // 添加Token认证
 
             // 2. 构建业务参数容器(inputs是接口必填字段)
             Map<String, Object> inputs = new HashMap<>();
             // 添加文本参数
             // 处理简历文件(转为可序列化的List<Map>,避免使用流对象)
             List<Map<String, String>> jianliFileList = new ArrayList<>();
             for (String fileName : jianli) {
                 // 拼接服务器上的完整文件路径
                 File file = new File(path + fileName.trim());
                 if (!file.exists() || !file.isFile()) {
                     return Result.error("服务器上不存在文件:" + fileName);
                 }
 
                 // 读取文件内容并转为Base64
                 byte[] fileBytes = readFileToBytes(file);
                 String base64Content = Base64.getEncoder().encodeToString(fileBytes);
 
                 // 封装文件信息
                 Map<String, String> fileInfo = new HashMap<>();
                 fileInfo.put("name", fileName);
                 fileInfo.put("content", base64Content);
                 jianliFileList.add(fileInfo);
             }
             inputs.put("jianli", jianliFileList); // 简历文件列表(符合接口要求)
             inputs.put("wenanyaoqiu", wenanyaoqiu);// 文案要求
             inputs.put("louchu", louchu);          // 露出
             inputs.put("youshang", youshang);      // 优势(修正笔误)
             inputs.put("wenti", wenti);            // 问题
 
             // 4. 构建完整请求体(包含所有必填顶层参数)
             Map<String, Object> requestBody = new HashMap<>();
             requestBody.put("inputs", inputs);    // 核心业务参数容器(解决inputs缺失问题)
             requestBody.put("user", userId);      // 用户标识(解决user缺失问题)
             // 如需指定工作流ID,添加以下参数(根据接口文档确认)
             // requestBody.put("workflow_id", "your_workflow_id");
 
             // 5. 发送请求到工作流接口
             RestTemplate restTemplate = new RestTemplate();
             HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
             ResponseEntity<String> response = restTemplate.postForEntity(
                     workflowUrl,
                     requestEntity,
                     String.class
             );
 
             // 6. 处理响应结果
             if (response.getStatusCode() == HttpStatus.OK) {
                 return Result.OK("文案生成成功", JSONObject.parseObject(response.getBody()).getJSONObject("data").getJSONObject("outputs").getString("http"));
             } else {
                 return Result.error("工作流接口返回异常,状态码:" + response.getStatusCodeValue());
             }
 
         } catch (Exception e) {
             return Result.error("生成文案异常:" + e.getMessage());
         }
     }
 
     private byte[] readFileToBytes(File file) throws Exception {
         try (FileInputStream fis = new FileInputStream(file)) {
             byte[] bytes = new byte[(int) file.length()];
             fis.read(bytes);
             return bytes;
         }
     }
}