package com.whdc.controller; import com.whdc.model.bean.ItemType; import com.whdc.model.dto.FindRuleDto; import com.whdc.model.entity.ERule; import com.whdc.model.group.Insert; import com.whdc.model.group.Update; import com.whdc.service.IERuleService; import com.whdc.utils.ResultJson; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.Objects; /** * @author xusan * @since 2023-03-19 */ @Api(tags = "规则管理 - Controller") @RestController @RequestMapping("/v2/eRule") public class ERuleController { @Autowired private IERuleService ieRuleService; @ApiOperation(value = "查询所有") @PostMapping(value = "list") public ResultJson list() { return ResultJson.ok(ieRuleService.list()); } @ApiOperation(value = "分页查询") @PostMapping(value = "page") public ResultJson page(@RequestBody FindRuleDto ruleDto) { return ResultJson.ok(ieRuleService.page(ruleDto)); } @ApiOperation(value = "添加") @PostMapping(value = "save") public ResultJson insert(@RequestBody @Validated(Insert.class) ERule model) { if (CollectionUtils.isNotEmpty( ieRuleService.lambdaQuery() .eq(ERule::getName,String.valueOf(model.getName()).trim()) .list()) ){ return ResultJson.error("该名称重复"); } if ( StringUtils.isBlank(model.getDiffMax()) && StringUtils.isBlank(model.getMin()) && StringUtils.isBlank(model.getMax()) ){ return ResultJson.error("请设置规则"); } if (!ItemType.map().containsKey(model.getItem())) { return ResultJson.error("规则类型不存在"); } return ResultJson.ok(ieRuleService.save(model)); } @ApiOperation(value = "修改") @PostMapping(value = "edit") public ResultJson update(@RequestBody @Validated(Update.class) ERule model) { if (CollectionUtils.isNotEmpty( ieRuleService.lambdaQuery() .eq(ERule::getName,String.valueOf(model.getName()).trim()) .ne(ERule::getId, model.getId()) .list()) ){ return ResultJson.error("该名称重复"); } return ResultJson.ok(ieRuleService.updateById(model)); } @ApiOperation(value = "删除") @GetMapping(value = "del/{id}") public ResultJson delete(@PathVariable("id") Integer id) { if (Objects.isNull(ieRuleService.getById(id))) { return ResultJson.error("当前数据不存在"); } return ResultJson.ok(ieRuleService.removeById(id)); } @ApiOperation(value = "获取规则类型") @GetMapping(value = "getItem") public ResultJson getItem() { return ResultJson.ok(ItemType.list()); } }