| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- package beans.remind;
- import beans.parameter.Parameter;
- import beans.user.User;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import common.BaseClass;
- import common.Controller;
- import common.YosException;
- import common.data.InsertSQL;
- import common.data.Rows;
- import common.data.SQLFactory;
- import common.websocket.WebSocket;
- import org.apache.commons.lang.StringUtils;
- import utility.email.Email;
- import utility.email.EmailContent;
- import utility.sms.Sms;
- import utility.tools.Encryption;
- import utility.wechat.wechatservice.WechatService;
- import utility.wechat.work.send.QYWechatMsgSender;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.LinkedHashSet;
- /**
- * 系统提醒公共方法,包括弹窗、短信、邮件等相关功能
- */
- public class Remind {
- public Remind() {
- }
- /**
- * 发送一个系统提醒,系统将自动判断发送的方式及自动生成发送的内容
- */
- public static void autoRemind(BaseClass controller, String remindname, Long dataid, String siteid, HashMap<String, Object> extradata) throws YosException {
- String[] remindnameArray = new String[]{"project_follow", "customers_follow_my"};
- 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 " +
- "inner join sys_remind_config t2 on t1.sys_remind_configid=t2.sys_remind_configid where siteid='" + siteid + "' and t2.remindname='" + remindname + "'");
- if (sys_remind_configsRows.isNotEmpty()) {
- String ownertable = sys_remind_configsRows.get(0).getString("ownertable");
- String remindtitle = sys_remind_configsRows.get(0).getString("remindtitle");
- JSONArray remindmodeArray = sys_remind_configsRows.get(0).getJSONArray("remindmode");
- String content = sys_remind_configsRows.get(0).getString("content");
- boolean isrole = sys_remind_configsRows.get(0).getBoolean("isrole");
- boolean isuser = sys_remind_configsRows.get(0).getBoolean("isuser");
- boolean issql = sys_remind_configsRows.get(0).getBoolean("issql");
- JSONArray systemappids = sys_remind_configsRows.get(0).getJSONArray("systemappids");
- //跟进消息提醒 跟进内容为空时不发送提醒
- for (String str : remindnameArray) {
- if (str.equals(remindname)) {
- Rows rows = controller.dbConnect.runSqlQuery("SELECT content FROM sys_datafollowup WHERE ownertable='" + ownertable + "' AND ownerid=" + dataid + " ORDER BY changedate DESC LIMIT 1");
- if (rows.isNotEmpty() && StringUtils.isEmpty(rows.get(0).getString("content"))) {
- return;
- }
- if (rows.isEmpty()) {
- return;
- }
- }
- }
- LinkedHashSet<Long> useridList = new LinkedHashSet<>();
- if (isrole) {
- JSONArray roleids = sys_remind_configsRows.get(0).getJSONArray("roleids");
- for (Object o : roleids) {
- useridList.addAll(controller.dbConnect.runSqlQuery("select userid from sys_userrole where siteid='" + siteid + "' and roleid=" + o).toArrayList("userid", new ArrayList<>()));
- }
- }
- if (isuser) {
- JSONArray userids = sys_remind_configsRows.get(0).getJSONArray("userids");
- for (Object o : userids) {
- useridList.add(Long.parseLong(o.toString()));
- }
- }
- if (issql) {
- String sqlstr = sys_remind_configsRows.get(0).getString("sqlstr");
- if (sqlstr.contains(":")) {
- Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
- if (datarows.isNotEmpty()) {
- for (String key : datarows.get(0).keySet()) {
- if (sqlstr.contains(":" + key)) {
- sqlstr = sqlstr.replace(":" + key, "'" + datarows.get(0).getString(key) + "'");
- }
- }
- }
- }
- try {
- useridList.addAll(controller.dbConnect.runSqlQuery(sqlstr).toArrayList("userid", new ArrayList<>()));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //添加额外的数据
- if (extradata.containsKey("extraUserList")) {
- Object extraUserList = extradata.get("extraUserList");
- useridList.addAll((ArrayList<Long>) extraUserList);
- }
- if (content.contains(":")) {
- Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
- if (datarows.isNotEmpty()) {
- for (String key : datarows.get(0).keySet()) {
- if (content.contains(":" + key)) {
- content = content.replace(":" + key, "'" + datarows.get(0).getString(key) + "'");
- }
- }
- }
- }
- int max = 10;
- while (content.contains("{") && content.contains("}")) {
- int start = content.indexOf("{");
- int end = content.indexOf("}");
- String parm = content.substring(start, end + 1);
- String sql = parm.replace("{", "").replace("}", "");
- Rows rows = controller.dbConnect.runSqlQuery(sql);
- if (rows.size() != 1 || rows.getFieldList().size() != 1) {
- content = content.replace(parm, "-");
- continue;
- } else {
- String value = rows.get(0).getString(rows.getFieldList().get(0));
- content = content.replace(parm, value);
- }
- if (max-- == 0) {
- break;
- }
- }
- //去掉单引号
- content = content.replace("'", "");
- Rows uploadRows = new Rows();
- if (extradata.containsKey("emailurl")) {
- uploadRows = (Rows) extradata.get("emailurl");
- }
- for (Object o : remindmodeArray) {
- String remindmode = (String) o;
- switch (remindmode) {
- case "邮件": {
- for (Long userid : useridList) {
- try {
- EmailContent emailContent = new EmailContent();
- emailContent.addText(content);
- if (uploadRows.isNotEmpty()) {
- emailContent.addFile(uploadRows.get(0).getString("url"));
- }
- 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");
- Remind remind = new Remind(siteid);
- remind.setTitle(remindtitle);
- remind.setToemail(emailList);
- remind.setContent(emailContent);
- remind.setSystemappids(systemappids);
- remind.sendByMail();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- break;
- }
- case "短信": {
- for (Long userid : useridList) {
- try {
- ArrayList<String> phonenumberList = controller.dbConnect.runSqlQuery("select phonenumber from sys_users where userid=" + userid).toArrayList("phonenumber");
- Remind remind = new Remind(siteid);
- remind.setContent(content);
- remind.setTophonenumber(phonenumberList);
- remind.setSystemappids(systemappids);
- remind.sendBySms();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- break;
- }
- case "弹框": {
- for (Long userid : useridList) {
- try {
- Remind remind = new Remind(siteid);
- remind.setTitle(remindtitle);
- remind.setContent(content);
- remind.setTouserid(userid);
- remind.setType("应用");
- remind.setSystemappids(systemappids);
- remind.sendByDialogMsg();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- break;
- }
- case "企业微信": {
- for (Long userid : useridList) {
- try {
- Remind remind = new Remind(siteid);
- remind.setContent(content);
- remind.setTouserid(userid);
- remind.sendByWorkWechat();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- break;
- }
- case "微信公众号": {
- String wechatservice_systemclient = sys_remind_configsRows.get(0).getString("wechatservice_systemclient");
- JSONObject wechatservice_content = null;
- try {
- wechatservice_content = JSONObject.parseObject(Encryption.Decode_Base64(sys_remind_configsRows.get(0).getString("wechatservice_content").replace(" ", "\n")));
- } catch (Exception e) {
- wechatservice_content = new JSONObject();
- }
- if (wechatservice_content.isEmpty() || wechatservice_systemclient.equals("")) {
- break;
- }
- String template_id = wechatservice_content.getJSONObject("templates").getStringValue("template_id");
- if (template_id.equals("")) {
- break;
- }
- String tourl = wechatservice_content.getJSONObject("data").getStringValue("tourl");
- JSONArray dataparams = wechatservice_content.getJSONObject("data").getJSONArray("param");
- HashMap<String, String> datamap = new HashMap<>();
- for (Object dataparam : dataparams) {
- JSONObject dataparamObject = (JSONObject) dataparam;
- String key = dataparamObject.getString("key");
- String value = String.valueOf(dataparamObject.get("value"));
- if (value.contains(":")) {
- Rows datarows = controller.dbConnect.runSqlQuery("select * from " + ownertable + " where " + controller.getuniquecolumnname(ownertable) + "=" + dataid);
- if (datarows.isNotEmpty()) {
- for (String field : datarows.get(0).keySet()) {
- if (value.contains(":" + field)) {
- value = value.replace(":" + field, "'" + datarows.get(0).getString(field) + "'");
- }
- }
- }
- }
- int max2 = 10;
- while (value.contains("{") && value.contains("}")) {
- int start = value.indexOf("{");
- int end = value.indexOf("}");
- String parm = value.substring(start, end + 1);
- String sql = parm.replace("{", "").replace("}", "");
- Rows rows = controller.dbConnect.runSqlQuery(sql);
- if (rows.size() != 1 || rows.getFieldList().size() != 1) {
- break;
- } else {
- value = value.replace(parm, rows.get(0).getString(rows.getFieldList().get(0)));
- }
- if (max2-- == 0) {
- break;
- }
- }
- value = value.replace("'", "");
- datamap.put(key, value);
- }
- String tominiprogram_appid = "";
- String tominiprogram_pagepath = "";
- Rows wechatapps = controller.dbConnect.runSqlQuery("select *from sys_wechatapp where systemclient='" + wechatservice_content.getJSONObject("data").getStringValue("tominiprogram_systemclient") + "'");
- if (wechatapps.isNotEmpty()) {
- tominiprogram_appid = wechatapps.get(0).getString("appid");
- tominiprogram_pagepath = wechatservice_content.getJSONObject("data").getStringValue("tominiprogram_pagepath");
- }
- if (!tominiprogram_pagepath.equals("")) {
- tominiprogram_pagepath = tominiprogram_pagepath + "?id=" + dataid + "&ownertable=\"" + ownertable + "\"";
- tourl = "http://weixin.qq.com/" + tominiprogram_pagepath + "?id=" + dataid + "&ownertable=\"" + ownertable + "\"";
- }
- WechatService wechatService = new WechatService(wechatservice_systemclient);
- for (Long userid : useridList) {
- try {
- wechatService.send(userid, template_id, String.valueOf(Calendar.getInstance().getTimeInMillis()), tourl, datamap, tominiprogram_appid, tominiprogram_pagepath);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- break;
- }
- }
- }
- for (Object obj : useridList) {
- Long userid = Long.valueOf(obj.toString());
- try {
- Remind remind = new Remind(siteid);
- remind.setTitle(remindtitle);
- remind.setType("应用");
- remind.setContent(content);
- remind.setTouserid(userid);
- remind.setObjectname(ownertable);
- remind.setObjectid(dataid);
- remind.setSystemappids(systemappids);
- remind.createSys_message();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- private String type = "";
- private String title = "";
- private String content = "";
- private JSONArray systemappids = new JSONArray();
- private ArrayList<String> tophonenumber = new ArrayList<>();
- private ArrayList<String> toemail = new ArrayList<>();
- private String objectname = "null";
- private Long objectid = 0L;
- private Controller controller;
- private String siteid = "";
- private ArrayList<Long> touserid = new ArrayList<>();
- private EmailContent emailContent = null;
- public Remind(Controller controller) {
- this.controller = controller;
- this.siteid = this.controller.siteid;
- }
- public Remind(String siteid) throws YosException {
- this.siteid = siteid;
- JSONObject object = new JSONObject();
- object.put("siteid", siteid);
- this.controller = new Controller(object);
- this.controller.siteid = this.siteid;
- }
- /**
- * 提醒标题,必填
- *
- * @param title
- */
- public void setTitle(String title) {
- this.title = title;
- }
- /**
- * 提醒内容,非邮件提醒时必填。
- *
- * @param content
- */
- public void setContent(String content) {
- this.content = content;
- }
- /**
- * 邮件提醒内容。content和EmailContent 必填其一
- *
- * @param emailContent
- */
- public void setContent(EmailContent emailContent) {
- this.emailContent = emailContent;
- }
- /**
- * 短信发送对象。短信方式发送时必填
- *
- * @param phonenumber
- */
- public void setTophonenumber(String phonenumber) {
- this.tophonenumber.add(phonenumber);
- }
- /**
- * 短信发送对象。短信方式发送时必填
- *
- * @param tophonenumber
- */
- public void setTophonenumber(ArrayList<String> tophonenumber) {
- this.tophonenumber = tophonenumber;
- }
- /**
- * 邮件发送对象。邮件发送时必填
- *
- * @param toemail
- */
- public void setToemail(String toemail) {
- this.toemail.add(toemail);
- }
- /**
- * 邮件发送对象。邮件发送时必填
- *
- * @param toemail
- */
- public void setToemail(ArrayList<String> toemail) {
- this.toemail = toemail;
- }
- /**
- * 邮件发送对象。邮件发送时必填
- *
- * @param userid
- */
- public void setToemail(Long userid) throws YosException {
- this.toemail = User.getUserMailAddress(controller, userid);
- }
- /**
- * 邮件发送对象。邮件发送时必填
- *
- * @param userids
- */
- public void setToemails(ArrayList<Long> userids) throws YosException {
- for (Long userid : userids) {
- this.toemail.addAll(User.getUserMailAddress(controller, userid));
- }
- }
- public void setObjectname(String objectname) {
- this.objectname = objectname;
- }
- public void setObjectid(Long objectid) {
- this.objectid = objectid;
- }
- /**
- * 弹框发送对象
- *
- * @param userid
- */
- public void setTouserid(ArrayList<Long> userid) {
- this.touserid = userid;
- }
- /**
- * 弹框发送对象
- *
- * @param userid
- */
- public void setTouserid(long userid) {
- this.touserid.add(userid);
- }
- /**
- * 消息类型 应用、系统
- *
- * @param type
- */
- public void setType(String type) {
- this.type = type;
- }
- /**
- * 设置跳转应用id
- *
- * @return
- */
- public void setSystemappids(JSONArray systemappids) {
- this.systemappids = systemappids;
- }
- /**
- * 邮件发送
- *
- * @throws YosException
- */
- public Remind sendByMail() throws YosException {
- if (!Parameter.getBoolean(siteid, "remind_mail")) {
- return this;
- }
- if (siteid.equals("") || title.equals("") || toemail.size() == 0 || (emailContent == null && content.equals(""))) {
- return this;
- }
- try {
- Email email = new Email(siteid, title, toemail);
- if (emailContent == null) {
- EmailContent content = new EmailContent();
- content.addText(this.content);
- email.send(content);
- } else {
- email.send(emailContent);
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new YosException(e.toString());
- }
- return this;
- }
- /**
- * 短信发送
- */
- public Remind sendBySms() throws YosException {
- if (siteid.equals("") || content.equals("") || tophonenumber.size() == 0) {
- return this;
- }
- for (String phonenumber : tophonenumber) {
- Sms sms = new Sms();
- sms.sendout(Sms.SmsType.PassWord, siteid, phonenumber, content);
- }
- return this;
- }
- /**
- * 企业微信发送
- *
- * @return
- * @throws YosException
- */
- public Remind sendByWorkWechat() throws YosException {
- if (siteid.equals("") || content.equals("") || touserid.size() == 0) {
- return this;
- }
- String systemclient = Parameter.getString(siteid, "workwechatsystemclient");
- // SQLFactory sqlFactory = new SQLFactory("sql:select accountno from sys_workwechataccount where siteid='" + siteid + "',systemclient='" + systemclient + "' and userid in $userids$");
- SQLFactory sqlFactory = new SQLFactory(new Remind(), "获取hrcode");
- sqlFactory.addParameter_in("userids", touserid);
- // ArrayList<String> users = this.controller.dbConnect.runSqlQuery(sqlFactory.getSQL()).toArrayList("accountno");
- ArrayList<String> users = this.controller.dbConnect.runSqlQuery(sqlFactory.getSQL()).toArrayList("hrcode");
- try {
- QYWechatMsgSender wechatMsgSender = new QYWechatMsgSender(systemclient);
- String result = wechatMsgSender.sendTextMsg(users, content);
- System.err.println("企业微信发送:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return this;
- }
- /**
- * 弹窗发送
- *
- * @return
- * @throws YosException
- */
- public Remind sendByDialogMsg() throws YosException {
- if (type.equals("") || title.equals("") || content.equals("") || touserid.size() == 0) {
- return this;
- }
- for (Long userid : touserid) {
- for (long usersiteid : User.getUserSiteid(controller, userid)) {
- if (WebSocket.websocketClients.containsKey(usersiteid)) {
- JSONObject methodobject = new JSONObject();
- methodobject.put("class", "remind");//提醒
- methodobject.put("type", type);//消息类型
- methodobject.put("title", title);
- methodobject.put("text", content);
- for (WebSocket webClientSocket : WebSocket.websocketClients.get(usersiteid)) {
- webClientSocket.sendSystemMessage(methodobject);
- }
- }
- }
- }
- return this;
- }
- /**
- * 系统消息生成
- *
- * @throws YosException
- */
- public void createSys_message() throws YosException {
- if (siteid.equals("") || type.equals("") || title.equals("") || content.equals("") || touserid.size() == 0) {
- return;
- }
- for (Long userid : touserid) {
- InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sys_message");
- insertSQL.setUniqueid(controller.createTableID("sys_message"));
- insertSQL.setSiteid(siteid);
- insertSQL.setValue("title", title);
- insertSQL.setValue("type", type);
- insertSQL.setValue("userid", userid);
- insertSQL.setValue("message", content);
- insertSQL.setValue("systemappids", systemappids);
- insertSQL.setValue("objectid", objectid == 0 ? "null" : objectid);
- insertSQL.setValue("objectname", objectname);
- insertSQL.setValue("isread", 0);
- insertSQL.insert();
- }
- }
- }
|