Bläddra i källkod

修复导出人群时,缺少筛选条件的bug

William 2 år sedan
förälder
incheckning
5b35abec9e

+ 6 - 13
src/main/java/org/springblade/useranalysis/mapper/clickhouse/UserTagsMapper.java

@@ -222,9 +222,7 @@ public interface UserTagsMapper extends BaseMapper<ZUserTagEntity> {
 	 * 人群导出新方法(关联BIZ_USER_GROUP_USERID表)
 	 * 
 	 * @param fields     要查询的字段
-	 * @param groupId    人群ID
-	 * @param monthStart 开始月份
-	 * @param monthEnd   结束月份
+	 * @param whereWrapper 查询条件
 	 * @param sqlLimit   分页子语句
 	 * @return
 	 */
@@ -232,18 +230,13 @@ public interface UserTagsMapper extends BaseMapper<ZUserTagEntity> {
 			+ "	${fields}\r\n"
 			+ "FROM\r\n"
 			+ "	Z_USER_TAG_FLAT_ALL tag\r\n"
-			+ "GLOBAL INNER JOIN BIZ_USER_GROUP_USERID ur ON\r\n"
+			+ " GLOBAL INNER JOIN BIZ_USER_GROUP_USERID ur ON\r\n"
 			+ "	tag.USERID_V = ur.user_id\r\n"
-			+ "WHERE\r\n"
-			+ "	ur.user_group_id = #{groupId}\r\n"
-			+ "	and tag.THEMONTH_V >= #{monthStart}\r\n"
-			+ "	and tag.THEMONTH_V <=#{monthEnd}\r\n"
-			+ "ORDER BY tag.USERID_V ASC\r\n"
-			+ "${sqlLimit}")
+			+ " ${ew.customSqlSegment}\r\n"
+			+ " ORDER BY tag.USERID_V ASC\r\n"
+			+ " ${sqlLimit}")
 	List<Map<String, Object>> exportUserGroup(
 			@Param("fields") String fields,
-			@Param("groupId") String groupId,
-			@Param("monthStart") String monthStart,
-			@Param("monthEnd") String monthEnd,
+			@Param(Constants.WRAPPER) QueryWrapper<ZUserTagEntity> whereWrapper,
 			@Param("sqlLimit") String sqlLimit);
 }

+ 2 - 4
src/main/java/org/springblade/useranalysis/service/UserTagsService.java

@@ -49,11 +49,9 @@ public interface UserTagsService extends IService<ZUserTagEntity> {
 	 * 人群导出新方法(关联BIZ_USER_GROUP_USERID表)
 	 * 
 	 * @param fields     要查询的字段
-	 * @param groupId    人群ID
-	 * @param monthStart 开始月份
-	 * @param monthEnd   结束月份
+	 * @param whereWrapper 查询条件
 	 * @param sqlLimit   分页子语句
 	 * @return
 	 */
-	public List<Map<String, Object>> exportUserGroup(String fields,String groupId,String monthStart,String monthEnd,String sqlLimit);
+	public List<Map<String, Object>> exportUserGroup(String fields,QueryWrapper<ZUserTagEntity> whereWrapper,String sqlLimit);
 }

+ 3 - 6
src/main/java/org/springblade/useranalysis/service/impl/UserTagsServiceImpl.java

@@ -11,7 +11,6 @@ import java.util.Set;
 import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.ibatis.annotations.Param;
 import org.springblade.useranalysis.dto.TagHistory;
 import org.springblade.useranalysis.entity.AdsUserprofileTag;
 import org.springblade.useranalysis.entity.TagColumn;
@@ -294,14 +293,12 @@ public class UserTagsServiceImpl extends ServiceImpl<UserTagsMapper, ZUserTagEnt
 	 * 人群导出新方法(关联BIZ_USER_GROUP_USERID表)
 	 * 
 	 * @param fields     要查询的字段
-	 * @param groupId    人群ID
-	 * @param monthStart 开始月份
-	 * @param monthEnd   结束月份
+	 * @param whereWrapper 查询条件
 	 * @param sqlLimit   分页子语句
 	 * @return
 	 */
