549f75de6b22b1678391d6fda011cb4a0a46abca.svn-base 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package com.chinacreator.process.job;
  2. import com.alibaba.fastjson.JSON;
  3. import com.chinacreator.common.util.DESUtil;
  4. import com.chinacreator.common.util.URLUtil;
  5. import com.chinacreator.process.dao.DictionaryDao;
  6. import com.chinacreator.process.dao.MqSmsConfDao;
  7. import com.chinacreator.process.dao.VirtualDao;
  8. import com.chinacreator.process.util.DesUtil;
  9. import com.chinacreator.process.util.JsonUtil;
  10. import com.chinacreator.process.util.SHAUtil;
  11. import com.chinacreator.process.util.WriteLogUtil;
  12. import com.chinacreator.video.queue.MessageService;
  13. import com.chinacreator.video.queue.bean.MessagePipe;
  14. import org.apache.commons.lang.StringUtils;
  15. import org.apache.commons.lang.time.DateFormatUtils;
  16. import org.apache.log4j.Logger;
  17. import org.quartz.DisallowConcurrentExecution;
  18. import org.quartz.PersistJobDataAfterExecution;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import java.net.URLEncoder;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.*;
  24. @PersistJobDataAfterExecution
  25. @DisallowConcurrentExecution
  26. public class CShandleSmsConfJob {
  27. private Logger logger = Logger.getLogger("cssms");
  28. //private Logger log = Logger.getLogger(CShandleSmsConfJob.class);
  29. private final static String KEY = "22fdse33";
  30. @Autowired
  31. private MqSmsConfDao mqSmsConfDao;
  32. @Autowired
  33. private DictionaryDao dictionaryDao;
  34. @Autowired
  35. private MessageService messageService;
  36. @Autowired
  37. private VirtualDao virtualDao;
  38. public void doProcess() throws Exception {
  39. WriteLogUtil.writeLong("业务短信CShandleSmsConfJob开始执行", logger, "CShandleSmsConfJob");//("业务短信CShandleSmsTempJob开始执行");
  40. //获取短信配置信息,会不会更新
  41. List<HashMap> confList = mqSmsConfDao.getConfList();
  42. //log.info("配置size: " + confList.size());
  43. if (confList != null && confList.size() > 0 && "0".equals(dictionaryDao.getValue("recivemq"))) {
  44. List<MessagePipe> list = messageService.reciveBatchMessage("cssms2", 500);
  45. if (list != null && list.size() > 0) {
  46. logger.info("CShandleSmsConfJob短信需要发送的条数:" + list.size());
  47. HashMap logmap = new HashMap();
  48. for (MessagePipe messagePipe : list) {
  49. logmap = new HashMap();
  50. logmap.put("job", "CShandleSmsConfJob");
  51. logmap.put("mqbody", messagePipe.getBody());
  52. String content = "";
  53. String result = "";
  54. String spid = (String) messagePipe.getBody().get("spid");
  55. String cpid = messagePipe.getBody().get("cpid") + "";
  56. String userid = (String) messagePipe.getBody().get("userid");
  57. String times = (String) messagePipe.getBody().get("times");
  58. String style = (String) messagePipe.getBody().get("style");
  59. String busiType = messagePipe.getBody().get("busiType") + "";
  60. String channel = messagePipe.getBody().get("channel") == null ? "" : (String) messagePipe.getBody().get("channel");
  61. String subchannel = messagePipe.getBody().get("subchannel") == null ? "" : (String) messagePipe.getBody().get("subchannel");
  62. String province = messagePipe.getBody().get("province") == null ? "" : (String) messagePipe.getBody().get("province");
  63. String param1 = messagePipe.getBody().get("param1") != null ? (String) messagePipe.getBody().get("param1") : "";
  64. String param2 = messagePipe.getBody().get("param2") != null ? (String) messagePipe.getBody().get("param2") : "";
  65. String param3 = messagePipe.getBody().get("param3") != null ? (String) messagePipe.getBody().get("param3") : "";
  66. String staffid = messagePipe.getBody().get("staffid") == null ? "" : (String) messagePipe.getBody().get("staffid");//员工工号
  67. try {
  68. content = this.getSmsContent(confList, messagePipe);
  69. //log.info("sms content:" + content);
  70. logmap.put("content", content);
  71. if (StringUtils.isNotEmpty(content)) {
  72. if (mqSmsConfDao.getChannel(userid, spid) || !("tran_succ".equals(busiType) || "vs_tran_succ".equals(busiType))) {
  73. result = URLUtil.get("http://172.16.1.81:809/videoif/channelSmsSend.do?userid=" + URLEncoder.encode(DESUtil.encode(userid, KEY), "utf-8") + "&content=" + URLEncoder.encode(content, "UTF-8") + "&channel=cssms", 2000);
  74. }
  75. /* //腾讯活动短信发送
  76. if (mqSmsConfDao.isSendSms(userid, spid) && "101".equals(spid)) {
  77. messagePipe.addBody("busiType","tran_succ_TX20");
  78. content = this.getSmsContent(confList, messagePipe);
  79. log.info("sms content:" + content);
  80. if (StringUtils.isNotEmpty(content)) {
  81. result = URLUtil.get("http://10.199.99.147:8092/videoif/channelSmsSend.do?userid=" + URLEncoder.encode(DESUtil.encode(userid, KEY), "utf-8") + "&content=" + URLEncoder.encode(content, "UTF-8") + "&channel=cssms", 2000);
  82. }
  83. }*/
  84. } else if (StringUtils.isEmpty(content) && "tran_succ".equals(busiType) && StringUtils.isNotEmpty(spid)) {
  85. //1.获取短信配置表id
  86. List<HashMap> virList = virtualDao.getConfList(spid);
  87. //log.info("虚商配置表数据:" + virList);
  88. if (virList != null && virList.size() > 0) {
  89. for (int i = 0; i < virList.size(); i++) {
  90. HashMap hashMap = virList.get(i);
  91. String contentid = hashMap.get("CONTENTID") + "";
  92. if (StringUtils.isNotEmpty(contentid)) {
  93. result = sendSMS(userid, contentid);
  94. }
  95. }
  96. }
  97. }
  98. logmap.put("result", result);
  99. } catch (Exception e) {
  100. logger.error(userid + "短信发送失败", e);
  101. logmap.put("result", "出现异常,"+e.getMessage());
  102. } finally {
  103. /**
  104. Map<String, String> map = new HashMap<String, String>();
  105. map.put("userid", userid);
  106. map.put("cpid", cpid);
  107. map.put("spid", spid);
  108. map.put("style", style + "");
  109. map.put("result", result);
  110. map.put("times", times);
  111. map.put("busiType", busiType);
  112. map.put("param1", param1);
  113. map.put("param2", param2);
  114. map.put("param3", param3);
  115. map.put("param3", content);
  116. map.put("channel", channel);
  117. map.put("subchannel", subchannel);
  118. map.put("province", province);
  119. map.put("staffid", staffid);
  120. **/
  121. logger.info(JsonUtil.objectToJson(logmap));
  122. }
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * 获取短信内容
  129. *
  130. * @param confList
  131. * @param messagePipe
  132. * @return
  133. * @throws Exception
  134. */
  135. public String getSmsContent(List<HashMap> confList, MessagePipe messagePipe) throws Exception {
  136. String content = "";
  137. try {
  138. String resultcode = (String) messagePipe.getBody().get("result");
  139. String userid = (String) messagePipe.getBody().get("userid");
  140. //原匹配信息
  141. String style = (String) messagePipe.getBody().get("style");
  142. //新匹配信息
  143. String busiType = (String) messagePipe.getBody().get("busiType");
  144. //二级渠道
  145. String subchannel = (String) messagePipe.getBody().get("subchannel");
  146. //渠道
  147. String channel = (String) messagePipe.getBody().get("channel");
  148. //省份
  149. String province = (String) messagePipe.getBody().get("province");
  150. //员工工号
  151. String staffid = (String) messagePipe.getBody().get("staffid");
  152. //扩展参数
  153. logger.info("mq取值信息:" + " subchannel:" + subchannel + " channel:" + channel + " busiType:" + busiType + " province:" + province + " staffid:" + staffid);
  154. String param1 = messagePipe.getBody().get("param1") != null ? (String) messagePipe.getBody().get("param1") : "";
  155. String param2 = messagePipe.getBody().get("param2") != null ? (String) messagePipe.getBody().get("param2") : "";
  156. String param3 = messagePipe.getBody().get("param3") != null ? (String) messagePipe.getBody().get("param3") : "";
  157. //配置信息
  158. HashMap mqSmsConf = null;
  159. //获取匹配的配置信息
  160. //二级渠道信息匹配
  161. Boolean flag = false;
  162. if ("cap".equals(channel) || "capftp".equals(channel)) {
  163. //员工工号优先级高于省份
  164. if (staffid != null && !"".equals(staffid)) {
  165. for (HashMap tmp : confList) {
  166. if (busiType != null && !"".equals(busiType.trim()) && tmp.get("BUSITYPE") != null && !"".equals(tmp.get("BUSITYPE"))) {
  167. if (staffid.trim().equals(tmp.get("STAFFID")) && tmp.get("SPID").equals(messagePipe.getBody().get("spid"))
  168. && tmp.get("BUSITYPE").equals(busiType.trim())) {
  169. if (checkTime(tmp)){
  170. continue;
  171. }
  172. flag = true;
  173. logger.info("staffid:数据库取值" + tmp.get("BUSITYPE") + tmp.get("SPID") + tmp.get("STAFFID"));
  174. mqSmsConf = tmp;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. if (mqSmsConf == null) {
  181. if (province != null && !"".equals(province)) {
  182. for (HashMap tmp : confList) {
  183. if (busiType != null && !"".equals(busiType.trim()) && tmp.get("BUSITYPE") != null && !"".equals(tmp.get("BUSITYPE"))) {
  184. if (province.trim().equals(tmp.get("PROVINCE")) && tmp.get("SPID").equals(messagePipe.getBody().get("spid"))
  185. && tmp.get("BUSITYPE").equals(busiType.trim())) {
  186. if (checkTime(tmp)){
  187. continue;
  188. }
  189. flag = true;
  190. logger.info("province:数据库取值" + tmp.get("BUSITYPE") + tmp.get("SPID") + tmp.get("PROVINCE"));
  191. mqSmsConf = tmp;
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. } else {
  199. for (HashMap tmp : confList) {
  200. if (busiType != null && !"".equals(busiType.trim()) && tmp.get("BUSITYPE") != null && !"".equals(tmp.get("BUSITYPE"))) {
  201. if (subchannel != null && channel != null && subchannel.trim().equals(tmp.get("SUBCHANNEL")) && channel.trim().equals(tmp.get("CHANNEL")) && tmp.get("SPID").equals(messagePipe.getBody().get("spid"))
  202. && tmp.get("BUSITYPE").equals(busiType.trim())) {
  203. if (checkTime(tmp)){
  204. continue;
  205. }
  206. flag = true;
  207. logger.info("false:数据库取值" + tmp.get("BUSITYPE") + tmp.get("SUBCHANNEL") + tmp.get("CHANNEL") + tmp.get("SPID"));
  208. mqSmsConf = tmp;
  209. break;
  210. }
  211. }
  212. }
  213. }
  214. if (!flag) {
  215. for (HashMap tmp : confList) {
  216. if (checkTime(tmp)){
  217. continue;
  218. }
  219. if (busiType != null && !"".equals(busiType.trim()) && tmp.get("BUSITYPE") != null && !"".equals(tmp.get("BUSITYPE"))) {
  220. if (subchannel != null && channel != null && subchannel.trim().equals(tmp.get("SUBCHANNEL")) && channel.trim().equals(tmp.get("CHANNEL")) && tmp.get("SPID").equals(messagePipe.getBody().get("spid"))
  221. && tmp.get("BUSITYPE").equals(busiType.trim())) {
  222. logger.info("数据库取值1" + tmp.get("BUSITYPE") + tmp.get("SUBCHANNEL") + tmp.get("CHANNEL") + tmp.get("SPID"));
  223. mqSmsConf = tmp;
  224. break;
  225. } else if (tmp.get("SPID").equals(messagePipe.getBody().get("spid"))
  226. && tmp.get("BUSITYPE").equals(busiType.trim()) && tmp.get("SUBCHANNEL") == null && tmp.get("PROVINCE") == null && tmp.get("STAFFID") == null) {
  227. logger.info("数据库取值2" + tmp.get("BUSITYPE") + tmp.get("SUBCHANNEL") + tmp.get("CHANNEL") + tmp.get("SPID"));
  228. mqSmsConf = tmp;
  229. break;
  230. }
  231. } else if (style != null && !"".equals(style.trim()) && tmp.get("STYLE") != null && style.trim().equals(tmp.get("STYLE"))) {
  232. mqSmsConf = tmp;
  233. break;
  234. }
  235. //System.out.println(tmp);
  236. }
  237. }
  238. //得到发送短信文本
  239. if (mqSmsConf != null) {
  240. content = (String) mqSmsConf.get("SMSTXT");
  241. //替换参数内容
  242. if (mqSmsConf.get("PARAMS") != null && !"".equals(mqSmsConf.get("PARAMS"))) {
  243. String[] params = ((String) mqSmsConf.get("PARAMS")).split("\\|");
  244. for (int i = 0; i < params.length; i++) {
  245. if ("MM".equals(params[i])) {
  246. content = content.replace("{" + i + "}", DateFormatUtils.format(new Date(), "MM"));
  247. } else if ("dd".equals(params[i])) {
  248. content = content.replace("{" + i + "}", DateFormatUtils.format(new Date(), "dd"));
  249. } else if ("resultcode".equals(params[i])) {
  250. content = content.replace("{" + i + "}", resultcode);
  251. } else if ("userid".equals(params[i])) {
  252. content = content.replace("{" + i + "}", userid);
  253. } else if ("param1".equals(params[i])) {
  254. content = content.replace("{" + i + "}", param1);
  255. } else if ("param2".equals(params[i])) {
  256. content = content.replace("{" + i + "}", param2);
  257. } else if ("param3".equals(params[i])) {
  258. content = content.replace("{" + i + "}", param3);
  259. }
  260. }
  261. }
  262. }
  263. } catch (Exception e) {
  264. logger.error("获取短信内容出错," + e.getMessage());
  265. throw e;
  266. }
  267. return content;
  268. }
  269. public boolean checkTime(HashMap mqSmsConf) throws ParseException {
  270. String sendtime = (String) mqSmsConf.get("SENDTIME");
  271. if (StringUtils.isNotEmpty(sendtime)) {
  272. long currentTime = System.currentTimeMillis();
  273. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  274. String[] split = sendtime.split("-");
  275. long startTime = simpleDateFormat.parse(split[0]).getTime();
  276. long endTime = simpleDateFormat.parse(split[1]).getTime();
  277. if (currentTime<startTime||currentTime>endTime){
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. /**
  284. * 发送虚拟短信内容
  285. *
  286. * @param userid
  287. * @param contentid
  288. * @return
  289. */
  290. private String sendSMS(String userid, String contentid) {
  291. try {
  292. String smsid = "106550077614";
  293. String key = "wo6bslq2";
  294. String timestamp = System.currentTimeMillis() / 1000 + "";
  295. String usermob = DesUtil.encode(userid, key);
  296. Calendar calendar = Calendar.getInstance();
  297. calendar.setTime(new Date());
  298. int month = calendar.get(Calendar.MONTH) + 1;
  299. int date = calendar.get(Calendar.DAY_OF_MONTH);
  300. String vars = "{\"userid\":" + userid + ",\"month\":" + month + ",\"date\":" + date + "}";
  301. String sign = SHAUtil.shaEncode(smsid + usermob + timestamp + vars + contentid + key).toLowerCase();
  302. usermob = URLEncoder.encode(usermob, "utf-8");
  303. contentid = URLEncoder.encode(contentid, "utf-8");
  304. vars = URLEncoder.encode(vars, "utf-8");
  305. String url = "http://172.16.1.81:809/xsbusi/sms/send?userid=" + usermob + "&smsid=" + smsid + "&timestamp=" + timestamp + "&sign=" + sign + "&contentid=" + contentid + "&vars=" + vars;
  306. String resp = URLUtil.get(url);
  307. //log.info("虚商短信路径:" + url);
  308. //log.info("调用虚商短信后返回的参数:" + resp);
  309. /**
  310. Map map = JSON.parseObject(resp, Map.class);
  311. String resultcode = map.get("resultcode") + "";
  312. if ("0".equals(resultcode)) {
  313. log.info("虚商短信发送成功");
  314. } else {
  315. log.info("虚商短信发送失败");
  316. }
  317. **/
  318. return resp;
  319. } catch (Exception e) {
  320. //logger.info("虚商短信发送失败");
  321. e.printStackTrace();
  322. return "虚商短信发送失败,"+e.getMessage();
  323. }
  324. //log.info("虚商短信发送失败");
  325. //return null;
  326. }
  327. public static void main(String[] args) {
  328. HashMap<Object, Object> objectObjectHashMap = new HashMap<Object, Object>();
  329. System.out.println("null".equals(String.valueOf(objectObjectHashMap.get("22"))));
  330. System.out.println("null".equals((String)objectObjectHashMap.get("22")));
  331. System.out.println(StringUtils.isEmpty((String)objectObjectHashMap.get("22")));
  332. System.out.println(StringUtils.isEmpty(String.valueOf(objectObjectHashMap.get("22"))));
  333. }
  334. }