潘志宝
2024-11-05 cf2287d43a3e6e6a87b799b484c1c6073226c1bf
提交 | 用户 | 时间
449017 1 package com.iailab.module.model.mpk.controller.admin;
D 2
3 import com.iailab.framework.common.page.PageData;
4 import com.iailab.framework.common.pojo.CommonResult;
5 import com.iailab.module.model.mpk.dto.MpkFileDTO;
0255ea 6 import com.iailab.module.model.mpk.service.MpkFileService;
cf2287 7 import com.iailab.module.model.mpk.service.PackService;
449017 8 import io.swagger.v3.oas.annotations.Operation;
D 9 import org.apache.commons.io.IOUtils;
10 import org.springframework.beans.factory.annotation.Autowired;
b425df 11 import org.springframework.security.access.prepost.PreAuthorize;
d8db4b 12 import org.springframework.util.CollectionUtils;
449017 13 import org.springframework.web.bind.annotation.*;
D 14 import org.springframework.web.multipart.MultipartFile;
15
16 import javax.servlet.http.HttpServletResponse;
17 import java.io.IOException;
18 import java.net.URLEncoder;
19 import java.util.*;
20
21 import static com.iailab.framework.common.pojo.CommonResult.success;
22
23 /**
24  * @author dzd
25  * @Description
26  * @createTime 2024年08月14日
27  */
28 @RestController
29 @RequestMapping("/model/mpk/file")
30 public class MpkFileController {
31     @Autowired
0255ea 32     private MpkFileService mpkFileService;
cf2287 33
34     @Autowired
35     private PackService packService;
449017 36
D 37     @GetMapping("page")
38     @Operation(summary = "分页")
b425df 39     @PreAuthorize("@ss.hasPermission('mpk:file:query')")
449017 40     public CommonResult<PageData<MpkFileDTO>> page(@RequestParam Map<String, Object> params) {
0255ea 41         PageData<MpkFileDTO> page = mpkFileService.page(params);
449017 42         return success(page);
D 43     }
44
b425df 45     @PreAuthorize("@ss.hasPermission('mpk:file:query')")
449017 46     @GetMapping("{id}")
D 47     public CommonResult<MpkFileDTO> info(@PathVariable("id") String id) {
0255ea 48         MpkFileDTO schedule = mpkFileService.get(id);
d8db4b 49         List<String> menuAndGroup = new ArrayList<>();
50         menuAndGroup.add(schedule.getMenuName());
51         menuAndGroup.add(schedule.getGroupName());
52         schedule.setMenuAndGroup(menuAndGroup);
449017 53         return success(schedule);
D 54     }
55
b425df 56     @PreAuthorize("@ss.hasPermission('mpk:file:query')")
449017 57     @GetMapping("list")
D 58     public CommonResult<List<MpkFileDTO>> list() {
0255ea 59         List<MpkFileDTO> list = mpkFileService.list(new HashMap<>());
449017 60
D 61         return success(list);
62     }
63
b425df 64     @PreAuthorize("@ss.hasPermission('mpk:file:create')")
449017 65     @PostMapping
b425df 66     public CommonResult<Boolean> save(@RequestBody MpkFileDTO dto) {
d8db4b 67         if (!CollectionUtils.isEmpty(dto.getMenuAndGroup())) {
68             dto.setMenuName(dto.getMenuAndGroup().get(0));
69             if (dto.getMenuAndGroup().size() > 1) {
70                 dto.setGroupName(dto.getMenuAndGroup().get(1));
71             }
72         }
cf2287 73         dto.setPyModule(packService.getModelPath(dto.getClassName()));
0255ea 74         mpkFileService.save(dto);
b425df 75         return CommonResult.success(true);
449017 76     }
D 77
b425df 78     @PreAuthorize("@ss.hasPermission('mpk:file:delete')")
449017 79     @DeleteMapping
b425df 80     public CommonResult<Boolean> delete(String id) {
0255ea 81         mpkFileService.delete(id);
b425df 82         return CommonResult.success(true);
449017 83     }
D 84
b425df 85     @PreAuthorize("@ss.hasPermission('mpk:file:update')")
449017 86     @PutMapping
b425df 87     public CommonResult<Boolean> update(@RequestBody MpkFileDTO dto) {
d8db4b 88         if (!CollectionUtils.isEmpty(dto.getMenuAndGroup())) {
89             dto.setMenuName(dto.getMenuAndGroup().get(0));
90             if (dto.getMenuAndGroup().size() > 1) {
91                 dto.setGroupName(dto.getMenuAndGroup().get(1));
92             }
93         }
cf2287 94         dto.setPyModule(packService.getModelPath(dto.getClassName()));
0255ea 95         mpkFileService.update(dto);
b425df 96         return CommonResult.success(true);
449017 97     }
D 98
99     @GetMapping("generat")
1fea3e 100     public void generat(String id, String remark,String zipFileName, HttpServletResponse response) {
D 101         try {
102             byte[] data = mpkFileService.generatorCode(id, remark,zipFileName);
449017 103
1fea3e 104             response.reset();
D 105             response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(zipFileName, "UTF-8") + "\"");
106             response.addHeader("Content-Length", "" + data.length);
107             response.setContentType("application/octet-stream; charset=UTF-8");
449017 108
1fea3e 109             IOUtils.write(data, response.getOutputStream());
D 110         } catch (Exception e) {
912a1e 111             throw new RuntimeException("代码生成异常",e);
1fea3e 112         }
449017 113     }
D 114
115     @GetMapping("packageModel")
116     public void packageModel(String ids ,String projectId,String log ,String projectName,String version,String zipFileName,HttpServletResponse response) throws IOException {
117         byte[] data;
118         try {
0255ea 119             data = mpkFileService.packageModel(Arrays.asList(ids.split(",")),projectId,projectName,zipFileName,log,version);
449017 120         } catch (InterruptedException e) {
D 121             throw new RuntimeException("模型打包失败",e);
122         }
123
124         response.reset();
125         response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(zipFileName, "UTF-8") + "\"");
126         response.addHeader("Content-Length", "" + data.length);
127         response.setContentType("application/octet-stream; charset=UTF-8");
128
129         IOUtils.write(data, response.getOutputStream());
130     }
131
132     @PostMapping("/upload")
133     @Operation(summary = "python文件上传")
c12dae 134     public CommonResult<Map<String,String>> upload(@RequestParam("file") MultipartFile file) throws Exception {
0255ea 135         Map<String,String> result = mpkFileService.savePyFile(file);
449017 136         return success(result);
D 137     }
c12dae 138
D 139     @PostMapping("/publish")
140     public CommonResult<String> publish(@RequestBody Map<String, Object> params) {
141         return mpkFileService.publish(params);
142     }
449017 143 }