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

84 lines
2.7 KiB
Java

/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
* 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.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.BeanUtil;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.List;
/**
* 业务封装基础类
*
* @param <M> mapper
* @param <T> model
*/
@Validated
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {
private Class<T> modelClass;
@SuppressWarnings("unchecked")
public BaseServiceImpl() {
Type type = this.getClass().getGenericSuperclass();
this.modelClass = (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[1];
}
@Override
public boolean save(T entity) {
BladeUser user = SecureUtil.getUser();
LocalDateTime now = LocalDateTime.now();
entity.setCreateUser(user.getUserId());
entity.setCreateTime(now);
entity.setUpdateUser(user.getUserId());
entity.setUpdateTime(now);
entity.setStatus(BladeConstant.DB_STATUS_NORMAL);
entity.setIsDeleted(BladeConstant.DB_NOT_DELETED);
return super.save(entity);
}
@Override
public boolean updateById(T entity) {
BladeUser user = SecureUtil.getUser();
entity.setUpdateUser(user.getUserId());
entity.setUpdateTime(LocalDateTime.now());
return super.updateById(entity);
}
@Override
public boolean deleteLogic(@NotEmpty List<Integer> ids) {
BladeUser user = SecureUtil.getUser();
T entity = BeanUtil.newInstance(modelClass);
entity.setUpdateUser(user.getUserId());
entity.setUpdateTime(LocalDateTime.now());
entity.setIsDeleted(BladeConstant.DB_IS_DELETED);
UpdateWrapper<T> uw = new UpdateWrapper<>();
uw.in(BladeConstant.DB_PRIMARY_KEY, ids);
return super.update(entity, uw);
}
}