From c4e17ff8472fd9f8123e86b593b2968ad5936b15 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 01 十一月 2021 13:49:11 +0800
Subject: [PATCH] fix 修复 xxl-job-admin 部署问题

---
 ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java |  187 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 169 insertions(+), 18 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java
index 77b9c28..e9c1f19 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/mybatisplus/core/ServicePlusImpl.java
@@ -1,21 +1,37 @@
 package com.ruoyi.common.core.mybatisplus.core;
 
-import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
+import cn.hutool.core.bean.copier.CopyOptions;
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.Assert;
+import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.core.page.PagePlus;
+import com.ruoyi.common.utils.BeanCopyUtils;
+import com.ruoyi.common.utils.reflect.ReflectUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.ResolvableType;
 
+import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.Map;
 
 /**
  * IServicePlus 瀹炵幇绫�
  *
+ * @param <M> Mapper绫�
+ * @param <T> 鏁版嵁瀹炰綋绫�
+ * @param <V> vo绫�
  * @author Lion Li
  */
 @Slf4j
 @SuppressWarnings("unchecked")
-public class ServicePlusImpl<M extends BaseMapperPlus<T>, T> extends ServiceImpl<M, T> implements IServicePlus<T> {
+public class ServicePlusImpl<M extends BaseMapperPlus<T>, T, V> extends ServiceImpl<M, T> implements IServicePlus<T, V> {
 
 	@Autowired
 	protected M baseMapper;
@@ -33,29 +49,31 @@
 		return entityClass;
 	}
 
-	protected Class<T> mapperClass = currentMapperClass();
+	protected Class<M> mapperClass = currentMapperClass();
+
+	protected Class<V> voClass = currentVoClass();
+
+	public Class<V> getVoClass() {
+		return voClass;
+	}
 
 	@Override
-	protected Class<T> currentMapperClass() {
-		return (Class<T>) this.getResolvableType().as(ServicePlusImpl.class).getGeneric(0).getType();
+	protected Class<M> currentMapperClass() {
+		return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 0);
 	}
 
 	@Override
 	protected Class<T> currentModelClass() {
-		return (Class<T>) this.getResolvableType().as(ServicePlusImpl.class).getGeneric(1).getType();
+		return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 1);
 	}
 
-	@Override
-	protected ResolvableType getResolvableType() {
-		return ResolvableType.forClass(ClassUtils.getUserClass(getClass()));
+	protected Class<V> currentVoClass() {
+		return (Class<V>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServicePlusImpl.class, 2);
 	}
 
 	/**
-	 * 鍗曟潯鎵ц鎬ц兘宸�
-	 *
-	 * {@link #saveAll(Collection)}
+	 * 鍗曟潯鎵ц鎬ц兘宸� 閫傜敤浜庡垪琛ㄥ璞″唴瀹逛笉纭畾
 	 */
-	@Deprecated
 	@Override
 	public boolean saveBatch(Collection<T> entityList, int batchSize) {
 		return super.saveBatch(entityList, batchSize);
@@ -67,11 +85,8 @@
 	}
 
 	/**
-	 * 鍗曟潯鎵ц鎬ц兘宸�
-	 *
-	 * {@link #saveAll(Collection)}
+	 * 鍗曟潯鎵ц鎬ц兘宸� 閫傜敤浜庡垪琛ㄥ璞″唴瀹逛笉纭畾
 	 */
-	@Deprecated
 	@Override
 	public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
 		return super.saveOrUpdateBatch(entityList, batchSize);
@@ -82,9 +97,145 @@
 		return super.updateBatchById(entityList, batchSize);
 	}
 
+	/**
+	 * 鍗晄ql鎵归噺鎻掑叆( 鍏ㄩ噺濉厖 鏃犺鏁版嵁搴撻粯璁ゅ�� )
+	 * 閫傜敤浜庢棤鑴戞彃鍏�
+	 */
+	@Override
+	public boolean saveBatch(Collection<T> entityList) {
+		return saveBatch(entityList, DEFAULT_BATCH_SIZE);
+	}
+
+	@Override
+	public boolean saveOrUpdateBatch(Collection<T> entityList) {
+		return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
+	}
+
+	@Override
+	public boolean updateBatchById(Collection<T> entityList) {
+		return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
+	}
+
+	/**
+	 * 鍗晄ql鎵归噺鎻掑叆( 鍏ㄩ噺濉厖 )
+	 */
 	@Override
 	public boolean saveAll(Collection<T> entityList) {
+		if (CollUtil.isEmpty(entityList)) {
+			return false;
+		}
 		return baseMapper.insertAll(entityList) == entityList.size();
 	}
 
