houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
package com.iailab.module.mcs.controller.admin;
 
import com.iailab.common.annotation.LogOperation;
import com.iailab.common.enums.IsEnableEnum;
import com.iailab.framework.common.page.PageData;
import com.iailab.common.utils.Constant;
import com.iailab.framework.common.util.object.ConvertUtils;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.validation.ValidationUtils;
import com.iailab.framework.common.validation.group.AddGroup;
import com.iailab.framework.common.validation.group.DefaultGroup;
import com.iailab.framework.common.validation.group.UpdateGroup;
import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
import com.iailab.module.mcs.dto.StModelDTO;
import com.iailab.module.mcs.dto.StModelOutDTO;
import com.iailab.module.mcs.dto.StModelParamDTO;
import com.iailab.module.mcs.dto.StModelSettingDTO;
import com.iailab.module.mcs.entity.StModelOutEntity;
import com.iailab.module.mcs.entity.StModelParamEntity;
import com.iailab.module.mcs.entity.StModelSettingEntity;
import com.iailab.module.mcs.service.StModelOutService;
import com.iailab.module.mcs.service.StModelParamService;
import com.iailab.module.mcs.service.StModelService;
import com.iailab.module.mcs.service.StModelSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.*;
 
import static com.iailab.framework.common.pojo.CommonResult.success;
 
 
/**
 * @author lirm 1343021927@qq.com
 * @since 1.0.0 2023-05-10
 */
@RestController
@RequestMapping("/model/mcs/st-model")
public class StModelController {
    @Resource
    private StModelService stModelService;
 
    @Resource
    private StModelParamService stModelParamService;
 
    @Resource
    private StModelSettingService stModelSettingService;
    
