WeComStaffDataJob.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.CorpMapper;
  8. import com.tzld.piaoquan.growth.common.dao.mapper.CorpStatisticsTotalMapper;
  9. import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
  10. import com.tzld.piaoquan.growth.common.dao.mapper.StaffStatisticsTotalMapper;
  11. import com.tzld.piaoquan.growth.common.model.bo.XxlJobParam;
  12. import com.tzld.piaoquan.growth.common.model.po.*;
  13. import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
  14. import com.tzld.piaoquan.growth.common.service.WeComSendService;
  15. import com.tzld.piaoquan.growth.common.utils.DateUtil;
  16. import com.tzld.piaoquan.growth.common.utils.DateUtils;
  17. import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
  18. import com.xxl.job.core.biz.model.ReturnT;
  19. import com.xxl.job.core.handler.annotation.XxlJob;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Component;
  24. import org.springframework.util.CollectionUtils;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.stream.Collectors;
  29. import static com.tzld.piaoquan.growth.common.common.constant.WeComConstant.*;
  30. @Slf4j
  31. @Component
  32. public class WeComStaffDataJob {
  33. @Autowired
  34. private HttpPoolClient httpPoolClient;
  35. @Autowired
  36. private ProxyHttpPoolClient proxyHttpPoolClient;
  37. @Autowired
  38. private WeComAccessTokenService weComAccessTokenService;
  39. @Autowired
  40. private StaffMapper staffMapper;
  41. @Autowired
  42. private CorpMapper corpMapper;
  43. @Autowired
  44. private StaffStatisticsTotalMapper staffStatisticsTotalMapper;
  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.getThatDayDateString();
  110. }
  111. long startTime = DateUtil.dateStrToTimestamp(date, "yyyy-MM-dd");
  112. long endTime = startTime + TimeEnum.DAY.getTime();
  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. }
  119. StaffStatisticsTotalExample staffStatisticsTotalExample = new StaffStatisticsTotalExample();
  120. staffStatisticsTotalExample.createCriteria().andDateEqualTo(date);
  121. List<StaffStatisticsTotal> staffStatisticsTotals = staffStatisticsTotalMapper.selectByExample(staffStatisticsTotalExample);
  122. if (!CollectionUtils.isEmpty(staffStatisticsTotals)) {
  123. // 2. 分组并多字段求和(不使用构造方法)
  124. List<CorpStatisticsTotal> results = new ArrayList<>(staffStatisticsTotals.stream()
  125. // 按产品类型分组
  126. .collect(Collectors.groupingBy(
  127. StaffStatisticsTotal::getCorpId,
  128. // 对每组数据累加:使用默认构造的结果对象,通过setter更新值
  129. Collectors.reducing(
  130. new CorpStatisticsTotal(), // 初始值:默认构造的空对象
  131. // 转换函数:将Order转换为临时结果对象(用setter设置初始值)
  132. s -> {
  133. CorpStatisticsTotal temp = new CorpStatisticsTotal();
  134. temp.setCorpId(s.getCorpId());
  135. temp.setChatCnt(s.getChatCnt());
  136. temp.setMessageCnt(s.getMessageCnt());
  137. temp.setNegativeFeedbackCnt(s.getNegativeFeedbackCnt());
  138. temp.setNewApplyCnt(s.getNewApplyCnt());
  139. temp.setNewContactCnt(s.getNewContactCnt());
  140. return temp;
  141. },
  142. // 累加函数:合并两个结果对象(用setter更新总和)
  143. (sum1, sum2) -> {
  144. // 确保分组类型一致(同组数据type相同,取一个即可)
  145. sum1.setCorpId(sum2.getCorpId());
  146. // 累加数量
  147. sum1.setChatCnt(sum1.getChatCnt() + sum2.getChatCnt());
  148. sum1.setMessageCnt(sum1.getMessageCnt() + sum2.getMessageCnt());
  149. sum1.setNegativeFeedbackCnt(sum1.getNegativeFeedbackCnt() + sum2.getNegativeFeedbackCnt());
  150. sum1.setNewApplyCnt(sum1.getNewApplyCnt() + sum2.getNewApplyCnt());
  151. sum1.setNewContactCnt(sum1.getNewContactCnt() + sum2.getNewContactCnt());
  152. return sum1;
  153. }
  154. )
  155. ))
  156. // 将Map的值(结果对象)转换为List
  157. .values());
  158. for (CorpStatisticsTotal corpStatisticsTotal : results) {
  159. corpStatisticsTotal.setDate(date);
  160. corpStatisticsTotalMapper.insertSelective(corpStatisticsTotal);
  161. }
  162. }
  163. return ReturnT.SUCCESS;
  164. }
  165. private void statisticsStaffTotal(Staff staff, long startTime, long endTime, String date) throws IOException {
  166. Long corpId = staff.getCorpId();
  167. String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
  168. String url = POST_WE_COM_USER_BEHAVIOR_DATA
  169. + "?access_token=" + accessToken;
  170. JSONObject params = new JSONObject();
  171. params.put("start_time", startTime);
  172. params.put("end_time", endTime);
  173. JSONArray userIds = new JSONArray();
  174. userIds.add(staff.getCarrierId());
  175. params.put("userid", userIds);
  176. String res;
  177. if (staff.getCorpId() == 1L) {
  178. res = httpPoolClient.post(url, params.toJSONString());
  179. } else {
  180. res = proxyHttpPoolClient.post(url);
  181. }
  182. if (StringUtils.isNotEmpty(res)) {
  183. JSONObject jsonObject = JSONObject.parseObject(res);
  184. Integer errcode = jsonObject.getInteger("errcode");
  185. if (errcode == 0) {
  186. JSONArray jsonArray = jsonObject.getJSONArray("behavior_data");
  187. if (!jsonArray.isEmpty()) {
  188. JSONObject data = jsonArray.getJSONObject(0);
  189. StaffStatisticsTotal statisticsTotal = new StaffStatisticsTotal();
  190. statisticsTotal.setStatTime(data.getLong("stat_time"));
  191. statisticsTotal.setChatCnt(data.getInteger("chat_cnt"));
  192. statisticsTotal.setMessageCnt(data.getInteger("message_cnt"));
  193. statisticsTotal.setNegativeFeedbackCnt(data.getInteger("negative_feedback_cnt"));
  194. statisticsTotal.setNewApplyCnt(data.getInteger("new_apply_cnt"));
  195. statisticsTotal.setNewContactCnt(data.getInteger("new_contact_cnt"));
  196. statisticsTotal.setDate(date);
  197. statisticsTotal.setCorpId(corpId);
  198. statisticsTotal.setStaffId(staff.getId());
  199. staffStatisticsTotalMapper.insertSelective(statisticsTotal);
  200. }
  201. }
  202. }
  203. }
  204. }