0430c069dd6e5467700b460b78e5c677d94fd049.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.chinacreator.process.dao.DictionaryDao;
  5. import com.chinacreator.process.dao.EverySmsDao;
  6. import com.chinacreator.process.exception.BusinessException;
  7. import com.chinacreator.process.util.JsonUtil;
  8. import com.chinacreator.process.util.URLUtil;
  9. import org.apache.log4j.Logger;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import java.text.SimpleDateFormat;
  12. import java.util.*;
  13. /**
  14. * @author zhengrong.yan
  15. * @date 2021/2/1 9:45
  16. */
  17. public class EverySmsJob {
  18. private Logger log = Logger.getLogger("everysms");
  19. @Autowired
  20. private DictionaryDao dictionaryDao;
  21. @Autowired
  22. private EverySmsDao everySmsDao;
  23. public void doProcess() throws Exception {
  24. log.info("每日流量超量短信下发定时任务开始");
  25. System.out.println("每日流量超量短信下发定时任务开始");
  26. long beginTime = System.currentTimeMillis();
  27. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  28. Calendar c = Calendar.getInstance();
  29. c.setTime(new Date());
  30. c.add(Calendar.DAY_OF_MONTH, -1);
  31. Date yesterday = c.getTime();//这是昨天
  32. String date = sdf.format(yesterday);
  33. //统计数据到数据库
  34. List<HashMap> list = everySmsDao.getData(date);
  35. if (list != null && list.size() > 0) {
  36. log.info("数据库有效需要处理的数:" + list.size());
  37. for (HashMap busimap : list) {
  38. if (busimap.get("COUNT") == 0) {
  39. log.info(date + "数据异常,统计数据为0");
  40. } else {
  41. deal(busimap);
  42. }
  43. }
  44. }
  45. log.info(Thread.currentThread().getName() + "定时任务完成,耗时:" + (System.currentTimeMillis() - beginTime) / 1000 + " 秒");
  46. }
  47. /*
  48. * 处理数据
  49. * */
  50. public void deal(Map busimap) {
  51. Map logMap = new HashMap();
  52. logMap.put("data", busimap);
  53. String resultCode = "-1";
  54. String resultInfo = "";
  55. String id = String.valueOf(busimap.get("ID"));
  56. try {
  57. //提交工单给合作方
  58. String result = invoke(busimap);
  59. JSONObject jsonObject = JSON.parseObject(result);
  60. resultCode = jsonObject.getString("code");
  61. resultInfo = jsonObject.getString("message");
  62. //重试三次
  63. if (!"0".equals(resultCode)) {
  64. int flag = 1;
  65. while (flag > 3 || "0".equals(resultCode)) {
  66. result = invoke(busimap);
  67. JSONObject object = JSON.parseObject(result);
  68. resultCode = object.getString("code");
  69. resultInfo = object.getString("message");
  70. flag++;
  71. }
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. if (e instanceof BusinessException) {
  76. resultInfo = ((BusinessException) e).getMessage();
  77. resultCode = ((BusinessException) e).getCode();
  78. } else {
  79. resultCode = "8000";
  80. resultInfo = "系统错误," + e.getMessage();
  81. }
  82. } finally {
  83. try {
  84. if (resultInfo != null && resultInfo.length() > 250) {
  85. resultInfo = resultInfo.substring(0, 250);
  86. }
  87. everySmsDao.upResult(id, resultCode, resultInfo);
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. log.error("更新数据出现异常," + id + ", resultCode:" + resultCode + ", resultInfo:" + resultInfo);
  91. }
  92. logMap.put("reusltCode", resultCode);
  93. logMap.put("resultInfo", resultInfo);
  94. log.info(JsonUtil.objectToJson(logMap));
  95. }
  96. }
  97. /**
  98. * 调合作方接口
  99. *
  100. * @param
  101. * @return
  102. * @throws Exception
  103. */
  104. private String invoke(Map params) throws Exception {
  105. String result = "";
  106. String url = dictionaryDao.getValue("everysmsUrl");
  107. String jsonParams = "";
  108. String id = String.valueOf(params.get("ID"));
  109. JSONObject json = new JSONObject();
  110. json.put("date", params.get("DATETM"));
  111. json.put("sendnumber", params.get("COUNT"));
  112. jsonParams = json.toJSONString();
  113. log.info("url: " + url);
  114. log.info(jsonParams);
  115. result = URLUtil.postJson(url, jsonParams);
  116. log.info("调合作方接口结果=> id: " + id + " , result: " + result);
  117. return result;
  118. }
  119. /**
  120. * 解析数据
  121. *
  122. * @param body orderId TD_POINTS_ORDER_REC表ID字段,我方生成的订单流水号
  123. * orderNo 积分商城订单号
  124. * requestId 返回给客户的请求ID
  125. * @return
  126. */
  127. public Map transBean(Map<String, Object> body) {
  128. String jsonStr = JsonUtil.objectToJson(body);
  129. return (Map) JsonUtil.jsonToBean(jsonStr, Map.class);
  130. }
  131. /**
  132. * 获取请求属性性
  133. *
  134. * @return
  135. */
  136. private static Map getProperty() {
  137. Map reqProperty = new HashMap();
  138. reqProperty.put("Content-type", "application/json;charset=UTF-8");
  139. return reqProperty;
  140. }
  141. }