    @Resource
    private StModelOutService stModelOutService;
 
    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段"),
            @Parameter(name = Constant.ORDER, description = "排序方式,可选值(asc、desc)")
    })
    public CommonResult<PageData<StModelDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<StModelDTO> page = stModelService.page(params);
 
        return success(page);
    }
 
    @GetMapping("{id}")
    @Operation(summary = "信息")
    public CommonResult<StModelDTO> get(@PathVariable("id") String id) {
        StModelDTO data = stModelService.get(id);
 
        List<StModelParamDTO> tParamPage = stModelParamService.getByModelId(id);
        data.setModelParamList(tParamPage);
 
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("modelId", data.getId());
        List<StModelSettingDTO> tSettingPage = stModelSettingService.getAll(params);
        data.setModelSettingList(tSettingPage);
        
        Map<String, Object> out = new HashMap<String, Object>(1);
        out.put("modelId", data.getId());
        List<StModelOutDTO> tOutPage = stModelOutService.getAll(out);
        data.setModelOutList(tOutPage);
 
        return new CommonResult<StModelDTO>().setData(data);
    }
 
    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @Transactional
    public CommonResult save(@RequestBody StModelDTO dto) {
        //效验数据
        ValidationUtils.validate(dto, AddGroup.class, DefaultGroup.class);
 
        dto.setId(UUID.randomUUID().toString());
        List<StModelParamDTO> tParamList = dto.getModelParamList();
        if (tParamList != null) {
            for (int i = 0; i < tParamList.size(); i++) {
                StModelParamDTO tParamDTO = new StModelParamDTO();
                tParamDTO = tParamList.get(i);
                StModelParamEntity tParamEntity = new StModelParamEntity();
                tParamEntity.setModelId(dto.getId());
                tParamEntity.setParamName(tParamDTO.getParamName());
                tParamEntity.setParamId(tParamDTO.getParamId());
                tParamEntity.setParamOrder(tParamDTO.getParamOrder());
                tParamEntity.setParamPortOrder(tParamDTO.getParamPortOrder());
                tParamEntity.setDataLength(tParamDTO.getDataLength());
                tParamEntity.setParamType(tParamDTO.getParamType());
                stModelParamService.insert(tParamEntity);
            }
        }
        List<StModelSettingDTO> tSettingList = dto.getModelSettingList();
        if (tSettingList != null) {
            for (int i = 0; i < tSettingList.size(); i++) {
                StModelSettingEntity tSettingEntity = ConvertUtils.sourceToTarget(tSettingList.get(i), StModelSettingEntity.class);
                tSettingEntity.setModelId(dto.getId());
                stModelSettingService.insert(tSettingEntity);
            }
        }
        
        List<StModelOutDTO> tOutList = dto.getModelOutList();
        if (tOutList != null) {
            for (int i = 0; i < tOutList.size(); i++) {
                StModelOutEntity tOutEntity = ConvertUtils.sourceToTarget(tOutList.get(i), StModelOutEntity.class);
                tOutEntity.setModelId(dto.getId());
                tOutEntity.setSort(i+1);
                stModelOutService.insert(tOutEntity);
            }
        }
        dto.setCreator(SecurityFrameworkUtils.getLoginUserId());
        dto.setCreateDate(new Date());
        dto.setUpdater(SecurityFrameworkUtils.getLoginUserId());
        dto.setUpdateDate(new Date());
        dto.setIsEnable(IsEnableEnum.ENABLE.value());
        stModelService.save(dto);
        return new CommonResult();
    }
 
    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @Transactional
    public CommonResult update(@RequestBody StModelDTO dto) {
        //效验数据
        ValidationUtils.validate(dto, UpdateGroup.class, DefaultGroup.class);
 
        List<StModelParamDTO> tParamList = dto.getModelParamList();
        if (tParamList != null) {
            stModelParamService.deleteByModelId(dto.getId());
            for (int i = 0; i < tParamList.size(); i++) {
                StModelParamDTO tParamDTO = new StModelParamDTO();
                tParamDTO = tParamList.get(i);
                StModelParamEntity tParamEntity = new StModelParamEntity();
                tParamEntity.setModelId(dto.getId());
                tParamEntity.setParamName(tParamDTO.getParamName());
                tParamEntity.setParamId(tParamDTO.getParamId());
                tParamEntity.setParamOrder(tParamDTO.getParamOrder());
                tParamEntity.setParamPortOrder(tParamDTO.getParamPortOrder());
                tParamEntity.setDataLength(tParamDTO.getDataLength());
                tParamEntity.setParamType(tParamDTO.getParamType());
 
                stModelParamService.insert(tParamEntity);
            }
        }
        List<StModelSettingDTO> tSettingList = dto.getModelSettingList();
        if (tSettingList != null) {
            stModelSettingService.deleteByModelId(dto.getId());
            for (int i = 0; i < tSettingList.size(); i++) {
                StModelSettingEntity tSettingEntity = ConvertUtils.sourceToTarget(tSettingList.get(i), StModelSettingEntity.class);
                tSettingEntity.setModelId(dto.getId());
                stModelSettingService.insert(tSettingEntity);
            }
        }
        List<StModelOutDTO> tOutList = dto.getModelOutList();
        if (tOutList != null) {
            stModelOutService.deleteByModelId(dto.getId());
            for (int i = 0; i < tOutList.size(); i++) {
                StModelOutEntity tOutEntity = ConvertUtils.sourceToTarget(tOutList.get(i), StModelOutEntity.class);
                tOutEntity.setModelId(dto.getId());
                tOutEntity.setSort(i+1);
                stModelOutService.insert(tOutEntity);
            }
        }
 
        Long id = SecurityFrameworkUtils.getLoginUserId();
        dto.setUpdater(id);
        dto.setUpdateDate(new Date());
        stModelService.update(dto);
 
        return new CommonResult<Object>();
    }
 
    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @Transactional
    public CommonResult delete(@RequestBody String[] ids) {
        List<String> idsList = Arrays.asList(ids);
        for (int i = 0; i < idsList.size(); i++) {
            stModelParamService.deleteByModelId(idsList.get(i));//删除模型参数表数据
            stModelSettingService.deleteByModelId(idsList.get(i));//删除模型设置表数据
        }
 
        stModelService.deleteByIds(ids);//删除模型表数据
 
        return new CommonResult();
    }
 
    @PutMapping("/enable")
    @Operation(summary = "启用")
    @LogOperation("启用")
    @Transactional
    public CommonResult enable(@RequestBody String[] ids) {
        stModelService.enableByIds(ids);
        return new CommonResult();
    }
 
    @PutMapping("/disable")
    @Operation(summary = "禁用")
    @LogOperation("禁用")
    @Transactional
    public CommonResult disable(@RequestBody String[] ids) {
        stModelService.disableByIds(ids);
        return new CommonResult();
    }
 
 
    /**
     * 上传模型
     */
    @Operation(summary = "上传模型")
    @PostMapping("/upload")
    public CommonResult<Map<String, Object>> upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, Object> data = stModelService.upload(file);
        return new CommonResult<Map<String, Object>>().setData(data);
    }
 
    @Operation(summary = "上传模型")
    @GetMapping("/relation")
    public CommonResult getRelation() {
        Map<String, Object> data = stModelService.getRelation();
        return new CommonResult().setData(data);
    }
}