| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package com.xcgl.cloud;
- import com.xcgl.cloud.dao.CloudBaseDataSyncDao;
- import com.xcgl.cloud.entity.TSProject;
- import com.xcgl.device.entity.DeviceEntity;
- import com.xcgl.devicetype.entity.DeviceTypeEntity;
- import com.xcgl.sensorabnormal.entity.SensorAbnormalEntity;
- import com.xcgl.sensorabnormal.service.SensorAbnormalServiceI;
- import com.xcgl.sensormonitorpoint.entity.SensorMonitorPointEntity;
- import com.xcgl.sensorrecord.entity.SensorRecordEntity;
- import com.xcgl.sensorrecord.service.SensorRecordServiceI;
- import com.xcgl.sensortype.entity.SensorTypeEntity;
- import org.apache.log4j.Logger;
- import org.hibernate.criterion.Restrictions;
- import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
- import org.jeecgframework.web.system.pojo.base.TSUser;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Controller
- @RequestMapping(value = "/dscloud")
- public class CloudSyncService {
- private static final Logger logger = Logger.getLogger(CloudSyncService.class);
- @Autowired
- private SensorRecordServiceI sensorService;
- @Autowired
- private SensorAbnormalServiceI abnService;
- @Autowired
- private CloudBaseDataSyncDao baseDataDao;
- /**
- * 访问地址:http://localhost:8080/xcgl/api/dscloud
- * @return
- */
- @RequestMapping(method = RequestMethod.GET)
- @ResponseBody
- public String checkNet() {
- return "ok";
- }
-
- /**
- * 同步项目信息
- * @param pid 项目ID
- *
- * */
- @RequestMapping(value = "/project/{pid}",method = RequestMethod.GET)
- @ResponseBody
- public List<TSProject> getProject(@PathVariable("pid") String pid) {
- List<TSProject> listProjects;
- CriteriaQuery cq = new CriteriaQuery(TSProject.class);
- //id及上级id为pid的结果
- cq.or(Restrictions.eq("id", pid),Restrictions.eq("parentdepartid", pid));
- //加载查询条件
- cq.add();
- listProjects = this.sensorService.getListByCriteriaQuery(cq, false);
- return listProjects;
- }
-
- /**
- * 同步项目所属人员档案信息
- * @param pid 项目ID
- * */
- @RequestMapping(value = "/puser/{pid}",method = RequestMethod.GET)
- @ResponseBody
- public List<TSUser> getProjectUsers(@PathVariable("pid") String pid) {
- List<TSUser> listUsers=this.sensorService.findByProperty(TSUser.class, "tsUser.id", pid);
- return listUsers;
- }
-
- @RequestMapping(value = "/devicetype",method = RequestMethod.GET)
- @ResponseBody
- public List<DeviceTypeEntity> getAllDevicetype() {
- return baseDataDao.getAllDevicetype();
- }
-
- @RequestMapping(value = "/sensortype",method = RequestMethod.GET)
- @ResponseBody
- public List<SensorTypeEntity> getAllSensortype() {
- return baseDataDao.getAllSensortype();
- }
- @RequestMapping(value = "/device/{pid}",method = RequestMethod.GET)
- @ResponseBody
- public List<DeviceEntity> getProjectDevices(@PathVariable("pid") String pid) {
- return baseDataDao.getDevicebyProject(pid);
- }
-
- @RequestMapping(value = "/sensor/{pid}",method = RequestMethod.GET)
- @ResponseBody
- public List<SensorMonitorPointEntity> getProjectSensors(@PathVariable("pid") String pid) {
- return baseDataDao.getSensorbyProject(pid);
- }
-
-
- /**
- * 保存单条监测纪录
- *
- * */
- @RequestMapping(value = "/syncrecord/{pid}",method = RequestMethod.POST,
- consumes = MediaType.APPLICATION_JSON_VALUE)
- @ResponseBody
- public String syncRecordToCloud(@RequestBody SensorRecordEntity record,String projectid) {
- // TODO Auto-generated method stub
- return "ok";
- }
-
- /**
- * 保存单条异常纪录
- *
- * */
- @RequestMapping(value = "/syncabn/{pid}",method = RequestMethod.POST,
- consumes = MediaType.APPLICATION_JSON_VALUE)
- @ResponseBody
- public String syncAbnToCloud(@RequestBody SensorAbnormalEntity record,String projectid) {
- // TODO Auto-generated method stub
- return "ok";
- }
- /**
- * 批量保存监测纪录
- * lts:上次同步时间
- * cts:本次同步时间
- * 返回值:
- * 1、“ok” 同步成功;
- * 2、“yyyy-MM-dd HH:mm:ss”格式时间,表示本次未成功,返回最后成功同步时间
- * batchSyncRecords
- * */
- @RequestMapping(value = "/batchsyncrecords/{pid}/{lts}/{cts}",method = RequestMethod.POST,
- consumes = MediaType.APPLICATION_JSON_VALUE)
- @ResponseBody
- public String batchSyncRecordsToCloud(@RequestBody List<SensorRecordEntity> records,@PathVariable("pid") String pid,
- @PathVariable("lts") String lts,@PathVariable("cts") String cts) {
- lts = lts.substring(0, 4)+"-"
- +lts.substring(4, 6)+"-"
- +lts.substring(6, 8)+" "
- +lts.substring(8, 10)+":"
- +lts.substring(10, 12)+":"
- +lts.substring(12, 14);
- cts = cts.substring(0, 4)+"-"
- +cts.substring(4, 6)+"-"
- +cts.substring(6, 8)+" "
- +cts.substring(8, 10)+":"
- +cts.substring(10, 12)+":"
- +cts.substring(12, 14);
- String localLastTS = getLastSysnTS(pid, "p_sensor_record");
- String ret = "ok";
- if(localLastTS.equals(lts))
- {
- //TODO test 修改uuid,生产环境删除
- // for(SensorRecordEntity record :records)
- // {
- // record.setId(UUID.randomUUID().toString());
- // }
- //保存数据
- sensorService.batchSave(records);
- //记录同步日志
- String syncStr = "INSERT INTO p_cloud_sync_record (id, projectid, createdatetime, lastdatetime, tablename) "
- + "VALUES (uuid(), ?, NOW(), ? , ?)";
- abnService.executeSql(syncStr,pid,cts, "p_sensor_record");
- logger.info("传感器纪录保存:项目ID"+pid+",保存数据数量:"+records.size());
-
- }else
- {
- ret = localLastTS;
- }
- return ret;
- }
-
- /**
- * 批量保存监测纪录
- * lts:上次同步时间
- * cts:本次同步时间
- * 返回值:
- * 1、“ok” 同步成功;
- * 2、“yyyy-MM-dd HH:mm:ss”格式时间,表示本次未成功,返回最后成功同步时间
- * */
- @RequestMapping(value = "/batchsyncabns/{pid}/{lts}/{cts}",method = RequestMethod.POST,
- consumes = MediaType.APPLICATION_JSON_VALUE)
- @ResponseBody
- public String batchSyncAbnsToCloud(@RequestBody List<SensorAbnormalEntity> abns,@PathVariable("pid") String pid,
- @PathVariable("lts") String lts,@PathVariable("cts") String cts) {
- lts = lts.substring(0, 4)+"-"
- +lts.substring(4, 6)+"-"
- +lts.substring(6, 8)+" "
- +lts.substring(8, 10)+":"
- +lts.substring(10, 12)+":"
- +lts.substring(12, 14);
- cts = cts.substring(0, 4)+"-"
- +cts.substring(4, 6)+"-"
- +cts.substring(6, 8)+" "
- +cts.substring(8, 10)+":"
- +cts.substring(10, 12)+":"
- +cts.substring(12, 14);
- String localLastTS = getLastSysnTS(pid, "p_sensor_abnormal");
- String ret = "ok";
- if(localLastTS.equals(lts))
- {
- //TODO test 修改uuid,生产环境删除
- // for(SensorAbnormalEntity abn :abns)
- // {
- // abn.setId(UUID.randomUUID().toString());
- // }
- //保存数据
- abnService.batchSave(abns);
- //记录同步日志
- String syncStr = "INSERT INTO p_cloud_sync_record (id, projectid, createdatetime, lastdatetime, tablename) "
- + "VALUES (uuid(), ?, NOW(), ? , ?)";
- // TODO 事务处理需要以后考虑一下
- abnService.executeSql(syncStr,pid,cts, "p_sensor_abnormal");
- logger.info("异常信息保存:项目ID"+pid+",保存数据数量:"+abns.size());
- }else {
- ret = localLastTS;
- }
- return ret;
- }
-
- private String getLastSysnTS(String pid,String tablename )
- {
- String lastTS = "2016-01-01 00:00:00";//DateUtils.formatDate(org.jeecgframework.p3.core.common.utils.DateUtil.getCurrDate(), "yyyy-MM-dd HH:mm:ss");
- String querystr = "select id, lastdatetime from p_cloud_sync_record where tablename =? and projectid =? order by createdatetime desc";
- List<Map<String,Object>> ret = sensorService.findForJdbc(querystr.toString(),tablename,pid);
- Map<String,Object> map = ret.size()>0? ret.get(0) :new HashMap<String,Object>();
- if(map != null && map.containsKey("lastdatetime"))
- {
- lastTS = map.get("lastdatetime").toString().substring(0,19);
- }
- return lastTS;
- }
- }
|