|  | @ -1,12 +1,16 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.course;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.course.CourseCatalogueDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.course.CourseDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.course.CourseSalesOrderRecordDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.course.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.doctor.dao.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.course.CourseSalesOrderRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.course.PatientOrderRefundDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.course.RecruitStudentsRecordDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.order.BusinessOrderDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.order.dao.BusinessOrderDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.patient.dao.BasePatientDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.ResponseContant;
 | 
	
		
			
				|  |  | import com.yihu.jw.utils.StringUtil;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
	
		
			
				|  | @ -15,8 +19,6 @@ import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import static com.yihu.jw.rm.iot.IotRequestMapping.Common.hospital;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created with IntelliJ IDEA.
 | 
	
		
			
				|  |  |  *
 | 
	
	
		
			
				|  | @ -39,9 +41,99 @@ public class CourseService {
 | 
	
		
			
				|  |  |     private BaseDoctorDao doctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private RecruitStudentsRecordDao recruitStudentsRecordDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BusinessOrderDao businessOrderDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientOrderRefundDao patientOrderRefundDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getOrderNo(String type){
 | 
	
		
			
				|  |  |         return type + System.currentTimeMillis()+(int)(Math.random() * 900)+100 +"";
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 取消订单
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @param type
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public String cancalOrder(String id,String type){
 | 
	
		
			
				|  |  |         if("1".equals(type)){
 | 
	
		
			
				|  |  |             //招生
 | 
	
		
			
				|  |  |             RecruitStudentsRecordDO recordDO = recruitStudentsRecordDao.findOne(id);
 | 
	
		
			
				|  |  |             if(!"2".equals(recordDO.getStatus())){
 | 
	
		
			
				|  |  |                 return "只有待支付的订单才能取消";
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             recordDO.setStatus("7");
 | 
	
		
			
				|  |  |             recruitStudentsRecordDao.save(recordDO);
 | 
	
		
			
				|  |  |         }else  if("2".equals(type)){
 | 
	
		
			
				|  |  |             //课程
 | 
	
		
			
				|  |  |             CourseSalesOrderRecordDO recordDO = courseSalesOrderRecordDao.findOne(id);
 | 
	
		
			
				|  |  |             if(!"2".equals(recordDO.getStatus())){
 | 
	
		
			
				|  |  |                 return "只有待支付的订单才能取消";
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             recordDO.setStatus("7");
 | 
	
		
			
				|  |  |             courseSalesOrderRecordDao.save(recordDO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 订单详情接口
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      * @param type
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject orderInfo(String id,String type){
 | 
	
		
			
				|  |  |         JSONObject resJson = new JSONObject();
 | 
	
		
			
				|  |  |         resJson.put("type",type);
 | 
	
		
			
				|  |  |         resJson.put("id",id);
 | 
	
		
			
				|  |  |         String status = "";
 | 
	
		
			
				|  |  |         if("1".equals(type)){
 | 
	
		
			
				|  |  |             //招生
 | 
	
		
			
				|  |  |             RecruitStudentsRecordDO recordDO = recruitStudentsRecordDao.findOne(id);
 | 
	
		
			
				|  |  |             status = recordDO.getStatus();
 | 
	
		
			
				|  |  |             resJson.put("title",recordDO.getRecruitName());
 | 
	
		
			
				|  |  |             resJson.put("status",recordDO.getStatus());
 | 
	
		
			
				|  |  |             resJson.put("price",recordDO.getPrice());
 | 
	
		
			
				|  |  |             resJson.put("orderNo",recordDO.getOrderNo());
 | 
	
		
			
				|  |  |         }else  if("2".equals(type)){
 | 
	
		
			
				|  |  |             //课程
 | 
	
		
			
				|  |  |             CourseSalesOrderRecordDO recordDO = courseSalesOrderRecordDao.findOne(id);
 | 
	
		
			
				|  |  |             status = recordDO.getStatus();
 | 
	
		
			
				|  |  |             resJson.put("title",recordDO.getCourseName());
 | 
	
		
			
				|  |  |             resJson.put("status",recordDO.getStatus());
 | 
	
		
			
				|  |  |             resJson.put("price",recordDO.getPrice());
 | 
	
		
			
				|  |  |             resJson.put("orderNo",recordDO.getOrderNo());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         boolean flag = "3".equals(status)||"5".equals(status)||"6".equals(status);
 | 
	
		
			
				|  |  |         if(flag){
 | 
	
		
			
				|  |  |             //支付信息
 | 
	
		
			
				|  |  |             BusinessOrderDO businessOrderDO = businessOrderDao.selectByRelationCode(id);
 | 
	
		
			
				|  |  |             JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |             json.put("payPrice",businessOrderDO.getPayPrice());
 | 
	
		
			
				|  |  |             json.put("payTime",businessOrderDO.getPayTime());
 | 
	
		
			
				|  |  |             json.put("payType",businessOrderDO.getPayType());
 | 
	
		
			
				|  |  |             resJson.put("businessOrderDO",json);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if("5".equals(status)||"6".equals(status)){
 | 
	
		
			
				|  |  |             //退款信息
 | 
	
		
			
				|  |  |             PatientOrderRefundDO refundDO = patientOrderRefundDao.findByTypeAndOrderId(type,id);
 | 
	
		
			
				|  |  |             JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |             json.put("createTime",refundDO.getCreateTime());
 | 
	
		
			
				|  |  |             json.put("refundPrice",refundDO.getRefundPrice());
 | 
	
		
			
				|  |  |             json.put("status",refundDO.getStatus());
 | 
	
		
			
				|  |  |             json.put("refundDesc",refundDO.getRefundDesc());
 | 
	
		
			
				|  |  |             json.put("enclosure",refundDO.getEnclosure());
 | 
	
		
			
				|  |  |             resJson.put("refundDO",json);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return resJson;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 顶部状态栏订单各分类总条数
 | 
	
		
			
				|  |  |      * 待审核 1     待支付 2     已支付 3     审核未通过 4     已退款 5     退款中 6     已取消 7
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
	
		
			
				|  | @ -55,12 +147,14 @@ public class CourseService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
 | 
	
		
			
				|  |  |         Map<String, Integer> map = new HashMap<>();
 | 
	
		
			
				|  |  |         //状态 待服务 1、已完成 2 、已取消 -1
 | 
	
		
			
				|  |  |         //状态 待审核 1     待支付 2     已支付 3     审核未通过 4     已退款 5     退款中 6     已取消 7
 | 
	
		
			
				|  |  |         map.put("1",0);
 | 
	
		
			
				|  |  |         map.put("2",0);
 | 
	
		
			
				|  |  |         map.put("3",0);
 | 
	
		
			
				|  |  |         map.put("4",0);
 | 
	
		
			
				|  |  |         map.put("5",0);
 | 
	
		
			
				|  |  |         map.put("6",0);
 | 
	
		
			
				|  |  |         map.put("7",0);
 | 
	
		
			
				|  |  |         int total = 0;
 | 
	
		
			
				|  |  |         if(list.size()>0){
 | 
	
		
			
				|  |  |             for (Map<String, Object> one:list){
 | 
	
	
		
			
				|  | @ -69,79 +163,56 @@ public class CourseService {
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         map.put("total", total);
 | 
	
		
			
				|  |  |         return map;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject patientOrderList(String patient, Integer status, int page, int size) {
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 居民我的订单列表
 | 
	
		
			
				|  |  |      * @param patient
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public JSONObject patientOrderList(String patient, String status, int page, int size) {
 | 
	
		
			
				|  |  |         JSONObject result = new JSONObject();
 | 
	
		
			
				|  |  |         int start = 0 == page ? page++ : (page - 1) * size;
 | 
	
		
			
				|  |  |         int end = 0 == size ? 15 : size;
 | 
	
		
			
				|  |  |         StringBuffer buffer = new StringBuffer();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sql = "SELECT " +
 | 
	
		
			
				|  |  |                 "  p.name AS patientName, " +
 | 
	
		
			
				|  |  |                 "  p.photo AS photo, " +
 | 
	
		
			
				|  |  |                 "  p.idcard," +
 | 
	
		
			
				|  |  |                 "  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.type as type, " +
 | 
	
		
			
				|  |  |                 "  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("{status}", String.valueOf(status))
 | 
	
		
			
				|  |  |                 .replace("{start}", String.valueOf(start))
 | 
	
		
			
				|  |  |                 .replace("{end}", String.valueOf(end));
 | 
	
		
			
				|  |  |         String filter = " where patient = '"+patient+"' ";
 | 
	
		
			
				|  |  |         if(!StringUtil.isEmpty(status)){
 | 
	
		
			
				|  |  |             filter += " and status = "+ status +" ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         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 sqlUnion = " SELECT id,`status`,recruit_name title,pay_type payType,price,1 type,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') createTtime " +
 | 
	
		
			
				|  |  |                 " from base_recruit_students_record " + filter+
 | 
	
		
			
				|  |  |                 " UNION ALL " +
 | 
	
		
			
				|  |  |                 " SELECT id,`status`,course_name title,pay_type payType,price,2 type,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') createTtime  " +
 | 
	
		
			
				|  |  |                 " from base_course_sales_order_record " + filter+" ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String finqlCountSql = countSql.replace("{hospital}", hospital)
 | 
	
		
			
				|  |  |                 .replace("{status}", String.valueOf(status));
 | 
	
		
			
				|  |  |         String sql = "SELECT * from ( " + sqlUnion + " ) a " +
 | 
	
		
			
				|  |  |                 " ORDER BY a.createTtime desc " +
 | 
	
		
			
				|  |  |                 " LIMIT {start},{end};";
 | 
	
		
			
				|  |  |         String finalSql = sql.replace("{start}", String.valueOf(start))
 | 
	
		
			
				|  |  |                 .replace("{end}", String.valueOf(end));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String countSql = "SELECT  count(a.id)  FROM  ( " + sqlUnion + " ) a " ;
 | 
	
		
			
				|  |  |         List<Map<String,Object>> sqlResultlist;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             sqlResultlist = jdbcTemplate.queryForList(finalSql);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库查询生活照料工单列表信息失败");
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库查询工单列表信息失败");
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Long count;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             count = jdbcTemplate.queryForObject(finqlCountSql, Long.class);
 | 
	
		
			
				|  |  |             count = jdbcTemplate.queryForObject(countSql, Long.class);
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultFlag, ResponseContant.fail);
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库统计生活照料工单数量失败" );
 | 
	
		
			
				|  |  |             result.put(ResponseContant.resultMsg, "从数据库统计工单数量失败" );
 | 
	
		
			
				|  |  |             return result;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 |