Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

73 строки
2.7 KiB

  1. package com.ffii.fpsms.modules.usage;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.time.LocalDate;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.transaction.annotation.Isolation;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import com.ffii.core.support.AbstractService;
  11. import com.ffii.core.support.JdbcDao;
  12. import com.ffii.fpsms.modules.user.entity.User;
  13. @Service
  14. public class FeatureUsageLogService extends AbstractService {
  15. public static final String FEATURE_REPORT_MANAGEMENT = "REPORT_MANAGEMENT";
  16. public static final String FEATURE_TRUCK_ROUTING_SUMMARY = "TRUCK_ROUTING_SUMMARY";
  17. public static final String ACTION_PAGE_VIEW = "PAGE_VIEW";
  18. public static final String ACTION_DOWNLOAD = "DOWNLOAD";
  19. public static final String ACTION_PRINT = "PRINT";
  20. public FeatureUsageLogService(JdbcDao jdbcDao) {
  21. super(jdbcDao);
  22. }
  23. @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class, readOnly = false)
  24. public void insert(User user, String featureCode, String actionType, String detail) {
  25. String sql = "INSERT INTO feature_usage_log (`user_id`, `username`, `feature_code`, `action_type`, `detail`) "
  26. + "VALUES (:userId, :username, :featureCode, :actionType, :detail)";
  27. Map<String, Object> args = new HashMap<>(8);
  28. args.put("userId", user.getId());
  29. args.put("username", user.getUsername());
  30. args.put("featureCode", featureCode);
  31. args.put("actionType", actionType);
  32. args.put("detail", StringUtils.isBlank(detail) ? null : StringUtils.left(detail, 512));
  33. jdbcDao.executeUpdate(sql, args);
  34. }
  35. @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class, readOnly = true)
  36. public List<Map<String, Object>> summarizeByFeature(String featureCode, LocalDate startDate, LocalDate endDate) {
  37. StringBuilder sql = new StringBuilder("""
  38. SELECT user_id AS userId, username,
  39. SUM(CASE WHEN action_type = 'PAGE_VIEW' THEN 1 ELSE 0 END) AS pageViews,
  40. SUM(CASE WHEN action_type = 'DOWNLOAD' THEN 1 ELSE 0 END) AS downloads,
  41. SUM(CASE WHEN action_type = 'PRINT' THEN 1 ELSE 0 END) AS prints
  42. FROM feature_usage_log
  43. WHERE feature_code = :featureCode
  44. """);
  45. Map<String, Object> args = new HashMap<>(4);
  46. args.put("featureCode", featureCode);
  47. if (startDate != null) {
  48. sql.append(" AND created_at >= :startDate");
  49. args.put("startDate", startDate.atStartOfDay());
  50. }
  51. if (endDate != null) {
  52. sql.append(" AND created_at < :endDateExclusive");
  53. args.put("endDateExclusive", endDate.plusDays(1).atStartOfDay());
  54. }
  55. sql.append("""
  56. GROUP BY user_id, username
  57. ORDER BY username
  58. """);
  59. return jdbcDao.queryForList(sql.toString(), args);
  60. }
  61. }