3afe9f1107fe2d7d5a8f0e44e08beac9566a8d24.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.chinacreator.common.util.MD5;
  5. import com.chinacreator.process.dao.ColorSealDao;
  6. import com.chinacreator.process.dao.DictionaryDao;
  7. import com.chinacreator.process.util.DesUtil;
  8. import com.chinacreator.process.util.JsonUtil;
  9. import com.chinacreator.process.util.URLUtil;
  10. import com.chinacreator.video.queue.MessageService;
  11. import com.chinacreator.video.queue.bean.MessagePipe;
  12. import org.apache.log4j.Logger;
  13. import org.quartz.DisallowConcurrentExecution;
  14. import org.quartz.PersistJobDataAfterExecution;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import java.text.SimpleDateFormat;
  17. import java.util.*;
  18. /**
  19. * 彩铃侧给用户开通彩铃功能和彩铃权益
  20. *
  21. * @author zhengrong.yan
  22. * @date 20201102
  23. */
  24. @PersistJobDataAfterExecution
  25. @DisallowConcurrentExecution
  26. public class ColorSealPushMQJob {
  27. private Logger logger = Logger.getLogger("colorSeal");
  28. @Autowired
  29. private DictionaryDao dictionaryDao;
  30. @Autowired
  31. private MessageService messageService;
  32. @Autowired
  33. private ColorSealDao colorSealDao;
  34. public void doProcess() throws Exception {
  35. logger.info("彩铃侧给用户开通彩铃功能和彩铃权益队列JOB启动");
  36. if (dictionaryDao.getValue("recivemq").equals("0")) {
  37. long beginTime = System.currentTimeMillis();
  38. List<MessagePipe> list = messageService.reciveBatchMessage("colorSeal", 500);
  39. // List<MessagePipe> list = new ArrayList<>();
  40. // MessagePipe mpipe = new MessagePipe();
  41. // mpipe.setHeader("colorSeal");
  42. // mpipe.addBody("userid", "18673197465");
  43. // mpipe.addBody("type", "colorSeal");//直接推入彩铃的队列
  44. // mpipe.addBody("ordertime", "20201102140155");
  45. // mpipe.addBody("canceltime", "");
  46. // mpipe.addBody("cpid", "mangtv");
  47. // mpipe.addBody("spid", "1008");
  48. // mpipe.addBody("status", "0");
  49. // mpipe.addBody("id", "202011042043579937941");
  50. // list.add(mpipe);
  51. logger.info("接收消息队列花费时间:" + (System.currentTimeMillis() - beginTime));
  52. List<Map> dataList = new ArrayList<Map>();
  53. for (MessagePipe messagePipe : list) {
  54. Map reqBean = transBean(messagePipe.getBody());
  55. dataList.add(reqBean);
  56. }
  57. if (dataList != null && dataList.size() > 0) {
  58. logger.info("待处理数据条数:" + dataList.size());
  59. for (Map reqBean : dataList) {
  60. this.handleOrder(reqBean);
  61. }
  62. }
  63. } else {
  64. logger.info("停止接收队列消息");
  65. }
  66. }
  67. /**
  68. * 业务处理
  69. *
  70. * @param reqBean
  71. */
  72. public void handleOrder(Map reqBean) {
  73. Map logMap = new HashMap();
  74. HashMap hashMap = new HashMap();
  75. logMap.put("data", reqBean);
  76. long starttime = System.currentTimeMillis();
  77. String resultcode = "-1"; //默认失败
  78. String description = "执行中";
  79. String id = "";
  80. Date now = new Date();
  81. String ordertype = "";
  82. HashMap map = new HashMap();
  83. try {
  84. Object channel = reqBean.get("orderchannel");
  85. //调彩铃侧接口
  86. if (reqBean.get("status") != null) {
  87. ordertype = reqBean.get("status").toString();//status 0订购1退订
  88. } else {
  89. ordertype = reqBean.get("orderType") == null ? "" : reqBean.get("orderType").toString();
  90. }
  91. if("0".equals(ordertype)){
  92. channel = reqBean.get("orderchannel");
  93. ordertype = "1";
  94. }else {
  95. ordertype = "2";
  96. channel = reqBean.get("cancelchannel");
  97. }
  98. if (channel != null && "zongheorder".equals(channel) ) {
  99. resultcode = "7000";
  100. description = "不接受该队列数据";
  101. return;
  102. }
  103. /*添加到表*/
  104. map.put("id", colorSealDao.getNo());
  105. map.put("userid", reqBean.get("userid"));
  106. map.put("cpid", reqBean.get("cpid"));
  107. map.put("spid", reqBean.get("spid"));
  108. map.put("orderid", reqBean.get("orderid"));
  109. map.put("ordertype",ordertype);
  110. map.put("interface", "colorsealnotify");
  111. String isreverse = "1";
  112. if("MGTV_APP".equals(channel)){
  113. isreverse = "2";
  114. }
  115. map.put("isreverse", isreverse);
  116. map.put("channel",channel);
  117. reqBean.put("subchannel",channel);
  118. colorSealDao.addOrderRec(map);
  119. HashMap<String, String> remap = sendRights(reqBean);
  120. resultcode = remap.get("resultcode");
  121. description = remap.get("description");
  122. // }
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. resultcode = "8000";
  126. description = "系统错误," + e.getMessage();
  127. logger.error("订购关系出现异常," + e.toString());
  128. } finally {
  129. String time = System.currentTimeMillis() / 1000 - starttime / 1000 + "";//调接口时间
  130. //写日志
  131. logMap.put("time", time);
  132. logMap.put("reusltCode", resultcode);
  133. logMap.put("resultInfo", description);
  134. map.put("resultcode", resultcode);
  135. map.put("resultinfo", description);
  136. try {
  137. colorSealDao.update(map);
  138. }catch (Exception e){
  139. e.printStackTrace();
  140. logMap.put("error","更新表异常");
  141. }
  142. logger.info(JsonUtil.objectToJson(logMap));
  143. }
  144. }
  145. /**
  146. * 解析数据
  147. *
  148. * @param body orderId TD_POINTS_ORDER_REC表ID字段,我方生成的订单流水号
  149. * orderNo 积分商城订单号
  150. * requestId 返回给客户的请求ID
  151. * @return
  152. */
  153. public Map transBean(Map<String, Object> body) {
  154. String jsonStr = JsonUtil.objectToJson(body);
  155. return (Map) JsonUtil.jsonToBean(jsonStr, Map.class);
  156. }
  157. /*
  158. * 赠送会员超时异步重试队列推送,最多重试三次
  159. * @param orderInfo
  160. */
  161. public void inserRetryMq(Map map) {
  162. try {
  163. MessagePipe mpipe = new MessagePipe();
  164. mpipe.setHeader("colorsealretry");
  165. mpipe.addBody("id", map.get("id"));
  166. mpipe.addBody("userid", map.get("userid"));
  167. mpipe.addBody("type", "colorSeal");//直接推入彩铃的队列
  168. mpipe.addBody("ordertime", map.get("ordertime"));
  169. mpipe.addBody("canceltime", map.get("canceltime"));
  170. mpipe.addBody("effecttime", map.get("effecttime"));
  171. mpipe.addBody("cpid", map.get("cpid"));
  172. mpipe.addBody("spid", map.get("spid"));
  173. mpipe.addBody("status", map.get("status"));
  174. this.messageService.sendMessage(mpipe);
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. logger.error("超时异步重试队列推送出现异常," + e.getMessage());
  178. }
  179. }
  180. /**
  181. * 调接口
  182. *
  183. * @return
  184. * @throws Exception
  185. */
  186. private HashMap<String, String> sendRights(Map map) throws Exception {
  187. HashMap<String, String> remap = new HashMap<String, String>();
  188. HashMap<String, String> pushmap = new HashMap<String, String>();//发送接口map
  189. String resultcode = "-1"; //默认失败
  190. String description = "执行中";
  191. try {
  192. String url = this.dictionaryDao.getValue("colorsealNotify");
  193. String userid = map.get("userid").toString();
  194. String spid = map.get("spid").toString();
  195. String type = "0";
  196. if (map.get("status") != null) {
  197. type = map.get("status").toString();//status 0订购1退订
  198. } else {
  199. type = map.get("orderType") == null ? "" : map.get("orderType").toString();
  200. }
  201. Object channel = map.get("subchannel");
  202. /*调接口*/
  203. map.put("url", url);
  204. logger.info(JSON.toJSONString(map));
  205. String result = toInterface(map);//调接口
  206. logger.info("url: " + url);
  207. logger.info("result: " + result);
  208. if (!"".equals(result)) {
  209. net.sf.json.JSONObject resjson = net.sf.json.JSONObject.fromObject(result);
  210. logger.info("赠送权益结果=> userid: " + userid + ", result: " + result);
  211. Map<?, ?> resultmap = JsonUtil.jsonToMap(result);
  212. resultcode = resultmap.get("resultcode") + "";
  213. description = resultmap.get("errorinfo") + "";
  214. if ("0".equals(resultcode)) {//通知成功
  215. if (type.equals("0") && !"MGTV_APP".equals(channel)) {//订购成功的且渠道不为MGTV_APP
  216. url = this.dictionaryDao.getValue("colorsealSet");
  217. map.put("url", url);
  218. map.put("ringid", this.dictionaryDao.getValue("colorsealdefaultring"));//默认铃声
  219. result = toInterface(map);
  220. logger.info("seturl: " + url);
  221. logger.info("setresult: " + result);
  222. }
  223. }
  224. }
  225. } catch (Exception e) {
  226. e.printStackTrace();
  227. logger.error("userid: " + map.get("userid") + "赠送权益失败," + e);
  228. resultcode = "8000";
  229. description = "赠送权益出现异常," + e.getMessage();
  230. } finally {
  231. remap.put("resultcode", resultcode);
  232. remap.put("description", description);
  233. }
  234. return remap;
  235. }
  236. /**
  237. * 通过时间秒毫秒数判断两个时间的间隔
  238. *
  239. * @param date1
  240. * @param date2
  241. * @return
  242. */
  243. public int differentDaysByMillisecond(Date date1, Date date2) {
  244. int days = (int) ((date2.getTime() - date1.getTime()) / (1000));
  245. return days;
  246. }
  247. public static void main(String[] args) {
  248. String pwd = "#EDC2wsx";
  249. String channel = "clmangtv";
  250. String cpid = "mangtv16";
  251. String userid = "RVnTfKtPmqiXLJtmwmtMxA==";
  252. String timestamp = "1605839654";
  253. String spid = "1008";
  254. System.out.println((System.currentTimeMillis()) / 1000 + "");
  255. //System.out.println(userid + channel + spid + cpid + timestamp + pwd);
  256. String ringid = "99103000202008285994840";
  257. String signature_fornotify = MD5.MD5Encode(userid + cpid + spid + channel + ringid + timestamp + pwd);
  258. System.out.println(signature_fornotify);
  259. String lcalsign = "RVnTfKtPmqiXLJtmwmtMxA==mangtv161008clmangtv1605839654#EDC2wsx";
  260. System.out.println(MD5.MD5Encode(lcalsign));
  261. }
  262. private String getMonthLastDay() {
  263. //设置时间格式
  264. SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
  265. //获得实体类
  266. Calendar ca = Calendar.getInstance();
  267. //设置最后一天
  268. ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
  269. //最后一天格式化
  270. String lastDay = format.format(ca.getTime());
  271. return lastDay + "235959";
  272. }
  273. private String toInterface(Map map) throws Exception {
  274. try {
  275. String url = map.get("url").toString();
  276. String userid = map.get("userid").toString();
  277. String spid = map.get("spid").toString();
  278. String type = "0";
  279. if (map.get("status") != null) {
  280. type = map.get("status").toString();//status 0订购1退订
  281. } else {
  282. type = map.get("orderType") == null ? "" : map.get("orderType").toString();
  283. }
  284. Short consumeType = 1;//1订购2退订
  285. String subTime = map.get("ordertime").toString();//订购时间 consumeType为1时必传
  286. // String unsubTime = map.get("canceltime").toString();//退订时间 consumeType为2时必传
  287. // String startTime = subTime;//生效时间 consumeType为1时必传
  288. //接口传递参数
  289. if (type.equals("1")) {//退订
  290. consumeType = 2;
  291. }
  292. String pwd = "#EDC2wsx";
  293. String channel = "clmangtv";
  294. String cpid = "mangtv16";
  295. type = consumeType + "";
  296. userid = DesUtil.encode(userid, pwd);
  297. String timestamp = (System.currentTimeMillis()) / 1000 + "";
  298. System.out.println(userid + channel + spid + cpid + timestamp + pwd);
  299. String signature_fornotify = MD5.MD5Encode(userid + cpid + spid + channel + timestamp + pwd);
  300. String jsonParams = "";
  301. JSONObject json = new JSONObject();
  302. json.put("userid", userid);
  303. json.put("cpid", cpid);
  304. json.put("spid", spid);
  305. json.put("type", type);
  306. json.put("channel", channel);
  307. json.put("timestamp", timestamp);
  308. json.put("signature", signature_fornotify);
  309. json.put("subchannel",map.get("subchannel"));
  310. if (map.get("ringid") != null && !"".equals(map.get("ringid"))) {
  311. String ringid = map.get("ringid").toString();
  312. String signature_forset = MD5.MD5Encode(userid + cpid + spid + channel + ringid + timestamp + pwd);
  313. url = map.get("url").toString();
  314. json.put("ringid", ringid);
  315. json.put("signature", signature_forset);
  316. }
  317. json.put("isreverse", "1");//反向通知标志
  318. jsonParams = json.toJSONString();
  319. System.out.println(url);
  320. System.out.println(jsonParams);
  321. String result = invokePostJson(url, jsonParams);
  322. return result;
  323. } catch (Exception e) {
  324. e.printStackTrace();
  325. throw e;
  326. }
  327. }
  328. public static String invokePostJson(String url, String params) {
  329. try {
  330. String resp = URLUtil.postJson(url, params);
  331. System.out.println(resp);
  332. return resp;
  333. } catch (Exception e) {
  334. // TODO Auto-generated catch block
  335. e.printStackTrace();
  336. }
  337. return "";
  338. }
  339. }