潘志宝
2024-08-20 92d87eda945d68346475e6766673a3cdccf3d41c
提交 | 用户 | 时间
92d87e 1 package com.iailab.module.mcs.controller;
2
3 import com.iailab.common.annotation.LogOperation;
4 import com.iailab.common.enums.IsEnableEnum;
5 import com.iailab.framework.common.page.PageData;
6 import com.iailab.common.utils.Constant;
7 import com.iailab.framework.common.util.object.ConvertUtils;
8 import com.iailab.framework.common.pojo.CommonResult;
9 import com.iailab.framework.common.util.validation.ValidationUtils;
10 import com.iailab.framework.common.validation.group.AddGroup;
11 import com.iailab.framework.common.validation.group.DefaultGroup;
12 import com.iailab.framework.common.validation.group.UpdateGroup;
13 import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
14 import com.iailab.module.mcs.dto.StModelDTO;
15 import com.iailab.module.mcs.dto.StModelOutDTO;
16 import com.iailab.module.mcs.dto.StModelParamDTO;
17 import com.iailab.module.mcs.dto.StModelSettingDTO;
18 import com.iailab.module.mcs.entity.StModelOutEntity;
19 import com.iailab.module.mcs.entity.StModelParamEntity;
20 import com.iailab.module.mcs.entity.StModelSettingEntity;
21 import com.iailab.module.mcs.service.StModelOutService;
22 import com.iailab.module.mcs.service.StModelParamService;
23 import com.iailab.module.mcs.service.StModelService;
24 import com.iailab.module.mcs.service.StModelSettingService;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.Parameters;
28 import javax.annotation.Resource;
29 import org.springframework.transaction.annotation.Transactional;
30 import org.springframework.web.bind.annotation.*;
31 import org.springframework.web.multipart.MultipartFile;
32
33 import java.io.IOException;
34 import java.util.*;
35
36 import static com.iailab.framework.common.pojo.CommonResult.success;
37
38
39 /**
40  * @author lirm 1343021927@qq.com
41  * @since 1.0.0 2023-05-10
42  */
43 @RestController
44 @RequestMapping("/model/mcs/st-model")
45 public class StModelController {
46     @Resource
47     private StModelService stModelService;
48
49     @Resource
50     private StModelParamService stModelParamService;
51
52     @Resource
53     private StModelSettingService stModelSettingService;
54     
55     @Resource
56     private StModelOutService stModelOutService;
57
58     @GetMapping("page")
59     @Operation(summary = "分页")
60     @Parameters({
61             @Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
62             @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
63             @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
64             @Parameter(name = Constant.ORDER, description = "排序方式,可选值(asc、desc)")
65     })
66     public CommonResult<PageData<StModelDTO>> page(@RequestParam Map<String, Object> params) {
67         PageData<StModelDTO> page = stModelService.page(params);
68
69         return success(page);
70     }
71
72     @GetMapping("{id}")
73     @Operation(summary = "信息")
74     public CommonResult<StModelDTO> get(@PathVariable("id") String id) {
75         StModelDTO data = stModelService.get(id);
76
77         List<StModelParamDTO> tParamPage = stModelParamService.getByModelId(id);
78         data.setModelParamList(tParamPage);
79
80         Map<String, Object> params = new HashMap<String, Object>(1);
81         params.put("modelId", data.getId());
82         List<StModelSettingDTO> tSettingPage = stModelSettingService.getAll(params);
83         data.setModelSettingList(tSettingPage);
84         
85         Map<String, Object> out = new HashMap<String, Object>(1);
86         out.put("modelId", data.getId());
87         List<StModelOutDTO> tOutPage = stModelOutService.getAll(out);
88         data.setModelOutList(tOutPage);
89
90         return new CommonResult<StModelDTO>().setData(data);
91     }
92
93     @PostMapping
94     @Operation(summary = "保存")
95     @LogOperation("保存")
96     @Transactional
97     public CommonResult save(@RequestBody StModelDTO dto) {
98         //效验数据
99         ValidationUtils.validate(dto, AddGroup.class, DefaultGroup.class);
100
101         dto.setId(UUID.randomUUID().toString());
102         List<StModelParamDTO> tParamList = dto.getModelParamList();
103         if (tParamList != null) {
104             for (int i = 0; i < tParamList.size(); i++) {
105                 StModelParamDTO tParamDTO = new StModelParamDTO();
106                 tParamDTO = tParamList.get(i);
107                 StModelParamEntity tParamEntity = new StModelParamEntity();
108                 tParamEntity.setModelId(dto.getId());
109                 tParamEntity.setParamName(tParamDTO.getParamName());
110                 tParamEntity.setParamId(tParamDTO.getParamId());
111                 tParamEntity.setParamOrder(tParamDTO.getParamOrder());
112                 tParamEntity.setParamPortOrder(tParamDTO.getParamPortOrder());
113                 tParamEntity.setDataLength(tParamDTO.getDataLength());
114                 tParamEntity.setParamType(tParamDTO.getParamType());
115                 stModelParamService.insert(tParamEntity);
116             }
117         }
118         List<StModelSettingDTO> tSettingList = dto.getModelSettingList();
119         if (tSettingList != null) {
120             for (int i = 0; i < tSettingList.size(); i++) {
121                 StModelSettingEntity tSettingEntity = ConvertUtils.sourceToTarget(tSettingList.get(i), StModelSettingEntity.class);
122                 tSettingEntity.setModelId(dto.getId());
123                 stModelSettingService.insert(tSettingEntity);
124             }
125         }
126         
127         List<StModelOutDTO> tOutList = dto.getModelOutList();
128         if (tOutList != null) {
129             for (int i = 0; i < tOutList.size(); i++) {
130                 StModelOutEntity tOutEntity = ConvertUtils.sourceToTarget(tOutList.get(i), StModelOutEntity.class);
131                 tOutEntity.setModelId(dto.getId());
132                 tOutEntity.setSort(i+1);
133                 stModelOutService.insert(tOutEntity);
134             }
135         }
136         dto.setCreator(SecurityFrameworkUtils.getLoginUserId());
137         dto.setCreateDate(new Date());
138         dto.setUpdater(SecurityFrameworkUtils.getLoginUserId());
139         dto.setUpdateDate(new Date());
140         dto.setIsEnable(IsEnableEnum.ENABLE.value());
141         stModelService.save(dto);
142         return new CommonResult();
143     }
144
145     @PutMapping
146     @Operation(summary = "修改")
147     @LogOperation("修改")
148     @Transactional
149     public CommonResult update(@RequestBody StModelDTO dto) {
150         //效验数据
151         ValidationUtils.validate(dto, UpdateGroup.class, DefaultGroup.class);
152
153         List<StModelParamDTO> tParamList = dto.getModelParamList();
154         if (tParamList != null) {
155             stModelParamService.deleteByModelId(dto.getId());
156             for (int i = 0; i < tParamList.size(); i++) {
157                 StModelParamDTO tParamDTO = new StModelParamDTO();
158                 tParamDTO = tParamList.get(i);
159                 StModelParamEntity tParamEntity = new StModelParamEntity();
160                 tParamEntity.setModelId(dto.getId());
161                 tParamEntity.setParamName(tParamDTO.getParamName());
162                 tParamEntity.setParamId(tParamDTO.getParamId());
163                 tParamEntity.setParamOrder(tParamDTO.getParamOrder());
164                 tParamEntity.setParamPortOrder(tParamDTO.getParamPortOrder());
165                 tParamEntity.setDataLength(tParamDTO.getDataLength());
166                 tParamEntity.setParamType(tParamDTO.getParamType());
167
168                 stModelParamService.insert(tParamEntity);
169             }
170         }
171         List<StModelSettingDTO> tSettingList = dto.getModelSettingList();
172         if (tSettingList != null) {
173             stModelSettingService.deleteByModelId(dto.getId());
174             for (int i = 0; i < tSettingList.size(); i++) {
175                 StModelSettingEntity tSettingEntity = ConvertUtils.sourceToTarget(tSettingList.get(i), StModelSettingEntity.class);
176                 tSettingEntity.setModelId(dto.getId());
177                 stModelSettingService.insert(tSettingEntity);
178             }
179         }
180         List<StModelOutDTO> tOutList = dto.getModelOutList();
181         if (tOutList != null) {
182             stModelOutService.deleteByModelId(dto.getId());
183             for (int i = 0; i < tOutList.size(); i++) {
184                 StModelOutEntity tOutEntity = ConvertUtils.sourceToTarget(tOutList.get(i), StModelOutEntity.class);
185                 tOutEntity.setModelId(dto.getId());
186                 tOutEntity.setSort(i+1);
187                 stModelOutService.insert(tOutEntity);
188             }
189         }
190
191         Long id = SecurityFrameworkUtils.getLoginUserId();
192         dto.setUpdater(id);
193         dto.setUpdateDate(new Date());
194         stModelService.update(dto);
195
196         return new CommonResult<Object>();
197     }
198
199     @DeleteMapping
200     @Operation(summary = "删除")
201     @LogOperation("删除")
202     @Transactional
203     public CommonResult delete(@RequestBody String[] ids) {
204         List<String> idsList = Arrays.asList(ids);
205         for (int i = 0; i < idsList.size(); i++) {
206             stModelParamService.deleteByModelId(idsList.get(i));//删除模型参数表数据
207             stModelSettingService.deleteByModelId(idsList.get(i));//删除模型设置表数据
208         }
209
210         stModelService.deleteByIds(ids);//删除模型表数据
211
212         return new CommonResult();
213     }
214
215     @PutMapping("/enable")
216     @Operation(summary = "启用")
217     @LogOperation("启用")
218     @Transactional
219     public CommonResult enable(@RequestBody String[] ids) {
220         stModelService.enableByIds(ids);
221         return new CommonResult();
222     }
223
224     @PutMapping("/disable")
225     @Operation(summary = "禁用")
226     @LogOperation("禁用")
227     @Transactional
228     public CommonResult disable(@RequestBody String[] ids) {
229         stModelService.disableByIds(ids);
230         return new CommonResult();
231     }
232
233
234     /**
235      * 上传模型
236      */
237     @Operation(summary = "上传模型")
238     @PostMapping("/upload")
239     public CommonResult<Map<String, Object>> upload(@RequestParam("file") MultipartFile file) throws IOException {
240         Map<String, Object> data = stModelService.upload(file);
241         return new CommonResult<Map<String, Object>>().setData(data);
242     }
243
244     @Operation(summary = "上传模型")
245     @GetMapping("/relation")
246     public CommonResult getRelation() {
247         Map<String, Object> data = stModelService.getRelation();
248         return new CommonResult().setData(data);
249     }
250 }