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

tech.codingless.core.plugs.mybaties3.DBBaseGenericServiceImpl Maven / Gradle / Ivy

package tech.codingless.core.plugs.mybaties3;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson2.JSON;

import tech.codingless.core.plugs.mybaties3.annotation.OrderTypeEnum;
import tech.codingless.core.plugs.mybaties3.condition.ColumnSelector;
import tech.codingless.core.plugs.mybaties3.condition.QueryConditionWrapper;
import tech.codingless.core.plugs.mybaties3.data.BaseDO;
import tech.codingless.core.plugs.mybaties3.data.DataEnvProperties;
import tech.codingless.core.plugs.mybaties3.data.PageRollResult;
import tech.codingless.core.plugs.mybaties3.data.UpdateObject;
import tech.codingless.core.plugs.mybaties3.helper.ColumnHelper;
import tech.codingless.core.plugs.mybaties3.strategy.IdCreator;
import tech.codingless.core.plugs.mybaties3.util.DataEnvUtil;
import tech.codingless.core.plugs.mybaties3.util.DataSessionEnv;
import tech.codingless.core.plugs.mybaties3.util.MybatiesAssertUtil;
import tech.codingless.core.plugs.mybaties3.util.MybatiesStringUtil;
import tech.codingless.core.plugs.mybaties3.util.SnowFlakeNumberUtil;

public class DBBaseGenericServiceImpl implements DBBaseGenericService {
	@Autowired
	protected GenericUpdateDao updateDao;
	@Autowired
	protected GenericQueryDao queryDao;
	@SuppressWarnings("rawtypes")
	@Autowired
	protected GenericQueryDao queryGenericDao;
	@Autowired(required = false)
	protected IdCreator idcreator;

	private String namespace;

	private void generateId(T data) {
		if (idcreator != null) {
			// 允许用户实现自己的主键生成策略
			String newId = idcreator.generateId(data);
			if (MybatiesStringUtil.isNotEmpty(newId)) {
				data.setId(newId);
				return;
			}
		}

		// ObjectId objectId = new ObjectId();
		// data.setId(objectId.toHexString());
		// 默认数据级别
		if (data.getDataLevel() == null) {
			data.setDataLevel(1);
		}
		// 默认采用雪花算法生成ID
		data.setId(Long.toString(SnowFlakeNumberUtil.nextId()));
	}

	/**
	 * 
	 * 
	 * @return 获得对应Mapper的Name Spance
	 */
	protected String namespace() {
		if (namespace != null) {
			return namespace;
		}
		Type type = getClass().getGenericSuperclass();
		Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
		namespace = trueType.getTypeName() + ".";
		return namespace;
	}

	@Transactional
	@Override
	public boolean create(T data) {

		data.setDel(false);
		data.setEnv(DataEnvUtil.getEvn());
		if (MybatiesStringUtil.isEmpty(data.getId())) {
			generateId(data);
		}
		if (MybatiesStringUtil.isEmpty(data.getCreateUid())) {
			data.setCreateUid(DataEnvProperties.getOwnerId());
		}
		if (MybatiesStringUtil.isEmpty(data.getOwnerId())) {
			data.setOwnerId(DataEnvProperties.getOwnerId());
		}
		data.setGroupId(MybatiesStringUtil.isNotEmpty(data.getGroupId()) ? data.getGroupId() : DataEnvProperties.getGroupId());
		data.setWriteUid(MybatiesStringUtil.isNotEmpty(data.getWriteUid()) ? data.getWriteUid() : DataEnvProperties.getOptUserId());
		data.setCompanyId(MybatiesStringUtil.isNotEmpty(data.getCompanyId()) ? data.getCompanyId() : DataEnvProperties.getCompanyId());
		data.setVer(data.getVer() != null ? data.getVer() : 1L);
		return updateDao.createEntity(data) == 1;
	}

	@Transactional
	@Override
	public boolean create(String companyId, T obj) {
		MybatiesAssertUtil.assertNotEmpty(companyId, "COMPANY_ID_NOT_EXIST");
		BaseDO entiry = obj;
		entiry.setCompanyId(companyId);
		return create(obj);
	}

	@Transactional
	@Override
	public boolean update(T data) {
		if (DataEnvProperties.getOptUserId() == null || !DataEnvProperties.getOptUserId().equals(data.getOwnerId())) {
			return false;
		}
		data.setWriteUid(DataEnvProperties.getOptUserId());
		boolean success = updateDao.updateEntity(data) == 1;
		return success;

	}

