OrderDunTask.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.PaymentNodeMapper;
  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.PaymentNode;
  28. import com.goafanti.common.model.TArrearsDun;
  29. import com.goafanti.common.model.TOrderDun;
  30. import com.goafanti.common.model.TOrderMid;
  31. import com.goafanti.common.model.TOrderNew;
  32. import com.goafanti.common.utils.AsyncUtils;
  33. import com.goafanti.common.utils.LoggerUtils;
  34. import com.goafanti.order.bo.InputNewOrderDunBo;
  35. import com.goafanti.order.bo.OrderDunTaskBo;
  36. import com.goafanti.order.bo.OutNewOrderDunBo;
  37. import com.goafanti.order.bo.TOrderNewBo;
  38. import com.goafanti.order.enums.OrderDunProjectType;
  39. import com.goafanti.order.service.NewOrderDunService;
  40. import com.goafanti.order.service.OrderNewService;
  41. import com.goafanti.order.service.OrderProjectService;
  42. import com.goafanti.organization.bo.OutPaymentNode;
  43. @Component
  44. @Controller
  45. public class OrderDunTask {
  46. @Autowired
  47. private OrderNewService orderNewService;
  48. @Autowired
  49. private NewOrderDunService newOrderDunService;
  50. @Autowired
  51. private TOrderNewMapper tOrderNewMapper;
  52. @Autowired
  53. private TArrearsDunMapper tArrearsDunMapper;
  54. @Autowired
  55. private TOrderMidMapper tOrderMidMapper;
  56. @Autowired
  57. private PaymentNodeMapper paymentNodeMapper;
  58. @Autowired
  59. private OrderProjectService orderProjectService;
  60. @Autowired
  61. private AsyncUtils asyncUtils;
  62. Logger logger = LoggerFactory.getLogger(OrderDunTask.class);
  63. /**
  64. * 每天凌晨3点轮
  65. * @throws InterruptedException
  66. */
  67. @ResponseBody
  68. @Scheduled(cron = "0 0 3 * * ?")
  69. @Transactional(rollbackFor=Exception.class)
  70. public void startTask() throws InterruptedException {
  71. updateNewOrderDun();
  72. Thread.sleep(2000);
  73. updateorderDun();
  74. }
  75. /**
  76. * 询处理需要新催款的信息
  77. */
  78. @RequestMapping(value = "/open/updateNewOrderDun", method = RequestMethod.GET)
  79. public void updateNewOrderDun(){
  80. try {
  81. LoggerUtils.debug(logger, "======================新催款任务轮询启动===================");
  82. List<OutNewOrderDunBo> list= newOrderDunService.selectAllOrderDun();
  83. Calendar calendar=Calendar.getInstance();
  84. Calendar calendar2=Calendar.getInstance();
  85. Date date =new Date();
  86. for (OutNewOrderDunBo o : list) {
  87. calendar.setTime(date);
  88. calendar2.setTime(o.getCreateTime());
  89. if (o!=null&&o.getApproval()!=null&&o.getApproval()!=0&&o.getDunType()==1&&o.getWaitDay()!=null) {
  90. calendar2.add(Calendar.DATE, o.getWaitDay());
  91. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  92. updateOrderDun(o);
  93. Thread.sleep(2000);
  94. }
  95. }
  96. if(o.getProjectType()==OrderDunProjectType.HY.getCode()&&o.getDunType()==2) {
  97. calendar2.add(Calendar.DATE, o.getEffectiveCount()*180);
  98. CalendarTimeCleared(calendar2);
  99. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  100. updateOrderDun(o);
  101. Thread.sleep(2000);
  102. }
  103. }
  104. if(o.getProjectType()==OrderDunProjectType.CS.getCode()&&o.getDunType()==2) {
  105. //获取年
  106. int year = calendar2.get(Calendar.YEAR);
  107. //获取月份,0表示1月份
  108. int month = calendar2.get(Calendar.MONTH) + 1;
  109. if(month<10)year=year+1;
  110. if(month>9)year=year+2;
  111. calendar2.set(year, 5, 1);
  112. CalendarTimeCleared(calendar2);
  113. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  114. updateOrderDun(o);
  115. Thread.sleep(2000);
  116. }
  117. }
  118. System.out.println(o.getId()+"\n"+o.getCustomizeTime()+"\n"+o.getDunType());
  119. if (o.getDunType()==0) {
  120. System.out.println("======================================");
  121. calendar2.setTime(o.getCustomizeTime());
  122. CalendarTimeCleared(calendar);
  123. if (calendar.equals(calendar2)) {
  124. updateOrderDun(o);
  125. Thread.sleep(2000);
  126. }
  127. }
  128. }
  129. LoggerUtils.debug(logger, "======================催款任务结束,付款催款开始===================");
  130. List<OutPaymentNode> list2=paymentNodeMapper.selectByStatus(0);
  131. for (OutPaymentNode pn : list2) {
  132. calendar2.setTime(pn.getCreateTime());
  133. if(pn.getProjectType()==OrderDunProjectType.HY.getCode()&&pn.getDunType()==2) {
  134. calendar2.add(Calendar.DATE, pn.getEffectiveCount()*180);
  135. CalendarTimeCleared(calendar2);
  136. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  137. updatePaymentNode(pn);
  138. Thread.sleep(2000);
  139. }
  140. }
  141. if(pn.getProjectType()==OrderDunProjectType.CS.getCode()&&pn.getDunType()==2) {
  142. // 获取年
  143. int year = calendar2.get(Calendar.YEAR);
  144. //获取月份,0表示1月份
  145. int month = calendar2.get(Calendar.MONTH) + 1;
  146. if(month<10)year=year+1;
  147. if(month>9)year=year+2;
  148. calendar2.set(year, 5, 1);
  149. CalendarTimeCleared(calendar2);
  150. if (calendar.getTimeInMillis() > calendar2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  151. updatePaymentNode(pn);
  152. Thread.sleep(2000);
  153. }
  154. }
  155. if (pn.getDunType()==0) {
  156. calendar2.setTime(pn.getPartyTime());
  157. CalendarTimeCleared(calendar);
  158. if (calendar.equals(calendar2)) {
  159. updatePaymentNode(pn);
  160. Thread.sleep(2000);
  161. }
  162. }
  163. }
  164. } catch (Exception e) {
  165. EmailBo emailBo = new EmailBo( "催款处理失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", "催款处理失败");
  166. try {
  167. asyncUtils.send(emailBo);
  168. } catch (UnsupportedEncodingException | MessagingException e1) {
  169. e1.printStackTrace();
  170. }
  171. LoggerUtils.error(getClass(), "=============================================");
  172. LoggerUtils.error(getClass(), "新催款的信息处理失败", e);
  173. LoggerUtils.error(getClass(), "=============================================");
  174. }
  175. //处理旧催款
  176. updateorderDun();
  177. }
  178. private void updatePaymentNode(OutPaymentNode pn) {
  179. PaymentNode p=new PaymentNode();
  180. p.setId(pn.getId());
  181. p.setDunStatus(1);
  182. p.setStartTime(new Date());
  183. paymentNodeMapper.updateByPrimaryKeySelective(p);
  184. orderProjectService.addNoticAndEmail(pn,NoticeStatus.PAYMENT_DUN.getCode());
  185. }
  186. private void CalendarTimeCleared(Calendar calendar) {
  187. calendar.set(Calendar.HOUR_OF_DAY, 0);
  188. calendar.set(Calendar.MINUTE, 0);
  189. calendar.set(Calendar.SECOND, 0);
  190. calendar.set(Calendar.MILLISECOND, 0);
  191. }
  192. private void updateOrderDun(OutNewOrderDunBo t) throws UnsupportedEncodingException, MessagingException {
  193. InputNewOrderDunBo o=new InputNewOrderDunBo();
  194. o.setId(t.getId());
  195. o.setStatus(1);
  196. o.setStartTime(new Date());
  197. newOrderDunService.updateDun(o);
  198. tOrderMidMapper.addOrderReceivables(t.getOrderNo(),t.getMoney());
  199. TOrderMid tm=tOrderMidMapper.selectByOrderNo(t.getOrderNo());
  200. TArrearsDun td=new TArrearsDun();
  201. td.setOrderArrears(tm.getOrderArrears());
  202. td.setOrderReceivables(tm.getOrderReceivables());
  203. if (tArrearsDunMapper.checkOrderNo(t.getOrderNo(),null)<1) {
  204. td.setOrderNo(t.getOrderNo());
  205. tArrearsDunMapper.insertSelective(td);
  206. }else if(tArrearsDunMapper.checkOrderNo(t.getOrderNo(),0)>0){
  207. Integer i=tArrearsDunMapper.selectByStatus(t.getOrderNo(), 0);
  208. td.setId(i);
  209. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  210. }
  211. TOrderNew t2=tOrderNewMapper.selectByPrimaryKey(t.getOrderNo());
  212. orderNewService.addTimingTaskNewDunNoticAndSendEmail(t2, t);
  213. }
  214. /**
  215. * 每月1号凌晨4点轮询处理需要欠款催款的信息
  216. *
  217. */
  218. @Scheduled(cron = "0 0 4 1 * ?")
  219. @RequestMapping(value = "/open/pushOrderArrearsDun", method = RequestMethod.GET)
  220. public void pushOrderArrearsDun(){
  221. try {
  222. LoggerUtils.debug(logger, "======================欠款催款启动===================");
  223. Date date=new Date();
  224. //运算 list(未启动) list2(催款中) list3(已完成)订单的欠款催款
  225. List<TArrearsDun> list=tArrearsDunMapper.selectStatusAll(0);
  226. List<TArrearsDun> list2=tArrearsDunMapper.selectStatusAll(1);
  227. List<TArrearsDun> list3=tArrearsDunMapper.selectStatusAndAmount(2);
  228. for (TArrearsDun td : list) {
  229. TOrderMid tm=tOrderMidMapper.selectByOrderNo(td.getOrderNo());
  230. //如果欠款大于0则需要触发邮件
  231. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  232. td.setDunStatus(1);
  233. td.setStartTime(date);
  234. td.setOrderArrears(tm.getOrderArrears());
  235. td.setOrderReceivables(tm.getOrderReceivables());
  236. addNotic(td.getOrderNo(),td,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  237. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  238. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  239. td.setDunStatus(2);
  240. td.setEndTime(date);
  241. td.setOrderArrears(tm.getOrderArrears());
  242. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  243. }
  244. Thread.sleep(2000);
  245. }
  246. for (TArrearsDun ta : list2) {
  247. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  248. //如果欠款大于0则需要触发邮件
  249. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  250. ta.setDunStatus(3);
  251. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  252. ta.setId(null);
  253. ta.setDunStatus(1);
  254. ta.setStartTime(date);
  255. ta.setOrderArrears(tm.getOrderArrears());
  256. ta.setOrderReceivables(tm.getOrderReceivables());
  257. tArrearsDunMapper.insertSelective(ta);
  258. addNotic(ta.getOrderNo(),ta,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  259. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  260. ta.setDunStatus(2);
  261. ta.setOrderArrears(tm.getOrderArrears());
  262. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  263. }
  264. Thread.sleep(2000);
  265. }
  266. for (TArrearsDun ta : list3) {
  267. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  268. //如果欠款大于0则需要触发邮件
  269. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  270. ta.setDunStatus(3);
  271. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  272. ta.setId(null);
  273. ta.setDunStatus(1);
  274. ta.setStartTime(date);
  275. ta.setOrderArrears(tm.getOrderArrears());
  276. ta.setOrderReceivables(tm.getOrderReceivables());
  277. tArrearsDunMapper.insertSelective(ta);
  278. addNotic(ta.getOrderNo(),ta,NoticeStatus.ORDER_ARREARS_DUN.getCode());
  279. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  280. ta.setDunStatus(2);
  281. ta.setOrderReceivables(tm.getOrderReceivables());
  282. ta.setOrderArrears(tm.getOrderArrears());
  283. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  284. }
  285. Thread.sleep(2000);
  286. }
  287. } catch (Exception e) {
  288. LoggerUtils.error(getClass(), "===========================欠款催款失败==========================", e);
  289. }
  290. }
  291. private void addNotic(String orderNo, TArrearsDun td, Integer code) throws UnsupportedEncodingException, MessagingException {
  292. TOrderNewBo b = tOrderNewMapper.getSaleIdByOno(orderNo);
  293. String str=NoticeStatus.getValueByCode(code)+": 订单编号 -"+orderNo+", 操作人: 平台超管中心 - yxy。";
  294. Notice n =new Notice();
  295. n.setId(UUID.randomUUID().toString());
  296. n.setAid(b.getSalesmanId());
  297. n.setNoticeType(code);
  298. n.setContent(str);
  299. n.setReaded(0);//未读
  300. asyncUtils.addNotice(n);
  301. }
  302. /**
  303. *
  304. *
  305. */
  306. // @RequestMapping(value = "/open/updateOrderDun", method = RequestMethod.GET)
  307. // @Scheduled(cron = "0 0 2 * * ?")
  308. @Transactional(rollbackFor=Exception.class)
  309. public void updateorderDun(){
  310. try {
  311. LoggerUtils.debug(logger, "======================催款任务轮询启动===================");
  312. List<OrderDunTaskBo> list=orderNewService.selectAllOrderDun();
  313. for (OrderDunTaskBo t : list) {
  314. if (t.getDunSubject()/10==1) {//1则为订单催款信息否则为项目催款信息
  315. if (t.getDunSubject()%10==t.getOrderStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  316. updateOrderDun(t);
  317. Thread.sleep(2000);
  318. }
  319. }else {
  320. if (t.getDunSubject()%10>=t.getProjectStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  321. updateOrderDun(t);
  322. Thread.sleep(2000);
  323. }
  324. }
  325. }
  326. } catch (Exception e) {
  327. LoggerUtils.error(getClass(), "=============================================");
  328. LoggerUtils.error(getClass(), "催款的信息处理失败", e);
  329. LoggerUtils.error(getClass(), "=============================================");
  330. }
  331. }
  332. private void updateOrderDun(OrderDunTaskBo t) throws UnsupportedEncodingException, MessagingException {
  333. LoggerUtils.debug(logger, "======================修改催款任务===================");
  334. TOrderDun tDun=new TOrderDun();
  335. tDun.setId(t.getId());
  336. tDun.setDunStatus(1);
  337. tDun.setStartTime(new Date());
  338. orderNewService.updateOrderDun(tDun);
  339. orderNewService.addNoticAndSendEmail(t.getOrderNo(),t,NoticeStatus.ORDER_DUN.getCode());
  340. }
  341. }