YtiotVDataServiceImpl.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. package cn.com.usky.iot.data.service;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import cn.com.usky.iot.admin.dao.YtiotTAdminDao;
  9. import cn.com.usky.iot.admin.dao.YtiotVUserCompanyDao;
  10. import cn.com.usky.iot.alarm.dao.YtiotVAlarmDao;
  11. import cn.com.usky.iot.company.dao.YtiotVCompanyDao;
  12. import cn.com.usky.iot.data.dao.YtiotVDataDao;
  13. import cn.com.usky.iot.entity.YtiotTAdmin;
  14. import cn.com.usky.iot.entity.YtiotTArea;
  15. import cn.com.usky.iot.entity.YtiotVCompany;
  16. import cn.com.usky.iot.entity.YtiotVCompanyId;
  17. import cn.com.usky.iot.entity.YtiotVHj;
  18. import cn.com.usky.iot.entity.YtiotVHjId;
  19. import cn.com.usky.iot.entity.YtiotVRtu;
  20. import cn.com.usky.iot.entity.YtiotVRtuId;
  21. import cn.com.usky.iot.entity.YtiotVSj;
  22. import cn.com.usky.iot.entity.YtiotVSjId;
  23. import cn.com.usky.iot.entity.YtiotVSync;
  24. import cn.com.usky.iot.entity.YtiotVSyncData;
  25. import cn.com.usky.iot.entity.YtiotVSyncDataId;
  26. import cn.com.usky.iot.entity.YtiotVSyncId;
  27. import cn.com.usky.iot.entity.YtiotVSyncStatus;
  28. import cn.com.usky.iot.entity.YtiotVSyncStatusId;
  29. import cn.com.usky.iot.entity.YtiotVUsercompany;
  30. import cn.com.usky.iot.entity.YtiotVUsercompanyId;
  31. import cn.com.usky.utils.MD5Util;
  32. import net.sf.json.JSONArray;
  33. import net.sf.json.JSONObject;
  34. import java.util.Calendar;
  35. public class YtiotVDataServiceImpl implements YtiotVDataService {
  36. private YtiotVDataDao ytiotVDataDao;
  37. private YtiotVCompanyDao ytiotVCompanyDao;
  38. private YtiotVUserCompanyDao ytiotVUserCompanyDao;
  39. private YtiotVAlarmDao ytiotVAlarmDao;
  40. private YtiotTAdminDao ytiotTAdminDao;
  41. public YtiotVDataDao getYtiotVDataDao() {
  42. return ytiotVDataDao;
  43. }
  44. public void setYtiotVDataDao(YtiotVDataDao ytiotVDataDao) {
  45. this.ytiotVDataDao = ytiotVDataDao;
  46. }
  47. public YtiotVCompanyDao getYtiotVCompanyDao() {
  48. return ytiotVCompanyDao;
  49. }
  50. public void setYtiotVCompanyDao(YtiotVCompanyDao ytiotVCompanyDao) {
  51. this.ytiotVCompanyDao = ytiotVCompanyDao;
  52. }
  53. public YtiotVUserCompanyDao getYtiotVUserCompanyDao() {
  54. return ytiotVUserCompanyDao;
  55. }
  56. public void setYtiotVUserCompanyDao(YtiotVUserCompanyDao ytiotVUserCompanyDao) {
  57. this.ytiotVUserCompanyDao = ytiotVUserCompanyDao;
  58. }
  59. public YtiotTAdminDao getYtiotTAdminDao() {
  60. return ytiotTAdminDao;
  61. }
  62. public void setYtiotTAdminDao(YtiotTAdminDao ytiotTAdminDao) {
  63. this.ytiotTAdminDao = ytiotTAdminDao;
  64. }
  65. public YtiotVAlarmDao getYtiotVAlarmDao() {
  66. return ytiotVAlarmDao;
  67. }
  68. public void setYtiotVAlarmDao(YtiotVAlarmDao ytiotVAlarmDao) {
  69. this.ytiotVAlarmDao = ytiotVAlarmDao;
  70. }
  71. public boolean chk_string_param(String param) {
  72. if(param==null)
  73. return true;
  74. if(param.indexOf("=")>=0)
  75. return false;
  76. if(param.indexOf("\'")>=0)
  77. return false;
  78. if(param.indexOf("\"")>=0)
  79. return false;
  80. return true;
  81. }
  82. public String chkhw(String hqlwhere){
  83. if(hqlwhere==null)
  84. return " where ";
  85. else
  86. return hqlwhere+" and ";
  87. }
  88. public String fieldname(String name) {
  89. if(name.equals("company_code"))
  90. return "companyCode";
  91. if(name.equals("device_id"))
  92. return "deviceId";
  93. if(name.equals("device_name"))
  94. return "deviceName";
  95. if(name.equals("install_time"))
  96. return "installTime";
  97. if(name.equals("sync_id"))
  98. return "syncId";
  99. if(name.equals("table_name"))
  100. return "tableName";
  101. if(name.equals("data_time"))
  102. return "dataTime";
  103. if(name.equals("native_id"))
  104. return "nativeId";
  105. if(name.equals("point_code"))
  106. return "pointCode";
  107. if(name.equals("point_data"))
  108. return "pointData";
  109. if(name.equals("owner_name"))
  110. return "ownerName";
  111. if(name.equals("owner_id"))
  112. return "ownerId";
  113. return name;
  114. }
  115. @Override
  116. public String getSyncPointVList(String queryJson, String page, String start, String limit, String sort)
  117. throws Exception {
  118. // TODO Auto-generated method stub
  119. String company_code = null;
  120. String company_code_list = null;
  121. String device_id = null;
  122. String device_id_list = null;
  123. String device_name = null;
  124. String dwtype = null;
  125. String dwtype_list = null;
  126. String id = null;
  127. String id_list = null;
  128. String install_time_start = null;
  129. String install_time_end = null;
  130. String Company_Code_List = null;
  131. String V_LOGINNAME = null;
  132. String unitinfo = null;
  133. String hqlwhere = null;
  134. String order = null;
  135. String dir = null;
  136. String Start = null;
  137. String Limit = null;
  138. String Page = null;
  139. String order_str = null;
  140. JSONObject q_json = null;
  141. JSONArray Sort = null;
  142. JSONArray jSONArray = new JSONArray();
  143. JSONObject json = new JSONObject();
  144. json.put("action", "getSyncPointVList");
  145. if(queryJson!=null) {
  146. q_json = JSONObject.fromObject(queryJson);
  147. if(q_json.has("company_code")) {
  148. if(q_json.getJSONObject("company_code").isArray()) {
  149. JSONArray j_tmp = q_json.getJSONArray("company_code");
  150. for(int i=0;i<j_tmp.size();i++) {
  151. if(i==0)
  152. company_code_list = j_tmp.getString(i);
  153. else
  154. company_code_list += ","+j_tmp.getString(i);
  155. }
  156. }else
  157. company_code = q_json.getString("company_code");
  158. }
  159. if(q_json.has("device_id")) {
  160. if(q_json.getJSONObject("device_id").isArray()) {
  161. JSONArray j_tmp = q_json.getJSONArray("device_id");
  162. for(int i=0;i<j_tmp.size();i++) {
  163. if(i==0)
  164. device_id_list = j_tmp.getString(i);
  165. else
  166. device_id_list += ","+j_tmp.getString(i);
  167. }
  168. }else
  169. device_id = q_json.getString("device_id");
  170. }
  171. device_name = q_json.has("device_name")?q_json.getString("device_name"):null;
  172. if(q_json.has("dwtype")) {
  173. if(q_json.getJSONObject("dwtype").isArray()) {
  174. JSONArray j_tmp = q_json.getJSONArray("dwtype");
  175. for(int i=0;i<j_tmp.size();i++) {
  176. if(i==0)
  177. dwtype_list = j_tmp.getString(i);
  178. else
  179. dwtype_list += ","+j_tmp.getString(i);
  180. }
  181. }else
  182. dwtype = q_json.getString("dwtype");
  183. }
  184. if(q_json.has("id")) {
  185. if(q_json.getJSONObject("id").isArray()) {
  186. JSONArray j_tmp = q_json.getJSONArray("id");
  187. for(int i=0;i<j_tmp.size();i++) {
  188. if(i==0)
  189. id_list = j_tmp.getString(i);
  190. else
  191. id_list += ","+j_tmp.getString(i);
  192. }
  193. }else
  194. id = q_json.getString("id");
  195. }
  196. V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
  197. unitinfo = q_json.has("unitinfo")?q_json.getString("unitinfo"):null;
  198. install_time_start = q_json.has("install_time_start")?q_json.getString("install_time_start"):null;
  199. install_time_end = q_json.has("install_time_end")?q_json.getString("install_time_end"):null;
  200. }
  201. if(sort!=null) {
  202. Sort = JSONArray.fromObject(sort);
  203. if((Sort.isArray())&&(Sort.size()>0)) {
  204. JSONObject s_json = Sort.getJSONObject(0);
  205. order = s_json.has("property")?s_json.getString("property"):order;
  206. dir = s_json.has("direction")?s_json.getString("direction"):dir;
  207. }
  208. json.put("sort", sort);
  209. }
  210. Start = start;
  211. Limit = limit;
  212. Page = page;
  213. if(V_LOGINNAME!=null) {
  214. String where = " where u.id.agentid='"+V_LOGINNAME+"' ";
  215. List list = ytiotVUserCompanyDao.query(where, null, null, null, null);
  216. if(list.size()>0) {
  217. for(int i=0;i<list.size();i++) {
  218. YtiotVUsercompany o = (YtiotVUsercompany)list.get(i);
  219. YtiotVUsercompanyId oid = o.getId();
  220. if(i==0)
  221. Company_Code_List = oid.getOwnerCode();
  222. else
  223. Company_Code_List += ","+oid.getOwnerCode();
  224. }
  225. }
  226. }
  227. if(Company_Code_List!=null)
  228. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+Company_Code_List+") ";
  229. else {
  230. if(company_code!=null)
  231. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" like '%"+company_code+"%' ";
  232. if(company_code_list!=null)
  233. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+company_code_list+") ";
  234. }
  235. if(device_id!=null)
  236. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("device_id")+" like '%"+device_id+"%' ";
  237. if(device_id_list!=null)
  238. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("device_id")+" in ("+device_id_list+") ";
  239. if(device_name!=null)
  240. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("device_name")+" like '%"+device_name+"%' ";
  241. if(dwtype!=null)
  242. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("dwtype")+" = "+dwtype+" ";
  243. if(dwtype_list!=null)
  244. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("dwtype")+" in ("+dwtype_list+") ";
  245. if(id!=null)
  246. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("id")+" = "+id+" ";
  247. if(id_list!=null)
  248. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("id")+" in ("+id_list+") ";
  249. if(install_time_start!=null)
  250. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("install_time")+" >='"+install_time_start+"' ";
  251. if(install_time_end!=null)
  252. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("install_time")+" <='"+install_time_end+"' ";
  253. if(unitinfo!=null)
  254. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("unitinfo")+" like '%"+unitinfo+"%' ";
  255. int totalCount = ytiotVDataDao.getSyncPointCount(hqlwhere);
  256. json.put("totalCount", totalCount);
  257. if(Start!=null){
  258. if(Integer.parseInt(Start)>totalCount){
  259. Start = "0";
  260. Page = "0";
  261. }else if(page==null)
  262. Page="0";
  263. }else{
  264. Start = "0";
  265. Page = "0";
  266. }
  267. json.put("page", Page);
  268. json.put("start", Start);
  269. json.put("limit", Limit);
  270. if(totalCount>0) {
  271. List<?> list = ytiotVDataDao.SyncPointQuery(hqlwhere, order_str, dir, Start, Limit);
  272. if(list.size()>0) {
  273. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  274. for(int i=0;i<list.size();i++) {
  275. YtiotVSync o = (YtiotVSync)list.get(i);
  276. YtiotVSyncId oid = o.getId();
  277. @SuppressWarnings("rawtypes")
  278. Map<String, Comparable> map = new HashMap<String, Comparable>();
  279. map.put("company_code", oid.getCompanyCode());
  280. map.put("device_id", oid.getDeviceId());
  281. map.put("device_name", oid.getDeviceName());
  282. map.put("dwtype", oid.getDwtype());
  283. map.put("id", oid.getId());
  284. map.put("install_time", df.format(oid.getInstallTime()));
  285. map.put("rtmp", oid.getRtmp());
  286. map.put("sync_id", oid.getSyncId());
  287. map.put("tablename", oid.getTableName());
  288. map.put("unitinfo", oid.getUnitinfo());
  289. JSONObject jSONObject = JSONObject.fromObject(map);
  290. jSONArray.add(jSONObject);
  291. }
  292. json.put("RESULT", jSONArray);
  293. }
  294. }
  295. return json.toString();
  296. }
  297. @Override
  298. public String getSyncDataList(String queryJson)
  299. throws Exception {
  300. // TODO Auto-generated method stub
  301. String company_code = null;
  302. String company_code_list = null;
  303. String device_id = null;
  304. String device_id_list = null;
  305. String dwtype = null;
  306. String dwtype_list = null;
  307. String point_code = null;
  308. String point_code_list = null;
  309. String id = null;
  310. String id_list = null;
  311. String company = null;
  312. String data_time_start = null;
  313. String data_time_end = null;
  314. String name = null;
  315. String Company_Code_List = null;
  316. String V_LOGINNAME = null;
  317. String hqlwhere = null;
  318. String order = null;
  319. String dir = null;
  320. String Start = null;
  321. String Limit = null;
  322. String Page = null;
  323. String order_str = null;
  324. JSONObject q_json = null;
  325. JSONArray Sort = null;
  326. JSONArray jSONArray = new JSONArray();
  327. JSONObject json = new JSONObject();
  328. json.put("action", "getSyncDataVList");
  329. if(queryJson!=null) {
  330. q_json = JSONObject.fromObject(queryJson);
  331. if(q_json.has("company_code")) {
  332. if((q_json.optJSONObject("company_code")!=null)&&(q_json.optJSONObject("company_code").isArray())) {
  333. JSONArray j_tmp = q_json.getJSONArray("company_code");
  334. for(int i=0;i<j_tmp.size();i++) {
  335. if(i==0)
  336. company_code_list = j_tmp.getString(i);
  337. else
  338. company_code_list += ","+j_tmp.getString(i);
  339. }
  340. }else
  341. company_code = q_json.getString("company_code");
  342. }
  343. if(q_json.has("device_id")) {
  344. if((q_json.optJSONObject("device_id")!=null)&&(q_json.optJSONObject("device_id").isArray())) {
  345. JSONArray j_tmp = q_json.getJSONArray("device_id");
  346. for(int i=0;i<j_tmp.size();i++) {
  347. if(i==0)
  348. device_id_list = j_tmp.getString(i);
  349. else
  350. device_id_list += ","+j_tmp.getString(i);
  351. }
  352. }else
  353. device_id = q_json.getString("device_id");
  354. }
  355. if(q_json.has("dwtype")) {
  356. if((q_json.optJSONObject("dwtype")!=null)&&(q_json.optJSONObject("dwtype").isArray())) {
  357. JSONArray j_tmp = q_json.getJSONArray("dwtype");
  358. for(int i=0;i<j_tmp.size();i++) {
  359. if(i==0)
  360. dwtype_list = j_tmp.getString(i);
  361. else
  362. dwtype_list += ","+j_tmp.getString(i);
  363. }
  364. }else
  365. dwtype = q_json.getString("dwtype");
  366. }
  367. if(q_json.has("point_code")) {
  368. if((q_json.optJSONObject("point_code")!=null)&&(q_json.optJSONObject("point_code").isArray())) {
  369. JSONArray j_tmp = q_json.getJSONArray("point_code");
  370. for(int i=0;i<j_tmp.size();i++) {
  371. if(i==0)
  372. point_code_list = j_tmp.getString(i);
  373. else
  374. point_code_list += ","+j_tmp.getString(i);
  375. }
  376. }else
  377. point_code = q_json.getString("point_code");
  378. }
  379. if(q_json.has("id")) {
  380. if((q_json.optJSONObject("id")!=null)&&(q_json.optJSONObject("id").isArray())) {
  381. JSONArray j_tmp = q_json.getJSONArray("id");
  382. for(int i=0;i<j_tmp.size();i++) {
  383. if(i==0)
  384. id_list = j_tmp.getString(i);
  385. else
  386. id_list += ","+j_tmp.getString(i);
  387. }
  388. }else
  389. id = q_json.getString("id");
  390. }
  391. company = q_json.has("company")?q_json.getString("company"):null;
  392. data_time_start = q_json.has("data_time_start")?q_json.getString("data_time_start"):null;
  393. data_time_end = q_json.has("data_time_end")?q_json.getString("data_time_end"):null;
  394. name = q_json.has("name")?q_json.getString("name"):null;
  395. V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
  396. }
  397. // if(sort!=null) {
  398. // Sort = JSONArray.fromObject(sort);
  399. // if((Sort.isArray())&&(Sort.size()>0)) {
  400. // JSONObject s_json = Sort.getJSONObject(0);
  401. // order = s_json.has("property")?s_json.getString("property"):order;
  402. // dir = s_json.has("direction")?s_json.getString("direction"):dir;
  403. // }
  404. // json.put("sort", sort);
  405. // }
  406. // Start = start;
  407. // Limit = limit;
  408. // Page = page;
  409. if((V_LOGINNAME!=null)&&(company_code==null)&&(Company_Code_List==null)) {
  410. String where = " where u.id.agentid='"+V_LOGINNAME+"' ";
  411. List list = ytiotVUserCompanyDao.query(where, null, null, null, null);
  412. if(list.size()>0) {
  413. for(int i=0;i<list.size();i++) {
  414. YtiotVUsercompany o = (YtiotVUsercompany)list.get(i);
  415. YtiotVUsercompanyId oid = o.getId();
  416. if(i==0)
  417. Company_Code_List = oid.getOwnerCode();
  418. else
  419. Company_Code_List += ","+oid.getOwnerCode();
  420. }
  421. }
  422. }
  423. if(device_id!=null)
  424. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" = '"+device_id+"' ";
  425. else {
  426. if(company!=null) {
  427. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("owner_name")+" like '%"+company+"%' ";
  428. }
  429. if(Company_Code_List!=null)
  430. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+Company_Code_List+") ";
  431. else {
  432. if(company_code!=null)
  433. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" like '%"+company_code+"%' ";
  434. if(company_code_list!=null)
  435. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+company_code_list+") ";
  436. }
  437. }
  438. if(device_id_list!=null)
  439. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" in ("+device_id+") ";
  440. if(dwtype!=null)
  441. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" = "+dwtype+" ";
  442. if(dwtype_list!=null)
  443. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" in ("+dwtype_list+") ";
  444. if(point_code!=null)
  445. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code+") ";
  446. if(point_code_list!=null)
  447. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code_list+") ";
  448. if(id!=null)
  449. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" = "+id+" ";
  450. if(id_list!=null)
  451. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" in ("+id_list+") ";
  452. if(data_time_start!=null)
  453. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" >= '"+data_time_start+"' ";
  454. if(data_time_end!=null)
  455. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" <= '"+data_time_end+"' ";
  456. if(name!=null)
  457. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("name")+" like '%"+name+"%' ";
  458. int totalCount = ytiotVDataDao.getSyncDataCount(hqlwhere);
  459. json.put("totalCount", totalCount);
  460. if(order!=null) {
  461. order_str = " order by "+fieldname(order)+" ";
  462. } else {
  463. order_str = " order by data_time ";
  464. dir = "DESC";
  465. }
  466. if(totalCount>0) {
  467. List<?> list = ytiotVDataDao.SyncDataQuery(hqlwhere, order_str, dir, Start, Limit);
  468. if(list.size()>0) {
  469. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  470. for(int i=0;i<list.size();i++) {
  471. YtiotVSyncData o = (YtiotVSyncData)list.get(i);
  472. YtiotVSyncDataId oid = o.getId();
  473. @SuppressWarnings("rawtypes")
  474. Map<String, Comparable> map = new HashMap<String, Comparable>();
  475. map.put("orderIdx", Integer.parseInt(Start)+i+1);
  476. map.put("company", oid.getCompany());
  477. map.put("company_code", oid.getCompanyCode());
  478. map.put("content", oid.getContent());
  479. map.put("data_time", df.format(oid.getDataTime()));
  480. map.put("device_id", oid.getDeviceId());
  481. map.put("dwtype", oid.getDwtype());
  482. map.put("id", oid.getId());
  483. map.put("name", oid.getName());
  484. map.put("native_id", oid.getNativeId());
  485. map.put("point_code", oid.getPointCode());
  486. map.put("point_data", oid.getPointData());
  487. JSONObject jSONObject = JSONObject.fromObject(map);
  488. jSONArray.add(jSONObject);
  489. }
  490. json.put("RESULT", jSONArray);
  491. }
  492. }
  493. return json.toString();
  494. }
  495. @Override
  496. public String getSyncDataVList(String queryJson, String page, String start, String limit, String sort)
  497. throws Exception {
  498. // TODO Auto-generated method stub
  499. String company_code = null;
  500. String company_code_list = null;
  501. String device_id = null;
  502. String device_id_list = null;
  503. String dwtype = null;
  504. String dwtype_list = null;
  505. String point_code = null;
  506. String point_code_list = null;
  507. String id = null;
  508. String id_list = null;
  509. String company = null;
  510. String data_time_start = null;
  511. String data_time_end = null;
  512. String name = null;
  513. String Company_Code_List = null;
  514. String V_LOGINNAME = null;
  515. String hqlwhere = null;
  516. String order = null;
  517. String dir = null;
  518. String Start = null;
  519. String Limit = null;
  520. String Page = null;
  521. String order_str = null;
  522. JSONObject q_json = null;
  523. JSONArray Sort = null;
  524. JSONArray jSONArray = new JSONArray();
  525. JSONObject json = new JSONObject();
  526. json.put("action", "getSyncDataVList");
  527. if(queryJson!=null) {
  528. q_json = JSONObject.fromObject(queryJson);
  529. if(q_json.has("company_code")) {
  530. if((q_json.optJSONObject("company_code")!=null)&&(q_json.optJSONObject("company_code").isArray())) {
  531. JSONArray j_tmp = q_json.getJSONArray("company_code");
  532. for(int i=0;i<j_tmp.size();i++) {
  533. if(i==0)
  534. company_code_list = j_tmp.getString(i);
  535. else
  536. company_code_list += ","+j_tmp.getString(i);
  537. }
  538. }else
  539. company_code = q_json.getString("company_code");
  540. }
  541. if(q_json.has("device_id")) {
  542. if((q_json.optJSONObject("device_id")!=null)&&(q_json.optJSONObject("device_id").isArray())) {
  543. JSONArray j_tmp = q_json.getJSONArray("device_id");
  544. for(int i=0;i<j_tmp.size();i++) {
  545. if(i==0)
  546. device_id_list = j_tmp.getString(i);
  547. else
  548. device_id_list += ","+j_tmp.getString(i);
  549. }
  550. }else
  551. device_id = q_json.getString("device_id");
  552. }
  553. if(q_json.has("dwtype")) {
  554. if((q_json.optJSONObject("dwtype")!=null)&&(q_json.optJSONObject("dwtype").isArray())) {
  555. JSONArray j_tmp = q_json.getJSONArray("dwtype");
  556. for(int i=0;i<j_tmp.size();i++) {
  557. if(i==0)
  558. dwtype_list = j_tmp.getString(i);
  559. else
  560. dwtype_list += ","+j_tmp.getString(i);
  561. }
  562. }else
  563. dwtype = q_json.getString("dwtype");
  564. }
  565. if(q_json.has("point_code")) {
  566. if((q_json.optJSONObject("point_code")!=null)&&(q_json.optJSONObject("point_code").isArray())) {
  567. JSONArray j_tmp = q_json.getJSONArray("point_code");
  568. for(int i=0;i<j_tmp.size();i++) {
  569. if(i==0)
  570. point_code_list = j_tmp.getString(i);
  571. else
  572. point_code_list += ","+j_tmp.getString(i);
  573. }
  574. }else
  575. point_code = q_json.getString("point_code");
  576. }
  577. if(q_json.has("id")) {
  578. if((q_json.optJSONObject("id")!=null)&&(q_json.optJSONObject("id").isArray())) {
  579. JSONArray j_tmp = q_json.getJSONArray("id");
  580. for(int i=0;i<j_tmp.size();i++) {
  581. if(i==0)
  582. id_list = j_tmp.getString(i);
  583. else
  584. id_list += ","+j_tmp.getString(i);
  585. }
  586. }else
  587. id = q_json.getString("id");
  588. }
  589. company = q_json.has("company")?q_json.getString("company"):null;
  590. data_time_start = q_json.has("data_time_start")?q_json.getString("data_time_start"):null;
  591. data_time_end = q_json.has("data_time_end")?q_json.getString("data_time_end"):null;
  592. name = q_json.has("name")?q_json.getString("name"):null;
  593. V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
  594. }
  595. if(sort!=null) {
  596. Sort = JSONArray.fromObject(sort);
  597. if((Sort.isArray())&&(Sort.size()>0)) {
  598. JSONObject s_json = Sort.getJSONObject(0);
  599. order = s_json.has("property")?s_json.getString("property"):order;
  600. dir = s_json.has("direction")?s_json.getString("direction"):dir;
  601. }
  602. json.put("sort", sort);
  603. }
  604. Start = start;
  605. Limit = limit;
  606. Page = page;
  607. if((V_LOGINNAME!=null)&&(company_code==null)&&(Company_Code_List==null)) {
  608. String where = " where u.id.agentid='"+V_LOGINNAME+"' ";
  609. List list = ytiotVUserCompanyDao.query(where, null, null, null, null);
  610. if(list.size()>0) {
  611. for(int i=0;i<list.size();i++) {
  612. YtiotVUsercompany o = (YtiotVUsercompany)list.get(i);
  613. YtiotVUsercompanyId oid = o.getId();
  614. if(i==0)
  615. Company_Code_List = oid.getOwnerCode();
  616. else
  617. Company_Code_List += ","+oid.getOwnerCode();
  618. }
  619. }
  620. }
  621. if(device_id!=null)
  622. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" = '"+device_id+"' ";
  623. else {
  624. if(company!=null) {
  625. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("owner_name")+" like '%"+company+"%' ";
  626. }
  627. if(Company_Code_List!=null)
  628. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+Company_Code_List+") ";
  629. else {
  630. if(company_code!=null)
  631. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" like '%"+company_code+"%' ";
  632. if(company_code_list!=null)
  633. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+company_code_list+") ";
  634. }
  635. }
  636. if(device_id_list!=null)
  637. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" in ("+device_id+") ";
  638. if(dwtype!=null)
  639. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" = "+dwtype+" ";
  640. if(dwtype_list!=null)
  641. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" in ("+dwtype_list+") ";
  642. if(point_code!=null)
  643. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code+") ";
  644. if(point_code_list!=null)
  645. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code_list+") ";
  646. if(id!=null)
  647. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" = "+id+" ";
  648. if(id_list!=null)
  649. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" in ("+id_list+") ";
  650. if(data_time_start!=null)
  651. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" >= '"+data_time_start+"' ";
  652. if(data_time_end!=null)
  653. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" <= '"+data_time_end+"' ";
  654. if(name!=null)
  655. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("name")+" like '%"+name+"%' ";
  656. int totalCount = ytiotVDataDao.getSyncDataCount(hqlwhere);
  657. json.put("totalCount", totalCount);
  658. if(Start!=null){
  659. if(Integer.parseInt(Start)>totalCount){
  660. Start = "0";
  661. Page = "0";
  662. }else if(page==null)
  663. Page="0";
  664. }else{
  665. Start = "0";
  666. Page = "0";
  667. }
  668. json.put("page", Page);
  669. json.put("start", Start);
  670. json.put("limit", Limit);
  671. if(order!=null) {
  672. order_str = " order by "+fieldname(order)+" ";
  673. } else {
  674. order_str = " order by data_time ";
  675. dir = "DESC";
  676. }
  677. if(totalCount>0) {
  678. List<?> list = ytiotVDataDao.SyncDataQuery(hqlwhere, order_str, dir, Start, Limit);
  679. if(list.size()>0) {
  680. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  681. for(int i=0;i<list.size();i++) {
  682. YtiotVSyncData o = (YtiotVSyncData)list.get(i);
  683. YtiotVSyncDataId oid = o.getId();
  684. @SuppressWarnings("rawtypes")
  685. Map<String, Comparable> map = new HashMap<String, Comparable>();
  686. map.put("company", oid.getCompany());
  687. map.put("company_code", oid.getCompanyCode());
  688. map.put("content", oid.getContent());
  689. map.put("data_time", df.format(oid.getDataTime()));
  690. map.put("device_id", oid.getDeviceId());
  691. map.put("dwtype", oid.getDwtype());
  692. map.put("id", oid.getId());
  693. map.put("name", oid.getName());
  694. map.put("native_id", oid.getNativeId());
  695. map.put("point_code", oid.getPointCode());
  696. map.put("point_data", oid.getPointData());
  697. JSONObject jSONObject = JSONObject.fromObject(map);
  698. jSONArray.add(jSONObject);
  699. }
  700. json.put("RESULT", jSONArray);
  701. }
  702. }
  703. return json.toString();
  704. }
  705. @Override
  706. public String getSyncDataQList(String queryJson, String page, String start, String limit, String sort)
  707. throws Exception {
  708. // TODO Auto-generated method stub
  709. String company_code = null;
  710. String company_code_list = null;
  711. String device_id = null;
  712. String device_id_list = null;
  713. String dwtype = null;
  714. String dwtype_list = null;
  715. String point_code = null;
  716. String point_code_list = null;
  717. String id = null;
  718. String id_list = null;
  719. String company = null;
  720. String data_time_start = null;
  721. String data_time_end = null;
  722. String name = null;
  723. String Company_Code_List = null;
  724. String V_LOGINNAME = null;
  725. String hqlwhere = null;
  726. String order = null;
  727. String dir = null;
  728. String Start = null;
  729. String Limit = null;
  730. String Page = null;
  731. String order_str = null;
  732. JSONObject q_json = null;
  733. JSONArray Sort = null;
  734. JSONArray jSONArray = new JSONArray();
  735. JSONObject json = new JSONObject();
  736. json.put("action", "getSyncDataVList");
  737. if(queryJson!=null) {
  738. q_json = JSONObject.fromObject(queryJson);
  739. if(q_json.has("company_code")) {
  740. if((q_json.optJSONObject("company_code")!=null)&&(q_json.optJSONObject("company_code").isArray())) {
  741. JSONArray j_tmp = q_json.getJSONArray("company_code");
  742. for(int i=0;i<j_tmp.size();i++) {
  743. if(i==0)
  744. company_code_list = j_tmp.getString(i);
  745. else
  746. company_code_list += ","+j_tmp.getString(i);
  747. }
  748. }else
  749. company_code = q_json.getString("company_code");
  750. }
  751. if(q_json.has("device_id")) {
  752. if((q_json.optJSONObject("device_id")!=null)&&(q_json.optJSONObject("device_id").isArray())) {
  753. JSONArray j_tmp = q_json.getJSONArray("device_id");
  754. for(int i=0;i<j_tmp.size();i++) {
  755. if(i==0)
  756. device_id_list = j_tmp.getString(i);
  757. else
  758. device_id_list += ","+j_tmp.getString(i);
  759. }
  760. }else
  761. device_id = q_json.getString("device_id");
  762. }
  763. if(dwtype!=null) {
  764. if((dwtype.equals("2"))||(dwtype.equals("4"))||(dwtype.equals("5")))
  765. {
  766. dwtype_list = "2,4,5";
  767. dwtype=null;
  768. }
  769. // for(int i=0;i<2;i++) {
  770. // if(i==0)
  771. // dwtype_list = "2";
  772. // else
  773. // dwtype_list += ","+"5";
  774. // }
  775. }
  776. if(q_json.has("point_code")) {
  777. if((q_json.optJSONObject("point_code")!=null)&&(q_json.optJSONObject("point_code").isArray())) {
  778. JSONArray j_tmp = q_json.getJSONArray("point_code");
  779. for(int i=0;i<j_tmp.size();i++) {
  780. if(i==0)
  781. point_code_list = j_tmp.getString(i);
  782. else
  783. point_code_list += ","+j_tmp.getString(i);
  784. }
  785. }else
  786. point_code = q_json.getString("point_code");
  787. }
  788. if(q_json.has("id")) {
  789. if((q_json.optJSONObject("id")!=null)&&(q_json.optJSONObject("id").isArray())) {
  790. JSONArray j_tmp = q_json.getJSONArray("id");
  791. for(int i=0;i<j_tmp.size();i++) {
  792. if(i==0)
  793. id_list = j_tmp.getString(i);
  794. else
  795. id_list += ","+j_tmp.getString(i);
  796. }
  797. }else
  798. id = q_json.getString("id");
  799. }
  800. company = q_json.has("company")?q_json.getString("company"):null;
  801. data_time_start = q_json.has("data_time_start")?q_json.getString("data_time_start"):null;
  802. data_time_end = q_json.has("data_time_end")?q_json.getString("data_time_end"):null;
  803. name = q_json.has("name")?q_json.getString("name"):null;
  804. V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
  805. }
  806. if(sort!=null) {
  807. Sort = JSONArray.fromObject(sort);
  808. if((Sort.isArray())&&(Sort.size()>0)) {
  809. JSONObject s_json = Sort.getJSONObject(0);
  810. order = s_json.has("property")?s_json.getString("property"):order;
  811. dir = s_json.has("direction")?s_json.getString("direction"):dir;
  812. }
  813. json.put("sort", sort);
  814. }
  815. Start = start;
  816. Limit = limit;
  817. Page = page;
  818. if((V_LOGINNAME!=null)&&(company_code==null)&&(Company_Code_List==null)) {
  819. String where = " where u.id.agentid='"+V_LOGINNAME+"' ";
  820. List list = ytiotVUserCompanyDao.query(where, null, null, null, null);
  821. if(list.size()>0) {
  822. for(int i=0;i<list.size();i++) {
  823. YtiotVUsercompany o = (YtiotVUsercompany)list.get(i);
  824. YtiotVUsercompanyId oid = o.getId();
  825. if(i==0)
  826. Company_Code_List = oid.getOwnerCode();
  827. else
  828. Company_Code_List += ","+oid.getOwnerCode();
  829. }
  830. }
  831. }
  832. if(device_id!=null)
  833. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" = '"+device_id+"' ";
  834. else {
  835. if(company!=null) {
  836. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("owner_name")+" like '%"+company+"%' ";
  837. }
  838. if(Company_Code_List!=null)
  839. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+Company_Code_List+") ";
  840. else {
  841. if(company_code!=null)
  842. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" like '%"+company_code+"%' ";
  843. if(company_code_list!=null)
  844. hqlwhere = chkhw(hqlwhere) + " u.id."+fieldname("company_code")+" in ("+company_code_list+") ";
  845. }
  846. }
  847. if(device_id_list!=null)
  848. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("device_id")+" in ("+device_id+") ";
  849. if(dwtype!=null)
  850. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" = "+dwtype+" ";
  851. if(dwtype_list!=null)
  852. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("dwtype")+" in ("+dwtype_list+") ";
  853. if(point_code!=null)
  854. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code+") ";
  855. if(point_code_list!=null)
  856. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("point_code")+" in ("+point_code_list+") ";
  857. if(id!=null)
  858. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" = "+id+" ";
  859. if(id_list!=null)
  860. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("id")+" in ("+id_list+") ";
  861. if(data_time_start!=null)
  862. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" >= '"+data_time_start+"' ";
  863. if(data_time_end!=null)
  864. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("data_time")+" <= '"+data_time_end+"' ";
  865. if(name!=null)
  866. hqlwhere = chkhw(hqlwhere)+" u.id."+fieldname("name")+" like '%"+name+"%' ";
  867. int totalCount = ytiotVDataDao.getSyncDataCount(hqlwhere);
  868. json.put("totalCount", totalCount);
  869. if(Start!=null){
  870. if(Integer.parseInt(Start)>totalCount){
  871. Start = "0";
  872. Page = "0";
  873. }else if(page==null)
  874. Page="0";
  875. }else{
  876. Start = "0";
  877. Page = "0";
  878. }
  879. json.put("page", Page);
  880. json.put("start", Start);
  881. json.put("limit", Limit);
  882. if(order!=null) {
  883. order_str = " order by "+fieldname(order)+" ";
  884. } else {
  885. order_str = " order by data_time ";
  886. dir = "DESC";
  887. }
  888. if(totalCount>0) {
  889. List<?> list = ytiotVDataDao.SyncDataQuery(hqlwhere, order_str, dir, Start, Limit);
  890. if(list.size()>0) {
  891. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  892. for(int i=0;i<list.size();i++) {
  893. YtiotVSyncData o = (YtiotVSyncData)list.get(i);
  894. YtiotVSyncDataId oid = o.getId();
  895. @SuppressWarnings("rawtypes")
  896. Map<String, Comparable> map = new HashMap<String, Comparable>();
  897. map.put("company", oid.getCompany());
  898. map.put("company_code", oid.getCompanyCode());
  899. map.put("content", oid.getContent());
  900. map.put("data_time", df.format(oid.getDataTime()));
  901. map.put("device_id", oid.getDeviceId());
  902. map.put("dwtype", oid.getDwtype());
  903. map.put("id", oid.getId());
  904. map.put("name", oid.getName());
  905. map.put("native_id", oid.getNativeId());
  906. map.put("point_code", oid.getPointCode());
  907. map.put("point_data", oid.getPointData());
  908. JSONObject jSONObject = JSONObject.fromObject(map);
  909. jSONArray.add(jSONObject);