-	public List<Map<String, Object>> exportUserGroup(String fields,String groupId,String monthStart,String monthEnd,String sqlLimit) {
-		return baseMapper.exportUserGroup(fields, groupId, monthStart, monthEnd, sqlLimit);
+	public List<Map<String, Object>> exportUserGroup(String fields,QueryWrapper<ZUserTagEntity> whereWrapper,String sqlLimit) {
+		return baseMapper.exportUserGroup(fields, whereWrapper, sqlLimit);
 	}
 
 	@Override

+ 34 - 25
src/main/java/org/springblade/useranalysis/service/normal/ExcelService.java

@@ -18,6 +18,7 @@ import org.springblade.useranalysis.dto.param.ExcelExportParam;
 import org.springblade.useranalysis.entity.TagColumn;
 import org.springblade.useranalysis.entity.UserGroupEntity;
 import org.springblade.useranalysis.entity.UserProfileExcelLog;
+import org.springblade.useranalysis.entity.clickhouse.ZUserTagEntity;
 import org.springblade.useranalysis.service.UserProfileService;
 import org.springblade.useranalysis.service.UserTagsService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -28,6 +29,7 @@ import com.alibaba.excel.ExcelWriter;
 import com.alibaba.excel.converters.longconverter.LongStringConverter;
 import com.alibaba.excel.util.FileUtils;
 import com.alibaba.fastjson2.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.github.xiaoymin.knife4j.core.util.StrUtil;
 
 import lombok.extern.slf4j.Slf4j;
@@ -50,6 +52,9 @@ public class ExcelService {
 
 	@Autowired
 	private UserProfileService userProfileService;
+	
+	@Autowired
+	private UserGroupRuleService userGroupRuleService;
 
 	final ExecutorService threadPool = new ThreadPoolExecutor(1, 2, 5, TimeUnit.MINUTES,
 			// 有界队列5000,超过队列长度,无异常抛弃
@@ -68,8 +73,31 @@ public class ExcelService {
 			log.info(excelLog.getId() + " - exporting......");
 			long start = System.currentTimeMillis();
 			try {
+				String groupId = params.getGroupId();
+				// 使用前端传递的日期范围
+//				String monthStart = params.getMonthStart();
+//				String monthEnd = params.getMonthEnd();
+
+				// 使用圈定人群的日期范围
+//				String monthStart = "";
+//				String monthEnd = "";
+//				JSONObject rule = JSON.parseObject(userGroup.getRules());
+//				if (rule != null) {
+//					if (rule.containsKey("daterange")) {
+//						JSONArray daterange = rule.getJSONArray("daterange");
+//						if (daterange != null && daterange.size() == 2) {
+//							monthStart = daterange.getString(0);
+//							monthEnd = daterange.getString(1);
+//						}
+//					}
+//				}
+				QueryWrapper<ZUserTagEntity> whereWrapper = userGroupRuleService.parse(userGroup);
+				whereWrapper.eq("ur.user_group_id ", groupId);
+				//whereWrapper解析后已经包含日期范围,所以不需要再添加时间,只需要追加人群id即可
+//				whereWrapper.ge("tag.THEMONTH_V", monthStart);
+//				whereWrapper.le("tag.THEMONTH_V", monthEnd);
 				ConcurrentHashMap<String, String> mobileMap = new ConcurrentHashMap<>();
-				boolean isSuccess = handle(mobileMap, params, excelLog, userGroup);
+				boolean isSuccess = handle(mobileMap, params, excelLog, userGroup, whereWrapper);
 				if (isSuccess) {
 					excelLog.setCount(params.getCount());
 					excelLog.setStatus(UserProfileExcelLogService.STATUS_SUCCESS);
@@ -101,7 +129,7 @@ public class ExcelService {
 	}
 
 	private boolean handle(ConcurrentHashMap<String, String> mobileMap, ExcelExportParam params,
-			UserProfileExcelLog excelLog, UserGroupEntity userGroup) {
+			UserProfileExcelLog excelLog, UserGroupEntity userGroup, QueryWrapper<ZUserTagEntity> whereWrapper) {
 		boolean isSuccess = true;
 		List<String> codeList = params.getTagList();
 		boolean desens = params.isDesens();
@@ -124,31 +152,13 @@ public class ExcelService {
 				.registerConverter(new LongStringConverter())
 				.head(buidlHeader(columnList))
 				.build();
-		String groupId = params.getGroupId();
-		// 使用前端传递的日期范围
-		String monthStart = params.getMonthStart();
-		String monthEnd = params.getMonthEnd();
-
-		// 使用圈定人群的日期范围
-		// String monthStart = "";
-		// String monthEnd = "";
-		// JSONObject rule = JSON.parseObject(userGroup.getRules());
-		// if (rule != null) {
-		// if (rule.containsKey("daterange")) {
-		// JSONArray daterange = rule.getJSONArray("daterange");
-		// if (daterange != null && daterange.size() == 2) {
-		// monthStart = daterange.getString(0);
-		// monthEnd = daterange.getString(1);
-		// }
-		// }
-		// }
 		// 需要导出的数量
 		int count = params.getCount();
 		log.info("export count need is :" + count);
-		
+
 		// 已经导出的数量
 		int exportedCountTotal = 0;
-		
+
 		// 下次查询偏移量
 		int offset = 0;
 
@@ -158,7 +168,7 @@ public class ExcelService {
 			List<Map<String, Object>> tagList = null;
 			try {
 				log.info("export limit sql is :" + sqlLimit);
-				tagList = userTagsService.exportUserGroup(fields.toString(), groupId, monthStart, monthEnd, sqlLimit);
+				tagList = userTagsService.exportUserGroup(fields.toString(), whereWrapper, sqlLimit);
 			} catch (Exception e) {
 				log.error("ExcelService handle fail, the error is " + e.getMessage());
 				isSuccess = false;
@@ -192,7 +202,6 @@ public class ExcelService {
 		return isSuccess;
 	}
 
-
 	/**
 	 * 结果导出到excel文件中
 	 * 
@@ -263,7 +272,7 @@ public class ExcelService {
 		excelWriter.write(list, EasyExcel.writerSheet(groupName).build());
 		return count;
 	}
-	
+
 	/**
 	 * 动态生成表头
 	 */