cd7d98a1de9f6f4fa9cae3ef20a53c676607b654.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.chinacreator.process.dao.DictionaryDao;
  6. import com.chinacreator.process.dao.NewYearVideoDao;
  7. import com.chinacreator.process.exception.BusinessException;
  8. import com.chinacreator.process.util.Bash;
  9. import com.chinacreator.process.util.JsonUtil;
  10. import com.chinacreator.process.util.TripleDES;
  11. import com.chinacreator.process.util.URLUtil;
  12. import com.chinacreator.process.util.fakeid.MD5;
  13. import com.frameworkset.util.StringUtil;
  14. import oracle.sql.CLOB;
  15. import org.apache.log4j.Logger;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import sun.misc.BASE64Decoder;
  18. import javax.crypto.SecretKey;
  19. import javax.crypto.spec.SecretKeySpec;
  20. import javax.net.ssl.HttpsURLConnection;
  21. import javax.net.ssl.SSLContext;
  22. import java.io.BufferedInputStream;
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.OutputStreamWriter;
  26. import java.net.URL;
  27. import java.text.SimpleDateFormat;
  28. import java.util.*;
  29. import java.util.concurrent.*;
  30. /**
  31. * @author zhengrong.yan
  32. * @date 2021/2/1 9:45
  33. */
  34. public class NewYearMakeVideoJob {
  35. private Logger log = Logger.getLogger("newyearmakevideo");
  36. @Autowired
  37. private DictionaryDao dictionaryDao;
  38. @Autowired
  39. private NewYearVideoDao newYearVideoDao;
  40. public void doProcess() throws Exception {
  41. log.info(Thread.currentThread().getName() + "定时任务开始");
  42. long beginTime = System.currentTimeMillis();
  43. List<HashMap> list = newYearVideoDao.getData();
  44. if (list != null && list.size() > 0) {
  45. CountDownLatch threadSignal = new CountDownLatch(list.size());
  46. ExecutorService executorService = new ThreadPoolExecutor(12, 16, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  47. //去除重复数据
  48. //List<HashMap> dataList = paraseData(list);
  49. log.info("数据库有效需要处理的数:" + list.size());
  50. for (HashMap vipmap : list) {
  51. NewYearVideoMakeService continueService = new NewYearVideoMakeService(list.size(), threadSignal, vipmap, dictionaryDao, newYearVideoDao);
  52. executorService.execute(continueService);
  53. }
  54. executorService.shutdown();
  55. try {
  56. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  57. } catch (InterruptedException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. log.info(Thread.currentThread().getName() + "定时任务完成,耗时:" + (System.currentTimeMillis() - beginTime) / 1000 + " 秒");
  62. }
  63. /**
  64. * 去除重复数据,防止赠送会员接口被并发限制
  65. *
  66. * @param dataList
  67. * @return
  68. */
  69. private List<HashMap> paraseData(List<HashMap> dataList) {
  70. //去重复后的数据集
  71. List<HashMap> reDataList = new ArrayList<HashMap>();
  72. HashMap<String, List> tmpMap = new HashMap<String, List>();
  73. for (HashMap dataMap : dataList) {
  74. if (tmpMap.containsKey(dataMap.get("USERID").toString())) {
  75. log.info("重复数据," + dataMap.get("USERID"));
  76. } else {
  77. reDataList.add(dataMap);
  78. List tmpList = new ArrayList();
  79. tmpList.add(dataMap.get("ID"));
  80. tmpMap.put(dataMap.get("USERID").toString(), tmpList);
  81. }
  82. }
  83. return reDataList;
  84. }
  85. static class NewYearVideoMakeService implements Runnable {
  86. private static Logger log = Logger.getLogger("newyearmakevideo");
  87. private int totalSize;
  88. private CountDownLatch threadSignal;
  89. private HashMap busimap;
  90. private DictionaryDao dictionaryDao;
  91. private NewYearVideoDao newYearVideoDao;
  92. public NewYearVideoMakeService(int totalSize, CountDownLatch threadSignal, HashMap busimap, DictionaryDao dictionaryDao, NewYearVideoDao newYearVideoDao) {
  93. this.totalSize = totalSize;
  94. this.threadSignal = threadSignal;
  95. this.busimap = busimap;
  96. this.dictionaryDao = dictionaryDao;
  97. this.newYearVideoDao = newYearVideoDao;
  98. }
  99. /**
  100. * 业务处理
  101. *
  102. * @param
  103. */
  104. public void run() {
  105. long startime = System.currentTimeMillis();
  106. Map logMap = new HashMap();
  107. //logMap.put("data", busimap);
  108. String resultCode = "-1";
  109. String resultInfo = "";
  110. String result = "";
  111. String id = String.valueOf(busimap.get("ID"));
  112. try {
  113. String video_url = String.valueOf(busimap.get("RETURNVIDEOURL"));
  114. if (StringUtil.isEmpty(video_url)) {
  115. resultCode = "5000";
  116. resultInfo = "未回传视频地址";
  117. } else {
  118. //下载视频到服务器
  119. getPutFile(video_url, id);
  120. newYearVideoDao.updateHasDowload(String.valueOf(busimap.get("ID")));
  121. //调cdn脚本
  122. String command = "/kc/newyearvideo/publish.sh ../data/cdn/" + id + ".mp4";
  123. String res = Bash.exec(command);
  124. System.out.println("调用脚本结果===>" + res);
  125. log.info("调用脚本结果===>" + res);
  126. if (!"".equals(res)) {
  127. //调拓维接口
  128. result = invoke(busimap);
  129. JSONObject jsonObject = JSON.parseObject(result);
  130. resultCode = jsonObject.getString("code");
  131. resultInfo = jsonObject.getString("msg");
  132. resultCode = "0";
  133. } else {
  134. throw new BusinessException("8001", "cdn缓存失败");
  135. }
  136. }
  137. } catch (Exception e) {
  138. try {
  139. newYearVideoDao.updateRowback(String.valueOf(busimap.get("ID")));
  140. } catch (Exception e1) {
  141. e1.printStackTrace();
  142. }
  143. e.printStackTrace();
  144. if (e instanceof BusinessException) {
  145. resultInfo = ((BusinessException) e).getMessage();
  146. resultCode = ((BusinessException) e).getCode();
  147. } else {
  148. resultCode = "8000";
  149. resultInfo = "系统错误," + e.getMessage();
  150. }
  151. } finally {
  152. threadSignal.countDown();
  153. try {
  154. if (resultInfo != null && resultInfo.length() > 250) {
  155. resultInfo = resultInfo.substring(0, 250);
  156. }
  157. if ("0".equals(resultCode)) {
  158. String videourl = "http://res.tv.wo.cn/springfestival/" + id + ".mp4";
  159. newYearVideoDao.updateHasFinish(id, resultCode, resultInfo, videourl);
  160. }
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. log.error("更新数据出现异常," + id + ", resultCode:" + resultCode + ", resultInfo:" + resultInfo);
  164. }
  165. logMap.put("reusltCode", resultCode);
  166. logMap.put("resultInfo", resultInfo);
  167. logMap.put("result", result);
  168. log.info(JsonUtil.objectToJson(logMap));
  169. }
  170. }
  171. /**
  172. * 调合作方接口
  173. *
  174. * @param
  175. * @return
  176. * @throws Exception
  177. */
  178. private String invoke(Map params) throws Exception {
  179. String result = "";
  180. try {
  181. String url = dictionaryDao.getValue("twWXNotify");//调拓维通知接口
  182. String openid = String.valueOf(params.get("USERID"));
  183. System.out.println("openid===>"+openid);
  184. String templateId = "fa5QGaGoY9YDB_vPz4q890JO68tLAv97_BjTkY3hTY4";
  185. String tourl = "https://live.v.wo.cn/platform/ytpxz/univideo/personal.html?id="+openid;
  186. url +="?openId="+openid+"&templateId="+templateId+"&url="+tourl;
  187. //新年视频换脸
  188. //url = "https://fifa.wo186.tv/wx/sendTemplateMessage.do?openId=ojCvV0t_sRfF1a5AolUTextKHfvM&templateId=fa5QGaGoY9YDB_vPz4q890JO68tLAv97_BjTkY3hTY4&url=";
  189. //新年视频换脸
  190. String modulesname = String.valueOf(params.get("MODULESNAME"));
  191. String modulestime = String.valueOf(params.get("MODULESTIME"));
  192. String jsonParams = "";
  193. JSONObject jsondata = new JSONObject();
  194. String[] s = {"remark", "first", "keyword1", "keyword2", "keyword3", "keyword4"};
  195. String[] s2 = {"点击此通知直接进入制作好的视频页面,并可进行分享哦", "您的超清版视频已经制作完成!", "AI变脸送新春祝福", modulesname, modulestime, "超清无水印"};
  196. for (int i = 0; i < 6; i++) {
  197. JSONObject jsonObject = new JSONObject();
  198. jsonObject.put("color", "#000000");
  199. jsonObject.put("value", s2[i]);
  200. jsondata.put(s[i], jsonObject);
  201. }
  202. jsonParams = jsondata.toJSONString();
  203. log.info(url);
  204. log.info(jsondata.toJSONString());
  205. try {
  206. //加密
  207. String keyStr = "d663bd873d93a76ab2ce11e9";
  208. String txt = TripleDES.encrypt(jsonParams, keyStr);
  209. ;
  210. System.out.println(txt);
  211. //MD5
  212. String sid = "h5game";
  213. String timestamp = System.currentTimeMillis() / 1000 + "";
  214. String sign = sid + "&" + timestamp + "&" + txt + "&" + keyStr;
  215. sign = MD5.MD5Encode(sign);
  216. URL url2 = new URL(url);
  217. SSLContext sc = SSLContext.getInstance("TLSv1.2"); //$NON-NLS-1$
  218. sc.init(null, null, new java.security.SecureRandom());
  219. HttpsURLConnection conn = (HttpsURLConnection) url2.openConnection();
  220. conn.setSSLSocketFactory(sc.getSocketFactory());
  221. conn.setRequestMethod("POST");
  222. conn.addRequestProperty("content-type", "application/json;charset=UTF-8");
  223. conn.addRequestProperty("sid", sid);
  224. conn.addRequestProperty("timestamp", timestamp);
  225. conn.addRequestProperty("sign", sign);
  226. conn.setDoOutput(true);
  227. conn.setUseCaches(false);
  228. OutputStreamWriter out = null;
  229. out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
  230. out.write(txt);
  231. out.flush();
  232. result = URLUtil.readInputStream(conn.getInputStream());
  233. } catch (Exception e) {
  234. e.printStackTrace();
  235. }
  236. log.info("url: " + url);
  237. log.info("调合作方接口结果=> wxuserid: " + String.valueOf(busimap.get("USERID")) + ", id: " + String.valueOf(busimap.get("ID")) + " , result: " + result);
  238. } catch (Exception e) {
  239. e.printStackTrace();
  240. log.error("id=>" + String.valueOf(params.get("ID")) + "调拓维接口失败," + e);
  241. }
  242. return result;
  243. }
  244. /**
  245. * 解析数据
  246. *
  247. * @param body orderId TD_POINTS_ORDER_REC表ID字段,我方生成的订单流水号
  248. * orderNo 积分商城订单号
  249. * requestId 返回给客户的请求ID
  250. * @return
  251. */
  252. public Map transBean(Map<String, Object> body) {
  253. String jsonStr = JsonUtil.objectToJson(body);
  254. return (Map) JsonUtil.jsonToBean(jsonStr, Map.class);
  255. }
  256. /**
  257. * 获取请求属性性
  258. *
  259. * @return
  260. */
  261. private static Map getProperty() {
  262. Map reqProperty = new HashMap();
  263. reqProperty.put("Content-type", "application/json;charset=UTF-8");
  264. return reqProperty;
  265. }
  266. public static void getPutFile(String urlPath, String id) throws Exception {
  267. File file = new File("/kc/data/cdn/" + id + ".mp4");
  268. file.getParentFile().mkdirs();
  269. BufferedInputStream bufferedInputStream = new BufferedInputStream(new URL(urlPath).openStream());
  270. FileOutputStream fileOutputStream = new FileOutputStream("/kc/data/cdn/" + id + ".mp4");
  271. int count = 0;
  272. byte[] b = new byte[1000];
  273. while ((count = bufferedInputStream.read(b)) != -1) {
  274. fileOutputStream.write(b, 0, count);
  275. }
  276. }
  277. }
  278. }