All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.cn9750wang.webtools.utils.ServiceUtils Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright 2021 wwy
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.gitee.cn9750wang.webtools.utils;

import com.gitee.cn9750wang.webtools.error.defines.ParamErr;
import com.gitee.cn9750wang.webtools.entity.SoftDeleteEntity;
import com.gitee.cn9750wang.webtools.entity.TimeInfoEntity;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 顾名思义,没什么好说的
 * @author wwy
 */
public class ServiceUtils {


    /**
     * 更新操作前的检查
     * 检查 数据非空 entity != null
     * 检查 创建时间 createTime == null  (如果继承{@link TimeInfoEntity}则检查)
     * 检查 删除时间 deleteTime == null  (如果继承{@link SoftDeleteEntity}则检查)
     * 设置 修改时间 updateTime == now() (如果继承{@link TimeInfoEntity}则设置)
     * 如果以上任意检查项不满足要求将抛出异常
     * @param entity 数据
     */
    public static void updateCheck(Serializable entity){
        // 断言实体类不为空
        ParamErr.NOT_NULL.isNotNull(entity,"数据");
        // 限制客户端软删除非法操作
        if(entity instanceof SoftDeleteEntity){
            var obj = (SoftDeleteEntity) entity;
            ParamErr.NULL.isNull(obj.getDeleteTime(),"删除时间");
        }
        if(entity instanceof TimeInfoEntity){
            var obj = (TimeInfoEntity) entity;
            // 断言创建时间必须为空,限制客户端非法修改数据创建时间
            ParamErr.NULL.isNull(obj.getCreateTime(),"创建时间")
                    .isNull(obj.getUpdateTime(),"修改时间");
            // 设置修改时间
            obj.setUpdateTime(LocalDateTime.now());
        }
    }

    /**
     * 插入数据操作前的检查
     * 检查 数据非空 entity != null
     * 检查 创建时间 createTime == null  (如果继承{@link TimeInfoEntity}则检查)
     * 设置 修改时间 updateTime == null  (如果继承{@link TimeInfoEntity}则检查)
     * 检查 删除时间 deleteTime == null  (如果继承{@link SoftDeleteEntity}则检查)
     * 如果以上任意检查项不满足要求将抛出异常
     * @param entity 数据
     */
    public static void addCheck(Serializable entity){
        // 断言不能为空
        ParamErr.NOT_NULL.isNotNull(entity,"数据");
        // 限制客户端直接设置的软删除非法操作
        if(entity instanceof SoftDeleteEntity){
            var obj = (SoftDeleteEntity) entity;
            ParamErr.NULL.isNull(obj.getDeleteTime(),"删除时间");
        }
        if(entity instanceof TimeInfoEntity){
            var obj = (TimeInfoEntity) entity;
            // 断言创建时间必须为空,限制客户端非法设置数据创建时间以及更新时间
            ParamErr.NULL.isNull(obj.getCreateTime(),"创建时间")
                    .isNull(obj.getUpdateTime(),"修改时间");
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy