CloudSyncService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.xcgl.cloud;
  2. import com.xcgl.cloud.dao.CloudBaseDataSyncDao;
  3. import com.xcgl.cloud.entity.TSProject;
  4. import com.xcgl.device.entity.DeviceEntity;
  5. import com.xcgl.devicetype.entity.DeviceTypeEntity;
  6. import com.xcgl.sensorabnormal.entity.SensorAbnormalEntity;
  7. import com.xcgl.sensorabnormal.service.SensorAbnormalServiceI;
  8. import com.xcgl.sensormonitorpoint.entity.SensorMonitorPointEntity;
  9. import com.xcgl.sensorrecord.entity.SensorRecordEntity;
  10. import com.xcgl.sensorrecord.service.SensorRecordServiceI;
  11. import com.xcgl.sensortype.entity.SensorTypeEntity;
  12. import org.apache.log4j.Logger;
  13. import org.hibernate.criterion.Restrictions;
  14. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  15. import org.jeecgframework.web.system.pojo.base.TSUser;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.*;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. @Controller
  24. @RequestMapping(value = "/dscloud")
  25. public class CloudSyncService {
  26. private static final Logger logger = Logger.getLogger(CloudSyncService.class);
  27. @Autowired
  28. private SensorRecordServiceI sensorService;
  29. @Autowired
  30. private SensorAbnormalServiceI abnService;
  31. @Autowired
  32. private CloudBaseDataSyncDao baseDataDao;
  33. /**
  34. * 访问地址:http://localhost:8080/xcgl/api/dscloud
  35. * @return
  36. */
  37. @RequestMapping(method = RequestMethod.GET)
  38. @ResponseBody
  39. public String checkNet() {
  40. return "ok";
  41. }
  42. /**
  43. * 同步项目信息
  44. * @param pid 项目ID
  45. *
  46. * */
  47. @RequestMapping(value = "/project/{pid}",method = RequestMethod.GET)
  48. @ResponseBody
  49. public List<TSProject> getProject(@PathVariable("pid") String pid) {
  50. List<TSProject> listProjects;
  51. CriteriaQuery cq = new CriteriaQuery(TSProject.class);
  52. //id及上级id为pid的结果
  53. cq.or(Restrictions.eq("id", pid),Restrictions.eq("parentdepartid", pid));
  54. //加载查询条件
  55. cq.add();
  56. listProjects = this.sensorService.getListByCriteriaQuery(cq, false);
  57. return listProjects;
  58. }
  59. /**
  60. * 同步项目所属人员档案信息
  61. * @param pid 项目ID
  62. * */
  63. @RequestMapping(value = "/puser/{pid}",method = RequestMethod.GET)
  64. @ResponseBody
  65. public List<TSUser> getProjectUsers(@PathVariable("pid") String pid) {
  66. List<TSUser> listUsers=this.sensorService.findByProperty(TSUser.class, "tsUser.id", pid);
  67. return listUsers;
  68. }
  69. @RequestMapping(value = "/devicetype",method = RequestMethod.GET)
  70. @ResponseBody
  71. public List<DeviceTypeEntity> getAllDevicetype() {
  72. return baseDataDao.getAllDevicetype();
  73. }
  74. @RequestMapping(value = "/sensortype",method = RequestMethod.GET)
  75. @ResponseBody
  76. public List<SensorTypeEntity> getAllSensortype() {
  77. return baseDataDao.getAllSensortype();
  78. }
  79. @RequestMapping(value = "/device/{pid}",method = RequestMethod.GET)
  80. @ResponseBody
  81. public List<DeviceEntity> getProjectDevices(@PathVariable("pid") String pid) {
  82. return baseDataDao.getDevicebyProject(pid);
  83. }
  84. @RequestMapping(value = "/sensor/{pid}",method = RequestMethod.GET)
  85. @ResponseBody
  86. public List<SensorMonitorPointEntity> getProjectSensors(@PathVariable("pid") String pid) {
  87. return baseDataDao.getSensorbyProject(pid);
  88. }
  89. /**
  90. * 保存单条监测纪录
  91. *
  92. * */
  93. @RequestMapping(value = "/syncrecord/{pid}",method = RequestMethod.POST,
  94. consumes = MediaType.APPLICATION_JSON_VALUE)
  95. @ResponseBody
  96. public String syncRecordToCloud(@RequestBody SensorRecordEntity record,String projectid) {
  97. // TODO Auto-generated method stub
  98. return "ok";
  99. }
  100. /**
  101. * 保存单条异常纪录
  102. *
  103. * */
  104. @RequestMapping(value = "/syncabn/{pid}",method = RequestMethod.POST,
  105. consumes = MediaType.APPLICATION_JSON_VALUE)
  106. @ResponseBody
  107. public String syncAbnToCloud(@RequestBody SensorAbnormalEntity record,String projectid) {
  108. // TODO Auto-generated method stub
  109. return "ok";
  110. }
  111. /**
  112. * 批量保存监测纪录
  113. * lts:上次同步时间
  114. * cts:本次同步时间
  115. * 返回值:
  116. * 1、“ok” 同步成功;
  117. * 2、“yyyy-MM-dd HH:mm:ss”格式时间,表示本次未成功,返回最后成功同步时间
  118. * batchSyncRecords
  119. * */
  120. @RequestMapping(value = "/batchsyncrecords/{pid}/{lts}/{cts}",method = RequestMethod.POST,
  121. consumes = MediaType.APPLICATION_JSON_VALUE)
  122. @ResponseBody
  123. public String batchSyncRecordsToCloud(@RequestBody List<SensorRecordEntity> records,@PathVariable("pid") String pid,
  124. @PathVariable("lts") String lts,@PathVariable("cts") String cts) {
  125. lts = lts.substring(0, 4)+"-"
  126. +lts.substring(4, 6)+"-"
  127. +lts.substring(6, 8)+" "
  128. +lts.substring(8, 10)+":"
  129. +lts.substring(10, 12)+":"
  130. +lts.substring(12, 14);
  131. cts = cts.substring(0, 4)+"-"
  132. +cts.substring(4, 6)+"-"
  133. +cts.substring(6, 8)+" "
  134. +cts.substring(8, 10)+":"
  135. +cts.substring(10, 12)+":"
  136. +cts.substring(12, 14);
  137. String localLastTS = getLastSysnTS(pid, "p_sensor_record");
  138. String ret = "ok";
  139. if(localLastTS.equals(lts))
  140. {
  141. //TODO test 修改uuid,生产环境删除
  142. // for(SensorRecordEntity record :records)
  143. // {
  144. // record.setId(UUID.randomUUID().toString());
  145. // }
  146. //保存数据
  147. sensorService.batchSave(records);
  148. //记录同步日志
  149. String syncStr = "INSERT INTO p_cloud_sync_record (id, projectid, createdatetime, lastdatetime, tablename) "
  150. + "VALUES (uuid(), ?, NOW(), ? , ?)";
  151. abnService.executeSql(syncStr,pid,cts, "p_sensor_record");
  152. logger.info("传感器纪录保存:项目ID"+pid+",保存数据数量:"+records.size());
  153. }else
  154. {
  155. ret = localLastTS;
  156. }
  157. return ret;
  158. }
  159. /**
  160. * 批量保存监测纪录
  161. * lts:上次同步时间
  162. * cts:本次同步时间
  163. * 返回值:
  164. * 1、“ok” 同步成功;
  165. * 2、“yyyy-MM-dd HH:mm:ss”格式时间,表示本次未成功,返回最后成功同步时间
  166. * */
  167. @RequestMapping(value = "/batchsyncabns/{pid}/{lts}/{cts}",method = RequestMethod.POST,
  168. consumes = MediaType.APPLICATION_JSON_VALUE)
  169. @ResponseBody
  170. public String batchSyncAbnsToCloud(@RequestBody List<SensorAbnormalEntity> abns,@PathVariable("pid") String pid,
  171. @PathVariable("lts") String lts,@PathVariable("cts") String cts) {
  172. lts = lts.substring(0, 4)+"-"
  173. +lts.substring(4, 6)+"-"
  174. +lts.substring(6, 8)+" "
  175. +lts.substring(8, 10)+":"
  176. +lts.substring(10, 12)+":"
  177. +lts.substring(12, 14);
  178. cts = cts.substring(0, 4)+"-"
  179. +cts.substring(4, 6)+"-"
  180. +cts.substring(6, 8)+" "
  181. +cts.substring(8, 10)+":"
  182. +cts.substring(10, 12)+":"
  183. +cts.substring(12, 14);
  184. String localLastTS = getLastSysnTS(pid, "p_sensor_abnormal");
  185. String ret = "ok";
  186. if(localLastTS.equals(lts))
  187. {
  188. //TODO test 修改uuid,生产环境删除
  189. // for(SensorAbnormalEntity abn :abns)
  190. // {
  191. // abn.setId(UUID.randomUUID().toString());
  192. // }
  193. //保存数据
  194. abnService.batchSave(abns);
  195. //记录同步日志
  196. String syncStr = "INSERT INTO p_cloud_sync_record (id, projectid, createdatetime, lastdatetime, tablename) "
  197. + "VALUES (uuid(), ?, NOW(), ? , ?)";
  198. // TODO 事务处理需要以后考虑一下
  199. abnService.executeSql(syncStr,pid,cts, "p_sensor_abnormal");
  200. logger.info("异常信息保存:项目ID"+pid+",保存数据数量:"+abns.size());
  201. }else {
  202. ret = localLastTS;
  203. }
  204. return ret;
  205. }
  206. private String getLastSysnTS(String pid,String tablename )
  207. {
  208. String lastTS = "2016-01-01 00:00:00";//DateUtils.formatDate(org.jeecgframework.p3.core.common.utils.DateUtil.getCurrDate(), "yyyy-MM-dd HH:mm:ss");
  209. String querystr = "select id, lastdatetime from p_cloud_sync_record where tablename =? and projectid =? order by createdatetime desc";
  210. List<Map<String,Object>> ret = sensorService.findForJdbc(querystr.toString(),tablename,pid);
  211. Map<String,Object> map = ret.size()>0? ret.get(0) :new HashMap<String,Object>();
  212. if(map != null && map.containsKey("lastdatetime"))
  213. {
  214. lastTS = map.get("lastdatetime").toString().substring(0,19);
  215. }
  216. return lastTS;
  217. }
  218. }