IotDeviceService.java 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. package com.yihu.jw.iot;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.yihu.jw.device.dao.DevicePatientHealthIndexDao;
  6. import com.yihu.jw.device.dao.PatientDeviceDao;
  7. import com.yihu.jw.entity.care.device.DeviceHealthyInfoMapping;
  8. import com.yihu.jw.entity.care.device.DevicePatientHealthIndex;
  9. import com.yihu.jw.entity.patient.PatientDevice;
  10. import com.yihu.jw.patient.dao.BasePatientDao;
  11. import com.yihu.jw.util.date.DateUtil;
  12. import com.yihu.jw.util.http.HttpClientUtil;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.http.Consts;
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.client.utils.URLEncodedUtils;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.jdbc.core.JdbcTemplate;
  23. import org.springframework.scheduling.annotation.Async;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.*;
  27. /**
  28. * 物联网设备对接
  29. *
  30. * @author yeshijie on 2018/1/3.
  31. */
  32. @Service
  33. public class IotDeviceService {
  34. private Logger logger = LoggerFactory.getLogger(IotDeviceService.class);
  35. // @Value("${iot.url}")
  36. private String baseUrl;
  37. // @Value("${iot.appid}")
  38. private String appid;
  39. // @Value("${iot.appsecret}")
  40. private String appSecret;
  41. // @Value("${spring.profiles}")
  42. private String profiles;
  43. public static Map<String,String> tokenMap = new HashMap<>();
  44. private String grantType = "client_credentials";
  45. private String clientId = "Va5yQRHlA4Fq4eR3LT0vuXV4";
  46. private String clientSecret = "0rDSjzQ20XUj5itV7WRtznPQSzr5pVw2";
  47. private String dataSource = "iHealth";
  48. private String accessToken = "iHealth";
  49. private String bloodSugarUnit = "mmol/L";
  50. private String bloodPressureUnit = "mmHg";
  51. private String pulseUnit = "bpm";
  52. private String heightUnit = "cm";
  53. private String weightUnit = "kg";
  54. private String bmiUnit = "kg/m^2";
  55. private String waistUnit = "cm";
  56. @Autowired
  57. private HttpClientUtil httpClientUtil;
  58. @Autowired
  59. private JdbcTemplate jdbcTemplate;
  60. @Autowired
  61. private PatientDeviceDao patientDeviceDao;
  62. @Autowired
  63. private BasePatientDao patientDao;
  64. @Autowired
  65. private DevicePatientHealthIndexDao devicePatientHealthIndexDao;
  66. // @Autowired
  67. // private WlyyIotUploadDataLogDao wlyyIotUploadDataLogDao;
  68. // @Autowired
  69. // private MyJdbcTemplate myJdbcTemplate;
  70. // @Autowired
  71. // private SignFamilyDao signFamilyDao;
  72. // @Autowired
  73. // private SystemDictDao systemDictDao;
  74. // @Autowired
  75. // private DeviceHealthyInfoMappingDao deviceHealthyInfoMappingDao;
  76. // @Autowired
  77. // private SkJsonFormDao skJsonFormDao;
  78. /**
  79. * 判断是否数据上传到物联网
  80. * @return
  81. */
  82. // public Boolean isUploadIot(){
  83. // String value = systemDictDao.findByDictNameAndCode("SYSTEM_PARAMS","DEVICE_UPLOAD_IOT");
  84. // return "1".equals(value)?true:false;
  85. // }
  86. //
  87. // public Boolean isIotSearch(){
  88. // String value = systemDictDao.findByDictNameAndCode("SYSTEM_PARAMS","isIotSearch");
  89. // return "1".equals(value)?true:false;
  90. // }
  91. //
  92. public String getOauthAccessToken(){
  93. String url = baseUrl+"/oauth/2.0/token";
  94. List<NameValuePair> params = new ArrayList<>();
  95. params.add(new BasicNameValuePair("grant_type",grantType));
  96. params.add(new BasicNameValuePair("client_id",clientId));
  97. params.add(new BasicNameValuePair("client_secret",clientSecret));
  98. String response = httpClientUtil.post(url, params, "UTF-8");
  99. return null;
  100. }
  101. /**
  102. * 设备注册及绑定
  103. * @return
  104. */
  105. public String registedevice(JSONObject json){
  106. json.put("access_token",accessToken);
  107. json.put("data_source",dataSource);
  108. String url = baseUrl+"/dataInput/userBind";
  109. String response = sendPost(url, json.toString());
  110. return response;
  111. }
  112. /**
  113. * 设备绑定
  114. * @param patientDevice
  115. * @param address
  116. * @param name
  117. * @param diseaseCondition 病情:0绿标,1黄标,2红标
  118. * @return
  119. */
  120. public String saveDevice(PatientDevice patientDevice, String address, String name, Integer diseaseCondition, String hospitalName){
  121. JSONObject json = new JSONObject();
  122. json.put("categoryCode",patientDevice.getCategoryCode());
  123. json.put("address",address);
  124. json.put("hospitalName",hospitalName);
  125. json.put("patient",patientDevice.getUser());
  126. json.put("patientName",name);
  127. json.put("diseaseCondition",diseaseCondition);
  128. json.put("idcard",patientDevice.getUserIdcard());
  129. json.put("deviceId",patientDevice.getDeviceId());
  130. json.put("deviceName",patientDevice.getDeviceName());
  131. json.put("deviceSn",patientDevice.getDeviceSn());
  132. json.put("userType",patientDevice.getUserType());
  133. json.put("sim",patientDevice.getSim());
  134. json.put("agent",patientDevice.getAgent());
  135. json.put("doctor",patientDevice.getDoctor());
  136. return saveDevice(json.toString());
  137. }
  138. private String saveDevice(String json){
  139. String url = baseUrl+"/open/monitorPlatform/addPatientDevice";
  140. List<NameValuePair> params = new ArrayList<>();
  141. params.add(new BasicNameValuePair("jsonData",json));
  142. String response = httpClientUtil.post(url, params, "UTF-8");
  143. return response;
  144. }
  145. /**
  146. * 数据上传
  147. * @param json
  148. * @return
  149. */
  150. public String upload(JSONObject json){
  151. json.put("data_source",dataSource);
  152. String url = baseUrl+"/open/gc/uploadData";
  153. String response = sendPost(url, json.toString());
  154. return response;
  155. }
  156. /**
  157. * 保存i健康查询体征记录的记录
  158. */
  159. @Async
  160. // public void iHealthDataSearch(){
  161. // try{
  162. // if(isIotSearch()){
  163. // JSONObject json = new JSONObject();
  164. // String url = baseUrl+"/open/gc/iHealthDataSearch";
  165. // String response = sendPost(url, json.toString());
  166. // }
  167. // }catch (Exception e){
  168. // logger.error("调物联网接口报错:"+e.getMessage());
  169. // e.printStackTrace();
  170. // }
  171. // }
  172. /**
  173. * 访问i健康接口,自带登录信息
  174. * @param url
  175. * @return
  176. */
  177. private String sendGet(String url,String jsonData){
  178. String response = null;
  179. try{
  180. Map<String, Object> params = new HashMap();
  181. params.put("accesstoken",getAccessToken());
  182. params.put("jsonData",jsonData);
  183. params.put("appId",appid);
  184. List<BasicNameValuePair> jsonParams = new ArrayList<>();
  185. //配置参数
  186. if(params!=null) {
  187. for (String key : params.keySet()) {
  188. if (!StringUtils.isEmpty(String.valueOf(params.get(key))) && !"null".equals( String.valueOf(params.get(key)))) {
  189. jsonParams.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
  190. }
  191. }
  192. }
  193. response = httpClientUtil.get(url+"?"+ URLEncodedUtils.format(jsonParams, Consts.UTF_8),"utf-8");
  194. }catch (Exception e){
  195. e.printStackTrace();
  196. }
  197. return response;
  198. }
  199. private String sendPost(String url,String jsonData){
  200. String response = null;
  201. try{
  202. Map<String, Object> params = new HashMap();
  203. params.put("accesstoken",getAccessToken());
  204. List<NameValuePair> jsonParams = new ArrayList<>();
  205. if(StringUtils.isNotEmpty(jsonData)){
  206. jsonParams.add(new BasicNameValuePair("jsonData", jsonData));
  207. }
  208. jsonParams.add(new BasicNameValuePair("appId", appid));
  209. response = httpClientUtil.headerPost(url, jsonParams,"utf-8",params);
  210. }catch (Exception e){
  211. e.printStackTrace();
  212. }
  213. return response;
  214. }
  215. /**
  216. * 查询重复数据
  217. * @return
  218. */
  219. public String findRepeat(String user,String deviceSn,String value1,String value2,String type,String time){
  220. JSONArray jsonArray = new JSONArray();
  221. field("and","usercode","=",user,jsonArray);
  222. field("and","sn","=",deviceSn,jsonArray);
  223. field("and","del","=","1",jsonArray);
  224. if("1".equals(type)){
  225. //血糖
  226. field("and","value1","=",value1,jsonArray);
  227. }else {
  228. //血压
  229. field("and","value1","=",value1,jsonArray);
  230. if(StringUtils.isNotBlank(value2)){
  231. field("and","value2","=",value2,jsonArray);
  232. }
  233. }
  234. field("and","measure_time","=",time,jsonArray);
  235. JSONObject jsonObject = new JSONObject();
  236. jsonObject.put("filter",jsonArray);
  237. String url = baseUrl+"/dataSearch/getById";
  238. String response = sendPost(url, jsonObject.toString());
  239. return response;
  240. }
  241. /**
  242. * 按条件统计数量
  243. * @param start
  244. * @param end
  245. * @param status
  246. * @param patientCode
  247. * @return
  248. */
  249. public int getCountByTimeAndStatusAndPatient(String start,String end,Integer status,String patientCode){
  250. int re = 0;
  251. JSONArray jsonArray = new JSONArray();
  252. field("and","usercode","=",patientCode,jsonArray);
  253. field("and","status","=",String.valueOf(status),jsonArray);
  254. field("and","del","=","1",jsonArray);
  255. field("and","measure_time",">=",start,jsonArray);
  256. field("and","measure_time","<=",end,jsonArray);
  257. field("and","type",">=","1",jsonArray);
  258. field("and","type","<=","2",jsonArray);
  259. JSONObject jsonObject = new JSONObject();
  260. jsonObject.put("filter",jsonArray);
  261. jsonObject.put("page","1");
  262. jsonObject.put("size","50");
  263. String url = baseUrl+"/dataSearch/getById";
  264. String response = sendPost(url, jsonObject.toString());
  265. JSONObject json = JSONObject.parseObject(response);
  266. JSONArray ja = json.getJSONArray("obj");
  267. if(ja!=null){
  268. re = ja.size();
  269. }
  270. return re;
  271. }
  272. /**
  273. * 按居民code和类型查找
  274. * @param patient
  275. * @param type
  276. * @return
  277. */
  278. // public List<DevicePatientHealthIndex> findByPatientAndType(String patient, Integer type, Integer size){
  279. // List<DevicePatientHealthIndex> list = new ArrayList<>();
  280. // JSONArray jsonArray = new JSONArray();
  281. // field("and","usercode","=",patient,jsonArray);
  282. // field("and","del","=","1",jsonArray);
  283. // if(type!=null){
  284. // field("and","type","=",String.valueOf(type),jsonArray);
  285. // }
  286. //
  287. // JSONObject jsonObject = new JSONObject();
  288. // jsonObject.put("filter",jsonArray);
  289. // jsonObject.put("page","1");
  290. // jsonObject.put("size",size);
  291. //
  292. // JSONArray sort = new JSONArray();
  293. // sort(sort,"measure_time","desc");
  294. // jsonObject.put("sort",sort);
  295. // String url = baseUrl+"/dataSearch/getById";
  296. // String response = sendPost(url, jsonObject.toString());
  297. //
  298. // try {
  299. // JSONObject json = JSONObject.parseObject(response);
  300. // JSONArray obj = json.getJSONArray("obj");
  301. // if(obj!=null&&obj.size()>0){
  302. // for (int i=0;i<obj.size();i++){
  303. // DevicePatientHealthIndex index = transforHealthIndex(obj.getJSONObject(i),null);
  304. // list.add(index);
  305. // }
  306. // }
  307. //
  308. // }catch (Exception e){
  309. // e.printStackTrace();
  310. // }
  311. //
  312. // return list;
  313. // }
  314. /**
  315. * 按时间段查找
  316. * @param patient 居民code
  317. * @param type 健康指标类型(1血糖,2血压,3体重,4腰围)
  318. * @param gi_type 血糖就餐时间段(早餐前,早餐后等)
  319. * @param begin 开始时间
  320. * @param end 结束时间
  321. * @return
  322. */
  323. // public List<DevicePatientHealthIndex> findByPatientAndTime(String patient,Integer type,Integer page,Integer size,Integer gi_type, String begin, String end){
  324. // List<DevicePatientHealthIndex> list = new ArrayList<>();
  325. // JSONArray jsonArray = new JSONArray();
  326. // field("and","usercode","=",patient,jsonArray);
  327. // field("and","del","=","1",jsonArray);
  328. // if(type!=null){
  329. // field("and","type","=",String.valueOf(type),jsonArray);
  330. // }
  331. // if(gi_type!=null){
  332. // field("and","value2","=",String.valueOf(gi_type),jsonArray);
  333. // }
  334. // field("and","measure_time",">=",begin,jsonArray);
  335. // field("and","measure_time","<=",end,jsonArray);
  336. //
  337. // JSONObject jsonObject = new JSONObject();
  338. // jsonObject.put("filter",jsonArray);
  339. // jsonObject.put("page",page);
  340. // jsonObject.put("size",size);
  341. //
  342. // JSONArray sort = new JSONArray();
  343. // sort(sort,"measure_time","desc");
  344. // jsonObject.put("sort",sort);
  345. // String url = baseUrl+"/dataSearch/getById";
  346. // String response = sendPost(url, jsonObject.toString());
  347. //
  348. // try {
  349. // JSONObject json = JSONObject.parseObject(response);
  350. // JSONArray obj = json.getJSONArray("obj");
  351. // if(obj!=null&&obj.size()>0){
  352. // for (int i=0;i<obj.size();i++){
  353. // DevicePatientHealthIndex index = transforHealthIndex(obj.getJSONObject(i),null);
  354. // list.add(index);
  355. // }
  356. // }
  357. //
  358. // }catch (Exception e){
  359. // e.printStackTrace();
  360. // }
  361. //
  362. // return list;
  363. // }
  364. /**
  365. * 添加条件
  366. * 参数格式:[{"andOr":"and|or","condition":">|=|<|>=|<=|?","field":"<field>","value":"<value>"},<{...}>]
  367. * @param andOr
  368. * @param field
  369. * @param condition
  370. * @param value
  371. * @param jsonArray
  372. */
  373. private void field(String andOr,String field,String condition,String value,JSONArray jsonArray){
  374. JSONObject json = new JSONObject();
  375. json.put("andOr",andOr);
  376. json.put("field",field);
  377. json.put("condition",condition);
  378. json.put("value",value);
  379. jsonArray.add(json);
  380. }
  381. /**
  382. * 排序[{"key1":{"order":"asc|desc"}},{"key2":{"order":"asc|desc"}}]
  383. * @param jsonArray
  384. * @param key
  385. * @param order
  386. */
  387. private void sort(JSONArray jsonArray,String key,String order){
  388. JSONObject json = new JSONObject();
  389. json.put("order",order);
  390. JSONObject jsonObject = new JSONObject();
  391. jsonObject.put(key,json);
  392. jsonArray.add(jsonObject);
  393. }
  394. /**
  395. * 查询单条
  396. * @param rid
  397. * @return
  398. */
  399. public String getById(String rid){
  400. JSONObject json = new JSONObject();
  401. JSONArray filters = new JSONArray();
  402. field("and","rid","=",rid,filters);
  403. json.put("filter",filters);
  404. String url = baseUrl+"/dataSearch/getById";
  405. String response = sendPost(url, json.toString());
  406. return response;
  407. }
  408. /**
  409. * 查询
  410. * filter
  411. * - 参数格式:[{"andOr":"and|or","condition":">|=|<|>=|<=|?","field":"<field>","value":"<value>"},<{...}>]
  412. * - 参数说明:andOr跟数据库的中的AND和OR相似;condition指条件匹配程度,?相当于数据库中的like;filed指检索的字段;value为检索的值
  413. * page - 参数说明:页码
  414. * size - 参数说明:分页大小 默认1
  415. * sort - 参数格式:排序,key要排序的字段,order固定,取值asc或desc,不需要排序,传""
  416. * 排序[{"key1":{"order":"asc|desc"}},{"key2":{"order":"asc|desc"}}]
  417. * @param json
  418. * @return
  419. */
  420. public String searchList(JSONObject json){
  421. String url = baseUrl+"/dataSearch/searchList";
  422. String response = sendPost(url, json.toString());
  423. return response;
  424. }
  425. /**
  426. *
  427. * @param json
  428. * @return
  429. */
  430. // public String listPage(JSONObject json){
  431. // String url = baseUrl+"/dataSearch/listPage";
  432. // String response = httpClientUtil.iotPostBody(url, json.toString());
  433. // return response;
  434. // }
  435. /**
  436. * 获取最近5条数据
  437. * @param json
  438. * @param sort
  439. * @return
  440. */
  441. public String recent5(JSONObject json,JSONObject sort){
  442. json.put("access_token",accessToken);
  443. String url = baseUrl+"/dataSearch/recent5";
  444. String response = sendPost(url, json.toString());
  445. return response;
  446. }
  447. /**
  448. * 获取居民一周内体征数据异常次数
  449. * @param json
  450. * @param sort
  451. * @return
  452. */
  453. public String recent1(JSONObject json,JSONObject sort){
  454. json.put("access_token",accessToken);
  455. String url = baseUrl+"/dataSearch/recent1";
  456. String response = sendPost(url, json.toString());
  457. return response;
  458. }
  459. /**
  460. * 初始化设备绑定数据
  461. * @return
  462. */
  463. // public void initPatientDevice(){
  464. // List<PatientDevice> list = patientDeviceDao.findAll();
  465. // list.forEach(patientDevice -> {
  466. // Patient patient = patientDao.findByCode(patientDevice.getUser());
  467. // if(patient!=null){
  468. // String address = patient.getAddress();
  469. // String hospital = null;
  470. // if(StringUtils.isBlank(address)){
  471. // //居民地址为空,默认取居民的社区,如果社区为空默认填写签约的社区医院
  472. // SignFamily signFamily = signFamilyDao.findByPatient(patientDevice.getUser());
  473. // if(signFamily!=null){
  474. // hospital = StringUtils.isBlank(signFamily.getSickVillageName())?signFamily.getHospitalName():signFamily.getSickVillageName();
  475. // }
  476. // }
  477. // String response = saveDevice(patientDevice,address,patient.getName(),patient.getDiseaseCondition(),hospital);
  478. // if(StringUtils.isNotBlank(response)){
  479. // JSONObject re = JSONObject.parseObject(response);
  480. // if(re.getInteger("status")!=200){
  481. // logger.error("id:"+patientDevice.getId()+" "+re.getString("errorMsg"));
  482. // }
  483. // }
  484. // }
  485. // });
  486. // }
  487. /**
  488. * 导入正式环境的坐标信息
  489. * @return
  490. */
  491. // public void exportNormalPatientDevice(){
  492. // String sql = "SELECT * from wlyy_device_location_tmp";
  493. // List<JSONObject> list = myJdbcTemplate.queryJson(sql);
  494. // list.forEach(device -> {
  495. // JSONObject json = new JSONObject();
  496. // json.put("categoryCode",device.getString("category_code"));
  497. // json.put("hospitalName",device.getString("hospital_name"));
  498. // json.put("address",device.getString("address"));
  499. // json.put("patient",device.getString("user"));
  500. // json.put("patientName",device.getString("name"));
  501. // json.put("diseaseCondition",device.getString("disease_condition"));
  502. // json.put("idcard",device.getString("user_idcard"));
  503. // json.put("deviceId",device.getString("device_id"));
  504. // json.put("deviceName",device.getString("device_name"));
  505. // json.put("deviceSn",device.getString("device_sn"));
  506. // json.put("userType",device.getString(""));
  507. // json.put("sim",device.getString("sim"));
  508. // json.put("agent",device.getString("agent"));
  509. // json.put("doctor",device.getString("doctor"));
  510. // saveDevice(json.toString());
  511. // });
  512. // }
  513. /**
  514. * 测试设备绑定
  515. * @return
  516. */
  517. // public String testSaveDevice(){
  518. // List<PatientDevice> list = patientDeviceDao.findAll();
  519. // PatientDevice patientDevice = list.get(0);
  520. // Patient patient = patientDao.findByCode(patientDevice.getUser());
  521. // SignFamily signFamily = signFamilyDao.findByPatient(patientDevice.getUser());
  522. // String hospital = null;
  523. // if(signFamily!=null){
  524. // hospital = StringUtils.isBlank(signFamily.getSickVillageName())?signFamily.getHospitalName():signFamily.getSickVillageName();
  525. // }
  526. // String response = saveDevice(patientDevice,patient.getAddress(),patient.getName(),patient.getDiseaseCondition(),hospital);
  527. // return response;
  528. // }
  529. /**
  530. * 初始化上传
  531. * @param jsonObject
  532. * @return
  533. */
  534. public String initUpload(JSONObject jsonObject){
  535. JSONObject json = new JSONObject();
  536. Integer type = jsonObject.getInteger("type");
  537. json.put("data_source","iHealth");
  538. json.put("sn",StringUtils.trimToEmpty(jsonObject.getString("device_sn")));
  539. json.put("ext_code",StringUtils.trimToEmpty(jsonObject.getString("user_type")));
  540. if(StringUtils.isNotEmpty(jsonObject.getString("device_name"))){
  541. json.put("device_name",StringUtils.trimToEmpty(jsonObject.getString("device_name")));
  542. json.put("device_model",StringUtils.trimToEmpty(jsonObject.getString("device_name")));
  543. }else{
  544. //历史数据未绑定居民
  545. if(StringUtils.isNotEmpty(jsonObject.getString("device_sn"))){
  546. String sql = "SELECT i.device_code,i.device_model,i.device_name from device.wlyy_devices i WHERE device_code = '"+jsonObject.getString("device_sn")+"'";
  547. List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
  548. if(list.size()>0){
  549. json.put("device_name",StringUtils.trimToEmpty(list.get(0).get("device_name").toString()));
  550. json.put("device_model",StringUtils.trimToEmpty(list.get(0).get("device_model").toString()));
  551. }
  552. }
  553. }
  554. json.put("idcard",StringUtils.trimToEmpty(jsonObject.getString("idcard")));
  555. json.put("username",StringUtils.trimToEmpty(jsonObject.getString("name")));
  556. json.put("usercode",StringUtils.trimToEmpty(jsonObject.getString("user")));
  557. json.put("del",StringUtils.trimToEmpty(jsonObject.getString("del")));
  558. json.put("status",getStatus(jsonObject.getString("status")));
  559. JSONArray jsonArray = new JSONArray();
  560. String measure_time = subStringTime(jsonObject.getString("record_date"));
  561. switch (type){
  562. case 1:
  563. //血糖
  564. JSONObject blood_sugar = new JSONObject();
  565. blood_sugar.put("measure_time",measure_time);
  566. blood_sugar.put("type","BloodSugar");
  567. blood_sugar.put("sign_name","blood_sugar");
  568. blood_sugar.put("sign_value",jsonObject.getString("value1"));
  569. blood_sugar.put("sign_unit",bloodSugarUnit);
  570. jsonArray.add(blood_sugar);
  571. JSONObject blood_sugar_result = new JSONObject();
  572. blood_sugar_result.put("measure_time",measure_time);
  573. blood_sugar_result.put("type","BloodSugar");
  574. blood_sugar_result.put("sign_name","blood_sugar_result");
  575. blood_sugar_result.put("sign_value",jsonObject.getString("value2"));
  576. jsonArray.add(blood_sugar_result);
  577. break;
  578. case 2:
  579. //血压
  580. JSONObject systolic = new JSONObject();
  581. systolic.put("measure_time",measure_time);
  582. systolic.put("type","BloodPressure");
  583. systolic.put("sign_name","systolic");
  584. systolic.put("sign_value",jsonObject.getString("value1"));
  585. systolic.put("sign_unit",bloodPressureUnit);
  586. jsonArray.add(systolic);
  587. JSONObject diastolic = new JSONObject();
  588. diastolic.put("measure_time",measure_time);
  589. diastolic.put("type","BloodPressure");
  590. diastolic.put("sign_name","diastolic");
  591. diastolic.put("sign_value",jsonObject.getString("value2"));
  592. diastolic.put("sign_unit",bloodPressureUnit);
  593. jsonArray.add(diastolic);
  594. if(StringUtils.isNotBlank(jsonObject.getString("value3"))){
  595. JSONObject pulse = new JSONObject();
  596. pulse.put("measure_time",measure_time);
  597. pulse.put("type","BloodPressure");
  598. pulse.put("sign_name","pulse");
  599. pulse.put("sign_value",jsonObject.getString("value3"));
  600. pulse.put("sign_unit",pulseUnit);
  601. jsonArray.add(pulse);
  602. }
  603. break;
  604. case 3:
  605. //体重/身高/BMI
  606. JSONObject height = new JSONObject();
  607. height.put("measure_time",measure_time);
  608. height.put("type","Height");
  609. height.put("sign_name","height");
  610. height.put("sign_value",jsonObject.getString("value1"));
  611. height.put("sign_unit",heightUnit);
  612. jsonArray.add(height);
  613. JSONObject weight = new JSONObject();
  614. weight.put("measure_time",measure_time);
  615. weight.put("type","Height");
  616. weight.put("sign_name","weight");
  617. weight.put("sign_value",jsonObject.getString("value2"));
  618. weight.put("sign_unit",weightUnit);
  619. jsonArray.add(weight);
  620. if(StringUtils.isNotBlank(jsonObject.getString("value3"))){
  621. JSONObject bmi = new JSONObject();
  622. bmi.put("measure_time",measure_time);
  623. bmi.put("type","Height");
  624. bmi.put("sign_name","bmi");
  625. bmi.put("sign_value",jsonObject.getString("value3"));
  626. bmi.put("sign_unit",bmiUnit);
  627. jsonArray.add(bmi);
  628. }
  629. break;
  630. case 4:
  631. //腰围
  632. JSONObject waist = new JSONObject();
  633. waist.put("measure_time",measure_time);
  634. waist.put("type","Waist");
  635. waist.put("sign_name","waist");
  636. waist.put("sign_value",jsonObject.getString("value1"));
  637. waist.put("sign_unit",waistUnit);
  638. jsonArray.add(waist);
  639. break;
  640. default:
  641. break;
  642. }
  643. json.put("measure_data",jsonArray);
  644. //上传
  645. return json.toJSONString();
  646. }
  647. /************************************i健康业务相关 start************************************************************/
  648. /**
  649. * 更新状态
  650. * @param id
  651. */
  652. // public void updateStatus(Long id,String status){
  653. // DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(id,id);
  654. // if(mapping == null){
  655. // logger.error("未找到映射关系,id:"+id);
  656. // }else {
  657. // JSONObject json = new JSONObject();
  658. // json.put("rid",mapping.getRid());
  659. // json.put("status",status);
  660. // String url = baseUrl+"/open/gc/updateData";
  661. // String response = sendPost(url, json.toString());
  662. // JSONObject res = JSONObject.parseObject(response);
  663. // }
  664. // }
  665. /**
  666. * 查询单条
  667. * @param id
  668. * @return
  669. */
  670. // public DevicePatientHealthIndex getById(Long id){
  671. // DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(id,id);
  672. // if(mapping == null){
  673. // logger.error("未找到映射关系,id:"+id);
  674. // return null;
  675. // }else {
  676. // return transforOne(getById(mapping.getRid()),id);
  677. // }
  678. // }
  679. /**
  680. * 更新
  681. * @param obj
  682. * @return
  683. */
  684. // public DevicePatientHealthIndex update(DevicePatientHealthIndex obj){
  685. //
  686. // DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(obj.getId(),obj.getId());
  687. // if(mapping == null){
  688. // logger.error("未找到映射关系,id:"+obj.getId());
  689. // }else {
  690. // JSONObject json = new JSONObject();
  691. // json.put("rid",mapping.getRid());
  692. // json.put("del",obj.getDel());
  693. // transforIot(obj,json);
  694. // String url = baseUrl+"/open/gc/updateData";
  695. // String response = sendPost(url, json.toString());
  696. // JSONObject res = JSONObject.parseObject(response);
  697. // }
  698. //
  699. // return obj;
  700. // }
  701. /**
  702. * 删除
  703. * @param id
  704. */
  705. // public void delete(Long id){
  706. // DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByIndexId(id,id);
  707. // if(mapping == null){
  708. // logger.error("未找到映射关系,id:"+id);
  709. // }else {
  710. // JSONObject json = new JSONObject();
  711. // json.put("rid",mapping.getRid());
  712. // json.put("del","0");
  713. // String url = baseUrl+"/open/gc/deleteData";
  714. // String response = sendPost(url, json.toString());
  715. //
  716. // JSONObject res = JSONObject.parseObject(response);
  717. // if("true".equals(res.getString("obj"))){
  718. // logger.info("删除成功!");
  719. // }else {
  720. // logger.error("删除失败,id="+id+",rid="+mapping.getRid()+",msg="+res.getString("errorMsg"));
  721. // }
  722. //
  723. // }
  724. // }
  725. /**
  726. * 测试上传
  727. * @param id
  728. */
  729. // public void testUpload(Long id){
  730. // DevicePatientHealthIndex index = devicePatientHealthIndexDao.findOne(id);
  731. // save(index);
  732. // }
  733. /**
  734. * 体征上传
  735. * @param obj
  736. * @return
  737. */
  738. // public DevicePatientHealthIndex save(DevicePatientHealthIndex obj){
  739. // JSONObject json = new JSONObject();
  740. // if(StringUtils.isNotBlank(obj.getDeviceSn())){
  741. // json.put("sn",obj.getDeviceSn());
  742. // List<PatientDevice> devices = patientDeviceDao.findByPatientAndDeviceSn(obj.getUser(),obj.getDeviceSn());
  743. // if(devices!=null&&devices.size()>0){
  744. // PatientDevice device = devices.get(0);
  745. // json.put("ext_code",device.getUserType());
  746. // json.put("device_name",transfor(device.getDeviceName(),1,obj.getType()));
  747. // json.put("device_model",transfor(device.getDeviceName(),2,obj.getType()));
  748. // }
  749. // }
  750. // Patient patient = patientDao.findByCode(obj.getUser());
  751. // json.put("idcard",patient.getIdcard());
  752. // json.put("idcard_type","1");
  753. // json.put("username",patient.getName());
  754. // json.put("usercode",patient.getCode());
  755. // json.put("del",obj.getDel());
  756. // json.put("status",obj.getStatus());
  757. // transforIot(obj,json);
  758. // //上传
  759. // String response = upload(json);
  760. // JSONObject re = JSONObject.parseObject(response);
  761. // String errorMsg = re.getString("errorMsg");//错误信息(请求失败才有错误消息)
  762. // if(StringUtils.isBlank(errorMsg)){
  763. // String rid = re.getJSONObject("obj").getJSONArray("rid").getString(0);
  764. // DeviceHealthyInfoMapping mapping = new DeviceHealthyInfoMapping();
  765. // mapping.setCreateTime(new Date());
  766. // mapping.setRid(rid);
  767. // deviceHealthyInfoMappingDao.save(mapping);
  768. // obj.setId(mapping.getId());
  769. // }else {
  770. // logger.error(errorMsg);
  771. // return null;
  772. // }
  773. // return obj;
  774. // }
  775. /************************************i健康业务相关 end ************************************************************/
  776. /*****************************************工具方法 start************************************************************/
  777. /**
  778. * 单条转换
  779. * @param res
  780. * @return
  781. */
  782. // public DevicePatientHealthIndex transforOne(String res,Long id){
  783. // if(StringUtils.isBlank(res)){
  784. // return null;
  785. // }
  786. // JSONObject jsonObject = JSONObject.parseObject(res);
  787. // if(jsonObject.getInteger("status")!=200||jsonObject.getJSONArray("obj").size()==0){
  788. // logger.error(jsonObject.getString("errorMsg"));
  789. // return null;
  790. // }
  791. // JSONObject obj = jsonObject.getJSONArray("obj").getJSONObject(0);
  792. // return transforHealthIndex(obj,id);
  793. // }
  794. /**
  795. * 转化成数据库对象
  796. * @param obj
  797. * @param id
  798. * @return
  799. */
  800. // private DevicePatientHealthIndex transforHealthIndex(JSONObject obj,Long id){
  801. // DevicePatientHealthIndex index = new DevicePatientHealthIndex();
  802. //
  803. // index.setDeviceSn(obj.getString("sn"));
  804. // index.setUser(obj.getString("usercode"));
  805. // index.setIdcard(obj.getString("idcard"));
  806. //
  807. // //{"errorCode":null,"errorMsg":null,"successMsg":"search success","status":200,"pageSize":10,"currPage":0,"totalPage":0,"totalCount":0,"detailModelList":null,
  808. // // "obj":[{"access_token":"test","data_source":"iHealth","sn":"867967022337804","ext_code":"","device_name":"","device_model":"",
  809. // // "data":[{"rid":"lwKBG349zj/XBIJOP8CpyoFxFKl0xyxLLzxpN/l9+1U=","del":1,"measure_time":"2016-08-26 12:32:04","systolic":"115.00","systolic_unit":"mmHg","diastolic":"105.00","pulse":"105.00","pulse_unit":"bpm"}],
  810. // // "idCard":"350122198601145513","username":"谢挺盛","usercode":"443a196ef8744536a531260eb26c05d7"}]}
  811. //
  812. // JSONObject data = obj.getJSONArray("data").getJSONObject(0);
  813. // if(id==null){
  814. // //从数据库中取id
  815. // DeviceHealthyInfoMapping mapping = deviceHealthyInfoMappingDao.findByRid(data.getString("rid"));
  816. // if(mapping!=null){
  817. // id = mapping.getIndexId()==null?mapping.getId():mapping.getIndexId();
  818. // }
  819. // }
  820. // index.setId(id);
  821. // index.setRecordDate(DateUtil.strToDate(data.getString("measure_time")));
  822. // index.setSortDate(index.getRecordDate());
  823. // index.setType(data.getInteger("type"));
  824. // index.setDel(data.getString("del"));
  825. // index.setStatus(data.getInteger("status"));
  826. // if(data.getInteger("type")==1){
  827. // index.setValue1(data.getString("value1"));
  828. // index.setValue2(data.getString("value2"));
  829. // }else if(data.getInteger("type")==2){
  830. // index.setValue1(data.getString("value1"));
  831. // index.setValue2(data.getString("value2"));
  832. // index.setValue3(data.getString("value3"));
  833. // }else if(data.getInteger("type")==3){
  834. // index.setValue1(data.getString("value1"));
  835. // index.setValue2(data.getString("value2"));
  836. // index.setValue3(data.getString("value3"));
  837. // }else if(data.getInteger("type")==4){
  838. // index.setValue1(data.getString("value1"));
  839. // }
  840. // return index;
  841. // }
  842. /**
  843. * 字符串分割
  844. * @param name
  845. * @param type 1品牌,2 型号
  846. * @param deviceType 1血糖,2 血压
  847. * @return
  848. */
  849. private String transfor(String name,Integer type,Integer deviceType){
  850. String re = "";
  851. String[] brand = {"康为","爱奥乐","优瑞恩","三诺","云湃"};
  852. String[] model = {"A206G","G-777G","U80EH","亲智","RBP-980"};
  853. String model2 = "G-426-3";
  854. if(StringUtils.isBlank(name)){
  855. return re;
  856. }
  857. for(int i=0;i<brand.length;i++){
  858. String one = brand[i];
  859. if(name.contains(one)){
  860. re = type == 1? one:((i==0&&deviceType==1)?model2:model[i]);
  861. }
  862. }
  863. return re;
  864. }
  865. /**
  866. * 时间处理
  867. * @param time
  868. * @return
  869. */
  870. private String subStringTime(String time){
  871. return StringUtils.isBlank(time)? "":time.substring(0,19);
  872. }
  873. /**
  874. * 状态默认值
  875. * @param status
  876. * @return
  877. */
  878. private String getStatus(String status){
  879. return StringUtils.isBlank(status)?"0":status;
  880. }
  881. /**
  882. * 转化成物联网格式
  883. * @param obj
  884. * @param json
  885. * @return
  886. */
  887. public JSONObject transforIot(DevicePatientHealthIndex obj,JSONObject json){
  888. JSONArray jsonArray = new JSONArray();
  889. String measure_time = DateUtil.dateToStrLong(obj.getRecordDate());
  890. switch (obj.getType()){
  891. case 1:
  892. //血糖
  893. JSONObject blood_sugar = new JSONObject();
  894. blood_sugar.put("measure_time",measure_time);
  895. blood_sugar.put("type","BloodSugar");
  896. blood_sugar.put("sign_name","blood_sugar");
  897. blood_sugar.put("sign_value",obj.getValue1());
  898. blood_sugar.put("sign_unit",bloodSugarUnit);
  899. jsonArray.add(blood_sugar);
  900. JSONObject blood_sugar_result = new JSONObject();
  901. blood_sugar_result.put("measure_time",measure_time);
  902. blood_sugar_result.put("type","BloodSugar");
  903. blood_sugar_result.put("sign_name","blood_sugar_result");
  904. blood_sugar_result.put("sign_value",obj.getValue2());
  905. jsonArray.add(blood_sugar_result);
  906. break;
  907. case 2:
  908. //血压
  909. JSONObject systolic = new JSONObject();
  910. systolic.put("measure_time",measure_time);
  911. systolic.put("type","BloodPressure");
  912. systolic.put("sign_name","systolic");
  913. systolic.put("sign_value",obj.getValue1());
  914. systolic.put("sign_unit",bloodPressureUnit);
  915. jsonArray.add(systolic);
  916. JSONObject diastolic = new JSONObject();
  917. diastolic.put("measure_time",measure_time);
  918. diastolic.put("type","BloodPressure");
  919. diastolic.put("sign_name","diastolic");
  920. diastolic.put("sign_value",obj.getValue2());
  921. diastolic.put("sign_unit",bloodPressureUnit);
  922. jsonArray.add(diastolic);
  923. if(StringUtils.isNotBlank(obj.getValue3())){
  924. JSONObject pulse = new JSONObject();
  925. pulse.put("measure_time",measure_time);
  926. pulse.put("type","BloodPressure");
  927. pulse.put("sign_name","pulse");
  928. pulse.put("sign_value",obj.getValue3());
  929. pulse.put("sign_unit",pulseUnit);
  930. jsonArray.add(pulse);
  931. }
  932. break;
  933. case 3:
  934. //体重/身高/BMI
  935. JSONObject height = new JSONObject();
  936. height.put("measure_time",measure_time);
  937. height.put("type","Height");
  938. height.put("sign_name","height");
  939. height.put("sign_value",obj.getValue1());
  940. height.put("sign_unit",heightUnit);
  941. jsonArray.add(height);
  942. JSONObject weight = new JSONObject();
  943. weight.put("measure_time",measure_time);
  944. weight.put("type","Height");
  945. weight.put("sign_name","weight");
  946. weight.put("sign_value",obj.getValue2());
  947. weight.put("sign_unit",weightUnit);
  948. jsonArray.add(weight);
  949. if(StringUtils.isNotBlank(obj.getValue3())){
  950. JSONObject bmi = new JSONObject();
  951. bmi.put("measure_time",measure_time);
  952. bmi.put("type","Height");
  953. bmi.put("sign_name","bmi");
  954. bmi.put("sign_value",obj.getValue3());
  955. bmi.put("sign_unit",bmiUnit);
  956. jsonArray.add(bmi);
  957. }
  958. break;
  959. case 4:
  960. //腰围
  961. JSONObject waist = new JSONObject();
  962. waist.put("measure_time",measure_time);
  963. waist.put("type","Waist");
  964. waist.put("sign_name","waist");
  965. waist.put("sign_value",obj.getValue1());
  966. waist.put("sign_unit",waistUnit);
  967. jsonArray.add(waist);
  968. break;
  969. default:
  970. break;
  971. }
  972. json.put("measure_data",jsonArray);
  973. return json;
  974. }
  975. /**
  976. * 返回accessToken
  977. * @return
  978. */
  979. private synchronized String getAccessToken(){
  980. String token = "";
  981. if(tokenMap.get("token")!=null){
  982. token = tokenMap.get("token");
  983. Long outTime = Long.valueOf(tokenMap.get("outTime"));
  984. if(new Date().getTime()<outTime){
  985. return token;
  986. }
  987. }
  988. try {
  989. Map params = new HashMap();
  990. params.put("appid", appid);
  991. params.put("appSecret", appSecret);
  992. String url = "/open/gc/accesstoken";
  993. String response = httpClientUtil.httpPost(baseUrl + url, params);
  994. JSONObject jsonObject = JSON.parseObject(response);
  995. if(jsonObject.getInteger("status")==10000){
  996. String accesstoken = jsonObject.getJSONObject("result").getString("accesstoken");
  997. tokenMap.put("token",accesstoken);
  998. tokenMap.put("outTime", jsonObject.getJSONObject("result").getLong("outTime")+"");
  999. return accesstoken;
  1000. }
  1001. }catch (Exception e){
  1002. e.printStackTrace();
  1003. }
  1004. return null;
  1005. }
  1006. // public String iotDataImportJob(String idcard){
  1007. // try {
  1008. // String dayStr = DateUtil.getStringDateShort();
  1009. // List<DevicePatientHealthIndex> healthIndices = new ArrayList<>();
  1010. //
  1011. // logger.info(":start IOT_DATA_IMPORT_JOB===============");
  1012. //// 获取access token
  1013. // String tokenUrl = "http://iot.xmtyw.cn:9660/cityihealth/iot/svr-iot/open/gc/accesstoken";
  1014. // if ("prod".equals(profiles)){
  1015. // tokenUrl = "http://27.155.102.211:9660/cityihealth/iot/svr-iot/open/gc/accesstoken";
  1016. // }
  1017. // if ("local".equals(profiles)){
  1018. // tokenUrl = "http://10.95.22.10:9661/cityihealth/iot/svr-iot/open/gc/accesstoken";
  1019. // }
  1020. //
  1021. // Map params = new HashMap();
  1022. // params.put("appid", "b5125656570011ec8771005056ab2351");
  1023. // params.put("appSecret", "b5125668570011ec8771005056ab2351");
  1024. // String response = httpClientUtil.httpPost(tokenUrl, params);
  1025. // JSONObject jsonObject = JSON.parseObject(response);
  1026. // if(jsonObject.getInteger("status")==10000){
  1027. // String accesstoken = jsonObject.getJSONObject("result").getString("accesstoken");
  1028. // //获取体征数据
  1029. // String searchUrl = "http://iot.xmtyw.cn:9660/cityihealth/iot/svr-iot/open/gc/searchList";
  1030. // if ("prod".equals(profiles)){//外网
  1031. // searchUrl = "http://27.155.102.211:9660/cityihealth/iot/svr-iot/open/gc/searchList2";
  1032. // }
  1033. // if ("local".equals(profiles)){//内网
  1034. // searchUrl = "http://10.95.22.10:9661/cityihealth/iot/svr-iot/open/gc/searchList";
  1035. // }
  1036. // params = new HashMap();
  1037. // params.put("accesstoken",accesstoken);
  1038. // List<NameValuePair> bodyParams = new ArrayList<>();
  1039. // if (StringUtils.isNotBlank(idcard)){
  1040. // bodyParams.add(new BasicNameValuePair("idcard", idcard));
  1041. // }
  1042. // bodyParams.add(new BasicNameValuePair("startTime", dayStr+" 00:00:00"));
  1043. // bodyParams.add(new BasicNameValuePair("endTime", dayStr+" 23:59:59"));
  1044. // response = httpClientUtil.headerPost(searchUrl, bodyParams,"utf-8",params);
  1045. // jsonObject = JSONObject.parseObject(response);
  1046. //
  1047. // if(jsonObject.getInteger("status")==200){
  1048. // JSONArray array = jsonObject.getJSONArray("detailModelList");
  1049. // for (int i=0;i<array.size();i++){
  1050. // try {
  1051. // JSONObject tmp = array.getJSONObject(i);
  1052. // String sn = tmp.getString("sn");
  1053. // String userName = tmp.getString("username");
  1054. // idcard = tmp.getString("idcard");
  1055. // Patient patient = patientDao.findByIdcard(idcard);
  1056. // SignFamily sf = signFamilyDao.findByIdcard(idcard);
  1057. // if (null!=patient){
  1058. // JSONArray measureDatas = tmp.getJSONArray("measure_data");
  1059. // if (measureDatas.size()>0){
  1060. // try {
  1061. // JSONObject dataInfo = measureDatas.getJSONObject(0);
  1062. // //血压 BloodPressure 收缩压systolic 舒张压diastolic
  1063. // //血糖 BloodSugar blood_sugar
  1064. // //数据类型
  1065. // String dataType = dataInfo.getString("type");
  1066. // String sign_value = dataInfo.get("sign_value").toString();
  1067. // String sign_name = dataInfo.get("sign_name").toString();
  1068. // String measure_time = dataInfo.get("measure_time").toString();
  1069. // Date measureDate = DateUtil.strToDate(measure_time);
  1070. // String sign_unit = dataInfo.get("sign_unit").toString();
  1071. // if ("BloodPressure".equals(dataType)){
  1072. // DevicePatientHealthIndex index = new DevicePatientHealthIndex();
  1073. // index.setUser(patient.getCode());
  1074. // index.setDel("1");
  1075. // index.setRecordDate(measureDate); //记录时间
  1076. // index.setSortDate(measureDate); //排序时间
  1077. // index.setCzrq(measureDate); //排序时间
  1078. // index.setDeviceSn(sn);
  1079. // index.setIdcard(idcard);
  1080. // index.setStatus(0);
  1081. // index.setName(userName);
  1082. // if (null!=sf){
  1083. // index.setHospital(sf.getHospital());
  1084. // index.setHospitalName(sf.getHospitalName());
  1085. // }
  1086. // if ("systolic".equals(sign_name)){
  1087. // index.setValue1(sign_value);//收缩压
  1088. // }
  1089. // if ("diastolic".equals(sign_name)){
  1090. // index.setValue2(sign_value);//舒张压
  1091. // }
  1092. // if (measureDatas.size()>1){
  1093. // JSONObject dataInfo2 = measureDatas.getJSONObject(1);
  1094. // String sign_value2 = dataInfo2.get("sign_value").toString();
  1095. // String sign_name2 = dataInfo2.get("sign_name").toString();
  1096. // if ("systolic".equals(sign_name2)){
  1097. // index.setValue1(sign_value2);//收缩压
  1098. // }
  1099. // if ("diastolic".equals(sign_name2)){
  1100. // index.setValue2(sign_value2);//舒张压
  1101. // }
  1102. // }
  1103. // index.setType(2);//1血糖,2血压
  1104. // String sql = " select count(id) from device.wlyy_patient_health_index where `user`='"+patient.getCode()+"' " +
  1105. // "and type='2' and value1='"+index.getValue1()+"' and value2='"+index.getValue2()+"' and record_date='"+measure_time+"' ";
  1106. // if (0==jdbcTemplate.queryForObject(sql,Integer.class)){
  1107. // healthIndices.add(index);
  1108. // }
  1109. // } else if ("BloodSugar".equals(dataType)) {
  1110. // DevicePatientHealthIndex index = new DevicePatientHealthIndex();
  1111. // index.setUser(patient.getCode());
  1112. // index.setDel("1");
  1113. // index.setRecordDate(measureDate); //记录时间
  1114. // index.setSortDate(measureDate); //排序时间
  1115. // index.setCzrq(measureDate); //排序时间
  1116. // index.setDeviceSn(sn);
  1117. // index.setIdcard(idcard);
  1118. // index.setStatus(0);
  1119. // index.setName(userName);
  1120. // if (null!=sf){
  1121. // index.setHospital(sf.getHospital());
  1122. // index.setHospitalName(sf.getHospitalName());
  1123. // }
  1124. // index.setValue1(sign_value);//血糖
  1125. // index.setValue2("1");//血糖类型
  1126. // index.setType(1);//1血糖,2血压
  1127. // String sql = " select count(id) from device.wlyy_patient_health_index where `user`='"+patient.getCode()+"' " +
  1128. // "and type='1' and value1='"+index.getValue1()+"' and value2='1' and record_date='"+measure_time+"' ";
  1129. // if (0==jdbcTemplate.queryForObject(sql,Integer.class)){
  1130. // healthIndices.add(index);
  1131. // }
  1132. // }
  1133. // }catch (Exception e){}
  1134. // }
  1135. // }
  1136. // }catch (Exception e){}
  1137. // }
  1138. // }
  1139. // }else {
  1140. // throw new Exception("获取access token失败");
  1141. // }
  1142. // if (healthIndices.size()>0){
  1143. // devicePatientHealthIndexDao.save(healthIndices);
  1144. // }
  1145. // logger.info(":start IOT_DATA_IMPORT_JOB success===============");
  1146. // }catch (Exception e){
  1147. // e.printStackTrace();
  1148. // logger.info(":start IOT_DATA_IMPORT_JOB error==============message:"+e.getMessage());
  1149. // }
  1150. // return "success";
  1151. // }
  1152. // public String iotDataImportData(String json){
  1153. // List<DevicePatientHealthIndex> healthIndices = new ArrayList<>();
  1154. // try {
  1155. // WlyyIotUploadDataLogDO uploadDataLogDO = new WlyyIotUploadDataLogDO();
  1156. //
  1157. // JSONObject jsonObject = null;
  1158. // try {
  1159. // jsonObject = JSONObject.parseObject(json);
  1160. // } catch (Exception e) {
  1161. // uploadDataLogDO.setJson(json);
  1162. // uploadDataLogDO.setCreateTime(new Date());
  1163. // wlyyIotUploadDataLogDao.save(uploadDataLogDO);
  1164. // throw new Exception("json参数转换失败");
  1165. // }
  1166. // String deviceSn = jsonObject.getString("sn");
  1167. // String userName = jsonObject.getString("username");
  1168. // String idcard = jsonObject.getString("idcard");
  1169. // uploadDataLogDO.setJson(json);
  1170. // uploadDataLogDO.setDeviceSn(deviceSn);
  1171. // uploadDataLogDO.setUserName(userName);
  1172. // uploadDataLogDO.setIdcard(idcard);
  1173. // uploadDataLogDO.setCreateTime(new Date());
  1174. // wlyyIotUploadDataLogDao.save(uploadDataLogDO);
  1175. //
  1176. // Patient patient = patientDao.findByIdcard(idcard);
  1177. // SignFamily sf = signFamilyDao.findByIdcard(idcard);
  1178. // if (null!=patient){
  1179. // JSONArray measureDatas = jsonObject.getJSONArray("measure_data");
  1180. // if (measureDatas.size()>0){
  1181. // try {
  1182. // JSONObject dataInfo = measureDatas.getJSONObject(0);
  1183. // //数据类型
  1184. // String dataType = dataInfo.getString("type");
  1185. // if (StringUtils.isNotBlank(dataType)){
  1186. // switch (dataType){
  1187. // case "BloodPressure": //血压
  1188. // healthIndices.addAll(BloodPressureHandle(patient,sf,deviceSn,idcard,userName,measureDatas));
  1189. // break;
  1190. // case "BloodSugar"://血糖
  1191. // healthIndices.addAll(BloodSugarHandle(patient,sf,deviceSn,idcard,userName,measureDatas));
  1192. // break;
  1193. // }
  1194. // }
  1195. // }catch (Exception e){}
  1196. // }
  1197. // }
  1198. // }catch (Exception e){
  1199. // e.printStackTrace();
  1200. // }
  1201. // if (healthIndices.size()>0){
  1202. // devicePatientHealthIndexDao.save(healthIndices);
  1203. // }
  1204. // return "success";
  1205. // }
  1206. // public List<DevicePatientHealthIndex> BloodPressureHandle(Patient patient,SignFamily sf,String deviceSn,String idcard,String userName,JSONArray measureDatas){
  1207. // List<DevicePatientHealthIndex> result = new ArrayList<>();
  1208. // DevicePatientHealthIndex index = new DevicePatientHealthIndex();
  1209. // JSONObject dataInfo = measureDatas.getJSONObject(0);
  1210. //
  1211. // String sign_value = dataInfo.get("sign_value").toString();
  1212. // String sign_name = dataInfo.get("sign_name").toString();
  1213. // String measure_time = dataInfo.get("measure_time").toString();
  1214. // Date measureDate = DateUtil.strToDate(measure_time);
  1215. // String sign_unit = dataInfo.get("sign_unit").toString();
  1216. // //血压 BloodPressure 收缩压systolic 舒张压diastolic
  1217. //
  1218. // index.setUser(patient.getCode());
  1219. // index.setDel("1");
  1220. // index.setRecordDate(measureDate); //记录时间
  1221. // index.setSortDate(measureDate); //排序时间
  1222. // index.setCzrq(measureDate); //排序时间
  1223. // index.setDeviceSn(deviceSn);
  1224. // index.setIdcard(idcard);
  1225. // index.setStatus(0);
  1226. // index.setName(userName);
  1227. // if (null!=sf){
  1228. // index.setHospital(sf.getHospital());
  1229. // index.setHospitalName(sf.getHospitalName());
  1230. // }
  1231. // if ("systolic".equals(sign_name)){
  1232. // index.setValue1(sign_value);//收缩压
  1233. // }
  1234. // if ("diastolic".equals(sign_name)){
  1235. // index.setValue2(sign_value);//舒张压
  1236. // }
  1237. // if (measureDatas.size()>1){
  1238. // JSONObject dataInfo2 = measureDatas.getJSONObject(1);
  1239. // String sign_value2 = dataInfo2.get("sign_value").toString();
  1240. // String sign_name2 = dataInfo2.get("sign_name").toString();
  1241. // if ("systolic".equals(sign_name2)){
  1242. // index.setValue1(sign_value2);//收缩压
  1243. // }
  1244. // if ("diastolic".equals(sign_name2)){
  1245. // index.setValue2(sign_value2);//舒张压
  1246. // }
  1247. // }
  1248. // index.setType(2);//1血糖,2血压
  1249. // String sql = " select count(id) from device.wlyy_patient_health_index where `user`='"+patient.getCode()+"' " +
  1250. // "and type='2' and value1='"+index.getValue1()+"' and value2='"+index.getValue2()+"' and record_date='"+measure_time+"' ";
  1251. // if (0==jdbcTemplate.queryForObject(sql,Integer.class)){
  1252. // result.add(index);
  1253. // }
  1254. // return result;
  1255. // }
  1256. // public List<DevicePatientHealthIndex> BloodSugarHandle(Patient patient,SignFamily sf,String deviceSn,String idcard,String userName,JSONArray measureDatas){
  1257. // List<DevicePatientHealthIndex> result = new ArrayList<>();
  1258. // DevicePatientHealthIndex index = new DevicePatientHealthIndex();
  1259. // JSONObject dataInfo = measureDatas.getJSONObject(0);
  1260. // //血糖 BloodSugar blood_sugar
  1261. //
  1262. // String sign_value = dataInfo.get("sign_value").toString();
  1263. // String sign_name = dataInfo.get("sign_name").toString();
  1264. // String measure_time = dataInfo.get("measure_time").toString();
  1265. // Date measureDate = DateUtil.strToDate(measure_time);
  1266. // String sign_unit = dataInfo.get("sign_unit").toString();
  1267. //
  1268. //
  1269. // index.setUser(patient.getCode());
  1270. // index.setDel("1");
  1271. // index.setRecordDate(measureDate); //记录时间
  1272. // index.setSortDate(measureDate); //排序时间
  1273. // index.setCzrq(measureDate); //排序时间
  1274. // index.setDeviceSn(deviceSn);
  1275. // index.setIdcard(idcard);
  1276. // index.setStatus(0);
  1277. // index.setName(userName);
  1278. // if (null!=sf){
  1279. // index.setHospital(sf.getHospital());
  1280. // index.setHospitalName(sf.getHospitalName());
  1281. // }
  1282. // index.setValue1(sign_value);//血糖
  1283. // index.setValue2("1");//血糖类型
  1284. // index.setType(1);//1血糖,2血压
  1285. // String sql = " select count(id) from device.wlyy_patient_health_index where `user`='"+patient.getCode()+"' " +
  1286. // "and type='1' and value1='"+index.getValue1()+"' and value2='1' and record_date='"+measure_time+"' ";
  1287. // if (0==jdbcTemplate.queryForObject(sql,Integer.class)){
  1288. // result.add(index);
  1289. // }
  1290. // return result;
  1291. // }
  1292. /*****************************************工具方法 end ************************************************************/
  1293. /*****************************************塞克上传接口***********************************************************/
  1294. // @Transactional(rollbackFor = Exception.class)
  1295. // public void skJsonSave(String idcard, Integer type, String json) {
  1296. // SkJsonFormEntity skJsonFormEntity = new SkJsonFormEntity();
  1297. // skJsonFormEntity.setJson(json);
  1298. // skJsonFormEntity.setIdcard(idcard);
  1299. // skJsonFormEntity.setType(type);
  1300. // skJsonFormEntity.setStatus(0);
  1301. // skJsonFormEntity.setCreateTime(new Date());
  1302. // skJsonFormDao.save(skJsonFormEntity);
  1303. // }
  1304. public JSONObject getJoinPatient(String startTime,String endTime,String status,Integer page,Integer pageSize,String idcard) {
  1305. JSONObject jsonObject = new JSONObject();
  1306. String sql = "SELECT " +
  1307. " DISTINCT cp.idcard,cp.`name`,cp.hospital,cp.hospital_name AS hospitalName,DATE_FORMAT(cp.create_time,'%Y-%m-%d %H:%i:%S') AS createTime" +
  1308. ",DATE_FORMAT(cp.update_time,'%Y-%m-%d %H:%i:%S') AS updateTime" +
  1309. ",cp.group_screening_code as groupScreeningCode,cp.status,cp.group_screening_status groupScreeningStatus," +
  1310. " IF(mod(MID(cp.idcard,17,1),2),1,2) AS sex,p.birthday,p.mobile,cp.entry_time,cp.group_watch,cp.exit_time " +
  1311. ",vi1.bracelet_date,rr2.return_time return_time2,rr3.return_time return_time3,rr4.return_time return_time4" +
  1312. ",rr5.return_time return_time5,rr6.return_time return_time6,vi6.fill_date " +
  1313. "FROM wlyy_copd_patient cp " +
  1314. "LEFT JOIN wlyy_patient p ON cp.patient = p.`code` " +
  1315. "LEFT JOIN wlyy_followup f1 on f1.patient_code = cp.patient and f1.type='4' and f1.visit_count='1' " +
  1316. "LEFT JOIN wlyy_followup f2 on f2.patient_code = cp.patient and f2.type='4' and f2.visit_count='2' " +
  1317. "LEFT JOIN wlyy_followup f3 on f3.patient_code = cp.patient and f3.type='4' and f3.visit_count='3' " +
  1318. "LEFT JOIN wlyy_followup f4 on f4.patient_code = cp.patient and f4.type='4' and f4.visit_count='4' " +
  1319. "LEFT JOIN wlyy_followup f5 on f5.patient_code = cp.patient and f5.type='4' and f5.visit_count='5' " +
  1320. "LEFT JOIN wlyy_followup f6 on f6.patient_code = cp.patient and f6.type='4' and f6.visit_count='6' " +
  1321. "LEFT JOIN copd_information_of_this_visit vi1 on f1.id = vi1.followup_id " +
  1322. "LEFT JOIN copd_information_of_this_visit vi6 on f6.id = vi6.followup_id " +
  1323. "LEFT JOIN copd_device_return_record rr2 on f2.id = rr2.followup_id " +
  1324. "LEFT JOIN copd_device_return_record rr3 on f3.id = rr3.followup_id " +
  1325. "LEFT JOIN copd_device_return_record rr4 on f4.id = rr4.followup_id " +
  1326. "LEFT JOIN copd_device_return_record rr5 on f5.id = rr5.followup_id " +
  1327. "LEFT JOIN copd_device_return_record rr6 on f6.id = rr6.followup_id " +
  1328. " WHERE 1=1 ";
  1329. String sqlTotal = "SELECT " +
  1330. " count(DISTINCT idcard) " +
  1331. "FROM " +
  1332. " wlyy_copd_patient " +
  1333. "WHERE 1=1 ";
  1334. if("1".equals(status)){
  1335. sql += " and cp.status = '1'";
  1336. sqlTotal += " and status = '1'";
  1337. }else if("2".equals(status)){
  1338. sql += " AND group_screening_status <> '0' " +
  1339. " AND group_screening_status IS NOT NULL " +
  1340. " AND group_screening_status <> '' " ;
  1341. sqlTotal += " AND group_screening_status <> '0' " +
  1342. " AND group_screening_status IS NOT NULL " +
  1343. " AND group_screening_status <> '' " ;
  1344. }
  1345. if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)) {
  1346. // String start = startTime.replace("-","").substring(0,8);
  1347. // String end = endTime.replace("-","").substring(0,8);
  1348. endTime += " 23:59:59";
  1349. sql += " and ((cp.update_time >= '"+startTime+"' and cp.update_time <= '"+endTime+"') " +
  1350. " or (cp.inclusion_time>='"+startTime+"' and cp.inclusion_time <='"+endTime+"') " +
  1351. " or (cp.exit_time>='"+startTime+"' and cp.exit_time <='"+endTime+"') " +
  1352. " or (vi6.fill_date>='"+startTime+"' and vi6.fill_date <='"+endTime+"') " +
  1353. " or (second_inclusion_time>='"+startTime+"' and second_inclusion_time <='"+endTime+"')) ";
  1354. sqlTotal += " and ((update_time >= '"+startTime+"' and update_time <= '"+endTime+"') " +
  1355. " or (inclusion_time>='"+startTime+"' and inclusion_time <='"+endTime+"') " +
  1356. " or (exit_time>='"+startTime+"' and exit_time <='"+endTime+"') " +
  1357. " or (second_inclusion_time>='"+startTime+"' and second_inclusion_time <='"+endTime+"')) ";
  1358. }
  1359. if(StringUtils.isNotBlank(idcard)){
  1360. sql += " and cp.idcard='"+idcard+"'";
  1361. sqlTotal += " and idcard='"+idcard+"'";
  1362. }
  1363. sql +=" and hospital <> '5351e9756eb011e69f7c005056850d66' and hospital <> 'ac02a9b87a3611e69f7c005056850d66' and hospital is not null ";
  1364. sql += " order by cp.id ";
  1365. if (page==null&&pageSize==null) {
  1366. page = 1;
  1367. pageSize = 100;
  1368. }
  1369. sql += " LIMIT "+(page-1)*pageSize+","+pageSize;
  1370. Integer total = jdbcTemplate.queryForObject(sqlTotal, Integer.class);
  1371. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  1372. jsonObject.put("total",total);
  1373. jsonObject.put("listData",list);
  1374. jsonObject.put("pageNum",Math.ceil(Double.valueOf(total)/Double.valueOf(pageSize)));
  1375. return jsonObject;
  1376. }
  1377. }