浏览代码

添加根据应用统计本周,本月用户注册量接口

wangmh 7 年之前
父节点
当前提交
817acb0ed9

+ 49 - 17
sso-server/src/main/java/com/uas/sso/controller/UserManagerController.java

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.*;
 
 import java.io.*;
 import java.text.SimpleDateFormat;
-import java.util.Calendar;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.Set;
 
@@ -429,8 +429,8 @@ public class UserManagerController extends BaseController {
      * 优软云个人用户注册总数
      */
     @RequestMapping(value = "/count", method = RequestMethod.GET)
-    public ModelMap getUserCount() {
-        return new ModelMap("count", userService.getCount());
+    public ModelMap count() {
+        return new ModelMap("count", userService.count());
     }
 
     /**
@@ -438,13 +438,8 @@ public class UserManagerController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/currentMonth/count", method = RequestMethod.GET)
-    public ModelMap getCurrentMonthUserCount() {
-        Calendar start = Calendar.getInstance();
-        start.setTime(new Date());
-        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), 1, 0, 0, 0);
-        Calendar end = Calendar.getInstance();
-        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
-        return new ModelMap("count", userService.getCountByRegisterDate(start, end));
+    public ModelMap countInCurrentMonth() {
+        return new ModelMap("count",  userService.countInCurrentMonth());
     }
 
     /**
@@ -452,12 +447,49 @@ public class UserManagerController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/lastMonth/count", method = RequestMethod.GET)
-    public ModelMap getLastMonthUserCount() {
-        Calendar start = Calendar.getInstance();
-        start.setTime(new Date());
-        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) - 1, 1, 0, 0, 0);
-        Calendar end = Calendar.getInstance();
-        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
-        return new ModelMap("count", userService.getCountByRegisterDate(start, end));
+    public ModelMap countInLastMonth() {
+        return new ModelMap("count",  userService.countInLastMonth());
+    }
+
+    /**
+     * 获取本周用户注册数量
+     * @return
+     */
+    @RequestMapping(value = "/currentWeek/count", method = RequestMethod.GET)
+    public ModelMap countInCurrentWeek() {
+        return new ModelMap("count",  userService.countInCurrentWeek());
+    }
+
+    /**
+     * 获取指定应用注册数量
+     * @param fromApps 应用id,逗号分隔
+     * @return
+     */
+    @RequestMapping(value = "/count/app", method = RequestMethod.GET)
+    public ModelMap count(String fromApps) {
+        String[] apps = fromApps.split(",");
+        return success(userService.count(Arrays.asList(apps)));
+    }
+
+    /**
+     * 获取指定应用本月注册数量
+     * @param fromApps 应用id,逗号分隔
+     * @return
+     */
+    @RequestMapping(value = "/currentMonth/count/app", method = RequestMethod.GET)
+    public ModelMap countInCurrentMonth(String fromApps) {
+        String[] apps = fromApps.split(",");
+        return success(userService.countInCurrentMonth(Arrays.asList(apps)));
+    }
+
+    /**
+     * 获取指定应用本周注册数量
+     * @param fromApps 应用id,逗号分隔
+     * @return
+     */
+    @RequestMapping(value = "/currentWeek/count/app", method = RequestMethod.GET)
+    public ModelMap countInCurrentWeek(String fromApps) {
+        String[] apps = fromApps.split(",");
+        return success(userService.countInCurrentWeek(Arrays.asList(apps)));
     }
 }

+ 15 - 16
sso-server/src/main/java/com/uas/sso/controller/UserspaceManagerController.java

@@ -402,8 +402,8 @@ public class UserspaceManagerController extends BaseController {
      * 企业信息库总数
      */
     @RequestMapping(value = "/count", method = RequestMethod.GET)
-    public ModelMap getEnterpriseCount() {
-        return new ModelMap("count", userspaceService.getCount());
+    public ModelMap count() {
+        return new ModelMap("count", userspaceService.count());
     }
 
     /**
@@ -411,13 +411,8 @@ public class UserspaceManagerController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/currentMonth/count", method = RequestMethod.GET)
-    public ModelMap getCurrentMonthCount() {
-        Calendar start = Calendar.getInstance();
-        start.setTime(new Date());
-        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), 1, 0, 0, 0);
-        Calendar end = Calendar.getInstance();
-        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
-        return new ModelMap("count", userspaceService.getCountByRegisterDate(start, end));
+    public ModelMap countInCurrentMonth() {
+        return new ModelMap("count", userspaceService.countInCurrentMonth());
     }
 
     /**
@@ -425,13 +420,17 @@ public class UserspaceManagerController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/lastMonth/count", method = RequestMethod.GET)
-    public ModelMap getLastMonthEnterpriseCount() {
-        Calendar start = Calendar.getInstance();
-        start.setTime(new Date());
-        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) - 1, 1, 0, 0, 0);
-        Calendar end = Calendar.getInstance();
-        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
-        return new ModelMap("count", userspaceService.getCountByRegisterDate(start, end));
+    public ModelMap countInLastMonth() {
+        return new ModelMap("count", userspaceService.countInLastMonth());
+    }
+
+    /**
+     * 获取上个月企业注册数量
+     * @return
+     */
+    @RequestMapping(value = "/currentWeek/count", method = RequestMethod.GET)
+    public ModelMap countInCurrentWeek() {
+        return new ModelMap("count", userspaceService.countInCurrentWeek());
     }
 
 }

+ 20 - 0
sso-server/src/main/java/com/uas/sso/dao/UserDao.java

@@ -7,7 +7,9 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.query.Param;
 
+import java.sql.Timestamp;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 用户信息dao
@@ -106,4 +108,22 @@ public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExec
      */
     @Query("select u from User u where u.mobile in :tels")
     List<User> findUsersByTels(@Param("tels") List<String> tels);
+
+    /**
+     * 获取指定应用指定时间用户注册量
+     * @param start 开始时间
+     * @param end 结束时间
+     * @param fromApps 应用集合
+     * @return
+     */
+    @Query("select u.fromApp as appId , count(u) as count from User u where u.fromApp in ?3 and u.registerDate between ?1 and ?2 group by u.fromApp")
+    List<Map<String, Object>> getCountByRegisterDate(Timestamp start, Timestamp end, List<String> fromApps);
+
+    /**
+     * 获取指定应用所有用户注册量
+     * @param fromApps 应用集合
+     * @return
+     */
+    @Query("select u.fromApp as appId , count(u) as count from User u where u.fromApp in ?1 group by u.fromApp")
+    List<Map<String,Object>> getCountByRegisterDate(List<String> fromApps);
 }

+ 19 - 0
sso-server/src/main/java/com/uas/sso/i/CountCallBack.java

@@ -0,0 +1,19 @@
+package com.uas.sso.i;
+
+import java.util.Calendar;
+
+/**
+ * @author wangmh
+ * @create 2018-06-13 10:15
+ * @desc 统计接口
+ **/
+public interface CountCallBack<T> {
+
+    /**
+     * 接口回调,根据时间统计
+     * @return
+     * @param start 开始时间
+     * @param end 结束时间
+     */
+    T countByTime(Calendar start, Calendar end);
+}

+ 53 - 2
sso-server/src/main/java/com/uas/sso/service/UserService.java

@@ -6,8 +6,10 @@ import com.uas.sso.entity.UserRecord;
 import com.uas.sso.entity.UserSpaceDetailInfo;
 import org.springframework.data.domain.Page;
 
+import java.sql.Timestamp;
 import java.util.Calendar;
 import java.util.List;
+import java.util.Map;
 
 /**
  * 用户信息service
@@ -259,7 +261,7 @@ public interface UserService {
      * 获取用户数量
      * @return
      */
-    long getCount();
+    long count();
 
     /**
      * 统计某个时间段注册数量
@@ -267,5 +269,54 @@ public interface UserService {
      * @param end 结束时间
      * @return
      */
-    long getCountByRegisterDate(Calendar start, Calendar end);
+    long countByRegisterDate(Calendar start, Calendar end);
+
+    /**
+     * 统计某个时间段指定应用注册数量
+     * @param start 开始时间
+     * @param end 结束时间
+     * @param fromApps 指定应用集合
+     * @return
+     */
+    Map<String, Long> countByRegisterDate(Timestamp start, Timestamp end, List<String> fromApps);
+
+    /**
+     * 获取当前月注册数量
+     * @return
+     */
+    long countInCurrentMonth();
+
+    /**
+     * 获取上个月注册数量
+     * @return
+     */
+    long countInLastMonth();
+
+    /**
+     * 获取本周注册数量
+     * @return
+     */
+    long countInCurrentWeek();
+
+    /**
+     * 获取指定应用注册量
+     * @param fromApps 应用id集合
+     * @return key: appId, value: count
+     */
+    Map<String, Long> count(List<String> fromApps);
+
+    /**
+     * 获取指定应用本月注册量
+     * @param fromApps 应用id集合
+     * @return key: appId, value: count
+     */
+    Map<String, Long> countInCurrentMonth(List<String> fromApps);
+
+    /**
+     * 获取指定应用本周注册量
+     * @param fromApps 应用id集合
+     * @return key: appId, value: count
+     */
+    Map<String, Long> countInCurrentWeek(List<String> fromApps);
+
 }

