OrderDunTask.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.goafanti.common.task;
  2. import java.io.UnsupportedEncodingException;
  3. import java.math.BigDecimal;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.UUID;
  8. import javax.mail.MessagingException;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import com.goafanti.common.bo.EmailBo;
  20. import com.goafanti.common.constant.AFTConstants;
  21. import com.goafanti.common.dao.NoticeMapper;
  22. import com.goafanti.common.dao.TArrearsDunMapper;
  23. import com.goafanti.common.dao.TOrderMidMapper;
  24. import com.goafanti.common.dao.TOrderNewMapper;
  25. import com.goafanti.common.enums.NoticeStatus;
  26. import com.goafanti.common.model.Notice;
  27. import com.goafanti.common.model.TArrearsDun;
  28. import com.goafanti.common.model.TOrderDun;
  29. import com.goafanti.common.model.TOrderMid;
  30. import com.goafanti.common.model.TOrderNew;
  31. import com.goafanti.common.utils.LoggerUtils;
  32. import com.goafanti.common.utils.SendEmailUtil;
  33. import com.goafanti.order.bo.InputNewOrderDunBo;
  34. import com.goafanti.order.bo.OrderDunTaskBo;
  35. import com.goafanti.order.bo.OutNewOrderDunBo;
  36. import com.goafanti.order.bo.TOrderNewBo;
  37. import com.goafanti.order.enums.OrderDunProjectType;
  38. import com.goafanti.order.service.NewOrderDunService;
  39. import com.goafanti.order.service.OrderNewService;
  40. @Component
  41. @Controller
  42. public class OrderDunTask {
  43. @Autowired
  44. private OrderNewService orderNewService;
  45. @Autowired
  46. private NewOrderDunService newOrderDunService;
  47. @Autowired
  48. private TOrderNewMapper tOrderNewMapper;
  49. @Autowired
  50. private TArrearsDunMapper tArrearsDunMapper;
  51. @Autowired
  52. private TOrderMidMapper tOrderMidMapper;
  53. @Autowired
  54. private NoticeMapper noticeMapper;
  55. Logger logger = LoggerFactory.getLogger(OrderDunTask.class);
  56. /**
  57. * 每天凌晨3点轮询处理需要新催款的信息
  58. */
  59. @RequestMapping(value = "/open/updateNewOrderDun", method = RequestMethod.GET)
  60. @ResponseBody
  61. @Scheduled(cron = "0 0 3 * * ?")
  62. @Transactional(rollbackFor=Exception.class)
  63. public void updateNewOrderDun(){
  64. try {
  65. LoggerUtils.debug(logger, "======================新催款任务轮询启动===================");
  66. List<OutNewOrderDunBo> list= newOrderDunService.selectAllOrderDun();
  67. Calendar calendar=Calendar.getInstance();
  68. Calendar calendar2=Calendar.getInstance();
  69. Date date =new Date();
  70. for (OutNewOrderDunBo o : list) {
  71. calendar.setTime(date);
  72. calendar2.setTime(o.getCreateTime());
  73. if (o!=null&&o.getApproval()!=null&&o.getApproval()!=0&&o.getDunType()==1&&o.getWaitDay()!=null) {
  74. calendar2.add(Calendar.DATE, o.getWaitDay());
  75. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  76. updateOrderDun(o);
  77. Thread.sleep(2000);
  78. }
  79. }
  80. if(o.getProjectType()==OrderDunProjectType.HY.getCode()&&o.getDunType()==2) {
  81. calendar2.add(Calendar.DATE, o.getEffectiveCount()*180);
  82. CalendarTimeCleared(calendar2);
  83. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  84. updateOrderDun(o);
  85. Thread.sleep(2000);
  86. }
  87. }
  88. if(o.getProjectType()==OrderDunProjectType.CS.getCode()&&o.getDunType()==2) {
  89. //获取年
  90. int year = calendar2.get(Calendar.YEAR);
  91. //获取月份,0表示1月份
  92. int month = calendar2.get(Calendar.MONTH) + 1;
  93. if(month<10)year=year+1;
  94. if(month>9)year=year+2;
  95. calendar2.set(year, 5, 1);
  96. CalendarTimeCleared(calendar2);
  97. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  98. updateOrderDun(o);
  99. Thread.sleep(2000);
  100. }
  101. }
  102. if (o.getDunType()==0) {
  103. calendar2.setTime(o.getCustomizeTime());
  104. CalendarTimeCleared(calendar);
  105. if (calendar.equals(calendar2)) {
  106. updateOrderDun(o);
  107. Thread.sleep(2000);
  108. }
  109. }
  110. }
  111. LoggerUtils.debug(logger, "======================新催款任务轮询结束===================");
  112. } catch (Exception e) {
  113. EmailBo emailBo = new EmailBo( "催款处理失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", "催款处理失败");
  114. try {
  115. SendEmailUtil.getInstance().send(emailBo);
  116. } catch (UnsupportedEncodingException | MessagingException e1) {
  117. e1.printStackTrace();
  118. }
  119. LoggerUtils.error(getClass(), "=============================================");
  120. LoggerUtils.error(getClass(), "新催款的信息处理失败", e);
  121. LoggerUtils.error(getClass(), "=============================================");
  122. }
  123. }
  124. private void CalendarTimeCleared(Calendar calendar) {
  125. calendar.set(Calendar.HOUR_OF_DAY, 0);
  126. calendar.set(Calendar.MINUTE, 0);
  127. calendar.set(Calendar.SECOND, 0);
  128. calendar.set(Calendar.MILLISECOND, 0);
  129. }
  130. private void updateOrderDun(OutNewOrderDunBo t) throws UnsupportedEncodingException, MessagingException {
  131. InputNewOrderDunBo o=new InputNewOrderDunBo();
  132. o.setId(t.getId());
  133. o.setStatus(1);
  134. o.setStartTime(new Date());
  135. newOrderDunService.updateDun(o);
  136. tOrderMidMapper.addOrderReceivables(t.getOrderNo(),t.getMoney());
  137. TOrderMid tm=tOrderMidMapper.selectByOrderNo(t.getOrderNo());
  138. TArrearsDun td=new TArrearsDun();
  139. td.setOrderArrears(tm.getOrderArrears());
  140. td.setOrderReceivables(tm.getOrderReceivables());
  141. if (tArrearsDunMapper.checkOrderNo(t.getOrderNo(),null)<1) {
  142. td.setOrderNo(t.getOrderNo());
  143. tArrearsDunMapper.insertSelective(td);
  144. }else if(tArrearsDunMapper.checkOrderNo(t.getOrderNo(),0)>0){
  145. Integer i=tArrearsDunMapper.selectByStatus(t.getOrderNo(), 0);
  146. td.setId(i);
  147. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  148. }
  149. TOrderNew t2=tOrderNewMapper.selectByPrimaryKey(t.getOrderNo());
  150. orderNewService.addTimingTaskNewDunNoticAndSendEmail(t2, t);
  151. }
  152. /**
  153. * 每月1号凌晨4点轮询处理需要欠款催款的信息
  154. *
  155. */
  156. @Scheduled(cron = "0 0 4 1 * ?")
  157. @RequestMapping(value = "/open/pushOrderArrearsDun", method = RequestMethod.GET)
  158. @Transactional(rollbackFor=Exception.class)
  159. public void pushOrderArrearsDun(){
  160. try {
  161. LoggerUtils.debug(logger, "======================欠款催款启动===================");
  162. Date date=new Date();
  163. //运算 list(未启动) list2(催款中) list3(已完成)订单的欠款催款
  164. List<TArrearsDun> list=tArrearsDunMapper.selectStatusAll(0);
  165. List<TArrearsDun> list2=tArrearsDunMapper.selectStatusAll(1);
  166. List<TArrearsDun> list3=tArrearsDunMapper.selectStatusAndAmount(2);
  167. for (TArrearsDun td : list) {
  168. TOrderMid tm=tOrderMidMapper.selectByOrderNo(td.getOrderNo());
  169. //如果欠款大于0则需要触发邮件
  170. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  171. td.setDunStatus(1);
  172. td.setStartTime(date);
  173. td.setOrderArrears(tm.getOrderArrears());
  174. td.setOrderReceivables(tm.getOrderReceivables());
  175. addNoticAndSendEmail(td.getOrderNo(),td,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  176. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  177. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  178. td.setDunStatus(2);
  179. td.setEndTime(date);
  180. td.setOrderArrears(tm.getOrderArrears());
  181. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  182. }
  183. Thread.sleep(2000);
  184. }
  185. for (TArrearsDun ta : list2) {
  186. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  187. //如果欠款大于0则需要触发邮件
  188. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  189. ta.setDunStatus(3);
  190. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  191. ta.setId(null);
  192. ta.setDunStatus(1);
  193. ta.setStartTime(date);
  194. ta.setOrderArrears(tm.getOrderArrears());
  195. ta.setOrderReceivables(tm.getOrderReceivables());
  196. tArrearsDunMapper.insertSelective(ta);
  197. addNoticAndSendEmail(ta.getOrderNo(),ta,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  198. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  199. ta.setDunStatus(2);
  200. ta.setOrderArrears(tm.getOrderArrears());
  201. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  202. }
  203. Thread.sleep(2000);
  204. }
  205. for (TArrearsDun ta : list3) {
  206. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  207. //如果欠款大于0则需要触发邮件
  208. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  209. ta.setDunStatus(3);
  210. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  211. ta.setId(null);
  212. ta.setDunStatus(1);
  213. ta.setStartTime(date);
  214. ta.setOrderArrears(tm.getOrderArrears());
  215. ta.setOrderReceivables(tm.getOrderReceivables());
  216. tArrearsDunMapper.insertSelective(ta);
  217. addNoticAndSendEmail(ta.getOrderNo(),ta,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  218. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  219. ta.setDunStatus(2);
  220. ta.setOrderReceivables(tm.getOrderReceivables());
  221. ta.setOrderArrears(tm.getOrderArrears());
  222. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  223. }
  224. Thread.sleep(2000);
  225. }
  226. } catch (Exception e) {
  227. LoggerUtils.error(getClass(), "===========================欠款催款失败==========================", e);
  228. }
  229. }
  230. private void addNoticAndSendEmail(String orderNo, TArrearsDun td, Integer code) throws UnsupportedEncodingException, MessagingException {
  231. TOrderNewBo b = tOrderNewMapper.getSaleIdByOno(orderNo);
  232. String str=NoticeStatus.getValueByCode(code)+": 订单编号 -"+orderNo+", 操作人: 平台超管中心 - yxy。";
  233. Notice n =new Notice();
  234. n.setId(UUID.randomUUID().toString());
  235. n.setAid(b.getSalesmanId());
  236. n.setNoticeType(code);
  237. n.setContent(str);
  238. n.setReaded(0);//未读
  239. noticeMapper.insertSelective(n);
  240. /* LoggerUtils.debug(logger, "======================邮件信息发送===================");
  241. String content = "<div>客户名称: "+ b.getUserName() +"</div><div>订单编号: " + orderNo + "</div>";
  242. String tite="催收通知";
  243. content +="<div>催收时间: " + new Date() +
  244. "</div><div>催收科目: 欠款催收" +
  245. "</div><div>催收情况: 已启动" +"</div>";
  246. //发送驳回邮件
  247. EmailBo bo = new EmailBo(tite, b.getEmail(), "平台超管中心", "管理员", content,orderNo,
  248. DateUtils.formatDate(td.getStartTime(), AFTConstants.YYYYMMDDHHMMSS),"欠款催收",b.getSalesmanName(),b.getUserName(), 1,null);
  249. if(StringUtils.isNotBlank(b.getEmail())) {
  250. SendEmailUtil.getInstance().patentSend(bo);
  251. }
  252. LoggerUtils.debug(logger, "=========================================发送邮件成功");*/
  253. }
  254. /**
  255. * 每天凌晨2点轮询处理需要催款的信息
  256. *
  257. */
  258. // @RequestMapping(value = "/open/updateOrderDun", method = RequestMethod.GET)
  259. @Scheduled(cron = "0 0 2 * * ?")
  260. @Transactional(rollbackFor=Exception.class)
  261. public void updateorderDun(){
  262. try {
  263. LoggerUtils.debug(logger, "======================催款任务轮询启动===================");
  264. List<OrderDunTaskBo> list=orderNewService.selectAllOrderDun();
  265. for (OrderDunTaskBo t : list) {
  266. if (t.getDunSubject()/10==1) {//1则为订单催款信息否则为项目催款信息
  267. if (t.getDunSubject()%10==t.getOrderStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  268. updateOrderDun(t);
  269. Thread.sleep(2000);
  270. }
  271. }else {
  272. if (t.getDunSubject()%10>=t.getProjectStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  273. updateOrderDun(t);
  274. Thread.sleep(2000);
  275. }
  276. }
  277. }
  278. LoggerUtils.debug(logger, "======================催款任务轮询结束===================");
  279. } catch (Exception e) {
  280. LoggerUtils.error(getClass(), "=============================================");
  281. LoggerUtils.error(getClass(), "催款的信息处理失败", e);
  282. LoggerUtils.error(getClass(), "=============================================");
  283. }
  284. }
  285. private void updateOrderDun(OrderDunTaskBo t) throws UnsupportedEncodingException, MessagingException {
  286. LoggerUtils.debug(logger, "======================修改催款任务===================");
  287. TOrderDun tDun=new TOrderDun();
  288. tDun.setId(t.getId());
  289. tDun.setDunStatus(1);
  290. tDun.setStartTime(new Date());
  291. orderNewService.updateOrderDun(tDun);
  292. orderNewService.addNoticAndSendEmail(t.getOrderNo(),t,NoticeStatus.ORDER_DUN.getCode());
  293. }
  294. }