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