+ 19 - 1
sso-server/src/main/java/com/uas/sso/service/UserspaceService.java

@@ -169,7 +169,7 @@ public interface UserspaceService {
      * 获取企业数量
      * @return
      */
-    long getCount();
+    long count();
 
     /**
      * 根据注册时间统计企业注册数量
@@ -178,4 +178,22 @@ public interface UserspaceService {
      * @return
      */
     long getCountByRegisterDate(Calendar start, Calendar end);
+
+    /**
+     * 获取当前月企业注册量
+     * @return
+     */
+    long countInCurrentMonth();
+
+    /**
+     * 获取上个月企业注册量
+     * @return
+     */
+    long countInLastMonth();
+
+    /**
+     * 获取本周
+     * @return
+     */
+    long countInCurrentWeek();
 }

+ 79 - 4
sso-server/src/main/java/com/uas/sso/service/impl/UserServiceImpl.java

@@ -5,20 +5,20 @@ import com.alibaba.fastjson.JSONObject;
 import com.uas.sso.common.encrypt.MD5;
 import com.uas.sso.common.util.HttpUtil;
 import com.uas.sso.core.Const;
-import com.uas.sso.core.ICallable;
 import com.uas.sso.core.Status;
 import com.uas.sso.core.Type;
 import com.uas.sso.dao.UserDao;
 import com.uas.sso.dao.UserRecordDao;
 import com.uas.sso.entity.*;
 import com.uas.sso.exception.VisibleError;
