|  | @ -0,0 +1,171 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.hospital.prescription.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.message.SystemMessageDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.hospital.prescription.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.message.dao.SystemMessageDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.hospital.prescription.dao.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.PageEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.utils.hibernate.HibenateUtils;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by yeshijie on 2022/11/24.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class DrugsPlanService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDrugsPlanDao drugsPlanDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDrugsPlanDetailDao drugsPlanDetailDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PrescriptionDao prescriptionDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PrescriptionInfoDao prescriptionInfoDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PrescriptionDiagnosisDao prescriptionDiagnosisDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HibenateUtils hibenateUtils;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private SystemMessageDao systemMessageDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民今日的服药计划
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<SystemMessageDO> findDrugsPlanMessage(String patient){
 | 
	
		
			
				|  |  |         return systemMessageDao.findByReceiverAndCreateTime(patient,"51",DateUtil.getStringDateShort()+" 00:00:00");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 手动生成用药计划
 | 
	
		
			
				|  |  |      * @param prescriptionId
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public void createDrugsPlan(String prescriptionId){
 | 
	
		
			
				|  |  |         WlyyPrescriptionDO outpatient = prescriptionDao.findById(prescriptionId).orElse(null);
 | 
	
		
			
				|  |  |         if(outpatient!=null){
 | 
	
		
			
				|  |  |             BaseDrugsPlanDO planDO = new BaseDrugsPlanDO();
 | 
	
		
			
				|  |  |             planDO.setDoctor(outpatient.getDoctor());
 | 
	
		
			
				|  |  |             planDO.setDoctorName(outpatient.getDoctorName());
 | 
	
		
			
				|  |  |             planDO.setPatient(outpatient.getPatientCode());
 | 
	
		
			
				|  |  |             planDO.setPatientName(outpatient.getPatientName());
 | 
	
		
			
				|  |  |             planDO.setRelationId(outpatient.getId());
 | 
	
		
			
				|  |  |             planDO.setTitle("");
 | 
	
		
			
				|  |  |             planDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             planDO.setType("1");
 | 
	
		
			
				|  |  |             drugsPlanDao.save(planDO);
 | 
	
		
			
				|  |  |             String planId = planDO.getId();
 | 
	
		
			
				|  |  |             List<WlyyPrescriptionInfoDO> infoDOList = prescriptionInfoDao.findByPrescriptionId(prescriptionId,1);
 | 
	
		
			
				|  |  |             if(infoDOList.size()>0){
 | 
	
		
			
				|  |  |                 List<BaseDrugsPlanDetailDO> detailDOS = new ArrayList<>();
 | 
	
		
			
				|  |  |                 for (WlyyPrescriptionInfoDO info:infoDOList){
 | 
	
		
			
				|  |  |                     BaseDrugsPlanDetailDO planDetailDO = new BaseDrugsPlanDetailDO();
 | 
	
		
			
				|  |  |                     BeanUtils.copyProperties(info,planDetailDO);
 | 
	
		
			
				|  |  |                     planDetailDO.setId(null);
 | 
	
		
			
				|  |  |                     planDetailDO.setPlanId(planId);
 | 
	
		
			
				|  |  |                     planDetailDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                     detailDOS.add(planDetailDO);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 drugsPlanDetailDao.saveAll(detailDOS);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             SystemMessageDO messageDO = new SystemMessageDO();
 | 
	
		
			
				|  |  |             messageDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             messageDO.setDel("1");
 | 
	
		
			
				|  |  |             messageDO.setType("51");
 | 
	
		
			
				|  |  |             messageDO.setOver("1");
 | 
	
		
			
				|  |  |             messageDO.setTitle("用药计划");
 | 
	
		
			
				|  |  |             messageDO.setIsRead("0");
 | 
	
		
			
				|  |  |             messageDO.setContent("您今日有1条待服药计划,请按时服药");
 | 
	
		
			
				|  |  |             messageDO.setReceiver(outpatient.getPatientCode());
 | 
	
		
			
				|  |  |             messageDO.setReceiverName(outpatient.getPatientName());
 | 
	
		
			
				|  |  |             messageDO.setSender(outpatient.getDoctor());
 | 
	
		
			
				|  |  |             messageDO.setSenderName(outpatient.getDoctorName());
 | 
	
		
			
				|  |  |             messageDO.setRelationCode(planId);
 | 
	
		
			
				|  |  |             systemMessageDao.save(messageDO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取居民的今日用药计划
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<BaseDrugsPlanDO> findDrugsPlan(String patient){
 | 
	
		
			
				|  |  |         String sql = "select * from base_drugs_plan where patient ='"+patient+"' and type=1 and create_time>='"+ DateUtil.getStringDateShort() +" 00:00:00'";
 | 
	
		
			
				|  |  |         List<BaseDrugsPlanDO> planDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(BaseDrugsPlanDO.class));
 | 
	
		
			
				|  |  |         for (BaseDrugsPlanDO planDO:planDOList){
 | 
	
		
			
				|  |  |             planDO.setDetailList(drugsPlanDetailDao.findByPlanId(planDO.getId()));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return planDOList;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查询居民处方列表
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public PageEnvelop prescriptionList(String patient, Integer page, Integer pagesize) {
 | 
	
		
			
				|  |  |         String sql = "SELECT\n" +
 | 
	
		
			
				|  |  |                 "\tp.doctor as \"doctor\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.id as \"id\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.doctor_name as \"doctorName\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.dept as \"dept\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.dept_name as \"deptName\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.patient_code as \"patientCode\",\n" +
 | 
	
		
			
				|  |  |                 "\tp.patient_name as \"patientName\",\n";
 | 
	
		
			
				|  |  |         sql = sql + "date_format(p.create_time,'%Y-%m-%d %H:%i:%S' )  AS \"createTime\" ";
 | 
	
		
			
				|  |  |         sql += "\tFROM\n" +
 | 
	
		
			
				|  |  |                 "\twlyy_prescription P\n" +
 | 
	
		
			
				|  |  |                 "WHERE\n" +
 | 
	
		
			
				|  |  |                 "\t1 = 1 and  p.check_status not in (3,4) ";
 | 
	
		
			
				|  |  |         Map<String, Object> params = new HashedMap();
 | 
	
		
			
				|  |  |         if (StringUtils.isNoneBlank(patient)) {
 | 
	
		
			
				|  |  |             sql += " and p.patient_code =:patient";
 | 
	
		
			
				|  |  |             params.put("patient", patient);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         sql += " order by p.create_time desc";
 | 
	
		
			
				|  |  |         List<Map<String, Object>> mapList = hibenateUtils.createSQLQuery(sql, params, page, pagesize);
 | 
	
		
			
				|  |  |         String sqlTotal = "select COUNT(1) AS \"total\" from wlyy_prescription p where 1=1 and  p.check_status not in (3,4)  ";
 | 
	
		
			
				|  |  |         Map<String, Object> params1 = new HashedMap();
 | 
	
		
			
				|  |  |         if (StringUtils.isNoneBlank(patient)) {
 | 
	
		
			
				|  |  |             sqlTotal += " and p.patient_code =:patient";
 | 
	
		
			
				|  |  |             params1.put("patient", patient);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Long count = 0L;
 | 
	
		
			
				|  |  |         List<Map<String, Object>> total = hibenateUtils.createSQLQuery(sqlTotal, params1);
 | 
	
		
			
				|  |  |         if (total != null) {
 | 
	
		
			
				|  |  |             //mysql 与 Oracle 聚合函数返回类型不一致,需要判断装换
 | 
	
		
			
				|  |  |             count = hibenateUtils.objTransformLong(total.get(0).get("total"));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for (Map<String,Object> map:mapList){
 | 
	
		
			
				|  |  |             String prescriptionId = map.get("id")+"";
 | 
	
		
			
				|  |  |             List<WlyyPrescriptionDiagnosisDO> diagnosisDOList = prescriptionDiagnosisDao.findByPrescriptionId(prescriptionId,1);
 | 
	
		
			
				|  |  |             List<WlyyPrescriptionInfoDO> infoDOList = prescriptionInfoDao.findByPrescriptionId(prescriptionId,1);
 | 
	
		
			
				|  |  |             map.put("diagnosisDOList",diagnosisDOList);
 | 
	
		
			
				|  |  |             map.put("infoDOList",infoDOList);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return PageEnvelop.getSuccessListWithPage("获取成功",mapList,page,pagesize,count);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |