Controller.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. package com.cnd3b.common;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.cnd3b.common.data.Row;
  5. import com.cnd3b.common.data.Rows;
  6. import com.cnd3b.common.data.RowsMap;
  7. import com.cnd3b.common.data.SQLFactory;
  8. import com.cnd3b.common.data.db.DBConnect;
  9. import com.cnd3b.common.data.db.DataPool;
  10. import com.cnd3b.common.parameter.parameter;
  11. import com.cnd3b.common.websocket.WebClientSocket;
  12. import p2.p2server.P2Server;
  13. import p2.pao.PaoRemote;
  14. import p2.pao.PaoSetRemote;
  15. import p2.security.ConnectionKey;
  16. import p2.util.P2Exception;
  17. import java.lang.reflect.Constructor;
  18. import java.lang.reflect.Method;
  19. import java.sql.Connection;
  20. import java.util.*;
  21. public class Controller extends BaseClass {
  22. // 请求正文
  23. public JSONObject content;
  24. public int returnMaxCount = 1000;
  25. public D3BReturnObject returnObject;
  26. /**
  27. * 不要预加载数据,false表示需要预加载,true表示不需要预加载
  28. */
  29. public boolean donotpreloading = false;
  30. public int pageNumber = 1;
  31. public int pageSize = 20;
  32. public String $classname = "";
  33. public String $method = "";
  34. public String $accesstoken = "";
  35. public long userid = 0L;
  36. public String username = "";
  37. public String siteid = "";
  38. public long tagentsid = 0L;
  39. public String fusertype = "";
  40. public String fphonenumber = "";
  41. public DBConnect dbConnect = null;
  42. public boolean fissysadministrator = false;
  43. public boolean fisadministrator = false;
  44. public String hostipport;//访问地址url 如http://localhost:8080 http://127.0.0.1:8080
  45. public Date sysdate;
  46. /**
  47. * 构造函数
  48. *
  49. * @param content
  50. */
  51. public Controller(JSONObject content) {
  52. this.dbConnect = new DBConnect();
  53. sysdate = getDateTime();
  54. this.content = content;
  55. if (content.containsKey("returnMaxCount")) {
  56. returnMaxCount = content.getIntValue("returnMaxCount");
  57. }
  58. returnObject = new D3BReturnObject(this);
  59. donotpreloading = content.containsKey("donotpreloading") && content.getBoolean("donotpreloading");
  60. this.content.remove("donotpreloading");
  61. if (this.content.containsKey("pageNumber")) {
  62. pageNumber = this.content.getIntValue("pageNumber");
  63. }
  64. if (this.content.containsKey("pageSize")) {
  65. pageSize = this.content.getIntValue("pageSize");
  66. }
  67. if (this.content.containsKey("$classname")) {
  68. $classname = this.content.getString("$classname");
  69. }
  70. if (this.content.containsKey("$method")) {
  71. $method = this.content.getString("$method");
  72. }
  73. if (this.content.containsKey("$requestHost")) {
  74. hostipport = this.content.getString("$requestHost");
  75. }
  76. if (this.content.containsKey("userid")) {
  77. userid = this.content.getLongValue("userid");
  78. } else if (this.content.containsKey("$accesstoken")) {
  79. $accesstoken = this.content.getString("$accesstoken");
  80. if (parameter.tokenlist.containsKey($accesstoken)) {
  81. userid = parameter.tokenlist.get($accesstoken);
  82. }
  83. }
  84. if (parameter.userIdList.containsKey(userid)) {
  85. Row row = parameter.userIdList.get(userid);
  86. siteid = row.getString("siteid");
  87. tagentsid = row.getLong("tagentsid");
  88. fusertype = row.getString("fusertype");
  89. username = row.getString("fname");
  90. fphonenumber = row.getString("fphonenumber");
  91. fissysadministrator = row.getBoolean("fissysadministrator");
  92. fisadministrator = row.getBoolean("fisadministrator");
  93. createRequestLog();
  94. } else if ($classname.contains("publicmethod")) {
  95. siteid = (String) content.getOrDefault("siteid", parameter.defaultsiteid);
  96. }
  97. }
  98. public String getSort(String[] sortfield, String defaultsort) {
  99. String sort = "";
  100. if (content.containsKey("sort") && Arrays.asList(sortfield).contains(content.getString("sort").replace("desc", "").replace("asc", "").trim())) {
  101. sort = content.getString("sort");
  102. } else {
  103. sort = defaultsort;
  104. }
  105. return sort;
  106. }
  107. /**
  108. * 每次从数据库里获取数据时,后台自动加载一定数据的数据至缓存中
  109. *
  110. * @param loadcount
  111. */
  112. public void preloading(int loadcount) {
  113. if (!donotpreloading && loadcount > 0) {
  114. /**
  115. * 获得当前页码
  116. */
  117. int pageNumber = this.pageNumber;
  118. new Thread() {
  119. @Override
  120. public void run() {
  121. super.run();
  122. for (int i = 1; i <= loadcount; i++) {
  123. /**
  124. * 先将是否需要缓存数据的key移除
  125. */
  126. content.remove("donotpreloading");
  127. /**
  128. * 设定需要缓存的页码。
  129. */
  130. if (content.get("pageNumber") instanceof String) {
  131. content.put("pageNumber", String.valueOf(pageNumber + i));
  132. } else {
  133. content.put("pageNumber", (pageNumber + i));
  134. }
  135. /**
  136. * 判断该数据是否存在于缓存中,如果不存在则查询一条
  137. */
  138. if (!DataPool.datapoolCtrl.containsKey(content.toString())) {
  139. /**
  140. * 缓存时,强制命令下次不再进行缓存
  141. */
  142. content.put("donotpreloading", true);
  143. try {
  144. /**
  145. * 执行请求方法
  146. */
  147. Class clz = Class.forName("com.cnd3b.restcontroller." + $classname);
  148. Constructor cla = clz.getDeclaredConstructor(JSONObject.class);
  149. Object obj = cla.newInstance(content);
  150. Method method = obj.getClass().getDeclaredMethod($method);
  151. method.invoke(obj);
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. }
  157. }
  158. }.start();
  159. }
  160. }
  161. public long getUniqueIDValue(String tablename, String tableidname) {
  162. ConnectionKey locala = null;
  163. try {
  164. locala = P2Server.getP2Server().getSystemUserInfo()
  165. .getConnectionKey();
  166. Connection localConnection = P2Server.getP2Server()
  167. .getDBConnProvider().getConnection(locala);
  168. return p2.pao.m
  169. .a(localConnection, tablename, tableidname);
  170. } catch (Exception e) {
  171. return 0;
  172. } finally {
  173. P2Server.getP2Server().getDBConnProvider().freeConnection(locala);
  174. }
  175. }
  176. public void saveDataLog(PaoRemote pao) throws P2Exception {
  177. if (content.containsKey("datalog")) {
  178. JSONArray datalogArray = content.getJSONArray("datalog");
  179. PaoSetRemote tdatalog = pao.getPaoSet("$tdatalog", "tdatalog");
  180. for (Object o : datalogArray) {
  181. JSONObject datalogObject = (JSONObject) o;
  182. PaoRemote datalog = tdatalog.addAtEnd();
  183. datalog.setValue("siteid", siteid, 11L);
  184. datalog.setValue("ftablename", pao.getName(), 11L);
  185. datalog.setValue("ftableid", pao.getUniqueIDValue(), 11L);
  186. datalog.setValue("ftype", datalogObject.getString("ftype"), 11L);
  187. datalog.setValue("flog", datalogObject.getString("flog"), 11L);
  188. datalog.setValue("changeby", username, 11L);
  189. datalog.setValue("changedate", getDateTime(), 11L);
  190. datalog.setValue("tenterprise_userid", userid, 11L);
  191. }
  192. }
  193. }
  194. public Rows getDataLog(String ftablename, String ftableid) {
  195. return dbConnect.runSqlQuery("select changeby,changedate,ftype,flog,tenterprise_userid from tdatalog where siteid='" + siteid
  196. + "' and ftablename='" + ftablename + "' and ftableid='" + ftableid + "'");
  197. }
  198. public Rows getDataLog(String ftablename, long ftableid) {
  199. return getDataLog(ftablename, String.valueOf(ftableid));
  200. }
  201. //userid:tuserrequestlogid
  202. public static HashMap<Long, Long> userRequestLogMap = new HashMap<>();
  203. //userid:count
  204. public static HashMap<Long, Long> userRequestLogcountMap = new HashMap<>();
  205. public static String lastday = "";
  206. /**
  207. * 创建请求日志
  208. */
  209. private void createRequestLog() {
  210. new Thread() {
  211. @Override
  212. public void run() {
  213. String nowday = getDate_Str();
  214. if (!lastday.equals(nowday)) {
  215. lastday = nowday;
  216. userRequestLogMap.clear();
  217. }
  218. if (!userRequestLogMap.containsKey(userid)) {
  219. Rows rows = dbConnect.runSqlQuery("select tuserrequestlogid from tuserrequestlog where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and convert(varchar(10),fdate,120)=convert(varchar(10),getdate(),120) ");
  220. if (rows.isEmpty()) {
  221. long tuserrequestlogid = createTableID("tuserrequestlog", "tuserrequestlogid");
  222. SQLFactory sqlFactory = new SQLFactory(this, "请求日志插入");
  223. sqlFactory.addParameter("tuserrequestlogid", tuserrequestlogid);
  224. sqlFactory.addParameter("siteid", siteid);
  225. sqlFactory.addParameter("tenterprise_userid", userid);
  226. sqlFactory.addParameter("tagentsid", tagentsid);
  227. String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
  228. if ("true".equals(status)) {
  229. userRequestLogMap.put(userid, tuserrequestlogid);
  230. }
  231. } else {
  232. userRequestLogMap.put(userid, rows.get(0).getLong("tuserrequestlogid"));
  233. }
  234. userRequestLogcountMap.put(userid, 1L);
  235. } else {
  236. long count = userRequestLogcountMap.get(userid) + 1;
  237. if (count > 9) {
  238. userRequestLogcountMap.put(userid, 1L);
  239. long tuserrequestlogid = userRequestLogMap.get(userid);
  240. dbConnect.runSqlUpdate("update tuserrequestlog set flastrequestdate=getdate(),frequesttimes=frequesttimes+" + count + " where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tuserrequestlogid=" + tuserrequestlogid);
  241. } else {
  242. userRequestLogcountMap.put(userid, count);
  243. }
  244. }
  245. }
  246. }.start();
  247. }
  248. /**
  249. * 获取一个新的表数据ID
  250. */
  251. private static HashMap<String, Long> tableidmap = new HashMap<>();
  252. public long createTableID(String tablename, String fieldname) {
  253. synchronized (Controller.class) {
  254. tablename = tablename.toLowerCase();
  255. fieldname = fieldname.toLowerCase();
  256. if (!tableidmap.containsKey(tablename)) {
  257. Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename);
  258. if (!rows.isEmpty()) {
  259. long id = rows.get(0).getLong(fieldname);
  260. tableidmap.put(tablename, id);
  261. }
  262. }
  263. tableidmap.put(tablename, tableidmap.get(tablename) + 1);
  264. return tableidmap.get(tablename);
  265. }
  266. }
  267. public long createTableID(String tablename, String fieldname, int count) {
  268. synchronized (Controller.class) {
  269. tablename = tablename.toLowerCase();
  270. fieldname = fieldname.toLowerCase();
  271. if (!tableidmap.containsKey(tablename)) {
  272. Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename);
  273. if (!rows.isEmpty()) {
  274. long id = rows.get(0).getLong(fieldname);
  275. tableidmap.put(tablename, id);
  276. }
  277. }
  278. tableidmap.put(tablename, tableidmap.get(tablename) + 1 + count);
  279. return tableidmap.get(tablename) - count;
  280. }
  281. }
  282. public Row getUser(long userid) {
  283. return getUser(userid, siteid);
  284. }
  285. public Row getUser(long userid, String siteid) {
  286. Rows rows = dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid);
  287. return rows.isEmpty() ? null : rows.get(0);
  288. }
  289. public RowsMap getUser(long userids[]) {
  290. StringBuilder where = new StringBuilder("''");
  291. for (long userid : userids) {
  292. where.append(",'").append(userid).append("'");
  293. }
  294. return dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid in(" + where + ")").toRowsMap("tenterprise_userid");
  295. }
  296. public Row getMainUser(long tagentsid) {
  297. Rows rows = dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and fisadministrator=1 and tagentsid=" + tagentsid);
  298. return rows.isEmpty() ? null : rows.get(0);
  299. }
  300. public Rows getAgentUsers(long tagentsid) {
  301. Rows rows = dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tagentsid=" + tagentsid);
  302. return rows;
  303. }
  304. public Row getAgent(long tagentsid) {
  305. Rows rows = dbConnect.runSqlQuery("select * from tagents where siteid='" + siteid + "' and tagentsid=" + tagentsid);
  306. return rows.isEmpty() ? null : rows.get(0);
  307. }
  308. /**
  309. * 获取用户账户余额
  310. *
  311. * @return
  312. */
  313. public long getUserBalance() {
  314. Rows balancerows = dbConnect.runSqlQuery("select faccountbalance from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid);
  315. if (!balancerows.isEmpty()) {
  316. return balancerows.get(0).getLong("faccountbalance");
  317. } else {
  318. return 0L;
  319. }
  320. }
  321. /**
  322. * 获取用户账户冻结金额
  323. *
  324. * @return
  325. */
  326. public long getUserFreez() {
  327. Rows freezrows = dbConnect.runSqlQuery("select ffreezamount from accountfreez_view where siteid='" + siteid + "' and tenterprise_userid=" + userid);
  328. if (!freezrows.isEmpty()) {
  329. return freezrows.get(0).getLong("ffreezamount");
  330. } else {
  331. return 0L;
  332. }
  333. }
  334. /**
  335. * 获取账户可用余额
  336. *
  337. * @return
  338. */
  339. public long getUserCanUseAmount() {
  340. Rows canuseamountrows = dbConnect.runSqlQuery("select (isnull(t1.faccountbalance,0)-isnull(t2.ffreezamount,0))as fcanuseamount from tenterprise_users t1 left join accountfreez_view t2 on t1.siteid=t2.siteid and t1.tenterprise_userid=t2.tenterprise_userid where t1.siteid='" + siteid + "' and t1.tenterprise_userid='" + userid + "'");
  341. if (!canuseamountrows.isEmpty()) {
  342. return canuseamountrows.get(0).getLong("fcanuseamount");
  343. } else {
  344. return 0L;
  345. }
  346. }
  347. /**
  348. * 获取商学院功能说明
  349. *
  350. * @return
  351. */
  352. public long getAppHelpNotes(String ftype) {
  353. long tarchives_scid = 0L;
  354. if (parameter.isdebug()) {
  355. if ("销售系统".equals(ftype)) {
  356. tarchives_scid = 2305L;
  357. }
  358. } else {
  359. if ("销售系统".equals(ftype)) {
  360. tarchives_scid = 2305L;
  361. }
  362. }
  363. return tarchives_scid;
  364. }
  365. public D3BReturnObject_Err getErrReturnObject() {
  366. return returnObject.getErrObject();
  367. }
  368. public D3BReturnObject_Suc getSucReturnObject() {
  369. return returnObject.getSucObject();
  370. }
  371. /**
  372. * 获取当前及下属的账号列表,返回一个账号列表字符串
  373. *
  374. * @return
  375. */
  376. public String[] getSubUsers() {
  377. if (content.containsKey("fsalerid") && !"".equals(content.getString("fsalerid"))) {
  378. return getSubUsers(content.getString("fsalerid"));
  379. } else {
  380. Rows rows = getSubUserRows();
  381. return rows.toArray("tenterprise_userid");
  382. }
  383. }
  384. /**
  385. * 获取当前及下属的账号列表,返回一个账号列表字符串
  386. *
  387. * @return
  388. */
  389. public String[] getSubUsers(String userid) {
  390. Rows rows = getSubUserRows(siteid, userid);
  391. return rows.toArray("tenterprise_userid");
  392. }
  393. /**
  394. * 获取当前及下属的账号列表,返回一个账号列表字符串
  395. *
  396. * @return
  397. */
  398. public String[] getSubUsers(String siteid, String userid) {
  399. Rows rows = getSubUserRows(siteid, userid);
  400. return rows.toArray("tenterprise_userid");
  401. }
  402. public Rows getSubUserRows() {
  403. String key = siteid + userid;
  404. Object object = DataPool.get(key);
  405. if (object != null) {
  406. return (Rows) object;
  407. }
  408. SQLFactory sqlFactory = new SQLFactory("下属账号列表获取");
  409. sqlFactory.addParameter("userid", userid);
  410. sqlFactory.addParameter("siteid", siteid);
  411. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  412. DataPool.put(key, rows, 60);
  413. return rows;
  414. }
  415. public Rows getSubUserRows(String siteid, String userid) {
  416. String key = siteid + userid;
  417. Object object = DataPool.get(key);
  418. if (object != null) {
  419. return (Rows) object;
  420. }
  421. SQLFactory sqlFactory = new SQLFactory("下属账号列表获取");
  422. sqlFactory.addParameter("userid", userid);
  423. sqlFactory.addParameter("siteid", siteid);
  424. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  425. DataPool.put(key, rows, 60);
  426. return rows;
  427. }
  428. public Rows getAttachmentUrl(long tattachmentid) {
  429. SQLFactory docfactory = new SQLFactory("附件查询_ID");
  430. docfactory.addParameter("siteid", siteid);
  431. docfactory.addParameter("tattachmentid", tattachmentid);
  432. return dbConnect.runSqlQuery(docfactory.getSQL());
  433. }
  434. public Rows getAttachmentUrl(String serialnumber) {
  435. SQLFactory docfactory = new SQLFactory("附件查询_名称");
  436. docfactory.addParameter("siteid", siteid);
  437. docfactory.addParameter("serialnumber", serialnumber);
  438. return dbConnect.runSqlQuery(docfactory.getSQL());
  439. }
  440. public Rows getAttachmentUrl(String ownertable, String ownerid) {
  441. SQLFactory docfactory = new SQLFactory("附件查询");
  442. docfactory.addParameter("siteid", siteid);
  443. docfactory.addParameter("ownertable", ownertable);
  444. docfactory.addParameter_in("ownerid", ownerid);
  445. return dbConnect.runSqlQuery(docfactory.getSQL());
  446. }
  447. public RowsMap getAttachmentUrl(String ownertable, String[] ownerids) {
  448. SQLFactory docfactory = new SQLFactory("附件查询");
  449. docfactory.addParameter("siteid", siteid);
  450. docfactory.addParameter("ownertable", ownertable);
  451. docfactory.addParameter_in("ownerid", ownerids);
  452. return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid");
  453. }
  454. public RowsMap getAttachmentUrl(String ownertable, ArrayList<String> ownerids) {
  455. return getAttachmentUrl(ownertable, ownerids.toArray(new String[0]));
  456. }
  457. public Rows getAttachmentUrl(String ownertable, String ownerid, String ftype) {
  458. SQLFactory attquerysql = new SQLFactory("附件查询_类型");
  459. attquerysql.addParameter("siteid", siteid);
  460. attquerysql.addParameter("ownertable", ownertable);
  461. attquerysql.addParameter_in("ownerid", ownerid);
  462. attquerysql.addParameter("ftype", ftype);
  463. return dbConnect.runSqlQuery(attquerysql.getSQL());
  464. }
  465. public RowsMap getAttachmentUrl(String ownertable, String[] ownerids, String ftype) {
  466. SQLFactory docfactory = new SQLFactory("附件查询_类型");
  467. docfactory.addParameter("siteid", siteid);
  468. docfactory.addParameter("ownertable", ownertable);
  469. docfactory.addParameter_in("ownerid", ownerids);
  470. docfactory.addParameter("ftype", ftype);
  471. return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid");
  472. }
  473. public RowsMap getAttachmentUrl(String ownertable, ArrayList<String> ownerids, String ftype) {
  474. return getAttachmentUrl(ownertable, ownerids.toArray(new String[0]), ftype);
  475. }
  476. public static HashMap<Long, String> headPicMap = new HashMap<>();
  477. /**
  478. * 获取用户头像
  479. *
  480. * @return
  481. */
  482. public String getHeadPic(long userid) {
  483. if (!headPicMap.containsKey(userid)) {
  484. Rows rows = getAttachmentUrl("tenterprise_users", String.valueOf(userid), "headportrait");
  485. if (!rows.isEmpty()) {
  486. headPicMap.put(userid, rows.get(0).getString("fobsurl_thumbnail"));
  487. }
  488. }
  489. return headPicMap.getOrDefault(userid, "");
  490. }
  491. public RowsMap getAttachmentQty(String ownertable, String[] ownerids) {
  492. return getAttachmentQty(ownertable, ownerids, "default");
  493. }
  494. public RowsMap getAttachmentQty(String ownertable, ArrayList<String> ownerids) {
  495. return getAttachmentQty(ownertable, ownerids, "default");
  496. }
  497. public Rows getAttachmentQty(String ownertable, String ownerids) {
  498. return getAttachmentQty(ownertable, ownerids, "default");
  499. }
  500. public RowsMap getAttachmentQty(String ownertable, ArrayList<String> ownerids, String ftype) {
  501. return getAttachmentQty(ownertable, ownerids.toArray(new String[0]), ftype);
  502. }
  503. public RowsMap getAttachmentQty(String ownertable, String[] ownerids, String ftype) {
  504. SQLFactory docfactory = new SQLFactory("附件数量查询");
  505. docfactory.addParameter("siteid", siteid);
  506. docfactory.addParameter("ownertable", ownertable);
  507. docfactory.addParameter_in("ownerid", ownerids);
  508. docfactory.addParameter("ftype", ftype);
  509. return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid");
  510. }
  511. public Rows getAttachmentQty(String ownertable, String ownerids, String ftype) {
  512. SQLFactory docfactory = new SQLFactory("附件数量查询");
  513. docfactory.addParameter("siteid", siteid);
  514. docfactory.addParameter("ownertable", ownertable);
  515. docfactory.addParameter_in("ownerid", ownerids);
  516. docfactory.addParameter("ftype", ftype);
  517. Rows rows = dbConnect.runSqlQuery(docfactory.getSQL());
  518. return rows;
  519. }
  520. public static HashMap<String, String> uniquecolumnnamemap = new HashMap<>();
  521. public String getuniquecolumnname(String tablename) {
  522. tablename = tablename.toLowerCase();
  523. if (!uniquecolumnnamemap.containsKey(tablename)) {
  524. SQLFactory sqlFactory = new SQLFactory("表ID字段查询");
  525. sqlFactory.addParameter("tablename", tablename);
  526. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  527. for (Row row : rows) {
  528. uniquecolumnnamemap.put(row.getString("tablename").toLowerCase(), row.getString("uniquecolumnname").toLowerCase());
  529. }
  530. }
  531. return uniquecolumnnamemap.get(tablename);
  532. }
  533. public Rows addAttachmentUrl(Rows rows, String tablename, String fdoctype, String jsonkeyname) {
  534. String uniquecolumnname = getuniquecolumnname(tablename);
  535. RowsMap rowsMap = getAttachmentUrl(tablename, rows.toArrayList(uniquecolumnname), fdoctype);
  536. for (Row row : rows) {
  537. String ownerid = row.getString(uniquecolumnname);
  538. if (rowsMap.containsKey(ownerid)) {
  539. row.put(jsonkeyname, rowsMap.get(ownerid));
  540. } else {
  541. row.put(jsonkeyname, "[]");
  542. }
  543. }
  544. return rows;
  545. }
  546. /**
  547. * 获取请求访问地址数据key
  548. *
  549. * @return
  550. */
  551. public long getRequestAuthorDataKey() throws P2Exception {
  552. long datakey = 0L;
  553. PaoSetRemote trequestauthor = getP2ServerSystemPaoSet("trequestauthor");
  554. PaoRemote pao = trequestauthor.addAtEnd();
  555. pao.setValue("siteid", siteid, 11L);
  556. Calendar calendar = Calendar.getInstance();
  557. pao.setValue("createdate", calendar.getTime(), 11L);
  558. calendar.add(Calendar.SECOND, 20);
  559. pao.setValue("closedate", calendar.getTime(), 11L);
  560. pao.setValue("tenterprise_userid", userid, 11L);
  561. datakey = pao.getUniqueIDValue();
  562. trequestauthor.save();
  563. return datakey;
  564. }
  565. public void p2ServerSystemPaoSetClose() {
  566. for (PaoSetRemote paoset : PaoSetRemoteList) {
  567. try {
  568. paoset.close();
  569. } catch (Exception e) {
  570. e.printStackTrace();
  571. }
  572. }
  573. PaoSetRemoteList.clear();
  574. }
  575. public static HashMap<Long, ArrayList<Long>> imdialog_usermap = new HashMap<>();
  576. /**
  577. * 根据消息对话框ID获取用户id
  578. *
  579. * @param timdialogid
  580. * @return
  581. */
  582. public ArrayList<Long> getImDialogUserIds(long timdialogid) {
  583. if (!imdialog_usermap.containsKey(timdialogid)) {
  584. Rows rows = dbConnect.runSqlQuery("select tenterprise_userid from timdialogusers where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and fisremove=0");
  585. ArrayList<Long> list = new ArrayList<>();
  586. for (Row row : rows) {
  587. list.add(row.getLong("tenterprise_userid"));
  588. }
  589. imdialog_usermap.put(timdialogid, list);
  590. }
  591. return imdialog_usermap.get(timdialogid);
  592. }
  593. /**
  594. * 通知前端执行指定方法
  595. */
  596. public void sendSystemWebSocketMessage(String classname, String method,long userid) {
  597. if (parameter.websocketClients.containsKey(userid)) {
  598. JSONObject methodobject = new JSONObject();
  599. methodobject.put("type", "callmethod");
  600. methodobject.put("classname", classname);
  601. methodobject.put("method", method);
  602. for (WebClientSocket webClientSocket : parameter.websocketClients.get(userid).values()) {
  603. webClientSocket.sendSystemMessage(methodobject);
  604. }
  605. }
  606. }
  607. /**
  608. * 通知前端执行指定方法
  609. */
  610. public void sendSystemWebSocketMessage(String classname, String method) {
  611. if (parameter.websocketClients.containsKey(userid)) {
  612. JSONObject methodobject = new JSONObject();
  613. methodobject.put("type", "callmethod");
  614. methodobject.put("classname", classname);
  615. methodobject.put("method", method);
  616. for (WebClientSocket webClientSocket : parameter.websocketClients.get(userid).values()) {
  617. webClientSocket.sendSystemMessage(methodobject);
  618. }
  619. }
  620. }
  621. /**
  622. * 发送系统消息
  623. */
  624. public void sendSystemWebSocketMessage(long userid, String message) {
  625. if (parameter.websocketClients.containsKey(userid)) {
  626. JSONObject methodobject = new JSONObject();
  627. methodobject.put("type", "remind");//提醒
  628. methodobject.put("text", message);
  629. for (WebClientSocket webClientSocket : parameter.websocketClients.get(userid).values()) {
  630. webClientSocket.sendSystemMessage(methodobject);
  631. }
  632. }
  633. }
  634. /**
  635. * 生成系统消息
  636. *
  637. * @param userid 用户ID,传0表示全部用户
  638. * @param ftype 消息类型(公共、商户、团队)
  639. * @param ftitle 消息小标题
  640. * @param fmessage 消息内容
  641. */
  642. public String createSystemMessage(String ftype, String ftitle, String fmessage, long userid) {
  643. SQLFactory factory = new SQLFactory("系统消息插入");
  644. factory.addParameter("siteid", siteid);
  645. factory.addParameter("ftype", ftype);
  646. factory.addParameter("ftitle", ftitle);
  647. factory.addParameter("fmessage", fmessage);
  648. factory.addParameter("tenterprise_userid", userid);
  649. factory.addParameter("objectid", "null");
  650. factory.addParameter("objectname", "null");
  651. sendSystemWebSocketMessage(userid, fmessage);
  652. return dbConnect.runSqlUpdate(factory.getSQL());
  653. }
  654. /**
  655. * 生成系统消息
  656. *
  657. * @param userid 用户ID,传0表示全部用户
  658. * @param ftype 消息类型(公共、商户、团队)
  659. * @param ftitle 消息小标题
  660. * @param fmessage 消息内容
  661. */
  662. public String createSystemMessage(String ftype, String ftitle, String fmessage, long objectid, String objectname, long userid) {
  663. SQLFactory factory = new SQLFactory("系统消息插入");
  664. factory.addParameter("siteid", siteid);
  665. factory.addParameter("ftype", ftype);
  666. factory.addParameter("ftitle", ftitle);
  667. factory.addParameter("fmessage", fmessage);
  668. factory.addParameter("tenterprise_userid", userid);
  669. factory.addParameter("objectid", objectid);
  670. factory.addParameter("objectname", objectname);
  671. sendSystemWebSocketMessage(userid, fmessage);
  672. return dbConnect.runSqlUpdate(factory.getSQL());
  673. }
  674. /**
  675. * 生成系统消息
  676. *
  677. * @param tagentsid 用户ID,传0表示全部用户
  678. * @param ftype 消息类型(公共、商户、团队)
  679. * @param ftitle 消息小标题
  680. * @param fmessage 消息内容
  681. */
  682. public String createSystemMessageByAgentsid(String ftype, String ftitle, String fmessage, long tagentsid) {
  683. Row row = getMainUser(tagentsid);
  684. if (row != null) {
  685. return createSystemMessage(ftype, ftitle, fmessage, row.getLong("tenterprise_userid"));
  686. }
  687. return "false";
  688. }
  689. /**
  690. * 生成系统消息
  691. *
  692. * @param tagentsid 用户ID,传0表示全部用户
  693. * @param ftype 消息类型(公共、商户、团队)
  694. * @param ftitle 消息小标题
  695. * @param fmessage 消息内容
  696. */
  697. public String createSystemMessageByAgentsid(String ftype, String ftitle, String fmessage, long objectid, String objectname, long tagentsid) {
  698. Row row = getMainUser(tagentsid);
  699. if (row != null) {
  700. return createSystemMessage(ftype, ftitle, fmessage, objectid, objectname, row.getLong("tenterprise_userid"));
  701. }
  702. return "false";
  703. }
  704. public static HashMap<String, Row> tableColumnRowmap = new HashMap<>();
  705. public String[] getTableColumns(String tablename) {
  706. tablename = tablename.toLowerCase();
  707. if (!tableColumnRowmap.containsKey(tablename)) {
  708. Rows rows = dbConnect.runSqlQuery("select a.name as [column],b.name as type from syscolumns a,systypes b where a.id=object_id('" + tablename + "') and a.xtype=b.xtype");
  709. Row columnRow = new Row();
  710. for (Row row : rows) {
  711. columnRow.put(row.getString("column").toLowerCase(), row.getString("type").toLowerCase());
  712. }
  713. tableColumnRowmap.put(tablename, columnRow);
  714. }
  715. if (tableColumnRowmap.containsKey(tablename)) {
  716. return tableColumnRowmap.get(tablename).keySet().toArray(new String[0]);
  717. } else {
  718. return new String[]{};
  719. }
  720. }
  721. public String getTableColumnType(String tablename, String fieldname) {
  722. tablename = tablename.toLowerCase();
  723. fieldname = fieldname.toLowerCase();
  724. if (!tableColumnRowmap.containsKey(tablename)) {
  725. Rows rows = dbConnect.runSqlQuery("select a.name as [column],b.name as type from syscolumns a,systypes b where a.id=object_id('" + tablename + "') and a.xtype=b.xtype");
  726. Row columnRow = new Row();
  727. for (Row row : rows) {
  728. columnRow.put(row.getString("column").toLowerCase(), row.getString("type").toLowerCase());
  729. }
  730. tableColumnRowmap.put(tablename, columnRow);
  731. }
  732. if (tableColumnRowmap.containsKey(tablename) && tableColumnRowmap.get(tablename).containsKey(fieldname)) {
  733. return tableColumnRowmap.get(tablename).getString(fieldname);
  734. } else {
  735. return "";
  736. }
  737. }
  738. /**
  739. * 获取当前账号登陆时间
  740. *
  741. * @return
  742. */
  743. public Date getLoginDate() {
  744. return getLoginDate(userid);
  745. }
  746. /**
  747. * 获取指定账号登陆时间
  748. *
  749. * @return
  750. */
  751. public Date getLoginDate(long userid) {
  752. return parameter.loginDate.getOrDefault(userid, getDateTime());
  753. }
  754. }