ReleaseUserTask.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.goafanti.common.task;
  2. import java.util.*;
  3. import javax.annotation.Resource;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.scheduling.annotation.Scheduled;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import com.goafanti.common.bo.EmailBo;
  13. import com.goafanti.common.bo.userDaysBo;
  14. import com.goafanti.common.constant.AFTConstants;
  15. import com.goafanti.common.dao.NoticeMapper;
  16. import com.goafanti.common.enums.NoticeStatus;
  17. import com.goafanti.common.enums.NoticeTypes;
  18. import com.goafanti.common.model.Notice;
  19. import com.goafanti.common.model.User;
  20. import com.goafanti.common.utils.AsyncUtils;
  21. import com.goafanti.common.utils.LoggerUtils;
  22. import com.goafanti.common.utils.StringUtils;
  23. import com.goafanti.customer.bo.LockingReleaseBo;
  24. import com.goafanti.customer.service.CustomerService;
  25. import com.goafanti.user.service.UserService;
  26. import org.springframework.web.bind.annotation.RestController;
  27. @Component
  28. //@RestController
  29. public class ReleaseUserTask {
  30. @Resource
  31. private CustomerService customerService;
  32. @Resource
  33. private UserService userService;
  34. @Autowired
  35. private NoticeMapper noticeMapper;
  36. @Autowired
  37. private AsyncUtils asyncUtils;
  38. @Value(value = "${dev.name}")
  39. private String devName=null;
  40. int pointsDataLimit = 50;
  41. /**
  42. * 每天凌晨一点
  43. *
  44. * @throws InterruptedException
  45. */
  46. // @Scheduled(cron = "0 31 15 * * ?")
  47. @Scheduled(cron = "0 0 1 * * ?")
  48. public void startTask() {
  49. try {
  50. List<User> userList = userService.selectUserByRoleName("营销员", "营销经理");
  51. pushUserDays(userList);
  52. Thread.sleep(2000);
  53. updateUser(userList);
  54. Thread.sleep(2000);
  55. } catch (InterruptedException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. private void pushUserDays(List<User> userList ) throws InterruptedException {
  60. LoggerUtils.debug(getClass(), "==============客户提醒开始============");
  61. List<Notice> ln = new ArrayList<>();
  62. for (User u : userList) {
  63. if (StringUtils.isNotBlank(u.getId())) {
  64. List<userDaysBo> userTmpList = customerService.selectReleaseUserDays(u.getId());
  65. for (userDaysBo ub : userTmpList) {
  66. String str=null;
  67. if (ub.getChannel() == 0) {
  68. String shareType="";
  69. if(ub.getNewChannel()==0)shareType="私有";
  70. else if(ub.getNewChannel()==1)shareType="渠道";
  71. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时%s!", shareType, ub.getName(),ub.getType()==0?"跟进":"签单");
  72. }else {
  73. str = String.format("您的%s客户【%s】剩余天数不足十五天,请及时签单!", "外联", ub.getName());
  74. }
  75. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  76. NoticeStatus.CUSTOMER_LOSE_REMINDER.getCode(), str,ub.getUid()));
  77. }
  78. }
  79. }
  80. //查询15天未跟进,私有跟释放前15天重复,去除
  81. List<userDaysBo> userChannelList = new ArrayList<userDaysBo>();
  82. for (User u : userList) {
  83. if (StringUtils.isNotBlank(u.getId())) {
  84. userChannelList = customerService.selectChannelNotFollow(u.getId());
  85. }
  86. for (userDaysBo ub : userChannelList) {
  87. String str=null;
  88. if (ub.getChannel() == 1) {
  89. str = String.format("您的%s客户【%s】已经十五天未跟进,请及时跟进或者限时签单!", "外联", ub.getName());
  90. }
  91. ln.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, ub.getAid(),
  92. NoticeStatus.CUSTOMER_PRIVATE_REMINDER.getCode(), str,ub.getUid()));
  93. }
  94. }
  95. addNoticeBatch(ln);
  96. LoggerUtils.debug(getClass(), "==============客户提醒结束============");
  97. }
  98. /**
  99. * 将客户和业务转为待释放
  100. */
  101. public void updateUser(List<User> userList ) {
  102. LoggerUtils.debug(getClass(), "==============客户释放开始============");
  103. Date releaseTime = new Date();
  104. try {
  105. List<LockingReleaseBo> lockUserList = new ArrayList<LockingReleaseBo>();
  106. if (userList != null && !userList.isEmpty()) {
  107. for (User u : userList) {
  108. if (StringUtils.isNotBlank(u.getId())) {
  109. // 获取30天释放
  110. List<LockingReleaseBo> userTmpList = customerService.selectWaitReleaseCustomer(u.getId());
  111. customerService.pushReleaseLog(userTmpList, 0);
  112. addUserNotice(userTmpList);
  113. if (userTmpList != null && !userTmpList.isEmpty())
  114. lockUserList.addAll(userTmpList);
  115. }
  116. }
  117. }
  118. List<LockingReleaseBo> newList = new ArrayList<LockingReleaseBo>();
  119. if (lockUserList != null && lockUserList.size() > 0) {
  120. for (int i = 0; i < lockUserList.size(); i++) {
  121. newList.add(lockUserList.get(i));
  122. if (pointsDataLimit == newList.size() || i == lockUserList.size() - 1) {
  123. if (newList.size() > 0) {
  124. customerService.updatePendingReleaseLock(newList);
  125. }
  126. newList.clear();
  127. Thread.sleep(2000);
  128. }
  129. }
  130. }
  131. pushChannel(releaseTime, userList);
  132. } catch (Exception e) {
  133. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  134. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============客户释放失败================",null);
  135. addNotice(n);
  136. EmailBo emailBo = new EmailBo("释放客户失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", devName+"释放客户失败");
  137. asyncUtils.send(emailBo);
  138. LoggerUtils.error(getClass(), "====================客户释放失败=================");
  139. LoggerUtils.error(getClass(), "客户释放失败", e);
  140. }
  141. LoggerUtils.debug(getClass(), "==============客户释放完成============");
  142. }
  143. /**
  144. * 处理外联
  145. * @param releaseTime
  146. * @param userList
  147. * @throws InterruptedException
  148. */
  149. private void pushChannel(Date releaseTime, List<User> userList) throws InterruptedException {
  150. // 获取超过90天客户,判断是否回收过,未回收则回收,已回收则释放为公共客户
  151. List<userDaysBo> userChannelList =null;
  152. List<userDaysBo> count =new ArrayList<userDaysBo>();
  153. if (userList != null && !userList.isEmpty()) {
  154. for (User u : userList) {
  155. userChannelList=customerService.selectChannelUserDays(u.getId());
  156. if (userChannelList!=null) {
  157. count.addAll(userChannelList);
  158. }
  159. }
  160. }
  161. List<userDaysBo> hsList=new ArrayList<>();
  162. List<userDaysBo> sfList=new ArrayList<>();
  163. if (count != null && count.size() > 0) {
  164. for (int i = 0; i < count.size(); i++) {
  165. //回收
  166. if (count.get(i).getRecovery()==0) {
  167. hsList.add(count.get(i));
  168. }else {
  169. sfList.add(count.get(i));
  170. }
  171. if (pointsDataLimit == (hsList.size()+sfList.size()) || i == count.size() - 1) {
  172. if(!hsList.isEmpty())userService.pushReleaseUserChannel(hsList,0);
  173. if(!sfList.isEmpty())userService.pushReleaseUserChannel(sfList,1);
  174. addChannelNotice(hsList,sfList);
  175. hsList.clear();
  176. sfList.clear();
  177. Thread.sleep(2000);
  178. }
  179. }
  180. }
  181. List<LockingReleaseBo> lockList = customerService.selectPendinglockUserList();
  182. if (lockList.size() > 0)
  183. customerService.updateReleaseLock(releaseTime);
  184. }
  185. public void addNotice(Notice n ) {
  186. n.setType(NoticeTypes.getType(n.getNoticeType()));
  187. noticeMapper.insertSelective(n);
  188. }
  189. public void addNoticeBatch(List<Notice> ln) {
  190. List<Notice> newList=new ArrayList<Notice>();
  191. if (ln != null && ln.size() > 0) {
  192. for (int i = 0; i < ln.size(); i++) {
  193. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  194. newList.add(ln.get(i));
  195. if (50 == newList.size() || i == ln.size() - 1) {
  196. if (newList.size() > 0) {
  197. //发送站内信
  198. noticeMapper.insertBatch(newList);
  199. }
  200. newList.clear();
  201. try {
  202. Thread.sleep(2000);
  203. } catch (InterruptedException e) {
  204. e.printStackTrace();
  205. }
  206. }
  207. }
  208. }
  209. }
  210. private void addUserNotice(List<LockingReleaseBo> userTmpList) {
  211. List<Notice> nl=new ArrayList<Notice>();
  212. if (!userTmpList.isEmpty()) {
  213. for (LockingReleaseBo u : userTmpList) {
  214. String shareType="";
  215. if(u.getNewChannel()==0)shareType="私有";
  216. else if(u.getNewChannel()==1)shareType="渠道";
  217. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, u.getAid(),
  218. NoticeStatus.CUSTOMER_LOSE30.getCode(),
  219. String.format("您的%s客户【%s】已经30天未跟进,已经自动释放!", shareType, u.getUserName()),
  220. u.getUid()));
  221. }
  222. }
  223. if(!nl.isEmpty())addNoticeBatch(nl);
  224. }
  225. private void addChannelNotice(List<userDaysBo> hsList, List<userDaysBo> sfList) {
  226. List<Notice> nl=new ArrayList<Notice>();
  227. for (userDaysBo hs : hsList) {
  228. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, hs.getAid(),
  229. NoticeStatus.CUSTOMER_CHENNEL_RECOVERY.getCode(),
  230. String.format("您的%s客户【%s】已经90天未跟进,已经自动回收!", "外联", hs.getName()),
  231. hs.getUid()));
  232. }
  233. for (userDaysBo sf : sfList) {
  234. nl.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, sf.getAid(),
  235. NoticeStatus.CUSTOMER_CHENNEL_LOSE.getCode(),
  236. String.format("您的%s客户【%s】已经90天未跟进,已经自动释放!", "外联", sf.getName()),
  237. sf.getUid()));
  238. }
  239. if(!nl.isEmpty())addNoticeBatch(nl);
  240. }
  241. }