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

org.macrocloud.kernel.mf.base.BaseServiceImpl Maven / Gradle / Ivy

package org.macrocloud.kernel.mf.base;

import lombok.SneakyThrows;
import org.macrocloud.kernel.auth.MacroUser;
import org.macrocloud.kernel.auth.utils.AuthUtil;
import org.macrocloud.kernel.mf.base.BaseEntity;
import org.macrocloud.kernel.mf.service.BaseService;
import org.macrocloud.kernel.toolkit.constant.MacroConstant;
import org.macrocloud.kernel.toolkit.utils.BeanUtil;
import org.macrocloud.kernel.toolkit.utils.ClassUtil;
import org.macrocloud.kernel.toolkit.utils.ObjectUtil;
import org.macrocloud.kernel.toolkit.utils.ReflectUtil;
import org.macrocloud.kernel.toolkit.utils.DateUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import javax.validation.constraints.NotEmpty;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 业务封装基础类
 *
 * @param  mapper
 * @param  model
 */
@Validated
public class BaseServiceImpl, T extends BaseEntity> extends ServiceImpl implements BaseService {

	@Override
	public boolean save(T entity) {
		this.resolveSaveEntity(entity);
		return super.save(entity);
	}

	@Override
	public boolean saveBatch(Collection entityList, int batchSize) {
		entityList.forEach(this::resolveSaveEntity);
		return super.saveBatch(entityList, batchSize);
	}

	@Override
	public boolean updateById(T entity) {
		this.resolveEntity(entity);
		return super.updateById(entity);
	}

	@Override
	public boolean saveOrUpdate(T entity) {
		if (entity.getId() == null) {
			return this.save(entity);
		} else {
			return this.updateById(entity);
		}
	}


	@Transactional(rollbackFor = Exception.class)
	public boolean deleteLogic(@NotEmpty List ids) {
		MacroUser user = AuthUtil.getUser();
		List list = new ArrayList<>();

		return super.removeByIds(ids);
	}

	
	public boolean changeStatus(@NotEmpty List ids, Integer status) {
		MacroUser user = AuthUtil.getUser();
		List list = new ArrayList<>();

		return super.updateBatch(list);
	}

	@SneakyThrows
	private void resolveSaveEntity(T entity) {
		MacroUser user = AuthUtil.getUser();
		Date now = DateUtil.now();
		// 处理新增逻辑
		if (user != null) {
			entity.setCreatedBy(user.getUserId());
			entity.setCreateDept(user.getDeptId());
			entity.setUpdatedBy(user.getUserId());
			entity.setUpdatedBy(user.getUserId());
		}
		if (entity.getStatus() == null) {
			entity.setStatus(MacroConstant.DB_STATUS_NORMAL);
		}
		entity.setCreatedAt(now);
		entity.setUpdatedAt(now);
		entity.setIsDeleted(MacroConstant.DB_NOT_DELETED);
		// 处理多租户逻辑,若字段值为空,则不进行操作
		Field field = ReflectUtil.getField(entity.getClass(), MacroConstant.DB_TENANT_KEY);
		if (ObjectUtil.isNotEmpty(field)) {
			Method getTenantId = ClassUtil.getMethod(entity.getClass(), MacroConstant.DB_TENANT_KEY_GET_METHOD);
			String tenantId = String.valueOf(getTenantId.invoke(entity));
			if (ObjectUtil.isEmpty(tenantId)) {
				Method setTenantId = ClassUtil.getMethod(entity.getClass(), MacroConstant.DB_TENANT_KEY_SET_METHOD, String.class);
				setTenantId.invoke(entity, (Object) null);
			}
		}
	}

	@SneakyThrows
	private void resolveEntity(T entity) {
		MacroUser user = AuthUtil.getUser();
		Date now = DateUtil.now();
		if (entity.getId() == null) {
			// 处理新增逻辑
			if (user != null) {
				entity.setCreatedBy(user.getUserId());
				entity.setCreateDept(user.getDeptId());
				entity.setUpdatedBy(user.getUserId());
			}
			if (entity.getStatus() == null) {
				entity.setStatus(MacroConstant.DB_STATUS_NORMAL);
			}
			entity.setCreatedAt(now);
		} else if (user != null) {
			// 处理修改逻辑
			entity.setUpdatedBy(user.getUserId());
		}
		// 处理通用逻辑
		entity.setUpdatedAt(now);
		entity.setIsDeleted(MacroConstant.DB_NOT_DELETED);
		// 处理多租户逻辑,若字段值为空,则不进行操作
		Field field = ReflectUtil.getField(entity.getClass(), MacroConstant.DB_TENANT_KEY);
		if (ObjectUtil.isNotEmpty(field)) {
			Method getTenantId = ClassUtil.getMethod(entity.getClass(), MacroConstant.DB_TENANT_KEY_GET_METHOD);
			String tenantId = String.valueOf(getTenantId.invoke(entity));
			if (ObjectUtil.isEmpty(tenantId)) {
				Method setTenantId = ClassUtil.getMethod(entity.getClass(), MacroConstant.DB_TENANT_KEY_SET_METHOD, String.class);
				setTenantId.invoke(entity, (Object) null);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy