Procházet zdrojové kódy

Merge remote-tracking branch 'origin/master'

周箭河 před 4 roky
rodič
revize
a915f93952

+ 1 - 5
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/IndexBaseMonthDataDao.java

@@ -40,22 +40,18 @@ public interface IndexBaseMonthDataDao extends BaseDAO<Long, IndexBaseMonthData>
      * @author Joburgess
      * @date 2021/1/7 0007
      * @param startMonth:
-     * @param endMonth:
      * @return java.util.List<com.ym.mec.biz.dal.dto.IndexBaseMonthDto>
      */
-    List<IndexBaseMonthData> getStudentSignUpData(@Param("startMonth") String startMonth,
-                                                  @Param("endMonth") String endMonth);
+    List<IndexBaseMonthData> getStudentSignUpData(@Param("startMonth") String startMonth);
 
     /**
      * @describe 统计作业布置数据
      * @author Joburgess
      * @date 2021/1/7 0007
      * @param startMonth:
-     * @param endMonth:
      * @return java.util.List<com.ym.mec.biz.dal.dto.IndexBaseMonthDto>
      */
     List<IndexBaseMonthData> getHomeworkDate(@Param("startMonth") String startMonth,
-                                             @Param("endMonth") String endMonth,
                                              @Param("type") String type);
 
 }

+ 11 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/IndexBaseMonthData.java

