AsyncUtils.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package com.goafanti.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import javax.mail.MessagingException;
  8. import com.goafanti.admin.bo.AdminListBo;
  9. import com.goafanti.common.bo.OrderOperator;
  10. import com.goafanti.common.constant.AFTConstants;
  11. import com.goafanti.common.dao.*;
  12. import com.goafanti.common.enums.NoticeStatus;
  13. import com.goafanti.common.model.*;
  14. import com.goafanti.core.shiro.token.TokenManager;
  15. import com.goafanti.order.bo.NewOrderChangeBo;
  16. import com.goafanti.order.bo.TOrderNewBo;
  17. import com.goafanti.order.enums.OrderChangeProcess;
  18. import com.goafanti.order.enums.ProcessStatus;
  19. import org.apache.ibatis.annotations.Param;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.scheduling.annotation.Async;
  22. import org.springframework.stereotype.Component;
  23. import com.goafanti.common.bo.EmailBo;
  24. import com.goafanti.common.enums.NoticeTypes;
  25. @Component
  26. @Async
  27. public class AsyncUtils {
  28. @Autowired
  29. private NoticeMapper noticeMapper;
  30. @Autowired
  31. private AdminMapper adminMapper;
  32. @Autowired
  33. private TOrderNewMapper tOrderNewMapper;
  34. @Autowired
  35. private NewOrderChangeMapper newOrderChangeMapper;
  36. @Autowired
  37. private TOrderInvoiceMapper tOrderInvoiceMapper;
  38. @Autowired
  39. private TOrderTaskMapper tOrderTaskMapper;
  40. public void patentSend(EmailBo bo) throws UnsupportedEncodingException, MessagingException {
  41. SendEmailUtil.getInstance().patentSend(bo);
  42. }
  43. public void sendList(List<EmailBo> list) throws MessagingException, UnsupportedEncodingException {
  44. for (EmailBo emailBo : list) {
  45. SendEmailUtil.getInstance().patentSend(emailBo);
  46. }
  47. }
  48. public void send(EmailBo bo){
  49. try {
  50. SendEmailUtil.getInstance().send(bo);
  51. } catch (UnsupportedEncodingException | MessagingException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. public void addNotice(Notice n ) {
  56. n.setType(NoticeTypes.getType(n.getNoticeType()));
  57. noticeMapper.insertSelective(n);
  58. }
  59. public void addNotic(Integer type, String aid,String str) {
  60. Notice n =new Notice();
  61. n.setId(UUID.randomUUID().toString());
  62. n.setAid(aid);
  63. n.setNoticeType(type);
  64. n.setContent(str);
  65. n.setReaded(0);//未读
  66. addNotice(n);
  67. }
  68. public void addNoticAndEmail(Notice n) {
  69. n.setType(NoticeTypes.getType(n.getNoticeType()));
  70. AdminListBo a = adminMapper.getDeptNameByAid(n.getAid());
  71. EmailBo bo=new EmailBo(NoticeStatus.getStatus(n.getNoticeType()).getDesc(),a.getEmail(),n.getContent()) ;
  72. noticeMapper.insertSelective(n);
  73. if (StringUtils.isNotBlank(bo.getAddress())){
  74. send(bo);
  75. }
  76. }
  77. /**
  78. *
  79. * @param aid 通知对象
  80. * @param noticeType 通知类型
  81. * @param content 通知内容
  82. */
  83. public void addNoticAndEmail(String aid,Integer noticeType,String content) {
  84. Notice n=new Notice();
  85. n.setId(UUID.randomUUID().toString());
  86. n.setAid(aid);
  87. n.setNoticeType(noticeType);
  88. n.setContent(content);
  89. n.setReaded(0);
  90. n.setType(NoticeTypes.getType(n.getNoticeType()));
  91. addNoticAndEmail(n);
  92. }
  93. public void addNoticeBatch(List<Notice> ln) {
  94. List<Notice> newList=new ArrayList<Notice>();
  95. if (ln != null && ln.size() > 0) {
  96. for (int i = 0; i < ln.size(); i++) {
  97. ln.get(i).setType(NoticeTypes.getType(ln.get(i).getNoticeType()));
  98. newList.add(ln.get(i));
  99. if (50 == newList.size() || i == ln.size() - 1) {
  100. if (newList.size() > 0) noticeMapper.insertBatch(newList);
  101. newList.clear();
  102. try {
  103. Thread.sleep(2000);
  104. } catch (InterruptedException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. }
  110. }
  111. public void batchUpdateUnreaded(List<String> dl) {
  112. noticeMapper.batchUpdateUnreaded(dl);
  113. }
  114. /**
  115. * 切换审核人员
  116. * @param depId 部门编号
  117. * @param aid 切换后审核人
  118. * @return
  119. */
  120. public void updateExamineName( String depId, String aid) {
  121. tOrderNewMapper.updateExamineName(3,depId,aid,null);
  122. newOrderChangeMapper.updateExamineName(OrderChangeProcess.JSZJ.getCode(),depId,aid,null);
  123. tOrderInvoiceMapper.updateExamineName(0,depId,aid,null);
  124. }
  125. /**
  126. *
  127. * @param type 1营销管理 2公司管理 3财务管理 4咨询管理 5咨询师更换名称 6咨询师经理更换名称
  128. * @param id 管理员id
  129. * @param roleType
  130. */
  131. public void updateListExamineName(Integer type, String id, String roleType) {
  132. Admin admin = adminMapper.selectByPrimaryKey(id);
  133. String depId=admin.getDepartmentId();
  134. String names="";
  135. if (type==1){
  136. names=getNames(type,depId, roleType);
  137. tOrderNewMapper.updateExamineName(ProcessStatus.YPYXGLY.getCode(),depId,null,names);
  138. newOrderChangeMapper.updateExamineName(OrderChangeProcess.YXGLY.getCode(),depId,null,names);
  139. tOrderInvoiceMapper.updateExamineName(1,depId,null,names);
  140. }
  141. if (type==2){
  142. tOrderInvoiceMapper.updateListExamineName(type,depId,roleType);
  143. }
  144. if (type==3){
  145. names=getNames(type,depId, roleType);
  146. newOrderChangeMapper.updateExamineName(OrderChangeProcess.CWZJ.getCode(),depId,null,names);
  147. }
  148. if (type==4){
  149. names=getNames(type,depId, roleType);
  150. tOrderNewMapper.updateExamineName(ProcessStatus.YPZXSGLY.getCode(),depId,null,names);
  151. newOrderChangeMapper.updateExamineName(OrderChangeProcess.JSZJ.getCode(),depId,null,names);
  152. }
  153. if (type==5){
  154. newOrderChangeMapper.updateTaskExamineName(admin.getId(),admin.getId(),OrderChangeProcess.JSY.getCode());
  155. }
  156. if (type==6){
  157. newOrderChangeMapper.updateTaskExamineName(admin.getId(),admin.getId(),OrderChangeProcess.JSJL.getCode());
  158. }
  159. }
  160. private String getNames(Integer type,String depId, String roleType) {
  161. StringBuffer str=new StringBuffer();
  162. List<Admin> admins = adminMapper.selectAdminByRoleType(roleType);
  163. for (Admin admin : admins) {
  164. if (type==1&&admin.getDepartmentId().equals(depId)){
  165. str = str.append(admin.getName()).append(",");
  166. }else if(type!=1){
  167. str = str.append(admin.getName()).append(",");
  168. }
  169. }
  170. if (str.length()>1)return str.substring(0,str.length()-1);
  171. else return "暂无";
  172. }
  173. /**
  174. *
  175. * @param type 发送对象 0发起营销管理员 1 完成通知所有人 2拒绝发送给营销员
  176. * @param sotpType 发送对象 类型 0=项目暂停,1=项目重启
  177. *
  178. */
  179. public void addProjectSotpNotic(Integer type, Integer sotpType, String ids, TOrderTask task) {
  180. Integer noticeType=0;
  181. if (task==null)task = tOrderTaskMapper.selectByPrimaryKey(Integer.valueOf(ids.split(",")[0]));
  182. TOrderNewBo tOrderNew = tOrderNewMapper.getOrderNewDetail(task.getOrderNo());
  183. List<Notice> notes=new ArrayList<>();
  184. StringBuffer emails=new StringBuffer();
  185. if (type==0){
  186. noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_START.getCode() :NoticeStatus.PROJECT_RENEW_START.getCode();
  187. String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],请及时审核。",tOrderNew.getUserName(),task.getOrderNo(),ids);
  188. List<Admin> admins = adminMapper.listAdminBydepIdAndRoleType(tOrderNew.getOrderDep(),AFTConstants.SALESMAN_ADMIN);
  189. for (Admin admin : admins) {
  190. addNotic( noticeType,admin.getId(), str);
  191. send(new EmailBo(NoticeStatus.getValueByCode(noticeType),admin.getEmail(),str));
  192. }
  193. }else if(type==1){
  194. String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],已%s请悉知。",tOrderNew.getUserName(),task.getOrderNo(),task.getId(),sotpType==1?"重启":"暂停");
  195. noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_YES.getCode() :NoticeStatus.PROJECT_RENEW_YES.getCode();
  196. OrderOperator orderOperator = tOrderTaskMapper.selectAidByParam(task.getId());
  197. List<String >aids=new ArrayList<>();
  198. if (orderOperator.getSalesmanId()!=null&&!aids.contains(orderOperator.getFinanceId())){
  199. aids.add(orderOperator.getSalesmanId());
  200. }
  201. if (task.getConsultantId()!=null&&!aids.contains(task.getConsultantId())){
  202. aids.add(task.getConsultantId());
  203. }
  204. if (task.getManagerId()!=null&&!aids.contains(task.getManagerId())){
  205. aids.add(task.getManagerId());
  206. }
  207. if (orderOperator.getFinanceId()!=null&&!aids.contains(orderOperator.getFinanceId())){
  208. aids.add(orderOperator.getFinanceId());
  209. }
  210. List<Admin> cwgly = adminMapper.selectAdminByRoleType(AFTConstants.FINANCE_ADMIN);
  211. for (Admin admin : cwgly) {
  212. if (!aids.contains(admin.getId())){
  213. aids.add(admin.getId());
  214. }
  215. }
  216. List<Admin> zxsgly = adminMapper.selectAdminByRoleType(AFTConstants.TECH_ADMIN);
  217. for (Admin admin : zxsgly) {
  218. if (!aids.contains(admin.getId())){
  219. aids.add(admin.getId());
  220. }
  221. }
  222. List<Admin> zc = adminMapper.selectAdminByRoleType(AFTConstants.CED);
  223. for (Admin admin : zc) {
  224. if (!aids.contains(admin.getId())){
  225. aids.add(admin.getId());
  226. }
  227. }
  228. List<Admin> dsz = adminMapper.selectAdminByRoleType(AFTConstants.APPROVAL_DECISION);
  229. for (Admin admin : dsz) {
  230. if (!aids.contains(admin.getId())){
  231. aids.add(admin.getId());
  232. }
  233. }
  234. if (!aids.isEmpty()){
  235. for (String aid : aids) {
  236. pushParam( aid, noticeType, str, notes, emails);
  237. }
  238. }
  239. noticeMapper.insertBatch(notes);
  240. send(new EmailBo(NoticeStatus.getValueByCode(noticeType),emails.substring(0,emails.length()-1),str));
  241. }else if (type==2){
  242. noticeType=sotpType==0?NoticeStatus.PROJECT_SOPT_NO.getCode() :NoticeStatus.PROJECT_RENEW_NO.getCode();
  243. String str=String.format("客户名称[%s]订单编号[%s]项目编号[%s],已经被驳回。",tOrderNew.getUserName(),task.getOrderNo(),task.getId());
  244. if (tOrderNew.getSalesmanId()!=null){
  245. Admin admin = adminMapper.selectByPrimaryKey(tOrderNew.getSalesmanId());
  246. addNotic( noticeType,admin.getId(), str);
  247. send(new EmailBo(NoticeStatus.getValueByCode(noticeType),admin.getEmail(),str));
  248. }
  249. }
  250. }
  251. private void pushParam(String id, Integer noticeType, String str, List<Notice> notes, StringBuffer emails) {
  252. Admin admin = adminMapper.selectByPrimaryKey(id);
  253. pushParam(admin,noticeType,str,notes,emails);
  254. }
  255. private void pushParam(Admin admin, Integer noticeType, String str, List<Notice> notes, StringBuffer emails) {
  256. notes.add(new Notice(UUID.randomUUID().toString(), new Date(), 0, admin.getId(),
  257. noticeType, str));
  258. if (admin!=null &&StringUtils.isNotBlank(admin.getEmail())) {
  259. if (!emails.toString().contains(admin.getEmail())){
  260. emails = emails.append(admin.getEmail()).append(",");
  261. }
  262. }
  263. }
  264. }