+import com.uas.sso.i.CountCallBack;
 import com.uas.sso.logging.LoggerManager;
 import com.uas.sso.logging.SyncBufferedLogger;
 import com.uas.sso.logging.UserBufferedLogger;
 import com.uas.sso.service.*;
 import com.uas.sso.support.SyncFail;
 import com.uas.sso.util.AccountTypeUtils;
-import com.uas.sso.util.ExecuteUtils;
+import com.uas.sso.util.CountUtils;
 import com.uas.sso.util.PasswordLevelUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -604,12 +604,12 @@ public class UserServiceImpl implements UserService {
     }
 
     @Override
-    public long getCount() {
+    public long count() {
         return userDao.count();
     }
 
     @Override
-    public long getCountByRegisterDate(final Calendar start, final Calendar end) {
+    public long countByRegisterDate(final Calendar start, final Calendar end) {
         return userDao.count(new Specification<User>() {
             @Override
             public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
@@ -620,6 +620,81 @@ public class UserServiceImpl implements UserService {
         });
     }
 
+    @Override
+    public Map<String, Long> countByRegisterDate(Timestamp start, Timestamp end, List<String> fromApps) {
+        Map<String, Long> data = new HashMap<>();
+        List<Map<String, Object>> counts = userDao.getCountByRegisterDate(start, end, fromApps);
+        for (Map<String, Object> count : counts) {
+            data.put((String) count.get("appId"), (Long) count.get("count"));
+        }
+        return data;
+    }
+
+    @Override
+    public long countInCurrentMonth() {
+        return CountUtils.countInCurrentMonth(new CountCallBack<Long>() {
+
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return countByRegisterDate(start, end);
+            }
+        });
+    }
+
+    @Override
+    public long countInLastMonth() {
+        return CountUtils.countInLastMonth(new CountCallBack<Long>() {
+
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return countByRegisterDate(start, end);
+            }
+        });
+    }
+
+    @Override
+    public long countInCurrentWeek() {
+        return CountUtils.countInCurrentWeek(new CountCallBack<Long>() {
+
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return countByRegisterDate(start, end);
+            }
+        });
+    }
+
+    @Override
+    public Map<String, Long> count(final List<String> fromApps) {
+        Map<String, Long> data = new HashMap<>();
+        List<Map<String, Object>> counts = userDao.getCountByRegisterDate(fromApps);
+        for (Map<String, Object> count : counts) {
+            data.put((String) count.get("appId"), (Long) count.get("count"));
+        }
+        return data;
+    }
+
+    @Override
+    public Map<String, Long> countInCurrentMonth(final List<String> fromApps) {
+        return CountUtils.countInCurrentMonth(new CountCallBack<Map<String, Long>>() {
+
+            @Override
+            public Map<String, Long> countByTime(Calendar start, Calendar end) {
+                return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
+            }
+        });
+    }
+
+    @Override
+    public Map<String, Long> countInCurrentWeek(final List<String> fromApps) {
+        return CountUtils.countInCurrentWeek(new CountCallBack<Map<String, Long>>() {
+
+            @Override
+            public Map<String, Long> countByTime(Calendar start, Calendar end) {
+                return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
+            }
+        });
+    }
+
     /**
      * 同步用户信息到各个应用
      * @param user 用户信息

+ 34 - 1
sso-server/src/main/java/com/uas/sso/service/impl/UserspaceServiceImpl.java

@@ -7,11 +7,13 @@ import com.uas.sso.core.Status;
 import com.uas.sso.dao.UserspaceDao;
 import com.uas.sso.entity.*;
 import com.uas.sso.exception.VisibleError;
+import com.uas.sso.i.CountCallBack;
 import com.uas.sso.logging.LoggerManager;
 import com.uas.sso.logging.SyncBufferedLogger;
 import com.uas.sso.service.*;
 import com.uas.sso.support.SyncFail;
 import com.uas.sso.util.ChineseUtils;
+import com.uas.sso.util.CountUtils;
 import com.uas.sso.util.StringUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -389,7 +391,7 @@ public class UserspaceServiceImpl implements UserspaceService {
     }
 
     @Override
-    public long getCount() {
+    public long count() {
         return userspaceDao.count();
     }
 
@@ -404,4 +406,35 @@ public class UserspaceServiceImpl implements UserspaceService {
             }
         });
     }
+
+    @Override
+    public long countInCurrentMonth() {
+        return CountUtils.countInCurrentMonth(new CountCallBack<Long>() {
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return getCountByRegisterDate(start, end);
+            }
+        });
+    }
+
+    @Override
+    public long countInLastMonth() {
+        return CountUtils.countInLastMonth(new CountCallBack<Long>() {
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return getCountByRegisterDate(start, end);
+            }
+        });
+    }
+
+    @Override
+    public long countInCurrentWeek() {
+        return CountUtils.countInCurrentWeek(new CountCallBack<Long>() {
+            @Override
+            public Long countByTime(Calendar start, Calendar end) {
+                return getCountByRegisterDate(start, end);
+            }
+        });
+    }
+
 }

+ 66 - 0
sso-server/src/main/java/com/uas/sso/util/CountUtils.java

@@ -0,0 +1,66 @@
+package com.uas.sso.util;
+
+import com.uas.sso.exception.VisibleError;
+import com.uas.sso.i.CountCallBack;
+
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @author wangmh
+ * @create 2018-06-13 10:13
+ * @desc 统计工具类
+ **/
+public class CountUtils {
+
+    /**
+     * 获取当前月的数量
+     * @param countCallBack 用于给调用者自定义统计方法
+     * @return
+     */
+    public static <T> T countInCurrentMonth(CountCallBack<T> countCallBack) {
+        Calendar start = Calendar.getInstance();
+        start.setTime(new Date());
+        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), 1, 0, 0, 0);
+        Calendar end = Calendar.getInstance();
+        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
+        return countCallBack.countByTime(start, end);
+    }
+
+    /**
+     * 获取上个月的数量
+     * @param countCallBack 用于给调用者自定义统计方法
+     * @return
+     */
+    public static <T> T countInLastMonth(CountCallBack<T> countCallBack) {
+        Calendar start = Calendar.getInstance();
+        start.setTime(new Date());
+        start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) - 1, 1, 0, 0, 0);
+        Calendar end = Calendar.getInstance();
+        end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH) + 1, 1, 0, 0, 0);
+        return countCallBack.countByTime(start, end);
+    }
+
+    /**
+     * 获取上个月的数量
+     * @param countCallBack 用于给调用者自定义统计方法
+     * @return
+     */
+    public static <T> T countInCurrentWeek(CountCallBack<T> countCallBack) {
+        Date date = new Date();
+        if (date == null) {
+            throw new VisibleError("获取当前时间失败");
+        }
+        Calendar start = Calendar.getInstance();
+        start.setTime(date);
+        int dayofweek = start.get(Calendar.DAY_OF_WEEK);
+        if (dayofweek == 1) {
+            dayofweek += 7;
+        }
+        start.add(Calendar.DATE, 2 - dayofweek);
+        Calendar end = Calendar.getInstance();
+        end.setTime(start.getTime());
+        end.add(Calendar.DAY_OF_WEEK, 6);
+        return countCallBack.countByTime(start, end);
+    }
+}