	@Transactional
	@Override
	public boolean updateNotNull(String companyId, T data, Long ver) {
		MybatiesAssertUtil.assertNotEmpty(companyId, "COMPANY_ID_EMPTY");
		data.setCompanyId(companyId);
		return this.updateSkipNull(data, ver);
	}

	@Transactional
	@Override
	public boolean updateSkipNull(String companyId, T data, Long ver) {
		MybatiesAssertUtil.assertNotEmpty(companyId, "COMPANY_ID_EMPTY");
		data.setCompanyId(companyId);
		return this.updateSkipNull(data, ver);
	}

	@Transactional
	@Override
	public boolean updateSkipNull(T data, Long ver) {
		MybatiesAssertUtil.assertTrue(updateDao.updateNotNull(data, ver) == 1, "UPDATE_NOT_NULL_FAIL");
		return false;
	}

	@Override
	public int batchUpdateAppend(String companyId, T data, Long ver, int batchSize) {
		return updateDao.updateSkipNullBatchAppend(companyId, data, ver, batchSize);
	}

	@Override
	public int batchUpdateExecute(Class clazz) {
		return updateDao.updateSkipNullBatchExecute(clazz);
	}

	@Override
	public int batchUpdate(List updateList) {
		return updateDao.updateSkipNullBatchExecute(updateList);
	}

	@Transactional
	@Override
	public boolean update(T data, String companyId) {
		data.setWriteUid(DataSessionEnv.CURRENT_USER_ID.get());
		return updateDao.updateEntityWithCompanyId(data, companyId) == 1;
	}

	@Transactional
	@Override
	public boolean updateSkipCheckOwner(T data) {
		data.setWriteUid(DataSessionEnv.CURRENT_USER_ID.get());
		return updateDao.updateEntity(data) == 1;
	}

	@Transactional
	@Override
	public boolean deletePhysical(Class clazz, String id, String companyId) {
		return updateDao.deleteEntityWithCompanyId(clazz, id, companyId) == 1;
	}

	@Transactional
	@Override
	public boolean deleteLogical(Class clazz, String id, String companyId) {
		return updateDao.deleteLogicalWithCompanyId(clazz, id, companyId) == 1;
	}

	@Transactional
	@Override
	public int deleteLogical(Class clazz, Collection idList, String companyId) {
		return updateDao.deleteLogicalWithCompanyId(clazz, idList, companyId);
	}

	@Override
	public List list(Class clazz) {
		return queryDao.list(clazz);
	}

	@Override
	public T get(Class clazz, String entityId) {
		if (MybatiesStringUtil.isNotEmpty(DataEnvProperties.getCompanyId())) {
			return queryDao.getEntity(clazz, entityId);
		}
		return queryDao.getEntity(clazz, entityId);
	}

	@Override
	public T get(String id) {
		return this.get(getEntityClass(), id);
	}

	@Override
	public T get(Class clazz, String id, String companyId) {
		T t = queryDao.getEntity(clazz, id, companyId);
		return t;
	}

	@Override
	public List get(Class clazz, String companyId, Collection idList) {
		return queryDao.findEntityList(clazz, companyId, idList);
	}

	@Override
	public List get(Class clazz, String companyId, Collection idList, Collection columns) {

		// 检查列字段的合法性
		if (!CollectionUtils.isEmpty(columns)) {
			MybatiesAssertUtil.assertTrue(columns.stream().filter(column -> ColumnHelper.isIncorrectColumn(column)).count() == 0, "HAS_INCORRECT_COLUMN");
		}

		return queryDao.findEntityList(clazz, companyId, idList, columns);
	}

	@Transactional
	@Override
	public boolean create(List list) {
		list.forEach(item -> {
			if (MybatiesStringUtil.isEmpty(item.getId())) {
				generateId(item);
			}
			item.setCreateUid(DataEnvProperties.getOptUserId());
			item.setOwnerId(DataEnvProperties.getOptUserId());
			item.setWriteUid(DataEnvProperties.getOptUserId());
			item.setDel(false);
			if (MybatiesStringUtil.isEmpty(item.getCompanyId())) {
				item.setCompanyId(DataEnvProperties.getCompanyId());
			}
			if (item.getVer() == null) {
				item.setVer(1L);
			}
			item.setEnv(DataEnvProperties.getEnv());
		});
		return updateDao.createEntityList(list) == list.size();
	}

	@Transactional
	@Override
	public boolean create(String companyId, List list) {
		list.forEach(item -> {
			item.setCompanyId(companyId);
			item.setEnv(DataEnvProperties.getEnv());
		});
		return create(list);
	}

