Browse Source

Merge branch 'dev' of http://192.168.1.220:10080/Amoy2/wlyy2.0 into dev

LAPTOP-KB9HII50\70708 3 years ago
parent
commit
8cddf08494

+ 3 - 0
business/base-service/src/main/java/com/yihu/jw/doctor/dao/BaseDoctorDao.java

@ -53,4 +53,7 @@ public interface BaseDoctorDao extends PagingAndSortingRepository<BaseDoctorDO,
    @Query("from BaseDoctorDO d where d.idcard = ?1 AND d.del ='1'")
    BaseDoctorDO findByIdcardAndId(String idcard);
    @Query("SELECT d from BaseDoctorDO d,BaseTeamMemberDO te where d.id = te.doctorCode and te.del='1' AND d.del ='1' and te.teamCode in (?1) ")
    List<BaseDoctorDO> findDoctorsByTeam(List<String> teams);
}

+ 1 - 0
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/dao/sign/ArchiveDao.java

@ -3,6 +3,7 @@ package com.yihu.jw.care.dao.sign;
import com.yihu.jw.entity.care.archive.ArchiveDO;
import javafx.scene.shape.Arc;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

+ 15 - 11
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/endpoint/third/platForm/PatientInfoPlatFormEndpoint.java

@ -165,15 +165,15 @@ public class PatientInfoPlatFormEndpoint extends EnvelopRestEndpoint {
    @GetMapping(value = "getHealthIndexInfo")
    @ApiOperation("获取居民体征信息")
    public String getHealthIndexInfo(@ApiParam(name="patient",value="患者代码",defaultValue = "eb0b478fbe9245428ecf63cd7517206f")
                                             @RequestParam(value="patient",required = true) String patient,
                                      @ApiParam(name="type",value="指标类型",defaultValue = "1")
                                             @RequestParam(value="type",required = true) int type,
                                      @ApiParam(name="gi_type",value="就餐类型",defaultValue = "0")
                                             @RequestParam(value = "gi_type",required = false) int gi_type,
                                      @ApiParam(name="begin",value="开始时间",defaultValue = "2017-05-22 00:00:00")
                                             @RequestParam(value="begin",required = true) String begin,
                                      @ApiParam(name="end",value="结束时间",defaultValue = "2017-06-02 00:00:00")
                                             @RequestParam(value="end",required = true) String end){
                                     @RequestParam(value="patient",required = true) String patient,
                                     @ApiParam(name="type",value="指标类型",defaultValue = "1")
                                     @RequestParam(value="type",required = true) Integer type,
                                     @ApiParam(name="gi_type",value="就餐类型",defaultValue = "0")
                                     @RequestParam(value = "gi_type",required = false) Integer gi_type,
                                     @ApiParam(name="begin",value="开始时间",defaultValue = "2017-05-22 00:00:00")
                                     @RequestParam(value="begin",required = true) String begin,
                                     @ApiParam(name="end",value="结束时间",defaultValue = "2017-06-02 00:00:00")
                                     @RequestParam(value="end",required = true) String end){
        try {
            return baseController.write (200,"查询成功","data",patientInfoPlatFormService.getHealthIndexInfo(patient,type,gi_type,begin,end));
        }catch (Exception e){
@ -289,11 +289,13 @@ public class PatientInfoPlatFormEndpoint extends EnvelopRestEndpoint {
    @ApiOperation(value = "安防监测列表 v0.7.0")//        疑似离开安全区域+疑似跌倒
    public PageEnvelop getEmergencyWarnOrderListNew(@ApiParam(name="page",value = "page")
                                                 @RequestParam(value = "page") Integer page,
                                                    @ApiParam(name="patient",value = "patient")
                                                    @RequestParam(value = "patient",required = false) String patient,
                                                 @ApiParam(name="pageSize",value = "pageSize")
                                                 @RequestParam(value = "pageSize") Integer pageSize
    ){
        try {
            return patientInfoPlatFormService.getEmergencyWarnOrderListNew(page,pageSize);
            return patientInfoPlatFormService.getEmergencyWarnOrderListNew(page,pageSize,patient);
        }catch (Exception e){
            return failedPageEnvelopException2(e);
        }
@ -303,11 +305,13 @@ public class PatientInfoPlatFormEndpoint extends EnvelopRestEndpoint {
    @ApiOperation(value = "紧急预警列表 v0.7.0")//        发起紧急救助
    public PageEnvelop getEmergencyWarningListNew(@ApiParam(name="page",value = "page")
                                                    @RequestParam(value = "page") Integer page,
                                                  @ApiParam(name="patient",value = "patient")
                                                  @RequestParam(value = "patient",required = false) String patient,
                                                    @ApiParam(name="pageSize",value = "pageSize")
                                                    @RequestParam(value = "pageSize") Integer pageSize
    ){
        try {
            return patientInfoPlatFormService.getEmergencyWarningListNew(page,pageSize);
            return patientInfoPlatFormService.getEmergencyWarningListNew(page,pageSize,patient);
        }catch (Exception e){
            return failedPageEnvelopException2(e);
        }

+ 135 - 7
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/service/device/PatientDeviceService.java

@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yihu.jw.care.config.AqgConfig;
import com.yihu.jw.care.dao.device.*;
import com.yihu.jw.care.dao.security.BaseEmergencyWarnLogDao;
import com.yihu.jw.care.dao.security.SecurityMonitoringOrderDao;
import com.yihu.jw.care.service.assistance.EmergencyAssistanceService;
import com.yihu.jw.care.service.contacts.ContactsService;
import com.yihu.jw.care.service.security.SecurityMonitoringOrderService;
@ -16,6 +18,8 @@ import com.yihu.jw.doctor.dao.BaseDoctorDao;
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.care.device.*;
import com.yihu.jw.entity.care.securitymonitoring.BaseEmergencyWarnLogDO;
import com.yihu.jw.entity.care.securitymonitoring.SecurityMonitoringOrderDO;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.restmodel.ResponseContant;
import com.yihu.jw.util.common.GpsUtil;
@ -95,6 +99,9 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa
    @Autowired
    WeiXinAccessTokenUtils tokenUtils;
    @Autowired
    private BaseEmergencyWarnLogDao logDao;
    //健康问题 高血压
    private static final String gxy = "HP0093";
    //健康问题 糖尿病
@ -116,6 +123,8 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa
    private CountDistance countDistance;
    @Autowired
    private PatientSafeAreaDao safeAreaDao;
    @Autowired
    private SecurityMonitoringOrderDao securityMonitoringOrderDao;
    @PostConstruct
    public void init() {
@ -971,7 +980,7 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa
                        ")A ";
                Long orderCount = jdbcTemplate.queryForObject(sql,Long.class);
                devInfo.put("errorCount",orderCount);//         //异常量
            }else if (2==device.getDeviceType()){//健康设备
            }else if (0==device.getDeviceType()){//健康设备
                sql = " select count(id) from wlyy_patient_health_index where device_sn ='"+deviceSn+"' ";
                Long dataCount = jdbcTemplate.queryForObject(sql,Long.class);
                devInfo.put("dataCount",dataCount);//异常量
@ -1371,18 +1380,20 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa
        if (StringUtils.isBlank(type)||"2".equals(type)||"0".equals(type)){
            String sqlCount = "select SUM(total) from( \n" +
                    "select count(ord.id) as total from base_emergency_assistance_order ord  where ord.device_sn='"+deviceSn+"' " +
                    "select count(ord.id) as total from base_emergency_assistance_order ord INNER JOIN base_patient p on ord.patient = p.id   where ord.device_sn='"+deviceSn+"' " +
                    "  {{patient}} " +
                    "UNION All " +
                    "select count(ord.id)  as total from base_security_monitoring_order ord  where ord.device_sn='"+deviceSn+"' " +
                    "select count(ord.id)  as total from base_security_monitoring_order ord INNER JOIN base_patient p on ord.patient = p.id   where ord.device_sn='"+deviceSn+"' " +
                    " {{patient}} )A ";
            String sql = "select '20' as OrderType,ord.id,ord.patient,ord.patient_name,ord.doctor,ord.status,ord.doctor_name," +
                    "'紧急呼叫' as serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time " +
                    "from base_emergency_assistance_order ord  where ord.device_sn='"+deviceSn+"' {{patient}} " +
                    "'紧急呼叫' as serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time ,case p.sex when 1 then '男' when 2 then '女' else p.sex end as sex," +
                    " TIMESTAMPDIFF(YEAR,p.birthday,now()) as age " +
                    "from base_emergency_assistance_order ord  INNER JOIN base_patient p on ord.patient = p.id  where ord.device_sn='"+deviceSn+"' {{patient}} " +
                    "UNION " +
                    "select  '22' as 'OrderType',ord.id,ord.patient,ord.patient_name,ord.doctor,ord.status,ord.doctor_name," +
                    "ord.serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time from base_security_monitoring_order ord " +
                    " where ord.device_sn='"+deviceSn+"' {{patient}}  order by create_time desc limit "+page*pageSize+","+pageSize;
                    "ord.serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time ,case p.sex when 1 then '男' when 2 then '女' else p.sex end as sex, " +
                    "TIMESTAMPDIFF(YEAR,p.birthday,now()) as age from base_security_monitoring_order ord " +
                    " INNER JOIN base_patient p on ord.patient = p.id  where ord.device_sn='"+deviceSn+"' {{patient}}  order by create_time desc limit "+page*pageSize+","+pageSize;
            if (StringUtils.isNotBlank(patient)){
                sqlCount = sqlCount.replace("{{patient}}","and ord.patient='"+patient+"' ");
                sql = sql.replace("{{patient}}","and ord.patient='"+patient+"' ");
@ -1394,7 +1405,124 @@ public class PatientDeviceService extends BaseJpaService<DevicePatientDevice, Pa
            List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
            for (Map<String,Object> map:list){
                //添加工单响应时间、动态
                String orderId = map.get("id").toString();
                String orderType = map.get("OrderType").toString();
                List<BaseEmergencyWarnLogDO> logDOS = logDao.findByOrderIdOrderByCreateTimeDesc(orderId);
                for (BaseEmergencyWarnLogDO tmp:logDOS){
                    tmp.setTimeInfo(DateUtil.getDifferentTimeInfo(new Date(),tmp.getCreateTime(),0));
                }
                map.put("orderLog",logDOS);
                // (起床预警时长3 午睡预警时长5 起夜预警时长2 详情见设备项目睡眠带发起时定义) (离开安全区域超出距离km) (燃气烟雾 报警时浓度)
                if ("22".equals(orderType)){
                    JSONObject info = new JSONObject();
                    SecurityMonitoringOrderDO orderDO = securityMonitoringOrderDao.findOne(orderId);
                    if ("疑似离开安全区域".equals(orderDO.getServeDesc())){
                        //超出距离
                        info.put("info1","超出距离: 1m");//todo
                    }
                    if ("超时未起床".equals(orderDO.getServeDesc())){
                        List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                        if (sleepPlan.size()>0){
                            info.put("info1","预设起床时间: "+sleepPlan.get(0).getGetUpTime());
                        }
                        info.put("info2","超时时长: 3小时");
                    }
                    if ("午睡超时未起床".equals(orderDO.getServeDesc())){
                        if (null!=orderDO.getWarnInfo()) {
                            if (org.apache.commons.lang.StringUtils.isNotBlank(orderDO.getWarnInfo())){
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                                if (jsonObject.containsKey("siestaTimeBegin") && null != jsonObject.get("siestaTimeBegin")) {
                                    String siestaTimeBegin = jsonObject.getString("siestaTimeBegin");
                                    info.put("info1", "预计午休起床时间: " +DateUtil.getPreTime(siestaTimeBegin,60*5+""));
                                }
                            }
                        }
                        info.put("info2","超时时长: 5小时");
                    }
                    if ("起夜超时未回床".equals(orderDO.getServeDesc())){
                        if (null!=orderDO.getWarnInfo()){
                            if (org.apache.commons.lang.StringUtils.isNotBlank(orderDO.getWarnInfo())){
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                                if (jsonObject.containsKey("sleepNightRecordList")&&null!=jsonObject.get("sleepNightRecordList")){
                                    com.alibaba.fastjson.JSONArray nightArr = jsonObject.getJSONArray("sleepNightRecordList");
                                    if (nightArr.size()>0){
                                        com.alibaba.fastjson.JSONObject tmp = nightArr.getJSONObject(0);
                                        String time = tmp.getString("createTime");
                                        info.put("info1","离床时间: "+time);
                                    }
                                }
                            }
                        }
                        info.put("info2","离床时长: 2小时");
                    }
                    if ("超时未上床晚休".equals(orderDO.getServeDesc())){
                        List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                        if (sleepPlan.size()>0){
                            info.put("info1","预设晚休时间: "+sleepPlan.get(0).getNightRestTime());
                        }
                        info.put("info2","超时时长: 2小时");
                    }
                    if ("超时未午睡".equals(orderDO.getServeDesc())){
                        List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                        if (sleepPlan.size()>0){
                            info.put("info1","预设午休时间: "+sleepPlan.get(0).getNapTime());
                        }
                        info.put("info2","5小时");
                    }
                    if ("疑似发生火灾".equals(orderDO.getServeDesc())){
                        //烟雾浓度
                        if (null!=orderDO.getWarnInfo()){
                            if (org.apache.commons.lang.StringUtils.isNotBlank(orderDO.getWarnInfo())){
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                                if (jsonObject.containsKey("smoke")&&null!=jsonObject.get("smoke")){
                                    String value = jsonObject.getString("smoke");
                                    if (!value.contains("%")){
                                        value+="%";
                                    }
                                    info.put("info1","烟雾浓度: "+value);
                                }
                            }
                        }
                    }
                    if ("疑似燃气泄漏".equals(orderDO.getServeDesc())){
                        //燃气浓度
                        if (null!=orderDO.getWarnInfo()) {
                            if (org.apache.commons.lang.StringUtils.isNotBlank(orderDO.getWarnInfo())){
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                                if (jsonObject.containsKey("gas")&&null!=jsonObject.get("gas")){
                                    String value = jsonObject.getString("gas");
                                    if (!value.contains("%")) {
                                        value += "%";
                                    }
                                    info.put("info1", "燃气浓度: "+value);
                                }
                            }
                        }
                    }
                    if("心率和呼吸频率异常".equals(orderDO.getServeDesc())){
                        if (null!=orderDO.getWarnInfo()){
                            if (org.apache.commons.lang.StringUtils.isNotBlank(orderDO.getWarnInfo())){
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                                if(null!=jsonObject.get("breath")){
                                    String breath = jsonObject.getString("breath");
                                    if (org.apache.commons.lang3.StringUtils.isNotBlank(breath)){
                                        info.put("info1", "预警频率 "+breath+"次/分钟");
                                    }
                                }
                                if(null!=jsonObject.get("heartRate")){
                                    String heartRate = jsonObject.getString("heartRate");
                                    if (org.apache.commons.lang3.StringUtils.isNotBlank(heartRate)){
                                        info.put("info2", "预警心率: "+heartRate+"次/分钟");
                                    }
                                }
                            }
                        }
                    }
                    map.put("infos",info);
                }
            }
            String str = JSON.toJSONString(list,SerializerFeature.WriteMapNullValue);

+ 17 - 1
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/service/security/SecurityMonitoringOrderService.java

@ -162,6 +162,18 @@ public class SecurityMonitoringOrderService extends BaseJpaService<SecurityMonit
            logger.error(failMsg);
            return result;
        }
        //V1.4 同一类型的安防工单 距离上次发起小于6小时不可重复发起
        String sql = " select count(id) from base_security_monitoring_order where `status`=1  " +
                "  and TIMESTAMPDIFF(HOUR,create_time,NOW())<=6 " +
                " and patient='"+orderDO.getPatient()+"' and serve_desc ='"+orderDO.getServeDesc()+"' ";
        Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
        if (count>0){
            result.put("resultFlag", 0);
            String failMsg = "居民六小时内已存在该预警类型工单,无法重复发起";
            result.put("resultMsg", failMsg);
            logger.error(failMsg);
            return result;
        }
        orderDO.setStatus(1);
        orderDO.setNumber(getRandomIntStr());
@ -189,7 +201,7 @@ public class SecurityMonitoringOrderService extends BaseJpaService<SecurityMonit
        }
        //签约防走失服务包
        String sql ="SELECT i.code,r.team_code,pack.org_code,pack.org_name from base_service_package_sign_record sr,base_service_package_record r, base_service_package_item i,base_service_package pack    \n" +
        sql ="SELECT i.code,r.team_code,pack.org_code,pack.org_name from base_service_package_sign_record sr,base_service_package_record r, base_service_package_item i,base_service_package pack    \n" +
                "where  sr.id = r.sign_id and sr.status=1 and r.service_package_id = i.service_package_id and r.service_package_id = pack.id  and i.del = 1 and sr.`status`=1 \n" +
                "and  sr.patient  = '"+orderDO.getPatient()+"' and i.code='preventLost' ";
        List<Map<String,Object>> items = jdbcTemplate.queryForList(sql);
@ -326,6 +338,10 @@ public class SecurityMonitoringOrderService extends BaseJpaService<SecurityMonit
        for (BaseEmergencyWarnLogDO tmp:logDOS){
            tmp.setTimeInfo(DateUtil.getDifferentTimeInfo(new Date(),tmp.getCreateTime(),0));
        }
        if (logDOS.size()==0&&1==emergencyOrderVO.getStatus()){//未响应
            emergencyOrderVO.setOverTimeNotResponse(DateUtil.getDifferentTimeInfo1(new Date(),orderDO.getCreateTime()));
        }
        emergencyOrderVO.setOrderLogInfo(logDOS);
        List<PatientSosContactsDO> contactsDOS = contactsService.getPatientSosContacts(orderDO.getPatient());
        emergencyOrderVO.setSosContacts(contactsDOS);

+ 21 - 5
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/service/sign/ServicePackageService.java

@ -389,6 +389,7 @@ public class ServicePackageService extends BaseJpaService<ServicePackageDO, Serv
        signRecordDO.setName(patientDO.getName());
        List<ServicePackageRecordDO> recordDOList = signRecordDO.getRecordDOList();
        List<String> packages = recordDOList.stream().map(ServicePackageRecordDO::getServicePackageId).collect(Collectors.toList());
        List<String> teams = recordDOList.stream().map(ServicePackageRecordDO::getTeamCode).collect(Collectors.toList());
        if (packages.size()>0){
            String packagesId = packages.stream().map(String::valueOf).collect(Collectors.joining("','"));
            String sql  = " SELECT  DISTINCT i.org_code FROM  base_service_package_sign_record sr, " +
@ -426,11 +427,26 @@ public class ServicePackageService extends BaseJpaService<ServicePackageDO, Serv
                signId = signRecordDO.getId();
                //建档状态修改
                ArchiveDO archiveDO = archiveDao.findByPatientAndDoctorCode(patientId,doctorId);
                if(archiveDO!=null){
                    archiveDO.setSignStatus(1);
                    archiveDao.save(archiveDO);
                List<BaseDoctorDO> doctorDOS = doctorDao.findDoctorsByTeam(teams);
                List<ArchiveDO> saveList = new ArrayList<>();
                for (BaseDoctorDO tmp:doctorDOS){
                    ArchiveDO archiveDO = archiveDao.findByPatientAndDoctorCode(patientId,doctorId);
                    if(archiveDO!=null){
                        archiveDO.setSignStatus(1);
                    }else{
                        archiveDO = new ArchiveDO();
                        archiveDO.setCreateTime(new Date());
                        archiveDO.setArchiveOperatorName(tmp.getName());
                        archiveDO.setPatient(patientId);
                        archiveDO.setSickName(patientDO.getName());
                        archiveDO.setIdcard(patientDO.getIdcard());
                        archiveDO.setSignStatus(1);
                    }
                    saveList.add(archiveDO);
                }
                archiveDao.save(saveList);
//            }else{
//                //不能重复签约
//                result.put(ResponseContant.resultFlag, ResponseContant.fail);
@ -626,7 +642,7 @@ public class ServicePackageService extends BaseJpaService<ServicePackageDO, Serv
        if (StringUtils.isNotBlank(doctorLevel)){
         sql += " and doc.doctor_level= "+doctorLevel;
        }
        sql += "group by doc.id";
        sql += " group by doc.id ";
        List<Map<String,Object>>result = jdbcTemplate.queryForList(sql);
        if ("1".equals(onlineFlag)){//展示是否在线
            String listStr = imUtil.getOnlineListByType("helper");

+ 152 - 6
svr/svr-cloud-care/src/main/java/com/yihu/jw/care/service/third/PatientInfoPlatFormService.java

@ -1,8 +1,12 @@
package com.yihu.jw.care.service.third;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.care.dao.device.BaseSleepPlanDao;
import com.yihu.jw.care.dao.label.BaseCapacityLabelDao;
import com.yihu.jw.care.dao.label.WlyyPatientLabelDao;
import com.yihu.jw.care.dao.security.BaseEmergencyWarnLogDao;
import com.yihu.jw.care.dao.security.SecurityMonitoringOrderDao;
import com.yihu.jw.care.service.course.CourseService;
import com.yihu.jw.care.service.device.PatientDeviceService;
import com.yihu.jw.care.service.device.PatientHealthIndexService;
@ -12,12 +16,16 @@ import com.yihu.jw.care.service.security.SecurityMonitoringOrderService;
import com.yihu.jw.care.service.sign.CapacityAssessmentRecordService;
import com.yihu.jw.care.service.sign.ServicePackageService;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.care.device.BaseSleepPlan;
import com.yihu.jw.entity.care.label.BaseCapacityLabelDO;
import com.yihu.jw.entity.care.label.WlyyPatientLabelDO;
import com.yihu.jw.entity.care.securitymonitoring.BaseEmergencyWarnLogDO;
import com.yihu.jw.entity.care.securitymonitoring.SecurityMonitoringOrderDO;
import com.yihu.jw.entity.care.sign.CapacityAssessmentRecordDO;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.util.common.IdCardUtil;
import com.yihu.jw.util.date.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
@ -25,6 +33,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -59,6 +68,12 @@ public class PatientInfoPlatFormService {
    private CapacityAssessmentRecordService capacityAssessmentRecordService;
    @Autowired
    private BaseCapacityLabelDao capacityLabelDao;
    @Autowired
    private BaseEmergencyWarnLogDao logDao;
    @Autowired
    private SecurityMonitoringOrderDao securityMonitoringOrderDao;
    @Autowired
    private BaseSleepPlanDao sleepPlanDao;
    /**
@ -253,22 +268,153 @@ public class PatientInfoPlatFormService {
        return PageEnvelop.getSuccessListWithPage("查询成功",list,page,pageSize,count);
    }
    public PageEnvelop getEmergencyWarnOrderListNew(Integer page,Integer pageSize){
    public PageEnvelop getEmergencyWarnOrderListNew(Integer page,Integer pageSize,String patient){
        page = page>0?page-1:0;
        String sql = " select  '22' as 'OrderType',p.archive_type archiveType,p.residential_area residentialArea,ord.id,ord.patient,p.name,ord.serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time,ord.`status` \n" +
                "from base_security_monitoring_order ord INNER JOIN base_patient p on ord.patient = p.id order by create_time desc ";
        String sql = " select  '22' as 'OrderType',p.archive_type archiveType,p.residential_area residentialArea,ord.id,ord.patient,p.name,ord.serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time," +
                "ord.`status`,case p.sex when 1 then '男' when 2 then '女' else p.sex end as sex,TIMESTAMPDIFF(YEAR,p.birthday,now()) as age \n" +
                "from base_security_monitoring_order ord INNER JOIN base_patient p on ord.patient = p.id " ;
        if (StringUtils.isNotBlank(patient)){
            sql +=" and ord.patient='"+patient+"' ";
        }
        sql+= " order by create_time desc ";
        String countSql = "select count(id) from ("+sql+")A ";
        long count = jdbcTemplate.queryForObject(countSql,long.class);
        sql +=" limit "+page*pageSize+","+pageSize;
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String,Object> map:list){
            //添加工单响应时间、动态
            String orderId = map.get("id").toString();
            String orderType = map.get("OrderType").toString();
            List<BaseEmergencyWarnLogDO> logDOS = logDao.findByOrderIdOrderByCreateTimeDesc(orderId);
            for (BaseEmergencyWarnLogDO tmp:logDOS){
                tmp.setTimeInfo(DateUtil.getDifferentTimeInfo(new Date(),tmp.getCreateTime(),0));
            }
            map.put("orderLog",logDOS);
            // (起床预警时长3 午睡预警时长5 起夜预警时长2 详情见设备项目睡眠带发起时定义) (离开安全区域超出距离km) (燃气烟雾 报警时浓度)
            if ("22".equals(orderType)){
                JSONObject info = new JSONObject();
                SecurityMonitoringOrderDO orderDO = securityMonitoringOrderDao.findOne(orderId);
                if ("疑似离开安全区域".equals(orderDO.getServeDesc())){
                    //超出距离
                    info.put("info1","超出距离: 1m");//todo
                }
                if ("超时未起床".equals(orderDO.getServeDesc())){
                    List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                    if (sleepPlan.size()>0){
                        info.put("info1","预设起床时间: "+sleepPlan.get(0).getGetUpTime());
                    }
                    info.put("info2","超时时长: 3小时");
                }
                if ("午睡超时未起床".equals(orderDO.getServeDesc())){
                    if (null!=orderDO.getWarnInfo()) {
                        if (StringUtils.isNotBlank(orderDO.getWarnInfo())){
                            com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                            if (jsonObject.containsKey("siestaTimeBegin") && null != jsonObject.get("siestaTimeBegin")) {
                                String siestaTimeBegin = jsonObject.getString("siestaTimeBegin");
                                info.put("info1", "预计午休起床时间: " +DateUtil.getPreTime(siestaTimeBegin,60*5+""));
                            }
                        }
                    }
                    info.put("info2","超时时长: 5小时");
                }
                if ("起夜超时未回床".equals(orderDO.getServeDesc())){
                    if (null!=orderDO.getWarnInfo()){
                        if (StringUtils.isNotBlank(orderDO.getWarnInfo())){
                            com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                            if (jsonObject.containsKey("sleepNightRecordList")&&null!=jsonObject.get("sleepNightRecordList")){
                                com.alibaba.fastjson.JSONArray nightArr = jsonObject.getJSONArray("sleepNightRecordList");
                                if (nightArr.size()>0){
                                    com.alibaba.fastjson.JSONObject tmp = nightArr.getJSONObject(0);
                                    String time = tmp.getString("createTime");
                                    info.put("info1","离床时间: "+time);
                                }
                            }
                        }
                    }
                    info.put("info2","离床时长: 2小时");
                }
                if ("超时未上床晚休".equals(orderDO.getServeDesc())){
                    List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                    if (sleepPlan.size()>0){
                        info.put("info1","预设晚休时间: "+sleepPlan.get(0).getNightRestTime());
                    }
                    info.put("info2","超时时长: 2小时");
                }
                if ("超时未午睡".equals(orderDO.getServeDesc())){
                    List<BaseSleepPlan> sleepPlan = sleepPlanDao.findByPatient(orderDO.getPatient());
                    if (sleepPlan.size()>0){
                        info.put("info1","预设午休时间: "+sleepPlan.get(0).getNapTime());
                    }
                    info.put("info2","5小时");
                }
                if ("疑似发生火灾".equals(orderDO.getServeDesc())){
                    //烟雾浓度
                    if (null!=orderDO.getWarnInfo()){
                        if (StringUtils.isNotBlank(orderDO.getWarnInfo())){
                            com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                            if (jsonObject.containsKey("smoke")&&null!=jsonObject.get("smoke")){
                                String value = jsonObject.getString("smoke");
                                if (!value.contains("%")){
                                    value+="%";
                                }
                                info.put("info1","烟雾浓度: "+value);
                            }
                        }
                    }
                }
                if ("疑似燃气泄漏".equals(orderDO.getServeDesc())){
                    //燃气浓度
                    if (null!=orderDO.getWarnInfo()) {
                        if (StringUtils.isNotBlank(orderDO.getWarnInfo())){
                            com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                            if (jsonObject.containsKey("gas")&&null!=jsonObject.get("gas")){
                                String value = jsonObject.getString("gas");
                                if (!value.contains("%")) {
                                    value += "%";
                                }
                                info.put("info1", "燃气浓度: "+value);
                            }
                        }
                    }
                }
                if("心率和呼吸频率异常".equals(orderDO.getServeDesc())){
                    if (null!=orderDO.getWarnInfo()){
                        if (StringUtils.isNotBlank(orderDO.getWarnInfo())){
                            com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(orderDO.getWarnInfo());
                            if(null!=jsonObject.get("breath")){
                                String breath = jsonObject.getString("breath");
                                if (org.apache.commons.lang3.StringUtils.isNotBlank(breath)){
                                    info.put("info1", "预警频率 "+breath+"次/分钟");
                                }
                            }
                            if(null!=jsonObject.get("heartRate")){
                                String heartRate = jsonObject.getString("heartRate");
                                if (org.apache.commons.lang3.StringUtils.isNotBlank(heartRate)){
                                    info.put("info2", "预警心率: "+heartRate+"次/分钟");
                                }
                            }
                        }
                    }
                }
                map.put("infos",info);
            }
        }
        return PageEnvelop.getSuccessListWithPage("查询成功",list,page,pageSize,count);
    }
    public PageEnvelop getEmergencyWarningListNew(Integer page,Integer pageSize){
    public PageEnvelop getEmergencyWarningListNew(Integer page,Integer pageSize,String patient){
        page = page>0?page-1:0;
        String sql = " select '20' as OrderType,p.archive_type archiveType,p.residential_area residentialArea,ord.id,ord.patient, " +
                "p.name,'紧急呼叫' as serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time,ord.`status` \n" +
                "from base_emergency_assistance_order ord INNER JOIN base_patient p on ord.patient = p.id order by create_time desc ";
                "p.name,'紧急呼叫' as serve_desc,DATE_FORMAT(ord.create_time,'%Y-%m-%d %H:%i:%S') create_time,ord.`status`," +
                " case p.sex when 1 then '男' when 2 then '女' else p.sex end as sex,TIMESTAMPDIFF(YEAR,p.birthday,now()) as age \n" +
                "from base_emergency_assistance_order ord INNER JOIN base_patient p on ord.patient = p.id " ;
        if (StringUtils.isNotBlank(patient)){
            sql +=" and ord.patient='"+patient+"' ";
        }
        sql+=  " order by create_time desc ";
        String countSql = "select count(id) from ("+sql+")A ";
        long count = jdbcTemplate.queryForObject(countSql,long.class);
        sql +=" limit "+page*pageSize+","+pageSize;