9551dd189447d7c92b400644d0ef3e88d9fe3b21.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.exception.BusinessException;
  6. import com.chinacreator.process.dao.DictionaryDao;
  7. import com.chinacreator.process.dao.NewYearVideoDao;
  8. import com.chinacreator.process.util.JsonUtil;
  9. import com.chinacreator.process.util.URLUtil;
  10. import oracle.sql.CLOB;
  11. import org.apache.log4j.Logger;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import sun.misc.BASE64Decoder;
  14. import java.io.BufferedInputStream;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.net.URL;
  18. import java.text.SimpleDateFormat;
  19. import java.util.*;
  20. import java.util.concurrent.*;
  21. /**
  22. * @author zhengrong.yan
  23. * @date 2021/2/1 9:45
  24. */
  25. public class NewYearVideoJob {
  26. private Logger log = Logger.getLogger("newyearvideo");
  27. @Autowired
  28. private DictionaryDao dictionaryDao;
  29. @Autowired
  30. private NewYearVideoDao newYearVideoDao;
  31. public void doProcess() throws Exception {
  32. log.info(Thread.currentThread().getName() + "定时任务开始");
  33. long beginTime = System.currentTimeMillis();
  34. List<HashMap> list = newYearVideoDao.getpushData();
  35. if (list != null && list.size() > 0) {
  36. CountDownLatch threadSignal = new CountDownLatch(list.size());
  37. ExecutorService executorService = new ThreadPoolExecutor(12, 16, 10L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
  38. //去除重复数据
  39. //List<HashMap> dataList = paraseData(list);
  40. log.info("数据库有效需要处理的数:" + list.size());
  41. for (HashMap vipmap : list) {
  42. NewYearVideoService continueService = new NewYearVideoService(list.size(), threadSignal, vipmap, dictionaryDao, newYearVideoDao);
  43. executorService.execute(continueService);
  44. }
  45. executorService.shutdown();
  46. try {
  47. executorService.awaitTermination(5L, TimeUnit.MINUTES);
  48. } catch (InterruptedException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. log.info(Thread.currentThread().getName() + "定时任务完成,耗时:" + (System.currentTimeMillis() - beginTime) / 1000 + " 秒");
  53. }
  54. /**
  55. * 去除重复数据,防止赠送会员接口被并发限制
  56. *
  57. * @param dataList
  58. * @return
  59. */
  60. private List<HashMap> paraseData(List<HashMap> dataList) {
  61. //去重复后的数据集
  62. List<HashMap> reDataList = new ArrayList<HashMap>();
  63. HashMap<String, List> tmpMap = new HashMap<String, List>();
  64. for (HashMap dataMap : dataList) {
  65. if (tmpMap.containsKey(dataMap.get("USERID").toString())) {
  66. log.info("重复数据," + dataMap.get("USERID"));
  67. } else {
  68. reDataList.add(dataMap);
  69. List tmpList = new ArrayList();
  70. tmpList.add(dataMap.get("ID"));
  71. tmpMap.put(dataMap.get("USERID").toString(), tmpList);
  72. }
  73. }
  74. return reDataList;
  75. }
  76. static class NewYearVideoService implements Runnable {
  77. private static Logger log = Logger.getLogger("newyearvideo");
  78. private int totalSize;
  79. private CountDownLatch threadSignal;
  80. private HashMap busimap;
  81. private DictionaryDao dictionaryDao;
  82. private NewYearVideoDao newYearVideoDao;
  83. public NewYearVideoService(int totalSize, CountDownLatch threadSignal, HashMap busimap, DictionaryDao dictionaryDao, NewYearVideoDao newYearVideoDao) {
  84. this.totalSize = totalSize;
  85. this.threadSignal = threadSignal;
  86. this.busimap = busimap;
  87. this.dictionaryDao = dictionaryDao;
  88. this.newYearVideoDao = newYearVideoDao;
  89. }
  90. /**
  91. * 业务处理
  92. *
  93. * @param
  94. */
  95. public void run() {
  96. long startime = System.currentTimeMillis();
  97. Map logMap = new HashMap();
  98. //logMap.put("data", busimap);
  99. String resultCode = "-1";
  100. String resultInfo = "";
  101. String id = String.valueOf(busimap.get("ID"));
  102. try {
  103. //办理结果编码,0成功,-1待处理,1处理中,其他为异常
  104. newYearVideoDao.updateProcessing(id);
  105. //上传图片到服务器
  106. CLOB clob = (CLOB) busimap.get("BASE64CODE");
  107. String[] base64 = newYearVideoDao.ClobToString(clob).split(",");
  108. for (String s : base64) {
  109. BASE64Decoder decoder = new BASE64Decoder();
  110. //base64解码
  111. byte[] bytes = decoder.decodeBuffer(s);
  112. FileOutputStream fout = new FileOutputStream("/kc/data/cdn/images/" + id + ".jpg");
  113. //将字节写入文件
  114. fout.write(bytes);
  115. fout.close();
  116. }
  117. busimap.put("photosurl","http://res.tv.wo.cn/springfestival/images/"+id+".jpg");
  118. //提交工单给合作方
  119. String result = invoke(busimap);
  120. JSONObject jsonObject = JSON.parseObject(result);
  121. resultCode = jsonObject.getString("code");
  122. resultInfo = jsonObject.getString("msg");
  123. } catch (Exception e) {
  124. //回滚
  125. try {
  126. newYearVideoDao.updateRowbackPhotos(String.valueOf(busimap.get("ID")));
  127. } catch (Exception e1) {
  128. e1.printStackTrace();
  129. }
  130. e.printStackTrace();
  131. if (e instanceof BusinessException) {
  132. resultInfo = ((BusinessException) e).getMessage();
  133. resultCode = ((BusinessException) e).getCode();
  134. } else {
  135. resultCode = "8000";
  136. resultInfo = "系统错误," + e.getMessage();
  137. }
  138. } finally {
  139. threadSignal.countDown();
  140. try {
  141. if (resultInfo != null && resultInfo.length() > 250) {
  142. resultInfo = resultInfo.substring(0, 250);
  143. }
  144. newYearVideoDao.upHasSubmit(id, resultCode, resultInfo, "http://res.tv.wo.cn/springfestival/images/" + id + ".jpg");
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. log.error("更新数据出现异常," + id + ", resultCode:" + resultCode + ", resultInfo:" + resultInfo);
  148. }
  149. logMap.put("reusltCode", resultCode);
  150. logMap.put("resultInfo", resultInfo);
  151. log.info(JsonUtil.objectToJson(logMap));
  152. }
  153. }
  154. /**
  155. * 调合作方接口
  156. *
  157. * @param
  158. * @return
  159. * @throws Exception
  160. */
  161. private String invoke(Map params) throws Exception {
  162. String result = "";
  163. String url = dictionaryDao.getValue("NewYearVideoUrl");
  164. //新年视频换脸工单提交
  165. String jsonParams = "";
  166. String timestamp = System.currentTimeMillis() + "";
  167. String http_callback_url = dictionaryDao.getValue("NewYearVideoBackUrl");//从字典表里拿
  168. String wid =String.valueOf(params.get("ID")) ;
  169. //外网地址
  170. String img = String.valueOf(params.get("photosurl"));
  171. JSONObject json = new JSONObject();
  172. JSONArray jsonArray = new JSONArray();
  173. jsonArray.add(img);
  174. json.put("images", jsonArray);
  175. json.put("http_callback_url", http_callback_url);
  176. json.put("timestamp", timestamp);
  177. json.put("app_id", "b5czqjiujukzssmdw3lyjmdz4twhnfvs");
  178. json.put("secret_key", "MlOWTRj9ljqAfWPGbSy1x7dIiwKYKROp");
  179. json.put("wid", wid);
  180. System.out.println("modules===>"+params.get("VIDEOMODULES"));
  181. json.put("template_id", String.valueOf(params.get("VIDEOMODULES")));
  182. JSONArray jsonArray2 = new JSONArray();
  183. json.put("videos", jsonArray2);
  184. json.put("texts", jsonArray2);
  185. jsonParams = json.toJSONString();
  186. log.info("url: " + url);
  187. log.info(jsonParams);
  188. result = URLUtil.postJson(url, jsonParams);
  189. System.out.println("result=====>"+result);
  190. log.info("调合作方接口结果=> id: " + wid + " , result: " + result);
  191. return result;
  192. }
  193. /**
  194. * 解析数据
  195. *
  196. * @param body orderId TD_POINTS_ORDER_REC表ID字段,我方生成的订单流水号
  197. * orderNo 积分商城订单号
  198. * requestId 返回给客户的请求ID
  199. * @return
  200. */
  201. public Map transBean(Map<String, Object> body) {
  202. String jsonStr = JsonUtil.objectToJson(body);
  203. return (Map) JsonUtil.jsonToBean(jsonStr, Map.class);
  204. }
  205. /**
  206. * 获取请求属性性
  207. *
  208. * @return
  209. */
  210. private static Map getProperty() {
  211. Map reqProperty = new HashMap();
  212. reqProperty.put("Content-type", "application/json;charset=UTF-8");
  213. return reqProperty;
  214. }
  215. }
  216. }