com.ape9527.core.controller.BaseDataController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ape-core Show documentation
Show all versions of ape-core Show documentation
Ape low code platform core module
The newest version!
package com.ape9527.core.controller;
import com.ape9527.auth.annotation.LoginAuth;
import com.ape9527.core.controller.BaseController;
import com.ape9527.core.model.AjaxResult;
import com.ape9527.core.model.base.*;
import com.ape9527.core.service.BaseDataService;
import com.ape9527.core.service.BaseObjFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* 通用接口
*
* @author YuanShuai[[email protected]]
*/
@Api(tags = "BaseDataController", description = "通用接口")
@RestController
@RequestMapping("/api/base/data")
public class BaseDataController extends BaseController {
private final BaseDataService baseDataService;
private final BaseObjFieldService baseObjFieldService;
public BaseDataController(BaseDataService baseDataService, BaseObjFieldService baseObjFieldService){
this.baseDataService = baseDataService;
this.baseObjFieldService = baseObjFieldService;
}
@LoginAuth
@ApiOperation("通用列表查询")
@PostMapping("/list")
public AjaxResult baseDataList(@RequestBody BaseQueryVo vo) {
vo.setEnableCorrelateQuery(true);
return AjaxResult.success(baseDataService.queryList(vo));
}
@LoginAuth
@ApiOperation("通用单个查询")
@PostMapping("/detail")
public AjaxResult baseDataDetail(@RequestBody BaseQueryVo vo) {
return AjaxResult.success(baseDataService.queryById(vo));
}
@LoginAuth
@ApiOperation("通用删除")
@PostMapping("/del")
public AjaxResult baseDataDel(@RequestBody BaseDelVo vo) {
int del = baseDataService.del(vo);
return del > 0 ? AjaxResult.success() : AjaxResult.error();
}
@LoginAuth
@ApiOperation("通用保存")
@PostMapping("/save")
public AjaxResult baseSave(@RequestBody BaseSaveVo vo) {
// 解析更新的uuid
String uuid = null;
for (ConditionsVo condition : vo.getConditions()) {
if("uuid".equals(condition.getFieldCode())){
uuid = condition.getValue().toString();
break;
}
}
// 获取所有得唯一字段
List uniqueFields = baseObjFieldService.queryUniqueFields(vo.getObjectCode());
// 唯一字段判断(因建表时没有创建唯一约束,所以需要代码逻辑判断)
// 循环判断每一个唯一字段的值是否存在于数据库,如果存在就返回
for(FieldVo field:vo.getFields()){
if(uniqueFields.contains(field.getFieldCode())){
if(uuid != null){
// 修改时,先判断当前值是否被修改
// 若没有,则跳过判断
// 若有,则继续判断重复
BaseQueryVo oldDataVo = new BaseQueryVo(vo.getObjectCode());
oldDataVo.setUuid(uuid);
Map map = baseDataService.queryById(oldDataVo);
Object value = map.get(field.getFieldCode());
if (field.getValue().equals(value)) {
continue;
}
}
BaseQueryVo queryVo = new BaseQueryVo(vo.getObjectCode());
queryVo.eq(field.getFieldCode(), field.getValue());
Map map = baseDataService.queryOne(queryVo);
if (map != null) {
String fieldName = baseObjFieldService.queryFieldName(vo.getObjectCode(), field.getFieldCode());
return AjaxResult.error("【" + fieldName + "】不允许重复!");
}
}
}
int flag = baseDataService.save(vo);
return flag == 1 ? AjaxResult.success() : AjaxResult.error();
}
}