package com.cnd3b.common; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cnd3b.common.data.Rows; import com.cnd3b.common.data.RowsMap; import com.cnd3b.common.data.SQLFactory; import com.cnd3b.common.data.db.DBConnect; import com.cnd3b.common.data.db.DataPool; import com.cnd3b.common.parameter.parameter; import com.cnd3b.common.data.Row; import org.apache.poi.ss.formula.functions.T; import p2.p2server.P2Server; import p2.pao.PaoRemote; import p2.pao.PaoSetRemote; import p2.security.ConnectionKey; import p2.util.P2Exception; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.sql.Connection; import java.util.*; public class Controller extends BaseClass { // 请求正文 public JSONObject content; public int returnMaxCount = 1000; public D3BReturnObject returnObject; /** * 不要预加载数据,false表示需要预加载,true表示不需要预加载 */ public boolean donotpreloading = false; public int pageNumber = 1; public int pageSize = 20; public String $classname = ""; public String $method = ""; public String $accesstoken = ""; public long userid = 0L; public String username = ""; public String siteid = ""; public long tagentsid = 0L; public String fusertype = ""; public String fphonenumber = ""; public DBConnect dbConnect = null; public boolean fissysadministrator = false; public boolean fisadministrator = false; public String hostipport;//访问地址url 如http://localhost:8080 http://127.0.0.1:8080 public Date sysdate; /** * 构造函数 * * @param content */ public Controller(JSONObject content) { this.dbConnect = new DBConnect(); sysdate = getDateTime(); this.content = content; if (content.containsKey("returnMaxCount")) { returnMaxCount = content.getIntValue("returnMaxCount"); } returnObject = new D3BReturnObject(this); donotpreloading = content.containsKey("donotpreloading") && content.getBoolean("donotpreloading"); this.content.remove("donotpreloading"); if (this.content.containsKey("pageNumber")) { pageNumber = this.content.getIntValue("pageNumber"); } if (this.content.containsKey("pageSize")) { pageSize = this.content.getIntValue("pageSize"); } if (this.content.containsKey("$classname")) { $classname = this.content.getString("$classname"); } if (this.content.containsKey("$method")) { $method = this.content.getString("$method"); } if (this.content.containsKey("$requestHost")) { hostipport = this.content.getString("$requestHost"); } if (this.content.containsKey("userid")) { userid = this.content.getLongValue("userid"); } else if (this.content.containsKey("$accesstoken")) { $accesstoken = this.content.getString("$accesstoken"); if (parameter.tokenlist.containsKey($accesstoken)) { userid = parameter.tokenlist.get($accesstoken); } } if (parameter.userIdList.containsKey(userid)) { Row row = parameter.userIdList.get(userid); siteid = row.getString("siteid"); tagentsid = row.getLong("tagentsid"); fusertype = row.getString("fusertype"); username = row.getString("fname"); fphonenumber = row.getString("fphonenumber"); fissysadministrator = row.getBoolean("fissysadministrator"); fisadministrator = row.getBoolean("fisadministrator"); createRequestLog(); } } public String getSort(String[] sortfield, String defaultsort) { String sort = ""; if (content.containsKey("sort") && Arrays.asList(sortfield).contains(content.getString("sort").replace("desc", "").replace("asc", "").trim())) { sort = content.getString("sort"); } else { sort = defaultsort; } return sort; } /** * 每次从数据库里获取数据时,后台自动加载一定数据的数据至缓存中 * * @param loadcount */ public void preloading(int loadcount) { if (!donotpreloading && loadcount > 0) { /** * 获得当前页码 */ int pageNumber = this.pageNumber; new Thread() { @Override public void run() { super.run(); for (int i = 1; i <= loadcount; i++) { /** * 先将是否需要缓存数据的key移除 */ content.remove("donotpreloading"); /** * 设定需要缓存的页码。 */ if (content.get("pageNumber") instanceof String) { content.put("pageNumber", String.valueOf(pageNumber + i)); } else { content.put("pageNumber", (pageNumber + i)); } /** * 判断该数据是否存在于缓存中,如果不存在则查询一条 */ if (!DataPool.datapoolCtrl.containsKey(content.toString())) { /** * 缓存时,强制命令下次不再进行缓存 */ content.put("donotpreloading", true); try { /** * 执行请求方法 */ Class clz = Class.forName("com.cnd3b.restcontroller." + $classname); Constructor cla = clz.getDeclaredConstructor(JSONObject.class); Object obj = cla.newInstance(content); Method method = obj.getClass().getDeclaredMethod($method); method.invoke(obj); } catch (Exception e) { e.printStackTrace(); } } } } }.start(); } } public long getUniqueIDValue(String tablename, String tableidname) { ConnectionKey locala = null; try { locala = P2Server.getP2Server().getSystemUserInfo() .getConnectionKey(); Connection localConnection = P2Server.getP2Server() .getDBConnProvider().getConnection(locala); return p2.pao.m .a(localConnection, tablename, tableidname); } catch (Exception e) { return 0; } finally { P2Server.getP2Server().getDBConnProvider().freeConnection(locala); } } public void saveDataLog(PaoRemote pao) throws P2Exception { if (content.containsKey("datalog")) { JSONArray datalogArray = content.getJSONArray("datalog"); PaoSetRemote tdatalog = pao.getPaoSet("$tdatalog", "tdatalog"); for (Object o : datalogArray) { JSONObject datalogObject = (JSONObject) o; PaoRemote datalog = tdatalog.addAtEnd(); datalog.setValue("siteid", siteid, 11L); datalog.setValue("ftablename", pao.getName(), 11L); datalog.setValue("ftableid", pao.getUniqueIDValue(), 11L); datalog.setValue("ftype", datalogObject.getString("ftype"), 11L); datalog.setValue("flog", datalogObject.getString("flog"), 11L); datalog.setValue("changeby", username, 11L); datalog.setValue("changedate", getDateTime(), 11L); datalog.setValue("tenterprise_userid", userid, 11L); } } } public Rows getDataLog(String ftablename, String ftableid) { return dbConnect.runSqlQuery("select changeby,changedate,ftype,flog,tenterprise_userid from tdatalog where siteid='" + siteid + "' and ftablename='" + ftablename + "' and ftableid='" + ftableid + "'"); } public Rows getDataLog(String ftablename, long ftableid) { return getDataLog(ftablename, String.valueOf(ftableid)); } //userid:tuserrequestlogid public static HashMap userRequestLogMap = new HashMap<>(); //userid:count public static HashMap userRequestLogcountMap = new HashMap<>(); public static String lastday = ""; /** * 创建请求日志 */ private void createRequestLog() { new Thread() { @Override public void run() { String nowday = getDate_Str(); if (!lastday.equals(nowday)) { lastday = nowday; userRequestLogMap.clear(); } if (!userRequestLogMap.containsKey(userid)) { 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) "); if (rows.isEmpty()) { long tuserrequestlogid = createTableID("tuserrequestlog", "tuserrequestlogid"); SQLFactory sqlFactory = new SQLFactory(this, "请求日志插入"); sqlFactory.addParameter("tuserrequestlogid", tuserrequestlogid); sqlFactory.addParameter("siteid", siteid); sqlFactory.addParameter("tenterprise_userid", userid); sqlFactory.addParameter("tagentsid", tagentsid); String status = dbConnect.runSqlUpdate(sqlFactory.getSQL()); if ("true".equals(status)) { userRequestLogMap.put(userid, tuserrequestlogid); } } else { userRequestLogMap.put(userid, rows.get(0).getLong("tuserrequestlogid")); } userRequestLogcountMap.put(userid, 1L); } else { long count = userRequestLogcountMap.get(userid) + 1; if (count > 9) { userRequestLogcountMap.put(userid, 1L); long tuserrequestlogid = userRequestLogMap.get(userid); dbConnect.runSqlUpdate("update tuserrequestlog set flastrequestdate=getdate(),frequesttimes=frequesttimes+" + count + " where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tuserrequestlogid=" + tuserrequestlogid); } else { userRequestLogcountMap.put(userid, count); } } } }.start(); } /** * 获取一个新的表数据ID */ private static HashMap tableidmap = new HashMap<>(); public long createTableID(String tablename, String fieldname) { synchronized (Controller.class) { tablename = tablename.toLowerCase(); fieldname = fieldname.toLowerCase(); if (!tableidmap.containsKey(tablename)) { Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename); if (!rows.isEmpty()) { long id = rows.get(0).getLong(fieldname); tableidmap.put(tablename, id); } } tableidmap.put(tablename, tableidmap.get(tablename) + 1); return tableidmap.get(tablename); } } public long createTableID(String tablename, String fieldname, int count) { synchronized (Controller.class) { tablename = tablename.toLowerCase(); fieldname = fieldname.toLowerCase(); if (!tableidmap.containsKey(tablename)) { Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename); if (!rows.isEmpty()) { long id = rows.get(0).getLong(fieldname); tableidmap.put(tablename, id); } } tableidmap.put(tablename, tableidmap.get(tablename) + 1 + count); return tableidmap.get(tablename) - count; } } public Row getUser(String userid) { return dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid).get(0); } public Row getUser(String userid, String siteid) { return dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid).get(0); } public RowsMap getUser(String userids[]) { StringBuilder where = new StringBuilder("''"); for (String userid : userids) { where.append(",'").append(userid).append("'"); } return dbConnect.runSqlQuery("select * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid in(" + where + ")").toRowsMap("tenterprise_userid"); } /** * 获取用户账户余额 * * @return */ public long getUserBalance() { Rows balancerows = dbConnect.runSqlQuery("select faccountbalance from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid); if (!balancerows.isEmpty()) { return balancerows.get(0).getLong("faccountbalance"); } else { return 0L; } } /** * 获取用户账户冻结金额 * * @return */ public long getUserFreez() { Rows freezrows = dbConnect.runSqlQuery("select ffreezamount from accountfreez_view where siteid='" + siteid + "' and tenterprise_userid=" + userid); if (!freezrows.isEmpty()) { return freezrows.get(0).getLong("ffreezamount"); } else { return 0L; } } /** * 获取账户可用余额 * * @return */ public long getUserCanUseAmount() { 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 + "'"); if (!canuseamountrows.isEmpty()) { return canuseamountrows.get(0).getLong("fcanuseamount"); } else { return 0L; } } /** * 获取商学院功能说明 * * @return */ public long getAppHelpNotes(String ftype) { long tarchives_scid = 0L; if (parameter.isdebug()) { if ("销售系统".equals(ftype)) { tarchives_scid = 2305L; } } else { if ("销售系统".equals(ftype)) { tarchives_scid = 2305L; } } return tarchives_scid; } public D3BReturnObject_Err getErrReturnObject() { return returnObject.getErrObject(); } public D3BReturnObject_Suc getSucReturnObject() { return returnObject.getSucObject(); } /** * 获取当前及下属的账号列表,返回一个账号列表字符串 * * @return */ public String[] getSubUsers() { if (content.containsKey("fsalerid") && !"".equals(content.getString("fsalerid"))) { return getSubUsers(content.getString("fsalerid")); } else { Rows rows = getSubUserRows(); return rows.toArray("tenterprise_userid"); } } /** * 获取当前及下属的账号列表,返回一个账号列表字符串 * * @return */ public String[] getSubUsers(String userid) { Rows rows = getSubUserRows(siteid, userid); return rows.toArray("tenterprise_userid"); } /** * 获取当前及下属的账号列表,返回一个账号列表字符串 * * @return */ public String[] getSubUsers(String siteid, String userid) { Rows rows = getSubUserRows(siteid, userid); return rows.toArray("tenterprise_userid"); } public Rows getSubUserRows() { String key = siteid + userid; Object object = DataPool.get(key); if (object != null) { return (Rows) object; } SQLFactory sqlFactory = new SQLFactory("下属账号列表获取"); sqlFactory.addParameter("userid", userid); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); DataPool.put(key, rows, 60); return rows; } public Rows getSubUserRows(String siteid, String userid) { String key = siteid + userid; Object object = DataPool.get(key); if (object != null) { return (Rows) object; } SQLFactory sqlFactory = new SQLFactory("下属账号列表获取"); sqlFactory.addParameter("userid", userid); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL()); DataPool.put(key, rows, 60); return rows; } public Row getAttachmentUrl(long tattachmentid) { SQLFactory docfactory = new SQLFactory("附件查询_ID"); docfactory.addParameter("siteid", siteid); docfactory.addParameter("tattachmentid", tattachmentid); return dbConnect.runSqlQuery(docfactory.getSQL()).getRow(0); } public RowsMap getAttachmentUrl(String ownertable, String[] ownerids) { return getAttachmentUrl(ownertable, ownerids, "default"); } public RowsMap getAttachmentUrl(String ownertable, ArrayList ownerids) { return getAttachmentUrl(ownertable, ownerids, "default"); } public Rows getAttachmentUrl(String ownertable, String ownerid) { return getAttachmentUrl(ownertable, ownerid, "default"); } public RowsMap getAttachmentUrl(String ownertable, ArrayList ownerids, String ftype) { return getAttachmentUrl(ownertable, ownerids.toArray(new String[0]), ftype); } public RowsMap getAttachmentUrl(String ownertable, String[] ownerids, String ftype) { SQLFactory docfactory = new SQLFactory("附件查询"); docfactory.addParameter("siteid", siteid); docfactory.addParameter("ownertable", ownertable); docfactory.addParameter_in("ownerid", ownerids); docfactory.addParameter("ftype", ftype); return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid"); } public static HashMap headPicMap = new HashMap<>(); /** * 获取用户头像 * * @return */ public String getHeadPic(long userid) { if (!headPicMap.containsKey(userid)) { Rows rows = getAttachmentUrl("tenterprise_users", String.valueOf(userid), "headportrait"); if (!rows.isEmpty()) { headPicMap.put(userid, rows.get(0).getString("fobsurl_minimage")); } } return headPicMap.getOrDefault(userid, ""); } public Rows getAttachmentUrl(String ownertable, String ownerid, String ftype) { SQLFactory docfactory = new SQLFactory("附件查询"); docfactory.addParameter("siteid", siteid); docfactory.addParameter("ownertable", ownertable); docfactory.addParameter_in("ownerid", ownerid); docfactory.addParameter("ftype", ftype); return dbConnect.runSqlQuery(docfactory.getSQL()); } public RowsMap getAttachmentQty(String ownertable, String[] ownerids) { return getAttachmentQty(ownertable, ownerids, "default"); } public RowsMap getAttachmentQty(String ownertable, ArrayList ownerids) { return getAttachmentQty(ownertable, ownerids, "default"); } public Rows getAttachmentQty(String ownertable, String ownerids) { return getAttachmentQty(ownertable, ownerids, "default"); } public RowsMap getAttachmentQty(String ownertable, ArrayList ownerids, String ftype) { return getAttachmentQty(ownertable, ownerids.toArray(new String[0]), ftype); } public RowsMap getAttachmentQty(String ownertable, String[] ownerids, String ftype) { SQLFactory docfactory = new SQLFactory("附件数量查询"); docfactory.addParameter("siteid", siteid); docfactory.addParameter("ownertable", ownertable); docfactory.addParameter_in("ownerid", ownerids); docfactory.addParameter("ftype", ftype); return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid"); } public Rows getAttachmentQty(String ownertable, String ownerids, String ftype) { SQLFactory docfactory = new SQLFactory("附件数量查询"); docfactory.addParameter("siteid", siteid); docfactory.addParameter("ownertable", ownertable); docfactory.addParameter_in("ownerid", ownerids); docfactory.addParameter("ftype", ftype); Rows rows = dbConnect.runSqlQuery(docfactory.getSQL()); return rows; } public static HashMap uniquecolumnnamemap = new HashMap<>(); public String getuniquecolumnname(String tablename) { tablename = tablename.toLowerCase(); if (!uniquecolumnnamemap.containsKey(tablename)) { SQLFactory sqlFactory = new SQLFactory("表ID字段查询"); sqlFactory.addParameter("tablename", tablename); Rows rows = dbConnect.runSqlQuery(sqlFactory); for (Row row : rows) { uniquecolumnnamemap.put(row.getString("tablename").toLowerCase(), row.getString("uniquecolumnname").toLowerCase()); } } return uniquecolumnnamemap.get(tablename); } public Rows addAttachmentUrl(Rows rows, String tablename, String fdoctype, String jsonkeyname) { String uniquecolumnname = getuniquecolumnname(tablename); RowsMap rowsMap = getAttachmentUrl(tablename, rows.toArrayList(uniquecolumnname), fdoctype); for (Row row : rows) { String ownerid = row.getString(uniquecolumnname); if (rowsMap.containsKey(ownerid)) { row.put(jsonkeyname, rowsMap.get(ownerid)); } else { row.put(jsonkeyname, "[]"); } } return rows; } /** * 获取请求访问地址数据key * * @return */ public long getRequestAuthorDataKey() throws P2Exception { long datakey = 0L; PaoSetRemote trequestauthor = getP2ServerSystemPaoSet("trequestauthor"); PaoRemote pao = trequestauthor.addAtEnd(); pao.setValue("siteid", siteid, 11L); Calendar calendar = Calendar.getInstance(); pao.setValue("createdate", calendar.getTime(), 11L); calendar.add(Calendar.SECOND, 20); pao.setValue("closedate", calendar.getTime(), 11L); pao.setValue("tenterprise_userid", userid, 11L); datakey = pao.getUniqueIDValue(); trequestauthor.save(); return datakey; } public void p2ServerSystemPaoSetClose() { for (PaoSetRemote paoset : PaoSetRemoteList) { try { paoset.close(); } catch (Exception e) { e.printStackTrace(); } } PaoSetRemoteList.clear(); } public static HashMap> imdialog_usermap = new HashMap<>(); /** * 根据消息对话框ID获取用户id * * @param timdialogid * @return */ public ArrayList getUserIds(long timdialogid) { if (!imdialog_usermap.containsKey(timdialogid)) { Rows rows = dbConnect.runSqlQuery("select tenterprise_userid from timdialogusers where siteid='" + siteid + "' and timdialogid='" + timdialogid + "' and fisremove=0"); ArrayList list = new ArrayList<>(); for (Row row : rows) { list.add(row.getLong("tenterprise_userid")); } imdialog_usermap.put(timdialogid, list); } return imdialog_usermap.get(timdialogid); } }