1 概述
基础数据新增,通用:
新增主要考虑,传入的字符校验,是否需要校验数据重复,是否具备上移,下移,返回对象格式
@LogAnnotation(firstLevel = "基础数据", secondLevel = "国家", noteLevel = "新增")
@PostMapping(value = "save")
public Map save(@RequestBody TBaseCountry TBaseCountry, HttpServletRequest request) {
Map map = new HashMap(4);
try{
Map mapuser= getParametersUserinfo(request);
String userid=mapuser.get("userid").toString();
String name=TBaseCountry.getName();// 前端是空 不允许录入到后台 ,此处一定有数据
boolean iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
if(!iswd){
map.put("flag", false);
map.put("msg", StatusCode.C00511.getMsg());
map.put("status", StatusCode.C00511.getCode());
return map;
}
//数据验证 该字段是否已经存在数据内 存在的话 就不能录入
Map mapName = new HashMap(4);
mapName.put("name",name);
mapName.put("id",null);
Long eq= objServices.countById(mapName);
if(eq !=0){
map.put("flag", false);
map.put("msg", StatusCode.C00510.getMsg());
map.put("status", StatusCode.C00510.getCode());
return map;
}
// 处理 最大值
com.yinghui.soft.web.baseCountry.model.TBaseCountry objMax=objServices.selectByOrderNumMax();
if(null==objMax){
TBaseCountry.setOrderNum(1);// 如果是第一个值默认给1
}else{
int ordernUm=objMax.getOrderNum()+1;// 如果是后续新增 增加1
TBaseCountry.setOrderNum(ordernUm);
}
TBaseCountry.setCreateUser(Integer.parseInt(userid));
TBaseCountry.setCreateTime(new Date());
TBaseCountry.setDeleteFlag(1);
int i =objServices.save(TBaseCountry);
if (i == 1) {
map.put("flag", true);
map.put("msg", StatusCode.C00200.getMsg());
map.put("status", StatusCode.C00200.getCode());
} else {
map.put("flag", false);
map.put("msg", StatusCode.C00201.getMsg());
map.put("status", StatusCode.C00201.getCode());
}
} catch (Exception e) {
logger.error(StatusCode.C00400.getMsg(), e);
map.put("flag", false);
map.put("msg", StatusCode.C00400.getMsg());
map.put("status", StatusCode.C00400.getCode());
}
return map;
}
2 从token获取信息
Map mapuser= getParametersUserinfo(request); 这是获取参数 获取token内的参数
String userid=mapuser.get("userid").toString(); 获取用用户编码
String name=TBaseCountry.getName();// 前端是空 不允许录入到后台 ,此处一定有数据
3 校验特殊字符
boolean iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
if(!iswd){
map.put("flag", false);
map.put("msg", StatusCode.C00511.getMsg());
map.put("status", StatusCode.C00511.getCode());
return map;
}
4 数据重复校验
//数据验证 该字段是否已经存在数据内 存在的话 就不能录入 Map mapName = new HashMap(4); mapName.put("name",name); mapName.put("id",null); Long eq= objServices.countById(mapName); if(eq !=0){ map.put("flag", false); map.put("msg", StatusCode.C00510.getMsg()); map.put("status", StatusCode.C00510.getCode()); return map; }
5 处理上下移动获取最大值 赋值
// 处理 最大值 com.yinghui.soft.web.baseCountry.model.TBaseCountry objMax=objServices.selectByOrderNumMax(); if(null==objMax){ TBaseCountry.setOrderNum(1);// 如果是第一个值默认给1 }else{ int ordernUm=objMax.getOrderNum()+1;// 如果是后续新增 增加1 TBaseCountry.setOrderNum(ordernUm); }
6 保存并返回
TBaseCountry.setCreateUser(Integer.parseInt(userid)); TBaseCountry.setCreateTime(new Date()); TBaseCountry.setDeleteFlag(1); int i =objServices.save(TBaseCountry); if (i == 1) { map.put("flag", true); map.put("msg", StatusCode.C00200.getMsg()); map.put("status", StatusCode.C00200.getCode()); } else { map.put("flag", false); map.put("msg", StatusCode.C00201.getMsg()); map.put("status", StatusCode.C00201.getCode()); } } catch (Exception e) { logger.error(StatusCode.C00400.getMsg(), e); map.put("flag", false); map.put("msg", StatusCode.C00400.getMsg()); map.put("status", StatusCode.C00400.getCode()); }
如果不是基础数据,按照实际的业务进行组织即可,如下:
注意返回的参数用枚举
/**
* 保存
*
* @return
*/
@LogAnnotation(firstLevel = "项目管理", secondLevel = "项目管理", noteLevel = "新增")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public Map save(@RequestBody TProjectManagement TProjectManagement, HttpServletRequest request) {
Map map = new HashMap(4);
try{
Map mapuser= getParametersUserinfo(request);
String userid=mapuser.get("userid").toString();
System.out.println(userid);
String name=TProjectManagement.getName();// 前端是空 不允许录入到后台 ,此处一定有数据
boolean iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
if(!iswd){
map.put("flag", false);
map.put("msg", StatusCode.C00511.getMsg());
map.put("status", StatusCode.C00511.getCode());
return map;
}
//数据验证
Map mapName = new HashMap(4);
mapName.put("name",TProjectManagement.getName());
mapName.put("id",null);
Long eq= objServices.countById(mapName);
if(eq !=0){
map.put("flag", false);
map.put("msg", StatusCode.C00510.getMsg());
map.put("status", StatusCode.C00510.getCode());
return map;
}
TProjectManagement.setCreateUser(Integer.parseInt(userid));
TProjectManagement.setCreateTime(new Date());
TProjectManagement.setDeleteFlag(1);
int i =objServices.save(TProjectManagement);
if (i == 1) {
map.put("flag", true);
map.put("msg", StatusCode.C00200.getMsg());
map.put("status", StatusCode.C00200.getCode());
} else {
map.put("flag", false);
map.put("msg", StatusCode.C00201.getMsg());
map.put("status", StatusCode.C00201.getCode());
}
} catch (Exception e) {
logger.error(StatusCode.C00400.getMsg(), e);
map.put("flag", false);
map.put("msg", StatusCode.C00400.getMsg());
map.put("status", StatusCode.C00400.getCode());
}
return map;
}