|  | @ -0,0 +1,512 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.lifeCare;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.lifeCare.LifeCareCancelLogDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.lifeCare.LifeCareFeeDetailDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.lifeCare.LifeCareItemDictDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.lifeCare.LifeCareOrderDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.MessageUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorHospitalDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.patient.BasePatientDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.lifeCare.LifeCareCancelLogDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.lifeCare.LifeCareFeeDetailDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.lifeCare.LifeCareItemDictDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.lifeCare.LifeCareOrderDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.ResponseContant;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.IdCardUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.entity.EntityUtils;
 | 
	
		
			
				|  |  | import com.yihu.jw.wechat.dao.BasePatientWechatDao;
 | 
	
		
			
				|  |  | import com.yihu.mysql.query.BaseJpaService;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | 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.math.BigDecimal;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by yeshijie on 2021/3/26.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class LifeCareOrderService extends BaseJpaService<LifeCareOrderDO, LifeCareOrderDao> {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private Logger logger = LoggerFactory.getLogger(LifeCareOrderService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private LifeCareOrderDao lifeCareOrderDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private LifeCareFeeDetailDao lifeCareFeeDetailDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private LifeCareItemDictDao lifeCareItemDictDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private MessageUtil messageUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private LifeCareCancelLogDao lifeCareCancelLogDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientDao patientDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BasePatientWechatDao basePatientWechatDao;
 | 
	
		
			
				|  |  |     @Value("${wechat.id}")
 | 
	
		
			
				|  |  |     private String wxId;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorHospitalDao doctorHospitalDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 记录完成情况
 | 
	
		
			
				|  |  |      * @param orderId
 | 
	
		
			
				|  |  |      * @param complereImgs
 | 
	
		
			
				|  |  |      * @param completeRemark
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void completeOrder(String orderId,String complereImgs,String completeRemark){
 | 
	
		
			
				|  |  |         LifeCareOrderDO lifeCareOrderDO = lifeCareOrderDao.findOne(orderId);
 | 
	
		
			
				|  |  |         if(lifeCareOrderDO.getStatus().equals(LifeCareOrderDO.Status.waitForAccept.getType())){
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setStatus(2);
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setCompleteImgs(complereImgs);
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setCompleteRemark(completeRemark);
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setCompleteTime(new Date());
 | 
	
		
			
				|  |  |             lifeCareOrderDao.save(lifeCareOrderDO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据id获取服务工单信息
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public LifeCareOrderDO getServiceOrderById(String id) throws Exception {
 | 
	
		
			
				|  |  |         LifeCareOrderDO lifeCareOrderDO = lifeCareOrderDao.findOne(id);
 | 
	
		
			
				|  |  |         if (null == lifeCareOrderDO) {
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         BasePatientDO patient = patientDao.findById(lifeCareOrderDO.getPatient());
 | 
	
		
			
				|  |  |         if (patient != null) {
 | 
	
		
			
				|  |  |             String sex = IdCardUtil.getSexForIdcard_new(patient.getIdcard());
 | 
	
		
			
				|  |  |             int age = IdCardUtil.getAgeByIdcardOrBirthday(patient.getIdcard(),patient.getBirthday());
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setSex("1".equals(sex) ? "男" : "2".equals(sex) ? "女" : "未知");
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setAge(age);
 | 
	
		
			
				|  |  |             lifeCareOrderDO.setPhoto(patient.getPhoto());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         // 获取服务次数
 | 
	
		
			
				|  |  |         lifeCareOrderDO.setFeeDetailList(getFeeDetails(id));
 | 
	
		
			
				|  |  |         return lifeCareOrderDO;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据工单id获取服务工单-服务项价格
 | 
	
		
			
				|  |  |      * @param orderId
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<LifeCareFeeDetailDO> getFeeDetails(String orderId) {
 | 
	
		
			
				|  |  |         String sql = "SELECT * from base_life_care_fee_detail d where order_id='"+orderId+"' ";
 | 
	
		
			
				|  |  |         List<LifeCareFeeDetailDO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LifeCareFeeDetailDO.class));
 | 
	
		
			
				|  |  |         return list;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 顶部状态栏订单各分类总条数
 | 
	
		
			
				|  |  |      * @param doctor
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Map<String, Integer> getNumGroupByStatus(String doctor) {
 | 
	
		
			
				|  |  |         List<BaseDoctorHospitalDO> doctorHospitalDOs = doctorHospitalDao.findByDoctorCode(doctor);
 | 
	
		
			
				|  |  |         String hospital = doctorHospitalDOs.get(0).getOrgCode();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sql = "SELECT a.status, COUNT(DISTINCT a.id) as num FROM base_life_care_order a " ;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         sql +=  "  WHERE  a.hospital = ? group BY a.status";
 | 
	
		
			
				|  |  |         List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, hospital);
 | 
	
		
			
				|  |  |         Map<String, Integer> map = new HashMap<>();
 | 
	
		
			
				|  |  |         //状态 待服务 1、已完成 2 、已取消 -1
 | 
	
		
			
				|  |  |         map.put("1",0);
 | 
	
		
			
				|  |  |         map.put("2",0);
 | 
	
		
			
				|  |  |         map.put("-1",0);
 | 
	
		
			
				|  |  |         int total = 0;
 | 
	
		
			
				|  |  |         for (Map<String, Object> one:list){
 | 
	
		
			
				|  |  |             map.put(String.valueOf(one.get("status")), Integer.valueOf(String.valueOf(one.get("num"))));
 | 
	
		
			
				|  |  |             total+=Integer.valueOf(String.valueOf(one.get("num")));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         map.put("total", total);
 | 
	
		
			
				|  |  |         return map;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 医生/助老员-查询-工单列表
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject queryBriefList(String doctorCode,String name,String phone,Integer status,int page, int size) {
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |         List<BaseDoctorHospitalDO> doctorHospitalDOs = doctorHospitalDao.findByDoctorCode(doctorCode);
 | 
	
		
			
				|  |  |         String hospital = doctorHospitalDOs.get(0).getOrgCode();
 | 
	
		
			
				|  |  |         name = null == name ? "" : name;
 | 
	
		
			
				|  |  |         phone = null == phone ? "" : phone;
 | 
	
		
			
				|  |  |         status = null == status ? -100 : status;
 | 
	
		
			
				|  |  |         int start = 0 == page ? page++ : (page - 1) * size;
 | 
	
		
			
				|  |  |         int end = 0 == size ? 15 : page * size;
 | 
	
		
			
				|  |  |         StringBuffer buffer = new StringBuffer();
 | 
	
		
			
				|  |  |         if (StringUtils.isNoneBlank(name)){
 | 
	
		
			
				|  |  |             buffer.append(" AND (o.`patient_name` like '%"+name+"%' or p.idcard like '%"+name+"%')");
 | 
	
		
			
				|  |  |         }else if (StringUtils.isNoneBlank(phone)){
 | 
	
		
			
				|  |  |             buffer.append(" AND o.`proxy_patient_phone` like '%"+phone+"%'");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sql = "SELECT " +
 | 
	
		
			
				|  |  |                 "  p.name AS patientName, " +
 | 
	
		
			
				|  |  |                 "  p.photo AS photo, " +
 | 
	
		
			
				|  |  |                 "  case p.sex  " +
 | 
	
		
			
				|  |  |                 "  when 1 then '男'  " +
 | 
	
		
			
				|  |  |                 "  when 2 then '女' " +
 | 
	
		
			
				|  |  |                 "  end AS sex, " +
 | 
	
		
			
				|  |  |                 "  TIMESTAMPDIFF(year,p.birthday,NOW()) AS age," +
 | 
	
		
			
				|  |  |                 "  o.id as orderId, " +
 | 
	
		
			
				|  |  |                 "  o.patient_phone as phone, " +
 | 
	
		
			
				|  |  |                 "  o.proxy_patient as proxyPatient, " +
 | 
	
		
			
				|  |  |                 "  o.patient as patient, " +
 | 
	
		
			
				|  |  |                 "  o.number as number, " +
 | 
	
		
			
				|  |  |                 "  o.patient_expected_serve_time as serveTime, o.doctor, o.doctor_name as doctorName, " +
 | 
	
		
			
				|  |  |                 "  o.serve_address as address, " +
 | 
	
		
			
				|  |  |                 "  o.serve_lon as lon, " +
 | 
	
		
			
				|  |  |                 "  o.serve_lat as lat, " +
 | 
	
		
			
				|  |  |                 "  o.`status` as status " +
 | 
	
		
			
				|  |  |                 " FROM " +
 | 
	
		
			
				|  |  |                 " ( base_life_care_order o " +
 | 
	
		
			
				|  |  |                 " LEFT JOIN base_patient p ON o.patient = p.id ) "+
 | 
	
		
			
				|  |  |                 " WHERE " +
 | 
	
		
			
				|  |  |                 "  o.hospital = '{hospital}' and o.type != 3 " +buffer+
 | 
	
		
			
				|  |  |                 " AND ( o.`status` = {status} OR -100 = {status} ) " +
 | 
	
		
			
				|  |  |                 " ORDER BY o.create_time desc " +
 | 
	
		
			
				|  |  |                 " LIMIT {start},{end};";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String finalSql = sql.replace("{hospital}", hospital)
 | 
	
		
			
				|  |  |                 .replace("{status}", String.valueOf(status))
 | 
	
		
			
				|  |  |                 .replace("{start}", String.valueOf(start))
 | 
	
		
			
				|  |  |                 .replace("{end}", String.valueOf(end));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String countSql = "SELECT  " +
 | 
	
		
			
				|  |  |                 "   count(o.id)  " +
 | 
	
		
			
				|  |  |                 " FROM  " +
 | 
	
		
			
				|  |  |                 "   base_life_care_order o  " +
 | 
	
		
			
				|  |  |                 " LEFT JOIN base_patient p ON o.patient = p.id " +
 | 
	
		
			
				|  |  |                 " WHERE  " +
 | 
	
		
			
				|  |  |                 "  o.hospital = '{hospital}' " +buffer+
 | 
	
		
			
				|  |  |                 " AND (o.`status` = {status} or -100 = {status})";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String finqlCountSql = countSql.replace("{hospital}", hospital)
 | 
	
		
			
				|  |  |                 .replace("{status}", String.valueOf(status));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String,Object>> sqlResultlist;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             sqlResultlist = jdbcTemplate.queryForList(finalSql);
 | 
	
		
			
				|  |  |             for (Map<String,Object> orderDO:sqlResultlist){
 | 
	
		
			
				|  |  |                 orderDO.put("feeDetails",getFeeDetails(orderDO.get("orderId").toString()));
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库查询【调度员】生活照料工单列表信息失败:" + e.getMessage());
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Long count;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             count = jdbcTemplate.queryForObject(finqlCountSql, Long.class);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库统计【调度员】生活照料工单数量失败:" + e.getMessage());
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, sqlResultlist);
 | 
	
		
			
				|  |  |         JSONObject countItem = new JSONObject();
 | 
	
		
			
				|  |  |         countItem.put("count", count);
 | 
	
		
			
				|  |  |         result.putAll(countItem);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查找服务项字典
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<LifeCareItemDictDO> findItemDict(){
 | 
	
		
			
				|  |  |         return lifeCareItemDictDao.findByAll();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 居民端-查询生活照料工单列表
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject queryInfoList(String patient,Integer status, int page, int size) {
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         status = null == status ? -100 : status;
 | 
	
		
			
				|  |  |         int start = 0 == page ? page++ : (page - 1) * size;
 | 
	
		
			
				|  |  |         int end = 0 == size ? 15 : size;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sql = "SELECT *  FROM  base_life_care_order o " +
 | 
	
		
			
				|  |  |                 " WHERE (o.patient = '{patient}' or o.proxy_patient = '{patient}') "+
 | 
	
		
			
				|  |  |                 " AND (o.`status` = {status} or -100 = {status})" +
 | 
	
		
			
				|  |  |                 "  group by o.id " +
 | 
	
		
			
				|  |  |                 " ORDER BY o.create_time desc" +
 | 
	
		
			
				|  |  |                 " LIMIT {start},{end} ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String finalSql = sql.replace("{patient}", patient)
 | 
	
		
			
				|  |  |                 .replace("{status}", String.valueOf(status))
 | 
	
		
			
				|  |  |                 .replace("{start}", String.valueOf(start))
 | 
	
		
			
				|  |  |                 .replace("{end}", String.valueOf(end));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String countSql = "SELECT count(DISTINCT o.id) FROM base_life_care_order o " +
 | 
	
		
			
				|  |  |                 " WHERE  " +
 | 
	
		
			
				|  |  |                 "  (o.patient = '{patient}' or o.proxy_patient = '{patient}') " +
 | 
	
		
			
				|  |  |                 " AND (o.`status` = {status} or -100 = {status})";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String finqlCountSql = countSql.replace("{patient}", patient)
 | 
	
		
			
				|  |  |                 .replace("{status}", String.valueOf(status));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<LifeCareOrderDO> sqlResultlist= jdbcTemplate.query(finalSql,new BeanPropertyRowMapper(LifeCareOrderDO.class));
 | 
	
		
			
				|  |  |         for (LifeCareOrderDO orderDO:sqlResultlist){
 | 
	
		
			
				|  |  |             orderDO.setFeeDetailList(getFeeDetails(orderDO.getId()));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Integer count = jdbcTemplate.queryForObject(finqlCountSql, Integer.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, sqlResultlist);
 | 
	
		
			
				|  |  |         JSONObject countItem = new JSONObject();
 | 
	
		
			
				|  |  |         countItem.put("count", count);
 | 
	
		
			
				|  |  |         result.putAll(countItem);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 居民端-统计生活照料工单各状态下的数量列表(预约咨询记录)
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject queryInfoStatusCountList(String patient) {
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |         int total = 0;
 | 
	
		
			
				|  |  |         Map<String,Object> res = new HashedMap();
 | 
	
		
			
				|  |  |         //状态 待服务 1、已完成 2 、已取消 -1
 | 
	
		
			
				|  |  |         res.put("1",0);
 | 
	
		
			
				|  |  |         res.put("2",0);
 | 
	
		
			
				|  |  |         res.put("-1",0);
 | 
	
		
			
				|  |  |         String countSql = "SELECT " +
 | 
	
		
			
				|  |  |                 "  count(o.id) as count, " +
 | 
	
		
			
				|  |  |                 "  o.status as status " +
 | 
	
		
			
				|  |  |                 " FROM " +
 | 
	
		
			
				|  |  |                 "  base_life_care_order o " +
 | 
	
		
			
				|  |  |                 " WHERE " +
 | 
	
		
			
				|  |  |                 "  o.patient = '"+ patient + "' " +
 | 
	
		
			
				|  |  |                 " GROUP BY o.`status`";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String,Object>> countMapList = jdbcTemplate.queryForList(countSql);
 | 
	
		
			
				|  |  |         for(Map<String,Object> map:countMapList){
 | 
	
		
			
				|  |  |             int c = Integer.valueOf(map.get("count").toString());
 | 
	
		
			
				|  |  |             total +=c;
 | 
	
		
			
				|  |  |             res.put(String.valueOf(map.get("status")),c);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         res.put("total",total);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, res);
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 取消工单
 | 
	
		
			
				|  |  |      * @param orderId
 | 
	
		
			
				|  |  |      * @param type 取消类型
 | 
	
		
			
				|  |  |      * @param reason 取消理由
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject cancelOrder(String orderId,int type,String reason){
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |         LifeCareOrderDO orderDO = lifeCareOrderDao.findOne(orderId);
 | 
	
		
			
				|  |  |         if(null == orderDO){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "【取消工单】该工单不存在:," + orderId;
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(orderDO.getStatus().equals(LifeCareOrderDO.Status.waitForAccept.getType())){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "只有医生服务前的工单才可取消:," + orderId;
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         orderDO.setCancelType(type);
 | 
	
		
			
				|  |  |         orderDO.setCancelTime(new Date());
 | 
	
		
			
				|  |  |         orderDO.setCancelReason(reason);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //如果是调度员取消,推送IM取消工单json消息,
 | 
	
		
			
				|  |  |         orderDO.setStatus(LifeCareOrderDO.Status.cancel.getType());
 | 
	
		
			
				|  |  |         this.save(orderDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(type == LifeCareOrderDO.CancelType.patient.getType()){  //居民取消,消息列表也应该不显示
 | 
	
		
			
				|  |  |             messageUtil.updateLifeCareMessage(orderDO,new String[]{"502","503","530"},"patientCancel");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //保存取消记录
 | 
	
		
			
				|  |  |         LifeCareCancelLogDO cancelLogDO = new LifeCareCancelLogDO();
 | 
	
		
			
				|  |  |         cancelLogDO.setOrderId(orderId);
 | 
	
		
			
				|  |  |         cancelLogDO.setPatient(orderDO.getProxyPatient());
 | 
	
		
			
				|  |  |         cancelLogDO.setCancelType(type);
 | 
	
		
			
				|  |  |         cancelLogDO.setCancelReason(reason);
 | 
	
		
			
				|  |  |         cancelLogDO.setTime(orderDO.getCancelTime());
 | 
	
		
			
				|  |  |         lifeCareCancelLogDao.save(cancelLogDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         // 发送微信模板消息,通知居民工单已取消(smyyyqx-上门预约已取消)
 | 
	
		
			
				|  |  | //        String first = "key1,您好,您的上门预约服务已退回,点击查看原因";
 | 
	
		
			
				|  |  | //        BasePatientDO patient = patientDao.findById(orderDO.getPatient());
 | 
	
		
			
				|  |  | //        first  = first.replace("key1", null != patient.getName() ? patient.getName() : "");
 | 
	
		
			
				|  |  | //        JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  | //        json.put("id", orderDO.getId());
 | 
	
		
			
				|  |  | //        List<BasePatientWechatDo> basePatientWechatDos = basePatientWechatDao.findByWechatIdAndPatientId(wxId,patient.getId());
 | 
	
		
			
				|  |  | //        if(basePatientWechatDos.size()>0){
 | 
	
		
			
				|  |  | //            String openId = basePatientWechatDos.get(0).getOpenid();
 | 
	
		
			
				|  |  | //            messageUtil.putTemplateWxMessage(wxId,"template_process_feedback","smyyyqx",openId,first,null,null,30,json, DateUtil.dateToChineseDate(new Date()),"生活照料已取消");
 | 
	
		
			
				|  |  | //        }
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, "工单服务已取消!");
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 申请生活照料
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param jsonData
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public JSONObject create(String jsonData) {
 | 
	
		
			
				|  |  |         logger.info("申请生活照料jsonData参数:" + jsonData);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |         JSONObject jsonObjectParam;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             jsonObjectParam = JSONObject.parseObject(jsonData);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "参数转换成JSON对象异常:" + e.getMessage();
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         LifeCareOrderDO orderDO = null;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             orderDO = EntityUtils.jsonToEntity(jsonObjectParam.get("order").toString(), LifeCareOrderDO.class);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "生活照料工单服务基本信息:" + e.getMessage();
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         orderDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |         orderDO.setCreateUser(orderDO.getProxyPatient());
 | 
	
		
			
				|  |  |         orderDO.setCreateUserName(orderDO.getProxyPatientName());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(StringUtils.isEmpty(orderDO.getPatient())){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "当前服务对象code为空,请联系管理员检查参数!patient = " + orderDO.getPatient();
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if(StringUtils.isEmpty(orderDO.getProxyPatient())){
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "当前代理对象code为空,请联系管理员检查参数!proxyPatient = " + orderDO.getProxyPatient();
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         orderDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |         //判断创建生活照料类型,发起类型(1本人发起 2家人待预约 3医生代预约)
 | 
	
		
			
				|  |  |         if(orderDO.getProxyPatient().equals(orderDO.getPatient())){
 | 
	
		
			
				|  |  |             orderDO.setType(1);
 | 
	
		
			
				|  |  |         }else if(!orderDO.getProxyPatient().equals(orderDO.getPatient())){
 | 
	
		
			
				|  |  |             orderDO.setType(2);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         this.save(orderDO);
 | 
	
		
			
				|  |  |         result.put("orderId",orderDO.getId());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //新增工单与服务项费用关联关系
 | 
	
		
			
				|  |  |         if (orderWithPackageItemFeeAdd(result, jsonObjectParam, orderDO,null)) {return result;}
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultFlag, ResponseContant.success);
 | 
	
		
			
				|  |  |         result.put(ResponseContant.resultMsg, orderDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return result;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public boolean orderWithPackageItemFeeAdd(JSONObject result, JSONObject jsonObjectParam, LifeCareOrderDO order,String doctorId) {
 | 
	
		
			
				|  |  |         List<LifeCareFeeDetailDO> feeDetailDOList = order.getFeeDetailList();
 | 
	
		
			
				|  |  |         BigDecimal totalFee = order.getTotalFee();
 | 
	
		
			
				|  |  |         if(null == totalFee){
 | 
	
		
			
				|  |  |             totalFee = new BigDecimal(0);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for ( LifeCareFeeDetailDO feeDetailDO : feeDetailDOList) {
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 //工单主表中记录总费用
 | 
	
		
			
				|  |  |                 totalFee = totalFee.add(feeDetailDO.getFee().multiply(BigDecimal.valueOf(feeDetailDO.getNumber())));
 | 
	
		
			
				|  |  |             } catch (Exception e) {
 | 
	
		
			
				|  |  |                 result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |                 String failMsg = "工单主表中记录总费用时," + e.getMessage();
 | 
	
		
			
				|  |  |                 result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |                 logger.error(failMsg);
 | 
	
		
			
				|  |  |                 return true;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             // 服务项可能一开始由居民预约,后期由医生新增或医生删除
 | 
	
		
			
				|  |  |             Integer status = jsonObjectParam.getInteger("status");
 | 
	
		
			
				|  |  |             if(null == status || status == 1){
 | 
	
		
			
				|  |  |                 feeDetailDO.setStatus(LifeCareFeeDetailDO.Status.patient.getType());
 | 
	
		
			
				|  |  |             }else{
 | 
	
		
			
				|  |  |                 feeDetailDO.setStatus(status);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             feeDetailDO.setOrderId(order.getId());
 | 
	
		
			
				|  |  |             if(StringUtils.isBlank(feeDetailDO.getId())) {
 | 
	
		
			
				|  |  |                 feeDetailDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 feeDetailDO.setUpdateTime(new Date());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             if(StringUtils.isBlank(feeDetailDO.getId())){
 | 
	
		
			
				|  |  |                 feeDetailDO.setPayStatus(0);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             lifeCareFeeDetailDao.save(feeDetailDOList);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             String failMsg = "保存服务费用到数据库异常:," + e.getMessage();
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, failMsg);
 | 
	
		
			
				|  |  |             logger.error(failMsg);
 | 
	
		
			
				|  |  |             return true;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return false;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |