WeComStaffDataJob.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.tzld.piaoquan.offline.job;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.tzld.piaoquan.growth.common.common.enums.TimeEnum;
  5. import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
  6. import com.tzld.piaoquan.growth.common.component.ProxyHttpPoolClient;
  7. import com.tzld.piaoquan.growth.common.dao.mapper.*;
  8. import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam;
  9. import com.tzld.piaoquan.growth.common.model.po.*;
  10. import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
  11. import com.tzld.piaoquan.growth.common.service.WeComSendService;
  12. import com.tzld.piaoquan.growth.common.utils.DateUtil;
  13. import com.tzld.piaoquan.growth.common.utils.DateUtils;
  14. import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
  15. import com.xxl.job.core.biz.model.ReturnT;
  16. import com.xxl.job.core.handler.annotation.XxlJob;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.util.CollectionUtils;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.*;
  28. @Slf4j
  29. @Component
  30. public class WeComStaffDataJob {
  31. @Autowired
  32. private HttpPoolClient httpPoolClient;
  33. @Autowired
  34. private ProxyHttpPoolClient proxyHttpPoolClient;
  35. @Autowired
  36. private WeComAccessTokenService weComAccessTokenService;
  37. @Autowired
  38. private StaffMapper staffMapper;
  39. @Autowired
  40. private CorpMapper corpMapper;
  41. @Autowired
  42. private StaffStatisticsTotalMapper staffStatisticsTotalMapper;
  43. @Autowired
  44. private StaffGroupStatisticsTotalMapper staffGroupStatisticsTotalMapper;
  45. @Autowired
  46. private CorpStatisticsTotalMapper corpStatisticsTotalMapper;
  47. @XxlJob("insertStaffJob")
  48. public ReturnT<String> insertStaff(String param) {
  49. try {
  50. XxlJobParam xxlJobParam = new XxlJobParam();
  51. if (StringUtils.isNotEmpty(param)) {
  52. xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
  53. }
  54. CorpExample corpExample = new CorpExample();
  55. if (xxlJobParam.getCorpId() != null) {
  56. corpExample.createCriteria().andIdEqualTo(xxlJobParam.getCorpId());
  57. }
  58. List<Corp> corps = corpMapper.selectByExample(corpExample);
  59. if (CollectionUtils.isEmpty(corps)) {
  60. return ReturnT.SUCCESS;
  61. }
  62. for (Corp corp : corps) {
  63. List<String> carrierIdList = getCarrierIdList(corp.getId());
  64. if (CollectionUtils.isEmpty(carrierIdList)) {
  65. continue;
  66. }
  67. for (String carrierId : carrierIdList) {
  68. StaffExample example = new StaffExample();
  69. example.createCriteria().andCarrierIdEqualTo(carrierId).andCorpIdEqualTo(corp.getId());
  70. List<Staff> staffList = staffMapper.selectByExample(example);
  71. if (CollectionUtils.isEmpty(staffList)) {
  72. Staff staff = new Staff();
  73. staff.setCorpId(corp.getId());
  74. staff.setCarrierId(carrierId);
  75. staff.setRemark("");
  76. staffMapper.insert(staff);
  77. }
  78. }
  79. }
  80. } catch (Exception e) {
  81. LarkRobotUtil.sendMessage("更新员工失败");
  82. log.error("insertStaff error", e);
  83. }
  84. return ReturnT.SUCCESS;
  85. }
  86. private List<String> getCarrierIdList(Long corpId) throws IOException {
  87. String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
  88. String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken);
  89. String res;
  90. if (corpId == 1L) {
  91. res = httpPoolClient.get(url);
  92. } else {
  93. res = proxyHttpPoolClient.get(url);
  94. }
  95. log.info("getCarrierIdList corp = {}, res={}", corpId, res);
  96. JSONObject jsonObject = JSONObject.parseObject(res);
  97. Integer errcode = jsonObject.getInteger("errcode");
  98. if (errcode == 0) {
  99. return jsonObject.getJSONArray("follow_user").stream().map(String::valueOf).collect(Collectors.toList());
  100. }
  101. return null;
  102. }
  103. @XxlJob("statisticsTotalJob")
  104. public ReturnT<String> statisticsTotal(String param) throws IOException {
  105. String date;
  106. if (StringUtils.isNotEmpty(param)) {
  107. date = param;
  108. } else {
  109. date = DateUtil.getBeforeDayDateString1();
  110. }
  111. long startTime = DateUtil.dateStrToTimestamp(date, "yyyy-MM-dd");
  112. long endTime = startTime + TimeEnum.DAY.getTime() - 1;
  113. StaffExample staffExample = new StaffExample();
  114. staffExample.createCriteria().andIsDeleteEqualTo(0);
  115. List<Staff> staffs = staffMapper.selectByExample(staffExample);
  116. for (Staff staff : staffs) {
  117. statisticsStaffTotal(staff, startTime, endTime, date);
  118. statisticsStaffGroupTotal(staff, startTime, date);
  119. }
  120. StaffStatisticsTotalExample staffStatisticsTotalExample = new StaffStatisticsTotalExample();
  121. staffStatisticsTotalExample.createCriteria().andDateEqualTo(date);
  122. List<StaffStatisticsTotal> staffStatisticsTotals = staffStatisticsTotalMapper.selectByExample(staffStatisticsTotalExample);
  123. if (!CollectionUtils.isEmpty(staffStatisticsTotals)) {
  124. // 2. 分组并多字段求和(不使用构造方法)
  125. List<CorpStatisticsTotal> results = processStaffStatistics(staffStatisticsTotals);
  126. for (CorpStatisticsTotal corpStatisticsTotal : results) {
  127. corpStatisticsTotal.setDate(date);
  128. try {
  129. corpStatisticsTotalMapper.insertSelective(corpStatisticsTotal);
  130. } catch (Exception e) {
  131. log.error("insert corpStatisticsTotal error", e);
  132. }
  133. }
  134. }
  135. return ReturnT.SUCCESS;
  136. }
  137. public List<CorpStatisticsTotal> processStaffStatistics(List<StaffStatisticsTotal> staffStatisticsTotals) {
  138. // 使用 toMap 收集器高效分组和聚合
  139. Map<Long, CorpStatisticsTotal> resultMap = staffStatisticsTotals.stream()
  140. .collect(Collectors.toMap(
  141. StaffStatisticsTotal::getCorpId,
  142. this::convertToCorpStats, // 方法引用转换
  143. this::mergeCorpStats // 方法引用合并
  144. ));
  145. return new ArrayList<>(resultMap.values());
  146. }
  147. private CorpStatisticsTotal mergeCorpStats(CorpStatisticsTotal existing, CorpStatisticsTotal newcomer) {
  148. existing.setChatCnt(existing.getChatCnt() + newcomer.getChatCnt());
  149. existing.setMessageCnt(existing.getMessageCnt() + newcomer.getMessageCnt());
  150. existing.setNegativeFeedbackCnt(existing.getNegativeFeedbackCnt() + newcomer.getNegativeFeedbackCnt());
  151. existing.setNewApplyCnt(existing.getNewApplyCnt() + newcomer.getNewApplyCnt());
  152. existing.setNewContactCnt(existing.getNewContactCnt() + newcomer.getNewContactCnt());
  153. return existing;
  154. }
  155. // 转换方法:将 Staff 对象转换为 Corp 对象
  156. private CorpStatisticsTotal convertToCorpStats(StaffStatisticsTotal staff) {
  157. CorpStatisticsTotal corp = new CorpStatisticsTotal();
  158. corp.setCorpId(staff.getCorpId());
  159. corp.setChatCnt(staff.getChatCnt());
  160. corp.setMessageCnt(staff.getMessageCnt());
  161. corp.setNegativeFeedbackCnt(staff.getNegativeFeedbackCnt());
  162. corp.setNewApplyCnt(staff.getNewApplyCnt());
  163. corp.setNewContactCnt(staff.getNewContactCnt());
  164. return corp;
  165. }
  166. private void statisticsStaffTotal(Staff staff, long startTime, long endTime, String date) throws IOException {
  167. Long corpId = staff.getCorpId();
  168. StaffStatisticsTotalExample example = new StaffStatisticsTotalExample();
  169. example.createCriteria().andCorpIdEqualTo(corpId).andStaffIdEqualTo(staff.getId()).andDateEqualTo(date);
  170. long l = staffStatisticsTotalMapper.countByExample(example);
  171. if (l > 0) {
  172. return;
  173. }
  174. String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
  175. String url = POST_WE_COM_USER_BEHAVIOR_DATA
  176. + "?access_token=" + accessToken;
  177. JSONObject params = new JSONObject();
  178. params.put("start_time", startTime);
  179. params.put("end_time", endTime);
  180. JSONArray userIds = new JSONArray();
  181. userIds.add(staff.getCarrierId());
  182. params.put("userid", userIds);
  183. String res;
  184. if (staff.getCorpId() == 1L) {
  185. res = httpPoolClient.post(url, params.toJSONString());
  186. } else {
  187. res = proxyHttpPoolClient.post(url, params.toJSONString());
  188. }
  189. if (StringUtils.isNotEmpty(res)) {
  190. JSONObject jsonObject = JSONObject.parseObject(res);
  191. Integer errcode = jsonObject.getInteger("errcode");
  192. if (errcode == 0) {
  193. JSONArray jsonArray = jsonObject.getJSONArray("behavior_data");
  194. if (!jsonArray.isEmpty()) {
  195. JSONObject data = jsonArray.getJSONObject(0);
  196. StaffStatisticsTotal statisticsTotal = new StaffStatisticsTotal();
  197. statisticsTotal.setStatTime(data.getLong("stat_time"));
  198. statisticsTotal.setChatCnt(data.getInteger("chat_cnt"));
  199. statisticsTotal.setMessageCnt(data.getInteger("message_cnt"));
  200. statisticsTotal.setNegativeFeedbackCnt(data.getInteger("negative_feedback_cnt"));
  201. statisticsTotal.setNewApplyCnt(data.getInteger("new_apply_cnt"));
  202. statisticsTotal.setNewContactCnt(data.getInteger("new_contact_cnt"));
  203. statisticsTotal.setDate(date);
  204. statisticsTotal.setCorpId(corpId);
  205. statisticsTotal.setStaffId(staff.getId());
  206. staffStatisticsTotalMapper.insertSelective(statisticsTotal);
  207. }
  208. }
  209. }
  210. }
  211. private void statisticsStaffGroupTotal(Staff staff, long startTime, String date) throws IOException {
  212. Long corpId = staff.getCorpId();
  213. StaffGroupStatisticsTotalExample example = new StaffGroupStatisticsTotalExample();
  214. example.createCriteria().andCorpIdEqualTo(corpId).andStaffIdEqualTo(staff.getId()).andDateEqualTo(date);
  215. long l = staffGroupStatisticsTotalMapper.countByExample(example);
  216. if (l > 0) {
  217. return;
  218. }
  219. String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
  220. String url = POST_WE_COM_USER_GROUP_CHAT_STATISTIC
  221. + "?access_token=" + accessToken;
  222. JSONObject params = new JSONObject();
  223. params.put("day_begin_time", startTime);
  224. JSONObject ownerFilter = new JSONObject();
  225. JSONArray userIds = new JSONArray();
  226. userIds.add(staff.getCarrierId());
  227. ownerFilter.put("userid_list", userIds);
  228. params.put("owner_filter", ownerFilter);
  229. System.out.println(JSONObject.toJSONString(params));
  230. String res;
  231. if (staff.getCorpId() == 1L) {
  232. res = httpPoolClient.post(url, params.toJSONString());
  233. } else {
  234. res = proxyHttpPoolClient.post(url, params.toJSONString());
  235. }
  236. if (StringUtils.isNotEmpty(res)) {
  237. JSONObject jsonObject = JSONObject.parseObject(res);
  238. System.out.println("jsonObject = " + jsonObject);
  239. Integer errcode = jsonObject.getInteger("errcode");
  240. if (errcode == 0) {
  241. JSONArray jsonArray = jsonObject.getJSONArray("behavior_data");
  242. if (!jsonArray.isEmpty()) {
  243. JSONObject data = jsonArray.getJSONObject(0);
  244. StaffGroupStatisticsTotal statisticsTotal = new StaffGroupStatisticsTotal();
  245. statisticsTotal.setStatTime(data.getLong("stat_time"));
  246. statisticsTotal.setNewChatCnt(data.getInteger("new_chat_cnt"));
  247. statisticsTotal.setChatTotal(data.getInteger("chat_total"));
  248. statisticsTotal.setChatHasMsg(data.getInteger("chat_has_msg"));
  249. statisticsTotal.setNewMemberCnt(data.getInteger("new_member_cnt"));
  250. statisticsTotal.setMemberTotal(data.getInteger("member_total"));
  251. statisticsTotal.setMemberHasMsg(data.getInteger("member_has_msg"));
  252. statisticsTotal.setMsgTotal(data.getInteger("msg_total"));
  253. statisticsTotal.setMigrateTraineeChatCnt(data.getInteger("migrate_trainee_chat_cnt"));
  254. statisticsTotal.setDate(date);
  255. statisticsTotal.setCorpId(corpId);
  256. statisticsTotal.setStaffId(staff.getId());
  257. staffGroupStatisticsTotalMapper.insertSelective(statisticsTotal);
  258. }
  259. }
  260. }
  261. }
  262. }