@@ -34,6 +34,17 @@ public class IndexBaseMonthData {
 
 	private Date updateTime;
 
+	public IndexBaseMonthData() {
+	}
+
+	public IndexBaseMonthData(Date month, Integer organId) {
+		this.month = month;
+		this.organId = organId;
+		this.totalNum = BigDecimal.ZERO;
+		this.activateNum = BigDecimal.ZERO;
+		this.percent = BigDecimal.ZERO;
+	}
+
 	public Long getId() {
 		return id;
 	}

+ 91 - 36
mec-biz/src/main/java/com/ym/mec/biz/service/impl/IndexBaseMonthDataServiceImpl.java

@@ -1,6 +1,8 @@
 package com.ym.mec.biz.service.impl;
 
+import com.ym.mec.biz.dal.dao.OrganizationDao;
 import com.ym.mec.biz.dal.dto.IndexBaseDto;
+import com.ym.mec.biz.dal.entity.Organization;
 import com.ym.mec.biz.dal.enums.IndexDataType;
 import com.ym.mec.common.dal.BaseDAO;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
@@ -11,9 +13,12 @@ import com.ym.mec.biz.dal.entity.IndexBaseMonthData;
 import com.ym.mec.biz.service.IndexBaseMonthDataService;
 import com.ym.mec.biz.dal.dao.IndexBaseMonthDataDao;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
+import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -22,6 +27,15 @@ public class IndexBaseMonthDataServiceImpl extends BaseServiceImpl<Long, IndexBa
 	
 	@Autowired
 	private IndexBaseMonthDataDao indexBaseMonthDataDao;
+	@Autowired
+	private OrganizationDao organizationDao;
+
+	private static ThreadLocal<Set<Integer>> organIds = new ThreadLocal<Set<Integer>>(){
+		@Override
+		protected Set<Integer> initialValue() {
+			return new HashSet<>();
+		}
+	};
 
 	@Override
 	public BaseDAO<Long, IndexBaseMonthData> getDAO() {
@@ -46,7 +60,6 @@ public class IndexBaseMonthDataServiceImpl extends BaseServiceImpl<Long, IndexBa
 			dataTypes = Arrays.stream(dataTypesStr.split(",")).collect(Collectors.toSet());
 		}
 
-
 		List<IndexBaseMonthData> indexBaseDatas = indexBaseMonthDataDao.getIndexBaseData(organIds, dataTypes, startMonth, endMonth);
 		if(CollectionUtils.isEmpty(indexBaseDatas)){
 			return result;
@@ -63,56 +76,98 @@ public class IndexBaseMonthDataServiceImpl extends BaseServiceImpl<Long, IndexBa
 	}
 
 	@Override
+	@Transactional(rollbackFor = Exception.class)
 	public Map<String, List<IndexBaseDto>> indexBaseDataTask(String startMonth, String endMonth) {
 		Map<String, List<IndexBaseDto>> result = new HashMap<>();
 
-		if(StringUtils.isBlank(startMonth)){
-			LocalDateTime nowDateTime = LocalDateTime.now();
-			startMonth = nowDateTime.getYear() + "-01";
-			endMonth = null;
+		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM");
+		LocalDate nowDate = LocalDate.now();
+		LocalDate startDate = nowDate.withMonth(1).withDayOfMonth(1);
+		LocalDate endDate = nowDate.withDayOfMonth(1);
+		if(StringUtils.isNotBlank(startMonth)){
+			startDate = LocalDate.parse(startMonth+"-01", DateUtil.dateFormatter);
+		}
+
+		if(StringUtils.isNotBlank(endMonth)){
+			endDate = LocalDate.parse(endMonth+"-01", DateUtil.dateFormatter);
 		}
 
-		countUserSignUpDate(startMonth, endMonth);
+		List<Organization> allOrgans = organizationDao.findAllOrgans();
+		Set<Integer> organIds = allOrgans.stream().map(Organization::getId).collect(Collectors.toSet());
+		this.organIds.get().clear();
+		this.organIds.get().addAll(organIds);
+
+		while (startDate.compareTo(endDate)<=0){
+			countUserSignUpData(df.format(startDate));
+			countHomeworkData(startMonth);
 
-		countHomeworkDate(startMonth, endMonth);
+			startDate = startDate.plusMonths(1);
+		}
 
 		return result;
 	}
 
-	private void countUserSignUpDate(String startMonth, String endMonth){
-		List<IndexBaseMonthData> dataList = indexBaseMonthDataDao.getStudentSignUpData(startMonth, endMonth);
-		if(CollectionUtils.isEmpty(dataList)){
-			return;
+	/**
+	 * @describe 处理并保存每月数据
+	 * @author Joburgess
+	 * @date 2021/1/11 0011
+	 * @return void
+	 */
+	private void saveData(List<IndexBaseMonthData> dataList, String startMonth, IndexDataType indexDataType){
+		startMonth = startMonth+"-01";
+		Date date = DateUtil.stringToDate(startMonth, "yyyy-MM-dd");
+		if(Objects.isNull(dataList)){
+			dataList = new ArrayList<>();
 		}
-		Set<String> months = dataList.stream().map(s -> DateUtil.dateToString(s.getMonth(), "yyyy-MM-dd")).collect(Collectors.toSet());
-		indexBaseMonthDataDao.deleteWithMonthAndType(months, IndexDataType.ACTIVATION_RATE);
-		indexBaseMonthDataDao.batchInsertWithDataType(dataList, IndexDataType.ACTIVATION_RATE);
+		Set<Integer> hasOrganIds = dataList.stream().map(IndexBaseMonthData::getOrganId).collect(Collectors.toSet());
+		for (Integer organId : this.organIds.get()) {
+			if(hasOrganIds.contains(organId)){
+				continue;
+			}
+			dataList.add(new IndexBaseMonthData(date, organId));
+		}
+		indexBaseMonthDataDao.deleteWithMonthAndType(Arrays.asList(startMonth), indexDataType);
+		indexBaseMonthDataDao.batchInsertWithDataType(dataList, indexDataType);
 	}
 
-	private void countHomeworkDate(String startMonth, String endMonth){
-		List<IndexBaseMonthData> dataList = indexBaseMonthDataDao.getHomeworkDate(startMonth, endMonth, null);
-		if(CollectionUtils.isEmpty(dataList)){
-			return;
-		}
-		Set<String> months = dataList.stream().map(s -> DateUtil.dateToString(s.getMonth(), "yyyy-MM-dd")).collect(Collectors.toSet());
-		indexBaseMonthDataDao.deleteWithMonthAndType(months, IndexDataType.HOMEWORK_CREATE_RATE);
-		indexBaseMonthDataDao.batchInsertWithDataType(dataList, IndexDataType.HOMEWORK_CREATE_RATE);
+	/**
+	 * @describe 激活率
+	 * @author Joburgess
+	 * @date 2021/1/11 0011
+	 * @param startMonth:
+	 * @return void
+	 */
+	private void countUserSignUpData(String startMonth){
+		List<IndexBaseMonthData> dataList = indexBaseMonthDataDao.getStudentSignUpData(startMonth);
+		saveData(dataList, startMonth, IndexDataType.ACTIVATION_RATE);
+	}
 
-		List<IndexBaseMonthData> dataList1 = indexBaseMonthDataDao.getHomeworkDate(startMonth, endMonth, "submit");
-		if(CollectionUtils.isEmpty(dataList1)){
-			return;
-		}
-		Set<String> months1 = dataList1.stream().map(s -> DateUtil.dateToString(s.getMonth(), "yyyy-MM-dd")).collect(Collectors.toSet());
-		indexBaseMonthDataDao.deleteWithMonthAndType(months1, IndexDataType.HOMEWORK_SUBMIT_RATE);
-		indexBaseMonthDataDao.batchInsertWithDataType(dataList1, IndexDataType.HOMEWORK_SUBMIT_RATE);
+	/**
+	 * @describe 作业数据
+	 * @author Joburgess
+	 * @date 2021/1/11 0011
+	 * @param startMonth:
+	 * @return void
+	 */
+	private void countHomeworkData(String startMonth){
+		List<IndexBaseMonthData> dataList = indexBaseMonthDataDao.getHomeworkDate(startMonth, null);
+		saveData(dataList, startMonth, IndexDataType.HOMEWORK_CREATE_RATE);
+
+		List<IndexBaseMonthData> dataList1 = indexBaseMonthDataDao.getHomeworkDate(startMonth, "submit");
+		saveData(dataList1, startMonth, IndexDataType.HOMEWORK_SUBMIT_RATE);
+
+		List<IndexBaseMonthData> dataList2 = indexBaseMonthDataDao.getHomeworkDate(startMonth, "comment");
+		saveData(dataList2, startMonth, IndexDataType.HOMEWORK_COMMENT_RATE);
+	}
 
+	/**
+	 * @describe 统计合作单位数据
+	 * @author Joburgess
+	 * @date 2021/1/11 0011
+	 * @param startMonth:
+	 * @return void
+	 */
+	private void countSchoolData(String startMonth){
 
-		List<IndexBaseMonthData> dataList2 = indexBaseMonthDataDao.getHomeworkDate(startMonth, endMonth, "comment");
-		if(CollectionUtils.isEmpty(dataList2)){
-			return;
-		}
-		Set<String> months2 = dataList2.stream().map(s -> DateUtil.dateToString(s.getMonth(), "yyyy-MM-dd")).collect(Collectors.toSet());
-		indexBaseMonthDataDao.deleteWithMonthAndType(months2, IndexDataType.HOMEWORK_COMMENT_RATE);
-		indexBaseMonthDataDao.batchInsertWithDataType(dataList2, IndexDataType.HOMEWORK_COMMENT_RATE);
 	}
 }

+ 7 - 12
mec-biz/src/main/resources/config/mybatis/IndexBaseMonthDataMapper.xml

@@ -139,28 +139,26 @@
 	<select id="getStudentSignUpData" resultMap="IndexBaseMonthData">
 		SELECT
 			organ_id_,
-			CONCAT(DATE_FORMAT( create_time_, '%Y-%m' ), '-01') month_,
+			CONCAT( #{startMonth}, '-01' ) month_,
 			COUNT( id_ ) total_num_,
 			COUNT(CASE WHEN password_ IS NOT NULL THEN id_ ELSE NULL END) activate_num_,
 			TRUNCATE(COUNT(CASE WHEN password_ IS NOT NULL THEN id_ ELSE NULL END)/COUNT( id_ )*100, 2) percent_
 		FROM sys_user
 		WHERE
 			del_flag_=0
+		  	AND organ_id_ IS NOT NULL
 			AND user_type_ LIKE '%STUDENT%'
 			<if test="startMonth!=null and startMonth!=''">
-				AND DATE_FORMAT(create_time_, '%Y-%m')&gt;=#{startMonth}
+				AND DATE_FORMAT(create_time_, '%Y-%m')=#{startMonth}
 			</if>
-			<if test="endMonth!=null and endMonth!=''">
-				AND DATE_FORMAT(create_time_, '%Y-%m')&lt;=#{endMonth}
-			</if>
-		GROUP BY organ_id_,month_
+		GROUP BY organ_id_
 		ORDER BY organ_id_,month_;
 	</select>
 
 	<select id="getHomeworkDate" resultMap="IndexBaseMonthData">
 		SELECT
 			su.organ_id_,
-			CONCAT(DATE_FORMAT(sees.monday_, '%Y-%m'), '-01') month_,
+			CONCAT( #{startMonth}, '-01' ) month_,
 			<choose>
 				<when test="type == 'submit'">
 					SUM(sees.expect_exercises_num_) total_num_,
@@ -183,12 +181,9 @@
 		WHERE su.del_flag_=0
 			AND su.organ_id_ IS NOT NULL
 			<if test="startMonth!=null and startMonth!=''">
-				AND DATE_FORMAT(sees.monday_, '%Y-%m')&gt;=#{startMonth}
-			</if>
-			<if test="endMonth!=null and endMonth!=''">
-				AND DATE_FORMAT(sees.monday_, '%Y-%m')&lt;=#{endMonth}
+				AND DATE_FORMAT(sees.monday_, '%Y-%m')=#{startMonth}
 			</if>
-		GROUP BY su.organ_id_,month_
+		GROUP BY su.organ_id_
 		ORDER BY su.organ_id_,month_
 	</select>
 </mapper>