1结果工具类封装:
public class ResultUtils{
public static Result ResultSuccess(){
return new Result(Result.CODE_SUCCESS,"成功");
}
public static Result ResultSuccess(Object o){
return new Result(Result.CODE_SUCCESS,"成功",o);
}
public static Result ResultFailure(){
return new Result(Result.CODE_FAILURE,"失败");
}
public static Result ResultFailure(Object o){
return new Result(Result.CODE_FAILURE,"失败",o);
}
public static Result ResultFailure(String msg){
return new Result(Result.CODE_FAILURE,msg);
}
}
2结果封装:
public class Result<T> implements Serializable{
public static final Integer CODE_SUCCESS = 1;
public static final Integer CODE_FAILURE = 0;
private Integer code;
private String msg;
private T data;
public Result() {
}
public Result(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess(){
if(this.code==Result.CODE_SUCCESS){
return true;
}
return false;
}
}
3利用枚举封装增删改查的的消息:
public enum ValidateEnum {
SUCCESS("000000", "成功"),
PARAM_NULL("000001", "参数为空"),
NAME_NULL("000002", "名称为空"),
/* ===========公共 结束============== */
private String code;
private String info;
private Object data;
ValidateEnum(String code, String info) {
this.code = code;
this.info = info;
}
public Boolean isSuccess() {
if (code.equals(ValidateEnum.SUCCESS.getCode())) {
return true;
}
return false;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
4增删改查各种参数的验证:
/**
* 创建信息
*/
@Override
public Result<ActivityInfoVo> createActivityInfo(ActivityInfoVo activityInfoVo) {
Log.APP.info("创建信息开始, 名称:" + activityInfoVo.getActivityName());
long start = System.currentTimeMillis();
try {
//验证是否可以创建(此为验证)
ValidateEnum validateEnum = activityInfoValidateService.validateActivityInfoVo(activityInfoVo);
if (!validateEnum.isSuccess()) {
Log.APP.info("创建失败, 原因 [" + validateEnum.getInfo() + "] ");
return ResultUtils.ResultFailure(validateEnum.getInfo());
}
//VO转PO
ActivityInfo activityInfo = convertActivityInfoService.VO2PO(activityInfoVo);
distinctRef(activityInfo);//去重
//创建
activityInfo = activityInfoWriteBpo.createActivityInfo(activityInfo);
//指定的信息
if (null != activityInfo.getActivityUserLimit()
&& null!=activityInfo.getActivityUserLimit().getLimitType()
&& activityInfo.getActivityUserLimit().getLimitType() == ActivityUserLimitTypeEnum.ASSIGN_USER.getCode()) {
List<UserInfo> list = activityInfo.getUserInfoList();
userInfoWriteBpo.insertUserInfo(activityInfo.getId() + "", list);
}
activityInfo.setUserInfoList(null);
//PO转化VO
activityInfoVo = convertActivityInfoService.PO2VO(activityInfo);
//返回信息
return ResultUtils.ResultSuccess(activityInfoVo);
} catch (Exception e) {
Log.APP.error("创建活动信息错误, 活动名称:" + activityInfoVo.getActivityName(), e);
} finally {
Log.APP.info("创建活动信息结束, 耗时 [" + (System.currentTimeMillis() - start) + "] 活动名称:" + activityInfoVo.getActivityName());
}
return ResultUtils.ResultFailure();
}
5验证字段是否合法
@Override
public ValidateEnum validateActivityInfoVo(ActivityInfoVo activityInfoVo) {
//对象为空
if (null == activityInfoVo) {
return ValidateEnum.PARAM_NULL;
}
//名称为空
if (StringUtils.isEmpty(activityInfoVo.getActivityName())) {
return ValidateEnum.NAME_NULL;
}
List<ActivityInfo> list = activityInfoReadBpo.findByName(activityInfoVo.getActivityName());
if (null == activityInfoVo.getId() && list.size() >= 1) {//说明存在改活动名称 并且是添加活动
return ValidateEnum.ACTIVITY_NAME_ERROR;
}
//修改,如果id不同但是name相同 说明重复
if (null != activityInfoVo.getId()) {
for (ActivityInfo activityInfo : list) {
if (activityInfo.getId().intValue() != activityInfoVo.getId().intValue() && activityInfo.getActivityName().equals(activityInfoVo.getActivityName())) {
return ValidateEnum.ACTIVITY_NAME_ERROR;
}
}
}
//类型为空
if (null == activityInfoVo.getActivityCTypeEnum()) {
return ValidateEnum.ACTIVITY_TYPE_ERROR;
}
//部门为空
if (null == activityInfoVo.getActivityDep()) {
return ValidateEnum.ACTIVITY_DEPT_ERROR;
}
//分类为空
// if (null == activityInfoVo.getActivityClassEnum()) {
// return ValidateEnum.ACTIVITY_CLASS_ERROR;
// }
//描述为空
if (null == activityInfoVo.getActivityDesc() || activityInfoVo.getActivityDesc().length() > 1000) {
return ValidateEnum.ACTIVITY_DESC_ERROR;
}
//创建人为空
if (StringUtils.isEmpty(activityInfoVo.getActivityCreateUser())) {
return ValidateEnum.CREATE_USER_ERROR;
}
//根据用户类型验证
ValidateEnum validateType = validateByActivityType(activityInfoVo);
if (!validateType.isSuccess()) {
return validateType;
}
//活动的验证
ValidateEnum validateActivityReward = validateActivityReward(activityInfoVo);
if (!validateActivityReward.isSuccess()) {
return validateActivityReward;
}
return ValidateEnum.SUCCESS;
}
public class ResultUtils{
public static Result ResultSuccess(){
return new Result(Result.CODE_SUCCESS,"成功");
}
public static Result ResultSuccess(Object o){
return new Result(Result.CODE_SUCCESS,"成功",o);
}
public static Result ResultFailure(){
return new Result(Result.CODE_FAILURE,"失败");
}
public static Result ResultFailure(Object o){
return new Result(Result.CODE_FAILURE,"失败",o);
}
public static Result ResultFailure(String msg){
return new Result(Result.CODE_FAILURE,msg);
}
}
2结果封装:
public class Result<T> implements Serializable{
public static final Integer CODE_SUCCESS = 1;
public static final Integer CODE_FAILURE = 0;
private Integer code;
private String msg;
private T data;
public Result() {
}
public Result(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess(){
if(this.code==Result.CODE_SUCCESS){
return true;
}
return false;
}
}
3利用枚举封装增删改查的的消息:
public enum ValidateEnum {
SUCCESS("000000", "成功"),
PARAM_NULL("000001", "参数为空"),
NAME_NULL("000002", "名称为空"),
/* ===========公共 结束============== */
private String code;
private String info;
private Object data;
ValidateEnum(String code, String info) {
this.code = code;
this.info = info;
}
public Boolean isSuccess() {
if (code.equals(ValidateEnum.SUCCESS.getCode())) {
return true;
}
return false;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
4增删改查各种参数的验证:
/**
* 创建信息
*/
@Override
public Result<ActivityInfoVo> createActivityInfo(ActivityInfoVo activityInfoVo) {
Log.APP.info("创建信息开始, 名称:" + activityInfoVo.getActivityName());
long start = System.currentTimeMillis();
try {
//验证是否可以创建(此为验证)
ValidateEnum validateEnum = activityInfoValidateService.validateActivityInfoVo(activityInfoVo);
if (!validateEnum.isSuccess()) {
Log.APP.info("创建失败, 原因 [" + validateEnum.getInfo() + "] ");
return ResultUtils.ResultFailure(validateEnum.getInfo());
}
//VO转PO
ActivityInfo activityInfo = convertActivityInfoService.VO2PO(activityInfoVo);
distinctRef(activityInfo);//去重
//创建
activityInfo = activityInfoWriteBpo.createActivityInfo(activityInfo);
//指定的信息
if (null != activityInfo.getActivityUserLimit()
&& null!=activityInfo.getActivityUserLimit().getLimitType()
&& activityInfo.getActivityUserLimit().getLimitType() == ActivityUserLimitTypeEnum.ASSIGN_USER.getCode()) {
List<UserInfo> list = activityInfo.getUserInfoList();
userInfoWriteBpo.insertUserInfo(activityInfo.getId() + "", list);
}
activityInfo.setUserInfoList(null);
//PO转化VO
activityInfoVo = convertActivityInfoService.PO2VO(activityInfo);
//返回信息
return ResultUtils.ResultSuccess(activityInfoVo);
} catch (Exception e) {
Log.APP.error("创建活动信息错误, 活动名称:" + activityInfoVo.getActivityName(), e);
} finally {
Log.APP.info("创建活动信息结束, 耗时 [" + (System.currentTimeMillis() - start) + "] 活动名称:" + activityInfoVo.getActivityName());
}
return ResultUtils.ResultFailure();
}
5验证字段是否合法
@Override
public ValidateEnum validateActivityInfoVo(ActivityInfoVo activityInfoVo) {
//对象为空
if (null == activityInfoVo) {
return ValidateEnum.PARAM_NULL;
}
//名称为空
if (StringUtils.isEmpty(activityInfoVo.getActivityName())) {
return ValidateEnum.NAME_NULL;
}
List<ActivityInfo> list = activityInfoReadBpo.findByName(activityInfoVo.getActivityName());
if (null == activityInfoVo.getId() && list.size() >= 1) {//说明存在改活动名称 并且是添加活动
return ValidateEnum.ACTIVITY_NAME_ERROR;
}
//修改,如果id不同但是name相同 说明重复
if (null != activityInfoVo.getId()) {
for (ActivityInfo activityInfo : list) {
if (activityInfo.getId().intValue() != activityInfoVo.getId().intValue() && activityInfo.getActivityName().equals(activityInfoVo.getActivityName())) {
return ValidateEnum.ACTIVITY_NAME_ERROR;
}
}
}
//类型为空
if (null == activityInfoVo.getActivityCTypeEnum()) {
return ValidateEnum.ACTIVITY_TYPE_ERROR;
}
//部门为空
if (null == activityInfoVo.getActivityDep()) {
return ValidateEnum.ACTIVITY_DEPT_ERROR;
}
//分类为空
// if (null == activityInfoVo.getActivityClassEnum()) {
// return ValidateEnum.ACTIVITY_CLASS_ERROR;
// }
//描述为空
if (null == activityInfoVo.getActivityDesc() || activityInfoVo.getActivityDesc().length() > 1000) {
return ValidateEnum.ACTIVITY_DESC_ERROR;
}
//创建人为空
if (StringUtils.isEmpty(activityInfoVo.getActivityCreateUser())) {
return ValidateEnum.CREATE_USER_ERROR;
}
//根据用户类型验证
ValidateEnum validateType = validateByActivityType(activityInfoVo);
if (!validateType.isSuccess()) {
return validateType;
}
//活动的验证
ValidateEnum validateActivityReward = validateActivityReward(activityInfoVo);
if (!validateActivityReward.isSuccess()) {
return validateActivityReward;
}
return ValidateEnum.SUCCESS;
}