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 getProject(@PathVariable("pid") String pid) { List 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 getProjectUsers(@PathVariable("pid") String pid) { List listUsers=this.sensorService.findByProperty(TSUser.class, "tsUser.id", pid); return listUsers; } @RequestMapping(value = "/devicetype",method = RequestMethod.GET) @ResponseBody public List getAllDevicetype() { return baseDataDao.getAllDevicetype(); } @RequestMapping(value = "/sensortype",method = RequestMethod.GET) @ResponseBody public List getAllSensortype() { return baseDataDao.getAllSensortype(); } @RequestMapping(value = "/device/{pid}",method = RequestMethod.GET) @ResponseBody public List getProjectDevices(@PathVariable("pid") String pid) { return baseDataDao.getDevicebyProject(pid); } @RequestMapping(value = "/sensor/{pid}",method = RequestMethod.GET) @ResponseBody public List 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 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 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> ret = sensorService.findForJdbc(querystr.toString(),tablename,pid); Map map = ret.size()>0? ret.get(0) :new HashMap(); if(map != null && map.containsKey("lastdatetime")) { lastTS = map.get("lastdatetime").toString().substring(0,19); } return lastTS; } }