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> page(@RequestParam Map params) { PageData page = stModelService.page(params); return success(page); } @GetMapping("{id}") @Operation(summary = "信息") public CommonResult get(@PathVariable("id") String id) { StModelDTO data = stModelService.get(id); List tParamPage = stModelParamService.getByModelId(id); data.setModelParamList(tParamPage); Map params = new HashMap(1); params.put("modelId", data.getId()); List tSettingPage = stModelSettingService.getAll(params); data.setModelSettingList(tSettingPage); Map out = new HashMap(1); out.put("modelId", data.getId()); List tOutPage = stModelOutService.getAll(out); data.setModelOutList(tOutPage); return new CommonResult().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 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 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 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 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 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 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(); } @DeleteMapping @Operation(summary = "删除") @LogOperation("删除") @Transactional public CommonResult delete(@RequestBody String[] ids) { List 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> upload(@RequestParam("file") MultipartFile file) throws IOException { Map data = stModelService.upload(file); return new CommonResult>().setData(data); } @Operation(summary = "上传模型") @GetMapping("/relation") public CommonResult getRelation() { Map data = stModelService.getRelation(); return new CommonResult().setData(data); } }