	@Transactional
	@Override
	public List list(Class clazz, String companyId) {
		return queryDao.list(clazz, companyId);
	}

	@Override
	public PageRollResult rollPage(String selectId, Map param, int size, int page) {
		return queryDao.rollPage(namespace(), selectId, param, size, page);
	}

	@Override
	public PageRollResult rollPage(ColumnSelector columns, QueryConditionWrapper wrapper, SerializableFunction sortColumn, OrderTypeEnum orderType, Integer size, Integer page) {

		int limit = size == null ? 100 : size;
		int offset = page == null ? 0 : (page - 1) * size;
		List list = select(columns, wrapper, sortColumn, orderType, offset, limit);
		long rows = count(wrapper);

		PageRollResult result = new PageRollResult<>();
		result.setList(list);
		result.setCurrentPage(page);
		result.setPageSize(limit);
		result.setTotalPage((int) Math.ceil(rows / limit));
		result.setTotalCount((int) rows);
		return result;
	}

	@Override
	public T findOneByExample(Class clazz, String companyId, T example) {
		List list = this.findByExample(clazz, companyId, example, 2);
		MybatiesAssertUtil.assertTrue(list == null || list.size() <= 1, "RESULT_MORE_THEN_ONE");
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List findByExample(Class clazz, String companyId, T example, Integer size) {
		if (MybatiesStringUtil.isEmpty(companyId)) {
			return Collections.emptyList();
		}
		example.setCompanyId(companyId);
		return this.findByExample(clazz, example, size);
	}

	@Override
	public List findByExample(Class clazz, T example, Integer size) {
		return queryDao.findByExample(clazz, null, example, null, null, size, 1);
	}

	@Override
	public String getEntityClassName() {
		Type type = getClass().getGenericSuperclass();
		Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
		return trueType.getTypeName();
	}

	@SuppressWarnings("unchecked")
	private Class getEntityClass() {
		Type type = getClass().getGenericSuperclass();
		Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
		return (Class) trueType;
	}

	@Override
	public T get(String id, String companyId) {
		return this.get(getEntityClass(), id, companyId);
	}

	@Override
	public List get(String companyId, Collection idList) {
		return this.get(getEntityClass(), companyId, idList);
	}

	@Override
	public List get(String companyId, Collection idList, Collection columns) {
		return this.get(getEntityClass(), companyId, idList, columns);
	}

	@Override
	public boolean deletePhysical(String id, String companyId) {
		return deletePhysical(getEntityClass(), id, companyId);
	}

	@Override
	public boolean deleteLogical(String id, String companyId) {
		return deleteLogical(getEntityClass(), id, companyId);
	}

	@Override
	public int deleteLogical(Collection idList, String companyId) {
		return deleteLogical(getEntityClass(), idList, companyId);
	}

	@Override
	public List list() {
		return list(getEntityClass());
	}

	@Override
	public List list(String companyId) {
		return list(getEntityClass(), companyId);
	}

	@Override
	public List findByExample(String companyId, T example, Integer size) {
		return findByExample(getEntityClass(), companyId, example, size);
	}

	@Override
	public List findByExample(T example, Integer size) {
		return findByExample(getEntityClass(), example, size);
	}

	@Override
	public T findOneByExample(String companyId, T example) {
		return findOneByExample(getEntityClass(), companyId, example);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List> select(String selectId, Map param, int offset, int limit) {
		return queryGenericDao.select(selectId, param, offset, limit);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List> select(String selectId, String param, int offset, int limit) {
		return queryGenericDao.select(selectId, MybatiesStringUtil.isEmpty(param) ? new HashMap() : JSON.parseObject(param), offset, limit);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List select(ColumnSelector columns, QueryConditionWrapper wrapper, SerializableFunction orderColumn, OrderTypeEnum orderType, int offset, int limit) {

		wrapper.isFalse(BaseDO::isDel);
		wrapper.eq(BaseDO::getEnv, DataEnvProperties.getEnv());
		return queryGenericDao.select(getEntityClass(), columns, wrapper, orderColumn, orderType, offset, limit);
	}

	@SuppressWarnings("unchecked")
	@Override
	public long count(QueryConditionWrapper wrapper) {
		wrapper.isFalse(BaseDO::isDel);
		wrapper.eq(BaseDO::getEnv, DataEnvProperties.getEnv());
		return queryGenericDao.count(getEntityClass(), wrapper);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy