OrderDunTask.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.goafanti.common.task;
  2. import java.math.BigDecimal;
  3. import java.util.*;
  4. import com.goafanti.admin.bo.AdminListBo;
  5. import com.goafanti.common.bo.ErrorDunListBo;
  6. import com.goafanti.common.dao.*;
  7. import com.goafanti.core.shiro.token.TokenManager;
  8. import com.goafanti.order.bo.*;
  9. import com.goafanti.order.service.LegalAffairsService;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.scheduling.annotation.Scheduled;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import com.goafanti.common.bo.EmailBo;
  18. import com.goafanti.common.constant.AFTConstants;
  19. import com.goafanti.common.enums.NoticeStatus;
  20. import com.goafanti.common.model.Notice;
  21. import com.goafanti.common.model.PaymentNode;
  22. import com.goafanti.common.model.TArrearsDun;
  23. import com.goafanti.common.model.TOrderMid;
  24. import com.goafanti.common.model.TOrderNew;
  25. import com.goafanti.common.utils.AsyncUtils;
  26. import com.goafanti.common.utils.LoggerUtils;
  27. import com.goafanti.order.enums.OrderDunProjectType;
  28. import com.goafanti.order.service.NewOrderDunService;
  29. import com.goafanti.order.service.OrderNewService;
  30. import com.goafanti.order.service.OrderProjectService;
  31. import com.goafanti.organization.bo.OutPaymentNode;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RequestMethod;
  34. import org.springframework.web.bind.annotation.RestController;
  35. @Component
  36. //@RestController
  37. public class OrderDunTask {
  38. @Autowired
  39. private OrderNewService orderNewService;
  40. @Autowired
  41. private NewOrderDunService newOrderDunService;
  42. @Autowired
  43. private TOrderNewMapper tOrderNewMapper;
  44. @Autowired
  45. private TArrearsDunMapper tArrearsDunMapper;
  46. @Autowired
  47. private TOrderMidMapper tOrderMidMapper;
  48. @Autowired
  49. private AdminMapper adminMapper;
  50. @Autowired
  51. private PaymentNodeMapper paymentNodeMapper;
  52. @Autowired
  53. private OrderProjectService orderProjectService;
  54. @Autowired
  55. private LegalAffairsService legalAffairsService;
  56. @Value(value = "${dev.name}")
  57. private String devName=null;
  58. @Autowired
  59. private AsyncUtils asyncUtils;
  60. Logger logger = LoggerFactory.getLogger(OrderDunTask.class);
  61. /**
  62. * 每天凌晨3点轮
  63. */
  64. // @ResponseBody
  65. @Scheduled(cron = "0 0 3 * * ?")
  66. // @RequestMapping(value = "/open/test", method = RequestMethod.GET)
  67. // @Scheduled(cron = "0 47 11 * * ?")
  68. @Transactional(rollbackFor=Exception.class)
  69. public void startTask() throws InterruptedException {
  70. updateNewOrderDun();
  71. Thread.sleep(2000);
  72. updatePaymentNode();
  73. Thread.sleep(2000);
  74. pushLegalAffairs();
  75. }
  76. /**
  77. * 法务订单催款处理
  78. */
  79. // @RequestMapping(value = "/open/pushLegalAffairs", method = RequestMethod.GET)
  80. public void pushLegalAffairs() {
  81. InputOrderListBo in = new InputOrderListBo();
  82. in.setType(0);
  83. in.setLegalAffairs(0);
  84. List<OutOrderLegalAffairsListBo> list=legalAffairsService.getLegalAffairsOrder(in);
  85. ArrayList<String> addList = new ArrayList<>();
  86. Date now=new Date();
  87. for (OutOrderLegalAffairsListBo out : list) {
  88. Long l=out.getDunStartTime().getTime()+AFTConstants.DUN_START_LIMIT_MS;
  89. if (l<now.getTime()){
  90. addList.add(out.getOrderNo());
  91. }
  92. }
  93. List<String> addList2=new ArrayList<>();
  94. if (!addList.isEmpty())
  95. for (int i=0;i<addList.size();i++){
  96. addList2.add(addList.get(i));
  97. if (50 == addList2.size() || i == addList.size() - 1){
  98. tOrderMidMapper.updateByList(addList2);
  99. legalAffairsService.addLagalAffairsLogList(addList2);
  100. addList2.clear();
  101. }
  102. }
  103. }
  104. /**
  105. * 询处理需要新催款的信息
  106. */
  107. // @RequestMapping(value = "/open/updateNewOrderDun", method = RequestMethod.GET)
  108. public void updateNewOrderDun(){
  109. try {
  110. LoggerUtils.debug(logger, "======================新催款任务轮询启动===================");
  111. List<OutNewOrderDunBo> list= newOrderDunService.selectAllOrderDun();
  112. Calendar now=Calendar.getInstance();
  113. Calendar now2=Calendar.getInstance();
  114. Date date =new Date();
  115. for (OutNewOrderDunBo o : list) {
  116. now.setTime(date);
  117. now2.setTime(o.getCreateTime());
  118. //计算特批首期款 特批通过 为催款为首期款 催款时间不为空则计算触发
  119. if (o.getApproval()!=null&& o.getApproval()==2&& o.getDunType()==1&&o.getWaitDay()!=null) {
  120. now2.add(Calendar.DATE, o.getWaitDay());
  121. updateOrderDun(o,now,now2);
  122. //会员-期限计算
  123. }else if(o.getProjectType().equals(OrderDunProjectType.HY.getCode())&&o.getDunType()==2) {
  124. now2.add(Calendar.DATE, o.getEffectiveCount()*180);
  125. CalendarTimeCleared(now2);
  126. updateOrderDun(o,now,now2);
  127. //财税-期限 计算时间
  128. } else if(o.getProjectType().equals(OrderDunProjectType.CS.getCode())&&o.getDunType()==2) {
  129. //获取年
  130. int year = now2.get(Calendar.YEAR);
  131. //获取月份,0表示1月份
  132. int month = now2.get(Calendar.MONTH) + 1;
  133. if(month<10)year=year+1;
  134. if(month>9)year=year+2;
  135. now2.set(year, 5, 1);
  136. CalendarTimeCleared(now2);
  137. updateOrderDun(o,now,now2);
  138. //自定义 超过设置的时间则触发
  139. }else if (o.getDunType()==0) {
  140. now2.setTime(o.getCustomizeTime());
  141. CalendarTimeCleared(now);
  142. updateOrderDun(o,now,now2);
  143. }
  144. }
  145. calibrationDun();
  146. } catch (Exception e) {
  147. EmailBo emailBo = new EmailBo( "催款处理失败", AFTConstants.ADMIN_EMAIL, "超管", "平台", "系统", devName+"催款处理失败");
  148. asyncUtils.send(emailBo);
  149. LoggerUtils.error(getClass(), "===================新催款的信息处理失败======================", e);
  150. }
  151. }
  152. /**
  153. * 如果有不对的数据就校准数据
  154. */
  155. private void calibrationDun() {
  156. List<ErrorDunListBo> el = tOrderMidMapper.selectErrorDun();
  157. if (!el.isEmpty()){
  158. StringBuffer sb=new StringBuffer();
  159. for (ErrorDunListBo e : el) {
  160. sb.append(e.toString());
  161. }
  162. EmailBo emailBo=new EmailBo("催款异常核对",AFTConstants.ADMIN_EMAIL,sb.toString());
  163. asyncUtils.send(emailBo);
  164. tOrderMidMapper.updateErrorDun();
  165. }
  166. }
  167. /**
  168. * 处理付款催款
  169. */
  170. private void updatePaymentNode() {
  171. LoggerUtils.debug(logger, "======================催款任务结束,付款催款开始===================");
  172. Calendar now=Calendar.getInstance();
  173. Calendar now2=Calendar.getInstance();
  174. try {
  175. List<OutPaymentNode> list2=paymentNodeMapper.selectByStatus(0);
  176. for (OutPaymentNode pn : list2) {
  177. now2.setTime(pn.getCreateTime());
  178. if(pn.getProjectType().equals(OrderDunProjectType.HY.getCode())&&pn.getDunType()==2) {
  179. now2.add(Calendar.DATE, pn.getEffectiveCount()*180);
  180. CalendarTimeCleared(now2);
  181. }else if(pn.getProjectType().equals(OrderDunProjectType.CS.getCode())&&pn.getDunType()==2) {
  182. // 获取年
  183. int year = now2.get(Calendar.YEAR);
  184. //获取月份,0表示1月份
  185. int month = now2.get(Calendar.MONTH) + 1;
  186. if(month<10)year=year+1;
  187. if(month>9)year=year+2;
  188. now2.set(year, 5, 1);
  189. CalendarTimeCleared(now2);
  190. }else if (pn.getDunType()==0) {
  191. now2.setTime(pn.getPartyTime());
  192. CalendarTimeCleared(now);
  193. }
  194. if (now.getTimeInMillis() > now2.getTimeInMillis()) {//当前时间大于计算后时间则触发
  195. updatePaymentNode(pn);
  196. Thread.sleep(1000);
  197. }
  198. }
  199. } catch (InterruptedException e) {
  200. LoggerUtils.error(getClass(), "===========================付款催款失败==========================", e);
  201. }
  202. }
  203. /**
  204. * 每月1号凌晨4点轮询处理需要欠款催款的信息
  205. *
  206. */
  207. @Scheduled(cron = "0 0 4 1 * ?")
  208. // @RequestMapping(value = "/open/pushOrderArrearsDun", method = RequestMethod.GET)
  209. public void pushOrderArrearsDun(){
  210. try {
  211. LoggerUtils.debug(logger, "======================欠款催款启动===================");
  212. Date date=new Date();
  213. //运算 list(未启动) list2(催款中) list3(已完成)订单的欠款催款
  214. List<TArrearsDun> list=tArrearsDunMapper.selectStatusAll(0);
  215. List<TArrearsDun> list2=tArrearsDunMapper.selectStatusAll(1);
  216. List<TArrearsDun> list3=tArrearsDunMapper.selectStatusAndAmount(2);
  217. for (TArrearsDun td : list) {
  218. TOrderNewBo b = tOrderNewMapper.getSaleIdByOno(td.getOrderNo());
  219. AdminListBo a = adminMapper.getDeptNameByAid(TokenManager.getAdminId()==null?"1":TokenManager.getAdminId());
  220. TOrderMid tm=tOrderMidMapper.selectByOrderNo(td.getOrderNo());
  221. //如果欠款大于0则需要触发邮件
  222. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  223. td.setDunStatus(1);
  224. td.setStartTime(date);
  225. td.setOrderArrears(tm.getOrderArrears());
  226. td.setOrderReceivables(tm.getOrderReceivables());
  227. orderNewService.addNotic(NoticeStatus.ORDER_ARREARS_DUN.getCode(),a,b);
  228. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  229. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  230. td.setDunStatus(2);
  231. td.setEndTime(date);
  232. td.setOrderArrears(tm.getOrderArrears());
  233. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  234. }
  235. Thread.sleep(2000);
  236. }
  237. for (TArrearsDun ta : list2) {
  238. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  239. TOrderNewBo b = tOrderNewMapper.getSaleIdByOno(ta.getOrderNo());
  240. AdminListBo a = adminMapper.getDeptNameByAid(TokenManager.getAdminId()==null?"1":TokenManager.getAdminId());
  241. //如果欠款大于0则需要触发邮件
  242. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  243. ta.setDunStatus(3);
  244. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  245. ta.setId(null);
  246. ta.setDunStatus(1);
  247. ta.setStartTime(date);
  248. ta.setOrderArrears(tm.getOrderArrears());
  249. ta.setOrderReceivables(tm.getOrderReceivables());
  250. tArrearsDunMapper.insertSelective(ta);
  251. orderNewService.addNotic(NoticeStatus.ORDER_ARREARS_DUN.getCode(),a,b);
  252. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  253. ta.setDunStatus(2);
  254. ta.setOrderArrears(tm.getOrderArrears());
  255. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  256. }
  257. Thread.sleep(2000);
  258. }
  259. for (TArrearsDun ta : list3) {
  260. TOrderMid tm=tOrderMidMapper.selectByOrderNo(ta.getOrderNo());
  261. TOrderNewBo b = tOrderNewMapper.getSaleIdByOno(ta.getOrderNo());
  262. AdminListBo a = adminMapper.getDeptNameByAid(TokenManager.getAdminId()==null?"1":TokenManager.getAdminId());
  263. //如果欠款大于0则需要触发邮件
  264. if (tm.getOrderArrears().compareTo(new BigDecimal(0))>0) {
  265. ta.setDunStatus(3);
  266. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  267. ta.setId(null);
  268. ta.setDunStatus(1);
  269. ta.setStartTime(date);
  270. ta.setOrderArrears(tm.getOrderArrears());
  271. ta.setOrderReceivables(tm.getOrderReceivables());
  272. tArrearsDunMapper.insertSelective(ta);
  273. orderNewService.addNotic(NoticeStatus.ORDER_ARREARS_DUN.getCode(),a,b);
  274. }else if(tm.getOrderArrears().compareTo(new BigDecimal(0))<1) {
  275. ta.setDunStatus(2);
  276. ta.setOrderReceivables(tm.getOrderReceivables());
  277. ta.setOrderArrears(tm.getOrderArrears());
  278. tArrearsDunMapper.updateByPrimaryKeySelective(ta);
  279. }
  280. Thread.sleep(2000);
  281. }
  282. } catch (Exception e) {
  283. LoggerUtils.error(getClass(), "===========================欠款催款失败==========================", e);
  284. }
  285. }
  286. // /**
  287. // * 旧催款处理
  288. // *
  289. // */
  290. // public void updateorderDun(){
  291. // try {
  292. // LoggerUtils.debug(logger, "======================催款任务轮询启动===================");
  293. // List<OrderDunTaskBo> list=orderNewService.selectAllOrderDun();
  294. // for (OrderDunTaskBo t : list) {
  295. // if (t.getDunSubject()/10==1) {//1则为订单催款信息否则为项目催款信息
  296. // if (t.getDunSubject()%10==t.getOrderStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  297. // updateOrderDun(t);
  298. // Thread.sleep(2000);
  299. // }
  300. // }else {
  301. // if (t.getDunSubject()%10>=t.getProjectStatus()&&t.getSettlementAmount().compareTo(t.getMoney()) == -1) {
  302. // updateOrderDun(t);
  303. // Thread.sleep(2000);
  304. // }
  305. // }
  306. // }
  307. // } catch (Exception e) {
  308. // LoggerUtils.error(getClass(), "==========================催款的信息处理失败==========================", e);
  309. // }
  310. //
  311. // }
  312. private void updatePaymentNode(OutPaymentNode pn) {
  313. PaymentNode p=new PaymentNode();
  314. p.setId(pn.getId());
  315. p.setDunStatus(1);
  316. p.setStartTime(new Date());
  317. paymentNodeMapper.updateByPrimaryKeySelective(p);
  318. orderProjectService.addNoticAndEmail(pn,NoticeStatus.PAYMENT_DUN.getCode());
  319. }
  320. private void CalendarTimeCleared(Calendar now) {
  321. now.set(Calendar.HOUR_OF_DAY, 0);
  322. now.set(Calendar.MINUTE, 0);
  323. now.set(Calendar.SECOND, 0);
  324. now.set(Calendar.MILLISECOND, 0);
  325. }
  326. /**
  327. * 定时催款触发
  328. * 如果当前时间大于等于触发时间才触发流程
  329. * @param t
  330. * @param now 当前时间
  331. * @param now2 触发时间
  332. */
  333. private void updateOrderDun(OutNewOrderDunBo t,Calendar now,Calendar now2) {
  334. if (now.getTimeInMillis() >= now2.getTimeInMillis()) {
  335. InputNewOrderDunBo o=new InputNewOrderDunBo();
  336. TOrderNew orderNew=tOrderNewMapper.selectByPrimaryKey(t.getOrderNo());
  337. Date date = new Date();
  338. o.setId(t.getId());
  339. o.setStatus(1);
  340. o.setStartTime(date);
  341. if (t.getMoney()==null)t.setMoney(new BigDecimal(0));
  342. newOrderDunService.updateDun(o);
  343. TOrderMid tm=orderNewService.pushOrderMidDun(t.getOrderNo(),orderNew.getSettlementAmount(),date,t);
  344. TArrearsDun td=new TArrearsDun();
  345. td.setOrderArrears(tm.getOrderArrears());
  346. td.setOrderReceivables(tm.getOrderReceivables());
  347. if (tArrearsDunMapper.checkOrderNo(t.getOrderNo(),null)<1) {
  348. td.setOrderNo(t.getOrderNo());
  349. tArrearsDunMapper.insertSelective(td);
  350. }else if(tArrearsDunMapper.checkOrderNo(t.getOrderNo(),0)>0){
  351. Integer i=tArrearsDunMapper.selectByStatus(t.getOrderNo(), 0);
  352. td.setId(i);
  353. tArrearsDunMapper.updateByPrimaryKeySelective(td);
  354. }
  355. orderNewService.addTimingTaskNewDunNoticAndSendEmail(orderNew, t);
  356. try {
  357. Thread.sleep(2000);
  358. } catch (InterruptedException e) {
  359. e.printStackTrace();
  360. }
  361. }
  362. }
  363. }