package com.example.demo.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.demo.constant.CodeConstant; import com.example.demo.entity.ModelExistEntity; import com.example.demo.entity.ModelUploadEntity; import com.example.demo.service.IModelExistService; import com.example.demo.service.IModelUploadService; import com.example.demo.util.Result; import com.example.demo.vo.request.RequestModelExist; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.*; /** * Created by Hb_zzZ on 2021/1/20. */ @Slf4j @RestController @RequestMapping("/change/modelExist") @Api(tags = "现有模型模块") public class ModelExistController extends BaseController { @Autowired private IModelExistService modelExistService; @Autowired private IModelUploadService modelUploadService; /** * 根据模型名称判断是否存在 * @return */ @ApiOperation("根据模型名称判断是否存在") @RequestMapping(value = "/findModelByNames", method = RequestMethod.POST) @ApiImplicitParams({ @ApiImplicitParam(name = "names", value = "名称(多个以逗号分割)", dataType = "MultipartFile")}) public Result findModelByNames(@RequestBody RequestModelExist param){ if(StringUtils.isEmpty(param.getNames())){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } String[] nameArr = param.getNames().split(","); LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.in(ModelExistEntity::getName,Arrays.asList(nameArr)); List list = modelExistService.list(wrapper); List nameList = new ArrayList<>(); for (ModelExistEntity modelExistEntity : list) { nameList.add(modelExistEntity.getName()); } List result = new ArrayList<>(); List> resultMap = new ArrayList<>(); Map map = null; ModelUploadEntity modelUploadEntity = null; for (String name : nameArr) { if(!nameList.contains(name)){ modelUploadEntity = modelUploadService.findByFileId(name); if(modelUploadEntity == null){ return Result.failure(CodeConstant.FAILURE_CODE_4009, CodeConstant.FAILURE_MSG_4009); } result.add(name); map = new HashMap<>(); map.put("name", name); map.put("userid", modelUploadEntity.getUserId()); resultMap.add(map); } } ModelExistEntity modelExistEntity = new ModelExistEntity(); for (String name : result) { modelExistEntity = new ModelExistEntity(); modelExistEntity.setName(name); modelExistService.save(modelExistEntity); } return Result.success(resultMap); } /** * 根据模型名称删除 * @return */ @ApiOperation("根据模型名称删除") @RequestMapping(value = "/deleteModelByNames", method = RequestMethod.POST) @ApiImplicitParams({ @ApiImplicitParam(name = "names", value = "名称(多个以逗号分割)", dataType = "MultipartFile")}) public Result deleteModelByNames(@RequestBody RequestModelExist param){ if(StringUtils.isEmpty(param.getNames())){ return Result.failure(CodeConstant.FAILURE_CODE_3001, CodeConstant.FAILURE_MSG_3001); } String[] nameArr = param.getNames().split(","); for (String name : nameArr) { modelExistService.deleteByName(name); } return Result.success(); } }