f1c5b2c6b3642f902589bb6f6079ab344abc7bf6.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.NewYearVideoDao;
  6. import com.chinacreator.process.exception.BusinessException;
  7. import com.chinacreator.process.util.Bash;
  8. import com.chinacreator.process.util.JsonUtil;
  9. import com.chinacreator.process.util.TripleDES;
  10. import com.chinacreator.process.util.URLUtil;
  11. import com.chinacreator.process.util.fakeid.MD5;
  12. import com.frameworkset.util.StringUtil;
  13. import org.apache.log4j.Logger;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import javax.net.ssl.HttpsURLConnection;
  16. import javax.net.ssl.SSLContext;
  17. import java.io.BufferedInputStream;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.OutputStreamWriter;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.concurrent.*;
  27. /**
  28. * @author zhengrong.yan
  29. * @date 2021/2/8 9:45
  30. */
  31. public class NewYearFailVideoJob {
  32. private Logger log = Logger.getLogger("newyearfailvideo");
  33. @Autowired
  34. private DictionaryDao dictionaryDao;
  35. @Autowired
  36. private NewYearVideoDao newYearVideoDao;
  37. public void doProcess() throws Exception {
  38. log.info(Thread.currentThread().getName() + "定时任务开始");
  39. long beginTime = System.currentTimeMillis();
  40. List<HashMap> list = newYearVideoDao.getFailData();
  41. if (list != null && list.size() > 0) {
  42. CountDownLatch threadSignal = new CountDownLatch(list.size());
  43. ExecutorService executorService = new ThreadPoolExecutor(12, 16, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  44. //去除重复数据
  45. //List<HashMap> dataList = paraseData(list);
  46. log.info("数据库有效需要处理的数:" + list.size());
  47. for (HashMap vipmap : list) {
  48. NewYearVideoMakeService continueService = new NewYearVideoMakeService(list.size(), threadSignal, vipmap, dictionaryDao, newYearVideoDao);
  49. executorService.execute(continueService);
  50. }
  51. executorService.shutdown();
  52. try {
  53. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  54. } catch (InterruptedException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. log.info(Thread.currentThread().getName() + "定时任务完成,耗时:" + (System.currentTimeMillis() - beginTime) / 1000 + " 秒");
  59. }
  60. /**
  61. * 去除重复数据,防止赠送会员接口被并发限制
  62. *
  63. * @param dataList
  64. * @return
  65. */
  66. private List<HashMap> paraseData(List<HashMap> dataList) {
  67. //去重复后的数据集
  68. List<HashMap> reDataList = new ArrayList<HashMap>();
  69. HashMap<String, List> tmpMap = new HashMap<String, List>();
  70. for (HashMap dataMap : dataList) {
  71. if (tmpMap.containsKey(dataMap.get("USERID").toString())) {
  72. log.info("重复数据," + dataMap.get("USERID"));
  73. } else {
  74. reDataList.add(dataMap);
  75. List tmpList = new ArrayList();
  76. tmpList.add(dataMap.get("ID"));
  77. tmpMap.put(dataMap.get("USERID").toString(), tmpList);
  78. }
  79. }
  80. return reDataList;
  81. }
  82. static class NewYearVideoMakeService implements Runnable {
  83. private static Logger log = Logger.getLogger("newyearfailvideo");
  84. private int totalSize;
  85. private CountDownLatch threadSignal;
  86. private HashMap busimap;
  87. private DictionaryDao dictionaryDao;
  88. private NewYearVideoDao newYearVideoDao;
  89. public NewYearVideoMakeService(int totalSize, CountDownLatch threadSignal, HashMap busimap, DictionaryDao dictionaryDao, NewYearVideoDao newYearVideoDao) {
  90. this.totalSize = totalSize;
  91. this.threadSignal = threadSignal;
  92. this.busimap = busimap;
  93. this.dictionaryDao = dictionaryDao;
  94. this.newYearVideoDao = newYearVideoDao;
  95. }
  96. /**
  97. * 业务处理
  98. *
  99. * @param
  100. */
  101. public void run() {
  102. long startime = System.currentTimeMillis();
  103. Map logMap = new HashMap();
  104. //logMap.put("data", busimap);
  105. String resultCode = "-1";
  106. String resultInfo = "";
  107. String result = "";
  108. String id = String.valueOf(busimap.get("ID"));
  109. try {
  110. //调拓维接口
  111. result = invoke(busimap);
  112. JSONObject jsonObject = JSON.parseObject(result);
  113. resultCode = jsonObject.getString("code");
  114. resultInfo = jsonObject.getString("msg");
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. if (e instanceof BusinessException) {
  118. resultInfo = ((BusinessException) e).getMessage();
  119. resultCode = ((BusinessException) e).getCode();
  120. } else {
  121. resultCode = "8000";
  122. resultInfo = "系统错误," + e.getMessage();
  123. }
  124. } finally {
  125. threadSignal.countDown();
  126. try {
  127. if (resultInfo != null && resultInfo.length() > 250) {
  128. resultInfo = resultInfo.substring(0, 250);
  129. }
  130. newYearVideoDao.updateHasNotify(id, resultCode, resultInfo);
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. log.error("更新数据出现异常," + id + ", resultCode:" + resultCode + ", resultInfo:" + resultInfo);
  134. }
  135. logMap.put("reusltCode", resultCode);
  136. logMap.put("resultInfo", resultInfo);
  137. logMap.put("result", result);
  138. log.info(JsonUtil.objectToJson(logMap));
  139. }
  140. }
  141. /**
  142. * 调合作方接口
  143. *
  144. * @param
  145. * @return
  146. * @throws Exception
  147. */
  148. private String invoke(Map params) throws Exception {
  149. String result = "";
  150. try {
  151. String url = dictionaryDao.getValue("twWXNotify");//调拓维通知接口
  152. String openid = String.valueOf(params.get("USERID"));
  153. System.out.println("openid===>" + openid);
  154. String finishtime = String.valueOf(params.get("FINISHTIME"));
  155. System.out.println("finishitime===>"+finishtime);
  156. String templateId = "onueANxf51HC-K0A-Imyjmyhf75aFP6M429rkcxS3Q8";
  157. String tourl = "https://live.v.wo.cn/platform/ytpxz/univideo/personal.html?id=" + openid;
  158. url += "?openId=" + openid + "&templateId=" + templateId + "&url=" + tourl;
  159. //新年视频换脸
  160. //url = "https://fifa.wo186.tv/wx/sendTemplateMessage.do?openId=ojCvV0t_sRfF1a5AolUTextKHfvM&templateId=fa5QGaGoY9YDB_vPz4q890JO68tLAv97_BjTkY3hTY4&url=";
  161. //新年视频换脸
  162. String jsonParams = "";
  163. JSONObject jsondata = new JSONObject();
  164. String[] s = {"remark", "first", "keyword1", "keyword2"};
  165. String[] s2 = {"您上传的照片不符合要求,视频制作失败,可点击重新制作", "非常抱歉,您的超清版视频制作失败了", "失败", finishtime};
  166. for (int i = 0; i < 4; i++) {
  167. JSONObject jsonObject = new JSONObject();
  168. jsonObject.put("color", "#000000");
  169. jsonObject.put("value", s2[i]);
  170. jsondata.put(s[i], jsonObject);
  171. }
  172. jsonParams = jsondata.toJSONString();
  173. log.info(url);
  174. log.info(jsondata.toJSONString());
  175. try {
  176. //加密
  177. String keyStr = "d663bd873d93a76ab2ce11e9";
  178. String txt = TripleDES.encrypt(jsonParams, keyStr);
  179. ;
  180. System.out.println(txt);
  181. //MD5
  182. String sid = "h5game";
  183. String timestamp = System.currentTimeMillis() / 1000 + "";
  184. String sign = sid + "&" + timestamp + "&" + txt + "&" + keyStr;
  185. sign = MD5.MD5Encode(sign);
  186. URL url2 = new URL(url);
  187. SSLContext sc = SSLContext.getInstance("TLSv1.2"); //$NON-NLS-1$
  188. sc.init(null, null, new java.security.SecureRandom());
  189. HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
  190. conn.setSSLSocketFactory(sc.getSocketFactory());
  191. conn.setRequestMethod("POST");
  192. conn.addRequestProperty("content-type", "application/json;charset=UTF-8");
  193. conn.addRequestProperty("sid", sid);
  194. conn.addRequestProperty("timestamp", timestamp);
  195. conn.addRequestProperty("sign", sign);
  196. conn.setDoOutput(true);
  197. conn.setUseCaches(false);
  198. OutputStreamWriter out = null;
  199. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  200. out.write(txt);
  201. out.flush();
  202. result = URLUtil.readInputStream(conn.getInputStream());
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. }
  206. log.info("url: " + url);
  207. log.info("调合作方接口结果=> wxuserid: " + String.valueOf(busimap.get("USERID")) + ", id: " + String.valueOf(busimap.get("ID")) + " , result: " + result);
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. log.error("id=>" + String.valueOf(params.get("ID")) + "调拓维接口失败," + e);
  211. }
  212. return result;
  213. }
  214. /**
  215. * 解析数据
  216. *
  217. * @param body orderId TD_POINTS_ORDER_REC表ID字段,我方生成的订单流水号
  218. * orderNo 积分商城订单号
  219. * requestId 返回给客户的请求ID
  220. * @return
  221. */
  222. public Map transBean(Map<String, Object> body) {
  223. String jsonStr = JsonUtil.objectToJson(body);
  224. return (Map) JsonUtil.jsonToBean(jsonStr, Map.class);
  225. }
  226. }
  227. }