+	/**
+	 * 鍏ㄩ噺淇濆瓨鎴栨洿鏂� ( 鎸変富閿尯鍒� )
+	 */
+	@Override
+	public boolean saveOrUpdateAll(Collection<T> entityList) {
+		if (CollUtil.isEmpty(entityList)) {
+			return false;
+		}
+		TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
+		Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
+		String keyProperty = tableInfo.getKeyProperty();
+		Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
+		List<T> addList = new ArrayList<>();
+		List<T> updateList = new ArrayList<>();
+		int row = 0;
+		for (T entity : entityList) {
+			Object id = ReflectUtils.invokeGetter(entity, keyProperty);
+			if (ObjectUtil.isNull(id)) {
+				addList.add(entity);
+			} else {
+				updateList.add(entity);
+			}
+		}
+		if (CollUtil.isNotEmpty(updateList) && updateBatchById(updateList)) {
+			row += updateList.size();
+		}
+        if (CollUtil.isNotEmpty(addList)) {
+            row += baseMapper.insertAll(addList);
+        }
+		return row == entityList.size();
+	}
+
+	/**
+	 * 鏍规嵁 ID 鏌ヨ
+	 *
+	 * @param id 涓婚敭ID
+	 */
+	@Override
+	public V getVoById(Serializable id, CopyOptions copyOptions) {
+		T t = getBaseMapper().selectById(id);
+		return BeanCopyUtils.oneCopy(t, copyOptions, voClass);
+	}
+
+	/**
+	 * 鏌ヨ锛堟牴鎹甀D 鎵归噺鏌ヨ锛�
+	 *
+	 * @param idList 涓婚敭ID鍒楄〃
+	 */
+	@Override
+	public List<V> listVoByIds(Collection<? extends Serializable> idList, CopyOptions copyOptions) {
+		List<T> list = getBaseMapper().selectBatchIds(idList);
+		if (list == null) {
+			return null;
+		}
+		return BeanCopyUtils.listCopy(list, copyOptions, voClass);
+	}
+
+	/**
+	 * 鏌ヨ锛堟牴鎹� columnMap 鏉′欢锛�
+	 *
+	 * @param columnMap 琛ㄥ瓧娈� map 瀵硅薄
+	 */
+	@Override
+	public List<V> listVoByMap(Map<String, Object> columnMap, CopyOptions copyOptions) {
+		List<T> list = getBaseMapper().selectByMap(columnMap);
+		if (list == null) {
+			return null;
+		}
+		return BeanCopyUtils.listCopy(list, copyOptions, voClass);
+	}
+
+	/**
+	 * 鏍规嵁 Wrapper锛屾煡璇竴鏉¤褰� <br/>
+	 * <p>缁撴灉闆嗭紝濡傛灉鏄涓細鎶涘嚭寮傚父锛岄殢鏈哄彇涓�鏉″姞涓婇檺鍒舵潯浠� wrapper.last("LIMIT 1")</p>
+	 *
+	 * @param queryWrapper 瀹炰綋瀵硅薄灏佽鎿嶄綔绫� {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
+	 */
+	@Override
+	public V getVoOne(Wrapper<T> queryWrapper, CopyOptions copyOptions) {
+		T t = getOne(queryWrapper, true);
+		return BeanCopyUtils.oneCopy(t, copyOptions, voClass);
+	}
+
+	/**
+	 * 鏌ヨ鍒楄〃
+	 *
+	 * @param queryWrapper 瀹炰綋瀵硅薄灏佽鎿嶄綔绫� {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
+	 */
+	@Override
+	public List<V> listVo(Wrapper<T> queryWrapper, CopyOptions copyOptions) {
+		List<T> list = getBaseMapper().selectList(queryWrapper);
+		if (list == null) {
+			return null;
+		}
+		return BeanCopyUtils.listCopy(list, copyOptions, voClass);
+	}
+
+	/**
+	 * 缈婚〉鏌ヨ
+	 *
+	 * @param page         缈婚〉瀵硅薄
+	 * @param queryWrapper 瀹炰綋瀵硅薄灏佽鎿嶄綔绫�
+	 */
+	@Override
+	public PagePlus<T, V> pageVo(PagePlus<T, V> page, Wrapper<T> queryWrapper, CopyOptions copyOptions) {
+		PagePlus<T, V> result = getBaseMapper().selectPage(page, queryWrapper);
+		List<V> volist = BeanCopyUtils.listCopy(result.getRecords(), copyOptions, voClass);
+		result.setRecordsVo(volist);
+		return result;
+	}
+
 }

--
Gitblit v1.9.3