blade-tool/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseServiceImpl.java

97 lines
2.7 KiB
Java

/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 org.springblade.core.mp.base;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* 业务封装基础类
*
* @param <M> mapper
* @param <T> model
* @author Chill
*/
@Validated
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
@Override
public boolean save(T entity) {
this.resolveSave(entity);
return super.save(entity);
}
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
entityList.forEach(this::resolveSave);
return super.saveBatch(entityList, batchSize);
}
@Override
public boolean updateById(T entity) {
this.resolveUpdate(entity);
return super.updateById(entity);
}
@Override
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
entityList.forEach(this::resolveUpdate);
return super.updateBatchById(entityList, batchSize);
}
@Override
public boolean deleteLogic(@NotEmpty List<Long> ids) {
return super.removeByIds(ids);
}
@SneakyThrows
private void resolveSave(T entity) {
BladeUser user = SecureUtil.getUser();
if (user != null) {
entity.setCreateUser(user.getUserId());
entity.setUpdateUser(user.getUserId());
}
Date now = DateUtil.now();
entity.setCreateTime(now);
entity.setUpdateTime(now);
if (entity.getStatus() == null) {
entity.setStatus(BladeConstant.DB_STATUS_NORMAL);
}
entity.setIsDeleted(BladeConstant.DB_NOT_DELETED);
}
@SneakyThrows
private void resolveUpdate(T entity) {
BladeUser user = SecureUtil.getUser();
if (user != null) {
entity.setUpdateUser(user.getUserId());
}
entity.setUpdateTime(DateUtil.now());
}
}