imdialog.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package com.cnd3b.restcontroller.system.im.imdialog;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.cnd3b.common.Controller;
  5. import com.cnd3b.common.D3bException;
  6. import com.cnd3b.common.data.Row;
  7. import com.cnd3b.common.data.Rows;
  8. import com.cnd3b.common.data.RowsMap;
  9. import com.cnd3b.common.data.SQLFactory;
  10. import com.cnd3b.common.parameter.parameter;
  11. import com.cnd3b.common.websocket.WebClientSocket;
  12. import p2.pao.PaoRemote;
  13. import p2.pao.PaoSetRemote;
  14. import p2.util.P2Exception;
  15. import java.util.ArrayList;
  16. public class imdialog extends Controller {
  17. /**
  18. * 构造函数
  19. *
  20. * @param content
  21. */
  22. public imdialog(JSONObject content) {
  23. super(content);
  24. }
  25. /**
  26. * 群对话框列表查询
  27. *
  28. * @return
  29. */
  30. public String query_imdialogList() {
  31. /**
  32. * 过滤条件设置
  33. */
  34. StringBuffer where = new StringBuffer(" 1=1 ");
  35. if (content.containsKey("where")) {
  36. JSONObject whereObject = content.getJSONObject("where");
  37. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  38. where.append(" and(");
  39. where.append("t1.fimdialogname like'%").append(whereObject.getString("condition")).append("%' ");
  40. where.append(")");
  41. }
  42. if (whereObject.containsKey("fimdialogtype") && !"".equals(whereObject.getString("fimdialogtype"))) {
  43. where.append(" and(");
  44. where.append("t1.fimdialogtype ='").append(whereObject.getString("fimdialogtype")).append("' ");
  45. where.append(")");
  46. }
  47. }
  48. SQLFactory timdialogSQL = new SQLFactory(this, "即时通讯普通群列表查询");
  49. timdialogSQL.addParameter("siteid", siteid);
  50. timdialogSQL.addParameter("tenterprise_userid", userid);
  51. timdialogSQL.addParameter_SQL("where", where);
  52. Rows rows = dbConnect.runSqlQuery(timdialogSQL.getSQL());
  53. SQLFactory timnewmsgSQL = new SQLFactory(this, "即时通讯普通群最新消息查询");
  54. timnewmsgSQL.addParameter("siteid", siteid);
  55. timnewmsgSQL.addParameter_in("timdialogid", rows.toArrayList("timdialogid"));
  56. RowsMap newmsgmap = timnewmsgSQL.runSqlQuery().toRowsMap("timdialogid");
  57. for (Row row : rows) {
  58. Rows newmsgrows = newmsgmap.get(row.getString("timdialogid"));
  59. for (Row newmsg : newmsgrows) {
  60. newmsg.put("message", JSONObject.parseObject(newmsg.getString("message")));
  61. }
  62. row.put("latestnews", newmsgrows);//最新消息
  63. }
  64. return getSucReturnObject().setData(rows).saveToDataPool().toString();
  65. }
  66. /**
  67. * 群主界面查询
  68. *
  69. * @return
  70. */
  71. public String query_imdialogMain() throws D3bException, P2Exception {
  72. long timdialogid = content.getLong("timdialogid");
  73. SQLFactory timdialogSQL = new SQLFactory(this, "即时通讯群主界面查询");
  74. timdialogSQL.addParameter("siteid", siteid);
  75. timdialogSQL.addParameter("tenterprise_userid", userid);
  76. timdialogSQL.addParameter("timdialogid", timdialogid);
  77. Rows rows = dbConnect.runSqlQuery(timdialogSQL.getSQL());
  78. for (Row row : rows) {
  79. SQLFactory timdialoguserSQL = new SQLFactory(this, "即时通讯群成员列表查询");
  80. timdialoguserSQL.addParameter("siteid", siteid);
  81. timdialoguserSQL.addParameter("timdialogid", content.getString("timdialogid"));
  82. Rows userrows = dbConnect.runSqlQuery(timdialoguserSQL.getSQL());
  83. row.put("users", userrows);
  84. }
  85. return getSucReturnObject().setData(rows).toString();
  86. }
  87. /**
  88. * 一键联系
  89. *
  90. * @return
  91. */
  92. public String quickcontact() throws D3bException, P2Exception {
  93. long tenterprise_userid = content.getLongValue("tenterprise_userid");//根据对方userid,查一对一聊天对话框
  94. SQLFactory factory = new SQLFactory(this, "根据用户ID查询一对一聊天对话框ID");
  95. factory.addParameter("siteid", siteid);
  96. factory.addParameter_in("tenterprise_userid", new String[]{String.valueOf(userid), String.valueOf(tenterprise_userid)});
  97. Rows rows = dbConnect.runSqlQuery(factory.getSQL());
  98. if (!rows.isEmpty()) {
  99. content.put("timdialogid", rows.get(0).getLong("timdialogid"));
  100. return query_imdialogMain();
  101. } else {
  102. JSONArray usersArray = new JSONArray();
  103. usersArray.add(String.valueOf(userid));
  104. usersArray.add(String.valueOf(tenterprise_userid));
  105. content.put("fimdialogname", username + "-" + getUser(tenterprise_userid).getString("fname"));
  106. content.put("fimdialogtype", "普通");
  107. content.put("users", usersArray);
  108. return insertOrModify();
  109. }
  110. }
  111. /**
  112. * 群消息查询
  113. *
  114. * @return
  115. */
  116. public String query_imdialogMessage() {
  117. long timdialogid = content.getLong("timdialogid");
  118. /**
  119. *排序条件设置
  120. */
  121. String[] sortfield = {"t1.timdialogmessageid"};
  122. String sort = getSort(sortfield, "t1.timdialogmessageid");
  123. /**
  124. * 过滤条件设置
  125. */
  126. StringBuffer where = new StringBuffer(" 1=1 ");
  127. if (content.containsKey("where")) {
  128. JSONObject whereObject = content.getJSONObject("where");
  129. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  130. where.append(" and(");
  131. where.append("t1.fnotes like'%").append(whereObject.getString("condition")).append("%' ");
  132. where.append(")");
  133. }
  134. if (whereObject.containsKey("begdate") && !"".equals(whereObject.getString("begdate"))) {
  135. where.append(" and convert(varchar(10),t1.createdate,120)>=").append("'").append(whereObject.getString("begdate")).append("'");
  136. }
  137. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  138. where.append(" and convert(varchar(10),t1.createdate,120)<=").append("'").append(whereObject.getString("enddate")).append("'");
  139. }
  140. }
  141. SQLFactory timdialogSQL = new SQLFactory(this, "即时通讯群历史消息查询", pageSize, pageNumber, sort);
  142. timdialogSQL.addParameter("siteid", siteid);
  143. timdialogSQL.addParameter("timdialogid", timdialogid);
  144. timdialogSQL.addParameter_SQL("where", where);
  145. Rows rows = dbConnect.runSqlQuery(timdialogSQL.getSQL());
  146. for (Row row : rows) {
  147. String message = row.getString("message");
  148. row.put("message", JSONObject.parseObject(message));
  149. }
  150. //清除未读消息数
  151. dbConnect.runSqlUpdate("update timdialogusers set funreadmsgcount=0 where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and tenterprise_userid='" + userid + "'");
  152. return getSucReturnObject().setDataByPaging(rows).toString();
  153. }
  154. public String restUnReadMsgCount() {
  155. long timdialogid = content.getLong("timdialogid");
  156. dbConnect.runSqlUpdate("update timdialogusers set funreadmsgcount=0 where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and tenterprise_userid='" + userid + "'");
  157. return getSucReturnObject().toString();
  158. }
  159. /**
  160. * 新建聊天对话框
  161. *
  162. * @return
  163. * @throws D3bException
  164. * @throws P2Exception
  165. */
  166. public String insertOrModify() throws D3bException, P2Exception {
  167. long timdialogid = content.getLongValue("timdialogid"); //群ID 新增时默认传0
  168. String fimdialogname = content.getString("fimdialogname", "timdialog.fimdialogname", "群名称");//群名称
  169. String fimdialogtype = content.getString("fimdialogtype");//对话类型
  170. String ownertable = content.getString("ownertable");//所有表
  171. String ownerid = content.getString("ownerid");//所有表ID
  172. JSONArray usersArray = content.getJSONArray("users");
  173. ArrayList<String> sqlList = new ArrayList<>();
  174. PaoSetRemote timdialogSet = getP2ServerSystemPaoSet("timdialog", "siteid='" + siteid + "' and timdialogid='" + timdialogid + "'");
  175. PaoRemote timdialog = null;
  176. if (timdialogid <= 0 || timdialogSet.isEmpty()) {
  177. /**
  178. * 创建对话框
  179. */
  180. timdialog = timdialogSet.addAtEnd();
  181. timdialog.setValue("siteid", siteid, 11L);//企业ID
  182. timdialog.setValue("createby", username, 11L);//创建人
  183. timdialog.setValue("createdate", sysdate, 11L);//创建时间
  184. timdialog.setValue("fimdialogtype", fimdialogtype, 11L);//对话框类型
  185. timdialog.setValue("ownertable", ownertable, 11L);//所有表
  186. timdialog.setValue("ownerid", ownerid, 11L);//所有表ID
  187. timdialog.setValue("tenterprise_userid", userid, 11L);//创建账号ID
  188. timdialogid = timdialog.getUniqueIDValue();
  189. /**
  190. * 在初始对话框中添加人员
  191. */
  192. ArrayList<String> userlist = new ArrayList<>();
  193. usersArray.add(userid);//默认将创建人员加入群
  194. for (Object user : usersArray) {
  195. userlist.add(String.valueOf(user));
  196. }
  197. SQLFactory sqlFactory = new SQLFactory(this, "即时通讯群人员添加");
  198. sqlFactory.addParameter("createby", username);
  199. sqlFactory.addParameter("timdialogid", timdialogid);
  200. sqlFactory.addParameter("siteid", siteid);
  201. sqlFactory.addParameter_in("tenterprise_userid", userlist);
  202. sqlList.add(sqlFactory.getSQL());
  203. } else {
  204. timdialog = timdialogSet.getPao(0);
  205. }
  206. timdialog.setValue("fimdialogname", fimdialogname, 11L);//对话框类型
  207. timdialogSet.save();
  208. dbConnect.runSqlUpdate(sqlList);
  209. content.put("timdialogid", timdialogid);
  210. return query_imdialogMain();
  211. }
  212. /**
  213. * 创建聊天话题
  214. *
  215. * @return
  216. */
  217. public String createSubject() throws D3bException, P2Exception {
  218. long timsubjectid = content.getLongValue("timsubjectid"); //话题ID 新增时默认传0
  219. long timdialogid = content.getLongValue("timdialogid");
  220. if (timsubjectid == 0 && !dbConnect.runSqlQuery("select * from timsubject where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and fisclose=0 ").isEmpty()) {
  221. return getErrReturnObject().setErrMsg("当前对话框存在为关闭的话题,不可创建新的话题").toString();
  222. }
  223. boolean ischange = timsubjectid > 0;
  224. PaoSetRemote timsubjectSet = getP2ServerSystemPaoSet("timsubject", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and timsubjectid='" + timsubjectid + "'");
  225. PaoRemote timsubject = null;
  226. if (timsubjectid <= 0 || timsubjectSet.isEmpty()) {
  227. timsubject = timsubjectSet.addAtEnd();
  228. timsubject.setValue("siteid", siteid, 11L);//企业ID
  229. timsubject.setValue("createby", username, 11L);//创建人
  230. timsubject.setValue("createdate", sysdate, 11L);//创建时间
  231. timsubject.setValue("tagentsid", tagentsid, 11L);
  232. timsubject.setValue("tenterprise_userid", userid, 11L);
  233. timsubject.setValue("timdialogid", timdialogid, 11L);
  234. timsubjectid = timsubject.getUniqueIDValue();
  235. } else {
  236. timsubject = timsubjectSet.getPao(0);
  237. }
  238. PaoSetRemote timsubjectcontentSet = timsubject.getPaoSet("$timsubjectcontent", "timsubjectcontent");
  239. if (content.containsKey("fcontent") && !"".equals(content.getString("fcontent"))) {
  240. String fcontent = content.getString("fcontent", "timsubjectcontent.fcontent", "话题内容");//话题内容
  241. PaoRemote timsubjectcontent = timsubjectcontentSet.addAtEnd();
  242. timsubjectcontent.setValue("siteid", siteid, 11L);
  243. timsubjectcontent.setValue("createdate", sysdate, 11L);
  244. timsubjectcontent.setValue("timsubjectid", timsubjectid, 11L);
  245. timsubjectcontent.setValue("fcontent", fcontent, 11L);
  246. }
  247. timsubjectSet.save();
  248. content.put("timsubjectid", timsubjectid);
  249. if (ischange) {//话题内容变更,通知前端进行数据刷新
  250. for (long userid : getImDialogUserIds(timdialogid)) {
  251. if (parameter.websocketClients.containsKey(userid)) {
  252. JSONObject methodobject = new JSONObject();
  253. methodobject.put("type", "refreshimdialog");
  254. methodobject.put("timdialogid", timdialogid);
  255. for (WebClientSocket webClientSocket : parameter.websocketClients.get(userid).values()) {
  256. webClientSocket.sendSystemMessage(methodobject);
  257. }
  258. }
  259. }
  260. }
  261. return querySubject();
  262. }
  263. /**
  264. * 话题关闭
  265. *
  266. * @return
  267. * @throws D3bException
  268. * @throws P2Exception
  269. */
  270. public String closeSubject() throws D3bException, P2Exception {
  271. long timsubjectid = content.getLongValue("timsubjectid"); //话题ID 新增时默认传0
  272. String status = dbConnect.runSqlUpdate("update timsubject set fisclose=1,closedate=getDate() where siteid='" + siteid + "' and timsubjectid='" + timsubjectid + "'");
  273. if ("true".equals(status)) {
  274. return getSucReturnObject().toString();
  275. } else {
  276. return getErrReturnObject().toString();
  277. }
  278. }
  279. /**
  280. * 话题内容查询
  281. *
  282. * @return
  283. */
  284. public String querySubject() {
  285. long timsubjectid = content.getLongValue("timsubjectid");
  286. SQLFactory timsubjectSQL = new SQLFactory(this, "话题查询");
  287. timsubjectSQL.addParameter("siteid", siteid);
  288. timsubjectSQL.addParameter("timsubjectid", timsubjectid);
  289. Rows imsubjectrows = dbConnect.runSqlQuery(timsubjectSQL.getSQL());
  290. for (Row imsubject : imsubjectrows) {
  291. SQLFactory timsubjectcontentSQL = new SQLFactory(this, "话题内容查询");
  292. timsubjectcontentSQL.addParameter("siteid", siteid);
  293. timsubjectcontentSQL.addParameter("timsubjectid", imsubject.getLong("timsubjectid"));
  294. Rows imsubjectcontentrows = dbConnect.runSqlQuery(timsubjectcontentSQL.getSQL());
  295. RowsMap map = getAttachmentUrl("timsubjectcontent", imsubjectcontentrows.toArrayList("timsubjectcontenid"));
  296. for (Row row : imsubjectcontentrows) {
  297. row.put("attinfos", map.get(row.getString("timsubjectcontenid")));
  298. }
  299. imsubject.put("imsubjectcontent", imsubjectcontentrows);
  300. if (!imsubject.getBoolean("fisclose")) {
  301. SQLFactory sqlFactory = new SQLFactory(this, "话题数据分析插入");
  302. sqlFactory.addParameter("siteid", siteid);
  303. sqlFactory.addParameter("timsubjectid", timsubjectid);
  304. sqlFactory.addParameter("tenterprise_userid", userid);
  305. sqlFactory.addParameter("tagentsid", tagentsid);
  306. dbConnect.runSqlUpdate(sqlFactory.getSQL());
  307. }
  308. }
  309. return getSucReturnObject().setData(imsubjectrows).toString();
  310. }
  311. /**
  312. * 根据对话框ID查询最新未关闭的话题内容
  313. *
  314. * @return
  315. */
  316. public String queryDialogUncloseSubject() {
  317. long timdialogid = content.getLongValue("timdialogid");
  318. Rows rows = dbConnect.runSqlQuery("select timsubjectid from timsubject where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and fisclose=0 order by timsubjectid desc");
  319. if (!rows.isEmpty()) {
  320. long timsubjectid = rows.get(0).getLong("timsubjectid");
  321. content.put("timsubjectid", timsubjectid);
  322. return querySubject();
  323. } else {
  324. return getSucReturnObject().toString();
  325. }
  326. }
  327. /**
  328. * 话题回复
  329. *
  330. * @return
  331. */
  332. public String subjectAnswer() {
  333. long timsubjectid = content.getLongValue("timsubjectid");
  334. Rows rows = dbConnect.runSqlQuery("select fisclose from timsubject where siteid='" + siteid + "' and timsubjectid=" + timsubjectid);
  335. if (!rows.isEmpty() && !rows.get(0).getBoolean("fisclose")) {
  336. dbConnect.runSqlUpdate("update timsubjectanalysis set fisanswer=1,fanswertime=getDate() where siteid='" + siteid + "' and timsubjectid='" + timsubjectid + "' and tenterprise_userid='" + userid + "' and fisanswer=0");
  337. return getSucReturnObject().toString();
  338. } else {
  339. return getErrReturnObject().setErrMsg("话题已关闭或不存在").toString();
  340. }
  341. }
  342. /**
  343. * 话题统计信息查询
  344. *
  345. * @return
  346. */
  347. public String querySubjectAnalysis() {
  348. long timsubjectid = content.getLongValue("timsubjectid");//话题ID
  349. long timdialogid = content.getLongValue("timdialogid");//对话框ID
  350. SQLFactory sqlFactory = new SQLFactory(this, "话题数据分析查询");
  351. sqlFactory.addParameter("siteid", siteid);
  352. sqlFactory.addParameter("timsubjectid", timsubjectid);
  353. sqlFactory.addParameter("timdialogid", timdialogid);
  354. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  355. for (Row row : rows) {
  356. row.put("headportraiturl", getHeadPic(row.getLong("tenterprise_userid")));
  357. }
  358. RowsMap fisreadmap = rows.toRowsMap("fisread");
  359. RowsMap fisanswermap = rows.toRowsMap("fisanswer");
  360. JSONObject object = new JSONObject();
  361. object.put("readlist", fisreadmap.get("1"));
  362. object.put("unreadlist", fisreadmap.get("0"));
  363. object.put("answerlist", fisanswermap.get("1"));
  364. object.put("unanswerlist", fisanswermap.get("0"));
  365. return getSucReturnObject().setData(object).toString();
  366. }
  367. }