be6435766ab4fea84f1d833975ad0c672c883a33.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.chinacreator.process.job;
  2. import java.net.URLEncoder;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5. import java.util.Calendar;
  6. import com.chinacreator.process.dao.DictionaryDao;
  7. import com.chinacreator.process.util.DesUtil;
  8. import com.frameworkset.util.StringUtil;
  9. import org.apache.log4j.Logger;
  10. import org.quartz.*;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import com.chinacreator.common.util.DESUtil;
  13. import com.chinacreator.common.util.URLUtil;
  14. import com.chinacreator.process.bean.MonthlyOrderSmsBean;
  15. import com.chinacreator.process.dao.MonthlyOrderSmsDao;
  16. import com.chinacreator.process.util.SHAUtil;
  17. import net.sf.json.JSONObject;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import javax.annotation.PostConstruct;
  20. @PersistJobDataAfterExecution
  21. @DisallowConcurrentExecution
  22. public class OrderSmsMonthJob {
  23. private static Logger logger = Logger.getLogger(OrderSmsMonthJob.class);
  24. public static final String urlbase = "https://cctv2.mige.tv/smsbusi/sms/send";
  25. private Logger log = Logger.getLogger("sendmonthly");
  26. @Autowired
  27. private MonthlyOrderSmsDao monthlyOrderSmsDao;
  28. @Autowired
  29. private DictionaryDao dictionaryDao;
  30. // public static OrderSmsMonthJob orderSmsMonthJob;
  31. //
  32. // @PostConstruct
  33. // public void init () {
  34. // orderSmsMonthJob = this;
  35. // orderSmsMonthJob.dictionaryDao = this.dictionaryDao;
  36. // logger.info("PostConstruct接口的innit()执行");
  37. // }
  38. public void doProcess() throws Exception {
  39. Date now = new Date();
  40. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  41. String dateStr = format.format(now);
  42. logger.info("dateStr : " + dateStr);
  43. //如果是月初就执行任务,发送上个月
  44. if (isFitstOfMonth(dateStr)) {
  45. //0 : 复合产品, 1: 非复合产品
  46. System.out.println("开始执行");
  47. doSend("1");
  48. }
  49. // log.info("执行发送非复合产品短信的任务");
  50. // doSend("1"); // 测试,直接发送,发送7月1日当天
  51. }
  52. public void doSend(String type) throws Exception {
  53. log.info("==========getMonthlyOrderSms==========");
  54. log.info("type : " + type);
  55. List<MonthlyOrderSmsBean> beanList = monthlyOrderSmsDao.queryUsersList(type); //得到要下发短信的用户
  56. log.info("resultSize : \n" + beanList.size());
  57. List<String> userList = new ArrayList<String>();
  58. Map<String, List<MonthlyOrderSmsBean>> maps = new HashMap<String, List<MonthlyOrderSmsBean>>();
  59. for (MonthlyOrderSmsBean u : beanList) {
  60. userList.add(u.getUserid());
  61. }
  62. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
  63. String sendMonth = format.format(new Date());
  64. removeDuplicate(userList);
  65. log.info("userListSize :" + userList.size());
  66. for (String key : userList) {
  67. List<MonthlyOrderSmsBean> smsList = new ArrayList<MonthlyOrderSmsBean>();
  68. for (int i = 0; i < beanList.size(); i++) {
  69. if (key.equals(beanList.get(i).getUserid())) {
  70. smsList.add(beanList.get(i));
  71. }
  72. }
  73. maps.put(key, smsList);
  74. }
  75. String context = "用户您好,您订购的{0}产品,系统将在本月3日自动为您续送当月会员权益,特此感谢您的理解,请您耐心等待。";
  76. String s = "";
  77. for (String key : userList) {
  78. for (MonthlyOrderSmsBean bean : maps.get(key)) {
  79. s = context.replace("{0}", bean.getSpname());
  80. bean.setContext(s);
  81. bean.setSendmonth(sendMonth);
  82. log.info("saveInfoBean : " + bean.toString());
  83. boolean b = monthlyOrderSmsDao.saveInfo(bean);
  84. if (b) {
  85. // sendUser(s, key, bean); //旧方法
  86. HashMap<String, String> map = new HashMap<>();
  87. map.put("sp_name",bean.getSpname());
  88. String result = sendSMSByContentid(key, "202208011446014411", map);// todo ,发送短信使用模板
  89. saveSMSLog(result,bean);
  90. }
  91. }
  92. }
  93. }
  94. /*
  95. 发短信的逻辑:调smsorder的channelSmsSend 接口,经过校验后,把短信保存到wait表,等待下发。
  96. 修改思路:
  97. 1.使用字典表获取smsbusi的地址 ; 待验证:正式环境是否是222库 ? 已验证,YES
  98. 2.配置contentid 与smsiid
  99. 模板内容:【联通畅视】用户您好,您订购的{$sp_name}产品,系统将在本月3日自动为您续送当月会员权益,特此感谢您的理解,请您耐心等待。
  100. 生产:
  101. smsid:2022041516441
  102. pwd: !qwcpbl@
  103. contentid:202208011446014411
  104. vars:sp_name
  105. */
  106. private void sendUser(String content,String oldId, MonthlyOrderSmsBean bean) throws Exception {
  107. content = URLEncoder.encode(content);
  108. try {
  109. String channel= "kczyf01";
  110. String key = "s9dcso2w";
  111. String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
  112. String sign = SHAUtil.shaEncode(channel+oldId+timestamp+key).toLowerCase();
  113. String usermob = URLEncoder.encode(DESUtil.encode(oldId, key),"utf-8");
  114. String url = "http://111.206.133.54/smsorder/channelSmsSend.do?userid=" + usermob + "&channel="
  115. + channel + "&timestamp=" + timestamp + "&sign=" + sign + "&content=" + content;
  116. String resultTemp = URLUtil.get(url);
  117. JSONObject jsonObject = JSONObject.fromObject(resultTemp);
  118. String resultCode = jsonObject.get("resultCode").toString();
  119. String resultInfo = jsonObject.get("resultInfo").toString();
  120. log.info(jsonObject.get("resultCode").toString());
  121. log.info(jsonObject.get("resultInfo").toString());
  122. bean.setResultcode(resultCode);
  123. bean.setErrorinfo(resultInfo);
  124. } catch (Exception e){
  125. log.info("发送短信异常:" + e.toString());
  126. } finally {
  127. //不管任何情况下,记录短信发送情况日志
  128. monthlyOrderSmsDao.saveSendCaseLog(bean);
  129. }
  130. }
  131. public String sendSMSByContentid(String userid, String contentid, Map<String, String> map) {
  132. String dbUrl = "";
  133. String nurl = "";
  134. try {
  135. //从数据字典表中查询地址,正式环境结果为:https://cctv2.mige.tv/smsbusi/sms/send
  136. // 测试环境结果为:https://testapi.mige.tv/smsbusi/sms/send
  137. // dbUrl = orderSmsMonthJob.dictionaryDao.getValue("newSmsSendUrl");
  138. dbUrl = dictionaryDao.getValue("newSmsSendUrl");
  139. } catch (Exception e) {
  140. log.error("smsUtil、dictionaryDao、url存在空指针问题,使用静态地址");
  141. e.printStackTrace();
  142. } finally {
  143. if (dbUrl == null || "".equals(dbUrl)) {
  144. nurl = urlbase;
  145. log.info("使用静态地址发短信,地址是:" + nurl);
  146. } else {
  147. nurl = dbUrl;
  148. log.info("发短信,地址是>>>>>>>>>>>>>>>>>:" + nurl);
  149. }
  150. }
  151. String result = "";
  152. try {
  153. String smsid = "2022041516441";
  154. String pwd = "!qwcpbl@";
  155. userid = DesUtil.encode(userid, pwd);
  156. String vars = com.alibaba.fastjson.JSONObject.toJSONString(map);
  157. String timestamp = System.currentTimeMillis() / 1000 + "";
  158. String sign = SHAUtil.shaEncode(smsid + userid + timestamp + vars + contentid + pwd).toLowerCase();
  159. userid = URLEncoder.encode(userid, "utf-8");
  160. vars = URLEncoder.encode(vars, "utf-8");
  161. String url = nurl + "?smsid=" + smsid + "&userid=" + userid + "&timestamp=" + timestamp + "&sign=" + sign + "&vars=" + vars + "&contentid=" + contentid;
  162. log.info("send SMS url:"+url);
  163. result = URLUtil.get(url);
  164. log.info("send SMS result:"+result);
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. log.error("send sms error,exception is:"+e +" detail is"+e.getMessage());
  168. }
  169. return result;
  170. }
  171. public static List removeDuplicate(List list) {
  172. HashSet h = new HashSet(list);
  173. list.clear();
  174. list.addAll(h);
  175. return list;
  176. }
  177. public boolean isFitstOfMonth(String dateStr) {
  178. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  179. Calendar cal_1 = Calendar.getInstance();//获取当前日期
  180. cal_1.setTime(new Date());
  181. cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
  182. String startDay = format.format(cal_1.getTime());
  183. System.out.println(startDay);
  184. if (startDay.equals(dateStr)) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. public void saveSMSLog(String result ,MonthlyOrderSmsBean bean){
  190. String resultCode = "";
  191. String errorinfo = "";
  192. if(!StringUtil.isEmpty(result)){
  193. JSONObject jsonObject = JSONObject.fromObject(result);
  194. resultCode = jsonObject.get("resultcode").toString();
  195. errorinfo = jsonObject.get("errorinfo").toString();
  196. }else{
  197. resultCode="-1";
  198. errorinfo="error";
  199. }
  200. log.info(resultCode);
  201. log.info(errorinfo);
  202. bean.setResultcode(resultCode);
  203. bean.setErrorinfo(errorinfo);
  204. monthlyOrderSmsDao.saveSendCaseLog(bean);//保存发送日志
  205. }
  206. public static void main(String[] args) {
  207. // Date now = new Date();
  208. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  209. // String dateStr = format.format(now);
  210. // logger.info("dateStr : " + dateStr);
  211. //
  212. // new OrderSmsMonthJob().isFitstOfMonth(dateStr);
  213. // HashMap<String, String> map = new HashMap<>();
  214. // map.put("sp_name","腾讯视频");
  215. // try {
  216. // String s = sendSMSByContentid("15581368293", "202208011446014411", map);
  217. // System.out.println(s); //{"resultcode":"0","errorinfo":"","taskid":"202208171028369988871","ssmsid":null}
  218. // } catch (Exception e) {
  219. // e.printStackTrace();
  220. //
  221. // }
  222. // String s = "{\"resultcode\":\"0\",\"errorinfo\":\"\",\"taskid\":\"202208171028369988871\",\"ssmsid\":null}";
  223. // JSONObject jsonObject = JSONObject.fromObject(s);
  224. // String resultCode = jsonObject.get("resultcode").toString();
  225. // String resultInfo = jsonObject.get("errorinfo").toString();
  226. // System.out.println(resultCode);
  227. // System.out.println(resultInfo);
  228. }
  229. }