|
@ -19,10 +19,19 @@ import com.yihu.wlyy.util.DateUtil;
|
|
|
import net.sf.json.JSONArray;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.Root;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@ -48,7 +57,8 @@ public class KitService extends BaseService {
|
|
|
private PatientDeviceDao patientDeviceDao;
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
public String format = "yyyy-MM-dd HH:mm";
|
|
|
private String format = "yyyy-MM-dd HH:mm";
|
|
|
private String format2 = "HH:mm" ;
|
|
|
|
|
|
/**
|
|
|
* 药盒绑定
|
|
@ -236,33 +246,245 @@ public class KitService extends BaseService {
|
|
|
kitDrugDetailDao.save(result);
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
String drugDetail =
|
|
|
"[{" +
|
|
|
"\"drugName\":\"aa\"," +
|
|
|
"\"measure\":\"bb\"," +
|
|
|
"\"drugImgUrl\":\"cc\"" +
|
|
|
"},"+"{" +
|
|
|
"\"drugName\":\"aa\"," +
|
|
|
"\"measure\":\"bb\"," +
|
|
|
"\"drugImgUrl\":\"cc\"" +
|
|
|
"}"+"]";
|
|
|
// JSONObject drugDetailJson = JSONObject.fromObject(drugDetail);
|
|
|
JSONArray a = JSONArray.fromObject(drugDetail);
|
|
|
for(int i = 0;i<a.size();i++){
|
|
|
String drugName = a.getJSONObject(i).getString("drugName");
|
|
|
String measure = a.getJSONObject(i).getString("measure");
|
|
|
String drugImgUrl = a.getJSONObject(i).getString("drugImgUrl");
|
|
|
System.out.println(i+","+drugName+","+measure+","+drugImgUrl);
|
|
|
|
|
|
/**
|
|
|
* 获取居民的用药记录
|
|
|
* @param patientCode 居民code
|
|
|
* @param recordType 记录方式 1、手动,2、设备
|
|
|
* @param startTime 时间搜索的开始时间
|
|
|
* @param endTime 时间搜索的结束时间
|
|
|
*/
|
|
|
public List<Map<String,Object>> getUseDrugRecordInfo(String patientCode,Integer recordType,String startTime,String endTime,Integer page,Integer pageSize) throws Exception{
|
|
|
|
|
|
Date startDate = DateUtil.strToDate(startTime);
|
|
|
Date endDate = DateUtil.strToDate(endTime);
|
|
|
|
|
|
Specification specification = new Specification<KitDrugUseRecord>() {
|
|
|
@Override
|
|
|
public Predicate toPredicate(Root<KitDrugUseRecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
|
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
predicates.add(criteriaBuilder.equal(root.get("patientCode"), patientCode));
|
|
|
if(recordType!=null){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("recordType"), recordType));
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(startTime)){
|
|
|
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("useTime"), startDate));
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(endTime)){
|
|
|
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("useTime"), endDate));
|
|
|
}
|
|
|
criteriaQuery.orderBy(criteriaBuilder.desc(root.get("useTime")));//按时间降序排序
|
|
|
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));//条件
|
|
|
return criteriaQuery.getRestriction();
|
|
|
}
|
|
|
};
|
|
|
List<Map<String,Object>> reuslt = new ArrayList<>();
|
|
|
List<KitDrugUseRecord> resutlList = null;
|
|
|
if(page!=null&&pageSize!=null){
|
|
|
page = page-1;
|
|
|
Pageable pageRequest = new PageRequest(page, pageSize);
|
|
|
Page<KitDrugUseRecord> resutlTemp = kitDrugUseRecordDao.findAll(specification,pageRequest);
|
|
|
resutlList = resutlTemp.getContent();
|
|
|
// return resutlList.getContent();
|
|
|
}else{
|
|
|
resutlList = kitDrugUseRecordDao.findAll(specification);
|
|
|
// return resutlList;
|
|
|
}
|
|
|
for(KitDrugUseRecord one :resutlList){
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
map.put("useTime",one.getUseTime());
|
|
|
map.put("recordType",one.getRecordType());
|
|
|
map.put("drugDetail",JSONArray.fromObject(one.getDrugDetail()));
|
|
|
reuslt.add(map);
|
|
|
}
|
|
|
return reuslt;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取某个居民的所绑定的所有药盒设备信息
|
|
|
* @param patientCode
|
|
|
*/
|
|
|
public List<Map<String,Object>> getAllKitDevice(String patientCode){
|
|
|
List<PatientDevice> list = patientDeviceDao.findByPatientAndDel(patientCode);
|
|
|
List<Map<String,Object>> result = new ArrayList<>();
|
|
|
for(PatientDevice one:list){
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
map.put("deviceSn",one.getDeviceSn());
|
|
|
map.put("deviceName",one.getDeviceName());
|
|
|
result.add(map);
|
|
|
}
|
|
|
// JSONArray b = drugDetailJson.getJSONArray("drug");
|
|
|
// for(int i = 0;i<b.size();i++){
|
|
|
// String drugName = b.getJSONObject(i).getString("drugName");
|
|
|
// String measure = b.getJSONObject(i).getString("measure");
|
|
|
// String drugImgUrl = b.getJSONObject(i).getString("drugImgUrl");
|
|
|
// System.out.println(i+","+drugName+","+measure+","+drugImgUrl);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 统计分析--近期使用药品,药品用法
|
|
|
* @param deviceSn
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public Map<String,Object> drugUseRecordStatistics(String deviceSn) throws Exception{
|
|
|
JSONArray dataJSONStr = jyKitService.getKitSetInfo(deviceSn);
|
|
|
if(dataJSONStr.size()>0){
|
|
|
|
|
|
JSONArray recentDrugInfo = dataJSONStr.getJSONObject(0).getJSONArray("drug_info");//近期使用药品
|
|
|
JSONArray oneAlarmInfo = dataJSONStr.getJSONObject(0).getJSONArray("one_alarm_info");//一天的所有闹钟信息
|
|
|
Map<String,Object> resultMap = new HashMap<>();
|
|
|
List<JSONObject> list = new ArrayList<JSONObject> ();
|
|
|
JSONObject jsonObj = null;
|
|
|
resultMap.put("recentDrugInfo",recentDrugInfo);
|
|
|
for (int i = 0; i < oneAlarmInfo.size(); i++) {
|
|
|
jsonObj = (JSONObject)oneAlarmInfo.get(i);
|
|
|
list.add(jsonObj);
|
|
|
}
|
|
|
//按闹钟时间排序
|
|
|
Collections.sort(list, new Comparator<JSONObject>() {
|
|
|
@Override
|
|
|
public int compare(JSONObject o1, JSONObject o2) {
|
|
|
Long alarmTime1 = DateUtil.strToDate(o1.get("alarm_time").toString(), format2).getTime();
|
|
|
Long alarmTime2 = DateUtil.strToDate(o1.get("alarm_time").toString(), format2).getTime();
|
|
|
if(alarmTime1-alarmTime2>0){
|
|
|
return -1;
|
|
|
}else if(alarmTime1-alarmTime2<0){
|
|
|
return 1;
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
});
|
|
|
resultMap.put("oneAlarmInfo",list);
|
|
|
return resultMap;
|
|
|
}else{
|
|
|
throw new Exception("请求的sn码巨烨无返回设备设置信息!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 本月用药情况
|
|
|
* @param useDrugStatus (1、按时,2、超时,3、漏用)
|
|
|
*/
|
|
|
public Map<String,Object> currentMonthDrugUse(String patientCode,Integer useDrugStatus,String deviceSn) throws Exception{
|
|
|
|
|
|
Map<String,Object> resultMap = new HashMap<>();
|
|
|
Date monthStart = DateUtil.strToDate(DateUtil.getFristDayOfMonthThisDate(new Date()));
|
|
|
Date monthEnd = new Date();
|
|
|
Integer onTimeCount = kitDrugUseRecordDao.findStatisticsCount(patientCode,1,deviceSn,monthStart,monthEnd);//按时次数
|
|
|
Integer overTimeCount = kitDrugUseRecordDao.findStatisticsCount(patientCode,2,deviceSn,monthStart,monthEnd);//超时次数
|
|
|
Integer leakageCount = kitDrugUseRecordDao.findStatisticsCount(patientCode,3,deviceSn,monthStart,monthEnd);//漏用次数
|
|
|
Specification specification = new Specification<KitDrugUseRecord>() {
|
|
|
@Override
|
|
|
public Predicate toPredicate(Root<KitDrugUseRecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
|
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
predicates.add(criteriaBuilder.equal(root.get("patientCode"), patientCode));
|
|
|
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("useTime"), monthStart));
|
|
|
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("useTime"), monthEnd));
|
|
|
if(useDrugStatus!=null){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("useDrugStatus"), useDrugStatus));
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(deviceSn)){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("deviceSn"), deviceSn));
|
|
|
}
|
|
|
criteriaQuery.orderBy(criteriaBuilder.desc(root.get("useDrugStatus")),criteriaBuilder.desc(root.get("useTime")));//按时间降序排序
|
|
|
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));//条件
|
|
|
return criteriaQuery.getRestriction();
|
|
|
}
|
|
|
};
|
|
|
List<KitDrugUseRecord> list = kitDrugUseRecordDao.findAll(specification);
|
|
|
// if(useDrugStatus==null){
|
|
|
// list = kitDrugUseRecordDao.findStatistics(patientCode,deviceSn,monthStart,monthEnd);
|
|
|
// }else{
|
|
|
// list = kitDrugUseRecordDao.findStatistics(patientCode,useDrugStatus,deviceSn,monthStart,monthEnd);
|
|
|
// }
|
|
|
System.out.println("appId:"+UUID.randomUUID().toString());
|
|
|
System.out.println("appSecret:"+UUID.randomUUID().toString());
|
|
|
Map<String,Map<String,Object>> map = new LinkedHashMap<>();
|
|
|
|
|
|
for(KitDrugUseRecord one:list){
|
|
|
String dateStr = DateUtil.dateToStr(one.getUseTime(),"MM-dd");
|
|
|
Map<String,Object> oneMap = null;
|
|
|
if(map.containsKey(dateStr)){
|
|
|
oneMap = map.get(dateStr);
|
|
|
}else{
|
|
|
oneMap = new LinkedHashMap<>();
|
|
|
}
|
|
|
Integer useDrugStatusTemp = one.getUseDrugStatus();
|
|
|
if(oneMap.containsKey(useDrugStatusTemp.toString())){
|
|
|
oneMap.put(useDrugStatusTemp.toString(),((Integer)oneMap.get(useDrugStatusTemp.toString()))+1);
|
|
|
}else{
|
|
|
if(!oneMap.isEmpty()){
|
|
|
continue;
|
|
|
}
|
|
|
oneMap.put(useDrugStatusTemp.toString(),1);
|
|
|
}
|
|
|
map.put(dateStr,oneMap);
|
|
|
}
|
|
|
List<Map<String,Object>> resultList = new ArrayList<>();
|
|
|
for(String key:map.keySet()){
|
|
|
Map<String,Object> temp = new HashMap<>();
|
|
|
Map<String,Object> m = map.get(key);
|
|
|
temp.put("day",key);
|
|
|
for(String key2:m.keySet()){
|
|
|
temp.put("statusType",key2);
|
|
|
temp.put("count",m.get(key2));
|
|
|
}
|
|
|
resultList.add(temp);
|
|
|
}
|
|
|
//按用药日期排序
|
|
|
Collections.sort(resultList, new Comparator<Map<String,Object>>() {
|
|
|
@Override
|
|
|
public int compare(Map<String,Object> o1, Map<String,Object> o2) {
|
|
|
Long day1 = DateUtil.strToDate(o1.get("day").toString(), "MM-dd").getTime();
|
|
|
Long day2 = DateUtil.strToDate(o2.get("day").toString(), "MM-dd").getTime();
|
|
|
if(day1-day2>0){
|
|
|
return -1;
|
|
|
}else if(day1-day2<0){
|
|
|
return 1;
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
});
|
|
|
resultMap.put("onTimeCount",onTimeCount);//按时
|
|
|
resultMap.put("overTimeCount",overTimeCount);//超时
|
|
|
resultMap.put("leakageCount",leakageCount);//漏用
|
|
|
resultMap.put("resultList",resultList);
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 某天的用药记录
|
|
|
* @param patientCode
|
|
|
* @param useDrugStatus
|
|
|
* @param deviceSn
|
|
|
* @param date 某天的日期 格式:yyyy-MM-dd
|
|
|
*/
|
|
|
public List<Map<String,Object>> oneDayDrugUse(String patientCode,Integer useDrugStatus,String deviceSn,String date) throws Exception{
|
|
|
|
|
|
Date dayStart = DateUtil.strToDate(date+" 00:00:00");
|
|
|
Date dayEnd = DateUtil.strToDate(date+" 23:59:59");
|
|
|
Specification specification = new Specification<KitDrugUseRecord>() {
|
|
|
@Override
|
|
|
public Predicate toPredicate(Root<KitDrugUseRecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
|
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
|
predicates.add(criteriaBuilder.equal(root.get("patientCode"), patientCode));
|
|
|
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("useTime"), dayStart));
|
|
|
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("useTime"), dayEnd));
|
|
|
if(useDrugStatus!=null){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("useDrugStatus"), useDrugStatus));
|
|
|
}
|
|
|
if(!StringUtils.isEmpty(deviceSn)){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("deviceSn"), deviceSn));
|
|
|
}
|
|
|
criteriaQuery.orderBy(criteriaBuilder.asc(root.get("useTime")));//按时间降序排序
|
|
|
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));//条件
|
|
|
return criteriaQuery.getRestriction();
|
|
|
}
|
|
|
};
|
|
|
List<KitDrugUseRecord> list = kitDrugUseRecordDao.findAll(specification);
|
|
|
List<Map<String,Object>> resultList = new ArrayList<>();
|
|
|
for(KitDrugUseRecord one:list){
|
|
|
Map<String,Object> temp = new HashMap<>();
|
|
|
temp.put("useTime",DateUtil.dateToStr(one.getUseTime(),"HH:mm"));
|
|
|
temp.put("drugDetail",JSONArray.fromObject(one.getDrugDetail()));
|
|
|
resultList.add(temp);
|
|
|
}
|
|
|
return resultList;
|
|
|
}
|
|
|
|
|
|
}
|