Remind.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. package beans.remind;
  2. import beans.parameter.Parameter;
  3. import beans.user.User;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.BaseClass;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.data.InsertSQL;
  10. import common.data.Rows;
  11. import common.data.SQLFactory;
  12. import common.websocket.WebSocket;
  13. import org.apache.commons.lang.StringUtils;
  14. import utility.email.Email;
  15. import utility.email.EmailContent;
  16. import utility.sms.Sms;
  17. import utility.tools.Encryption;
  18. import utility.wechat.wechatservice.WechatService;
  19. import utility.wechat.work.send.QYWechatMsgSender;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.HashMap;
  23. import java.util.LinkedHashSet;
  24. /**
  25. * 系统提醒公共方法,包括弹窗、短信、邮件等相关功能
  26. */
  27. public class Remind {
  28. public Remind() {
  29. }
  30. /**
  31. * 发送一个系统提醒,系统将自动判断发送的方式及自动生成发送的内容
  32. */
  33. public static void autoRemind(BaseClass controller, String remindname, Long dataid, String siteid, HashMap<String, Object> extradata) throws YosException {
  34. String[] remindnameArray = new String[]{"project_follow", "customers_follow_my"};
  35. Rows sys_remind_configsRows = controller.dbConnect.runSqlQuery("select t2.ownertable,t1.remindtitle,t1.remindmode,t1.content,t1.isrole,t1.roleids,t1.isuser,t1.userids,t1.issql,t1.sqlstr,t1.wechatservice_systemclient,t1.wechatservice_content,t1.systemappids from sys_remind_configs t1 " +
  36. "inner join sys_remind_config t2 on t1.sys_remind_configid=t2.sys_remind_configid where siteid='" + siteid + "' and t2.remindname='" + remindname + "'");
  37. if (sys_remind_configsRows.isNotEmpty()) {
  38. String ownertable = sys_remind_configsRows.get(0).getString("ownertable");
  39. String remindtitle = sys_remind_configsRows.get(0).getString("remindtitle");
  40. JSONArray remindmodeArray = sys_remind_configsRows.get(0).getJSONArray("remindmode");
  41. String content = sys_remind_configsRows.get(0).getString("content");
  42. boolean isrole = sys_remind_configsRows.get(0).getBoolean("isrole");
  43. boolean isuser = sys_remind_configsRows.get(0).getBoolean("isuser");
  44. boolean issql = sys_remind_configsRows.get(0).getBoolean("issql");
  45. JSONArray systemappids = sys_remind_configsRows.get(0).getJSONArray("systemappids");
  46. //跟进消息提醒 跟进内容为空时不发送提醒
  47. for (String str : remindnameArray) {
  48. if (str.equals(remindname)) {
  49. Rows rows = controller.dbConnect.runSqlQuery("SELECT content FROM sys_datafollowup WHERE ownertable='" + ownertable + "' AND ownerid=" + dataid + " ORDER BY changedate DESC LIMIT 1");
  50. if (rows.isNotEmpty() && StringUtils.isEmpty(rows.get(0).getString("content"))) {
  51. return;
  52. }
  53. if (rows.isEmpty()) {
  54. return;
  55. }
  56. }
  57. }
  58. LinkedHashSet<Long> useridList = new LinkedHashSet<>();
  59. if (isrole) {
  60. JSONArray roleids = sys_remind_configsRows.get(0).getJSONArray("roleids");
  61. for (Object o : roleids) {
  62. useridList.addAll(controller.dbConnect.runSqlQuery("select userid from sys_userrole where siteid='" + siteid + "' and roleid=" + o).toArrayList("userid", new ArrayList<>()));
  63. }
  64. }
  65. if (isuser) {
  66. JSONArray userids = sys_remind_configsRows.get(0).getJSONArray("userids");
  67. for (Object o : userids) {
  68. useridList.add(Long.parseLong(o.toString()));
  69. }
  70. }
  71. if (issql) {
  72. String sqlstr = sys_remind_configsRows.get(0).getString("sqlstr");
  73. if (sqlstr.contains(":")) {
  74. Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
  75. if (datarows.isNotEmpty()) {
  76. for (String key : datarows.get(0).keySet()) {
  77. if (sqlstr.contains(":" + key)) {
  78. sqlstr = sqlstr.replace(":" + key, "'" + datarows.get(0).getString(key) + "'");
  79. }
  80. }
  81. }
  82. }
  83. try {
  84. useridList.addAll(controller.dbConnect.runSqlQuery(sqlstr).toArrayList("userid", new ArrayList<>()));
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. //添加额外的数据
  90. if (extradata.containsKey("extraUserList")) {
  91. Object extraUserList = extradata.get("extraUserList");
  92. useridList.addAll((ArrayList<Long>) extraUserList);
  93. }
  94. if (content.contains(":")) {
  95. Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
  96. if (datarows.isNotEmpty()) {
  97. for (String key : datarows.get(0).keySet()) {
  98. if (content.contains(":" + key)) {
  99. content = content.replace(":" + key, "'" + datarows.get(0).getString(key) + "'");
  100. }
  101. }
  102. }
  103. }
  104. int max = 10;
  105. while (content.contains("{") && content.contains("}")) {
  106. int start = content.indexOf("{");
  107. int end = content.indexOf("}");
  108. String parm = content.substring(start, end + 1);
  109. String sql = parm.replace("{", "").replace("}", "");
  110. Rows rows = controller.dbConnect.runSqlQuery(sql);
  111. if (rows.size() != 1 || rows.getFieldList().size() != 1) {
  112. content = content.replace(parm, "-");
  113. continue;
  114. } else {
  115. String value = rows.get(0).getString(rows.getFieldList().get(0));
  116. content = content.replace(parm, value);
  117. }
  118. if (max-- == 0) {
  119. break;
  120. }
  121. }
  122. //去掉单引号
  123. content = content.replace("'", "");
  124. Rows uploadRows = new Rows();
  125. if (extradata.containsKey("emailurl")) {
  126. uploadRows = (Rows) extradata.get("emailurl");
  127. }
  128. for (Object o : remindmodeArray) {
  129. String remindmode = (String) o;
  130. switch (remindmode) {
  131. case "邮件": {
  132. for (Long userid : useridList) {
  133. try {
  134. EmailContent emailContent = new EmailContent();
  135. emailContent.addText(content);
  136. if (uploadRows.isNotEmpty()) {
  137. emailContent.addFile(uploadRows.get(0).getString("url"));
  138. }
  139. ArrayList<String> emailList = controller.dbConnect.runSqlQuery("select distinct email from ( SELECT userid,email FROM sys_hr where siteid='" + siteid + "' and userid='" + userid + "'and email like'%@%'and LENGTH(email)>2 union all select userid,email from sys_enterprise_hr where siteid='" + siteid + "' and userid='" + userid + "'and email like'%@%'and LENGTH(email)>2)t").toArrayList("email");
  140. Remind remind = new Remind(siteid);
  141. remind.setTitle(remindtitle);
  142. remind.setToemail(emailList);
  143. remind.setContent(emailContent);
  144. remind.setSystemappids(systemappids);
  145. remind.sendByMail();
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. break;
  151. }
  152. case "短信": {
  153. for (Long userid : useridList) {
  154. try {
  155. ArrayList<String> phonenumberList = controller.dbConnect.runSqlQuery("select phonenumber from sys_users where userid=" + userid).toArrayList("phonenumber");
  156. Remind remind = new Remind(siteid);
  157. remind.setContent(content);
  158. remind.setTophonenumber(phonenumberList);
  159. remind.setSystemappids(systemappids);
  160. remind.sendBySms();
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. break;
  166. }
  167. case "弹框": {
  168. for (Long userid : useridList) {
  169. try {
  170. Remind remind = new Remind(siteid);
  171. remind.setTitle(remindtitle);
  172. remind.setContent(content);
  173. remind.setTouserid(userid);
  174. remind.setType("应用");
  175. remind.setSystemappids(systemappids);
  176. remind.sendByDialogMsg();
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. break;
  182. }
  183. case "企业微信": {
  184. for (Long userid : useridList) {
  185. try {
  186. Remind remind = new Remind(siteid);
  187. remind.setContent(content);
  188. remind.setTouserid(userid);
  189. remind.sendByWorkWechat();
  190. } catch (Exception e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. break;
  195. }
  196. case "微信公众号": {
  197. String wechatservice_systemclient = sys_remind_configsRows.get(0).getString("wechatservice_systemclient");
  198. JSONObject wechatservice_content = null;
  199. try {
  200. wechatservice_content = JSONObject.parseObject(Encryption.Decode_Base64(sys_remind_configsRows.get(0).getString("wechatservice_content").replace(" ", "\n")));
  201. } catch (Exception e) {
  202. wechatservice_content = new JSONObject();
  203. }
  204. if (wechatservice_content.isEmpty() || wechatservice_systemclient.equals("")) {
  205. break;
  206. }
  207. String template_id = wechatservice_content.getJSONObject("templates").getStringValue("template_id");
  208. if (template_id.equals("")) {
  209. break;
  210. }
  211. String tourl = wechatservice_content.getJSONObject("data").getStringValue("tourl");
  212. JSONArray dataparams = wechatservice_content.getJSONObject("data").getJSONArray("param");
  213. HashMap<String, String> datamap = new HashMap<>();
  214. for (Object dataparam : dataparams) {
  215. JSONObject dataparamObject = (JSONObject) dataparam;
  216. String key = dataparamObject.getString("key");
  217. String value = String.valueOf(dataparamObject.get("value"));
  218. if (value.contains(":")) {
  219. Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
  220. if (datarows.isNotEmpty()) {
  221. for (String field : datarows.get(0).keySet()) {
  222. if (value.contains(":" + field)) {
  223. value = value.replace(":" + field, "'" + datarows.get(0).getString(field) + "'");
  224. }
  225. }
  226. }
  227. }
  228. int max2 = 10;
  229. while (value.contains("{") && value.contains("}")) {
  230. int start = value.indexOf("{");
  231. int end = value.indexOf("}");
  232. String parm = value.substring(start, end + 1);
  233. String sql = parm.replace("{", "").replace("}", "");
  234. Rows rows = controller.dbConnect.runSqlQuery(sql);
  235. if (rows.size() != 1 || rows.getFieldList().size() != 1) {
  236. break;
  237. } else {
  238. value = value.replace(parm, rows.get(0).getString(rows.getFieldList().get(0)));
  239. }
  240. if (max2-- == 0) {
  241. break;
  242. }
  243. }
  244. value = value.replace("'", "");
  245. datamap.put(key, value);
  246. }
  247. String tominiprogram_appid = "";
  248. String tominiprogram_pagepath = "";
  249. Rows wechatapps = controller.dbConnect.runSqlQuery("select *from sys_wechatapp where systemclient='" + wechatservice_content.getJSONObject("data").getStringValue("tominiprogram_systemclient") + "'");
  250. if (wechatapps.isNotEmpty()) {
  251. tominiprogram_appid = wechatapps.get(0).getString("appid");
  252. tominiprogram_pagepath = wechatservice_content.getJSONObject("data").getStringValue("tominiprogram_pagepath");
  253. }
  254. if (!tominiprogram_pagepath.equals("")) {
  255. tominiprogram_pagepath = tominiprogram_pagepath + "?id=" + dataid + "&ownertable=\"" + ownertable + "\"";
  256. tourl = "http://weixin.qq.com/" + tominiprogram_pagepath + "?id=" + dataid + "&ownertable=\"" + ownertable + "\"";
  257. }
  258. WechatService wechatService = new WechatService(wechatservice_systemclient);
  259. for (Long userid : useridList) {
  260. try {
  261. wechatService.send(userid, template_id, String.valueOf(Calendar.getInstance().getTimeInMillis()), tourl, datamap, tominiprogram_appid, tominiprogram_pagepath);
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. }
  265. }
  266. break;
  267. }
  268. }
  269. }
  270. for (Object obj : useridList) {
  271. Long userid = Long.valueOf(obj.toString());
  272. try {
  273. Remind remind = new Remind(siteid);
  274. remind.setTitle(remindtitle);
  275. remind.setType("应用");
  276. remind.setContent(content);
  277. remind.setTouserid(userid);
  278. remind.setObjectname(ownertable);
  279. remind.setObjectid(dataid);
  280. remind.setSystemappids(systemappids);
  281. remind.createSys_message();
  282. } catch (Exception e) {
  283. e.printStackTrace();
  284. }
  285. }
  286. }
  287. }
  288. private String type = "";
  289. private String title = "";
  290. private String content = "";
  291. private JSONArray systemappids = new JSONArray();
  292. private ArrayList<String> tophonenumber = new ArrayList<>();
  293. private ArrayList<String> toemail = new ArrayList<>();
  294. private String objectname = "null";
  295. private Long objectid = 0L;
  296. private Controller controller;
  297. private String siteid = "";
  298. private ArrayList<Long> touserid = new ArrayList<>();
  299. private EmailContent emailContent = null;
  300. public Remind(Controller controller) {
  301. this.controller = controller;
  302. this.siteid = this.controller.siteid;
  303. }
  304. public Remind(String siteid) throws YosException {
  305. this.siteid = siteid;
  306. JSONObject object = new JSONObject();
  307. object.put("siteid", siteid);
  308. this.controller = new Controller(object);
  309. this.controller.siteid = this.siteid;
  310. }
  311. /**
  312. * 提醒标题,必填
  313. *
  314. * @param title
  315. */
  316. public void setTitle(String title) {
  317. this.title = title;
  318. }
  319. /**
  320. * 提醒内容,非邮件提醒时必填。
  321. *
  322. * @param content
  323. */
  324. public void setContent(String content) {
  325. this.content = content;
  326. }
  327. /**
  328. * 邮件提醒内容。content和EmailContent 必填其一
  329. *
  330. * @param emailContent
  331. */
  332. public void setContent(EmailContent emailContent) {
  333. this.emailContent = emailContent;
  334. }
  335. /**
  336. * 短信发送对象。短信方式发送时必填
  337. *
  338. * @param phonenumber
  339. */
  340. public void setTophonenumber(String phonenumber) {
  341. this.tophonenumber.add(phonenumber);
  342. }
  343. /**
  344. * 短信发送对象。短信方式发送时必填
  345. *
  346. * @param tophonenumber
  347. */
  348. public void setTophonenumber(ArrayList<String> tophonenumber) {
  349. this.tophonenumber = tophonenumber;
  350. }
  351. /**
  352. * 邮件发送对象。邮件发送时必填
  353. *
  354. * @param toemail
  355. */
  356. public void setToemail(String toemail) {
  357. this.toemail.add(toemail);
  358. }
  359. /**
  360. * 邮件发送对象。邮件发送时必填
  361. *
  362. * @param toemail
  363. */
  364. public void setToemail(ArrayList<String> toemail) {
  365. this.toemail = toemail;
  366. }
  367. /**
  368. * 邮件发送对象。邮件发送时必填
  369. *
  370. * @param userid
  371. */
  372. public void setToemail(Long userid) throws YosException {
  373. this.toemail = User.getUserMailAddress(controller, userid);
  374. }
  375. /**
  376. * 邮件发送对象。邮件发送时必填
  377. *
  378. * @param userids
  379. */
  380. public void setToemails(ArrayList<Long> userids) throws YosException {
  381. for (Long userid : userids) {
  382. this.toemail.addAll(User.getUserMailAddress(controller, userid));
  383. }
  384. }
  385. public void setObjectname(String objectname) {
  386. this.objectname = objectname;
  387. }
  388. public void setObjectid(Long objectid) {
  389. this.objectid = objectid;
  390. }
  391. /**
  392. * 弹框发送对象
  393. *
  394. * @param userid
  395. */
  396. public void setTouserid(ArrayList<Long> userid) {
  397. this.touserid = userid;
  398. }
  399. /**
  400. * 弹框发送对象
  401. *
  402. * @param userid
  403. */
  404. public void setTouserid(long userid) {
  405. this.touserid.add(userid);
  406. }
  407. /**
  408. * 消息类型 应用、系统
  409. *
  410. * @param type
  411. */
  412. public void setType(String type) {
  413. this.type = type;
  414. }
  415. /**
  416. * 设置跳转应用id
  417. *
  418. * @return
  419. */
  420. public void setSystemappids(JSONArray systemappids) {
  421. this.systemappids = systemappids;
  422. }
  423. /**
  424. * 邮件发送
  425. *
  426. * @throws YosException
  427. */
  428. public Remind sendByMail() throws YosException {
  429. if (!Parameter.getBoolean(siteid, "remind_mail")) {
  430. return this;
  431. }
  432. if (siteid.equals("") || title.equals("") || toemail.size() == 0 || (emailContent == null && content.equals(""))) {
  433. return this;
  434. }
  435. try {
  436. Email email = new Email(siteid, title, toemail);
  437. if (emailContent == null) {
  438. EmailContent content = new EmailContent();
  439. content.addText(this.content);
  440. email.send(content);
  441. } else {
  442. email.send(emailContent);
  443. }
  444. } catch (Exception e) {
  445. e.printStackTrace();
  446. throw new YosException(e.toString());
  447. }
  448. return this;
  449. }
  450. /**
  451. * 短信发送
  452. */
  453. public Remind sendBySms() throws YosException {
  454. if (siteid.equals("") || content.equals("") || tophonenumber.size() == 0) {
  455. return this;
  456. }
  457. for (String phonenumber : tophonenumber) {
  458. Sms sms = new Sms();
  459. sms.sendout(Sms.SmsType.PassWord, siteid, phonenumber, content);
  460. }
  461. return this;
  462. }
  463. /**
  464. * 企业微信发送
  465. *
  466. * @return
  467. * @throws YosException
  468. */
  469. public Remind sendByWorkWechat() throws YosException {
  470. if (siteid.equals("") || content.equals("") || touserid.size() == 0) {
  471. return this;
  472. }
  473. String systemclient = Parameter.getString(siteid, "workwechatsystemclient");
  474. // SQLFactory sqlFactory = new SQLFactory("sql:select accountno from sys_workwechataccount where siteid='" + siteid + "',systemclient='" + systemclient + "' and userid in $userids$");
  475. SQLFactory sqlFactory = new SQLFactory(new Remind(), "获取hrcode");
  476. sqlFactory.addParameter_in("userids", touserid);
  477. // ArrayList<String> users = this.controller.dbConnect.runSqlQuery(sqlFactory.getSQL()).toArrayList("accountno");
  478. ArrayList<String> users = this.controller.dbConnect.runSqlQuery(sqlFactory.getSQL()).toArrayList("hrcode");
  479. try {
  480. QYWechatMsgSender wechatMsgSender = new QYWechatMsgSender(systemclient);
  481. String result = wechatMsgSender.sendTextMsg(users, content);
  482. System.err.println("企业微信发送:" + result);
  483. } catch (Exception e) {
  484. e.printStackTrace();
  485. }
  486. return this;
  487. }
  488. /**
  489. * 弹窗发送
  490. *
  491. * @return
  492. * @throws YosException
  493. */
  494. public Remind sendByDialogMsg() throws YosException {
  495. if (type.equals("") || title.equals("") || content.equals("") || touserid.size() == 0) {
  496. return this;
  497. }
  498. for (Long userid : touserid) {
  499. for (long usersiteid : User.getUserSiteid(controller, userid)) {
  500. if (WebSocket.websocketClients.containsKey(usersiteid)) {
  501. JSONObject methodobject = new JSONObject();
  502. methodobject.put("class", "remind");//提醒
  503. methodobject.put("type", type);//消息类型
  504. methodobject.put("title", title);
  505. methodobject.put("text", content);
  506. for (WebSocket webClientSocket : WebSocket.websocketClients.get(usersiteid)) {
  507. webClientSocket.sendSystemMessage(methodobject);
  508. }
  509. }
  510. }
  511. }
  512. return this;
  513. }
  514. /**
  515. * 系统消息生成
  516. *
  517. * @throws YosException
  518. */
  519. public void createSys_message() throws YosException {
  520. if (siteid.equals("") || type.equals("") || title.equals("") || content.equals("") || touserid.size() == 0) {
  521. return;
  522. }
  523. for (Long userid : touserid) {
  524. InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sys_message");
  525. insertSQL.setUniqueid(controller.createTableID("sys_message"));
  526. insertSQL.setSiteid(siteid);
  527. insertSQL.setValue("title", title);
  528. insertSQL.setValue("type", type);
  529. insertSQL.setValue("userid", userid);
  530. insertSQL.setValue("message", content);
  531. insertSQL.setValue("systemappids", systemappids);
  532. insertSQL.setValue("objectid", objectid == 0 ? "null" : objectid);
  533. insertSQL.setValue("objectname", objectname);
  534. insertSQL.setValue("isread", 0);
  535. insertSQL.insert();
  536. }
  537. }
  538. }