package com.yihu.wlyy.web.third;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.PatientReservation;
import com.yihu.wlyy.service.app.reservation.PatientReservationService;
import com.yihu.wlyy.service.common.account.DoctorService;
import com.yihu.wlyy.service.common.account.PatientService;
import com.yihu.wlyy.service.third.guahao.GuahaoDoctor;
import com.yihu.wlyy.service.third.guahao.GuahaoXMService;
import com.yihu.wlyy.service.third.guahao.GuahaoYihuService;
import com.yihu.wlyy.service.third.guahao.IGuahaoService;
import com.yihu.wlyy.task.PushMsgTask;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.web.WeixinBaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 预约挂号
 *
 * @author hzp at 2016-08-30
 */
@Controller
@RequestMapping(value = "/third/guahao")
@Api(description = "预约挂号接口")
public class BookingController extends WeixinBaseController {

    @Autowired
    private GuahaoXMService guahaoXM;

    @Autowired
    private GuahaoYihuService guahaoYihu;

    @Autowired
    private PatientReservationService patientReservationService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private DoctorService doctorService;

    /**
     * 根据城市编码获取相应挂号服务
     *
     * @return
     */
    private IGuahaoService getService(String city) {
        IGuahaoService re = guahaoYihu;
        if (city != null && city.equals("350200")) {
            re = guahaoXM;
        }
        return re;
    }

    /**
     * 发送短信参数
     */
    private static List<NameValuePair> buildSmsParams(String content, String mobile) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("SpCode", SystemConf.getInstance().getSmsCode()));
        params.add(new BasicNameValuePair("LoginName", SystemConf.getInstance().getSmsName()));
        params.add(new BasicNameValuePair("Password", SystemConf.getInstance().getSmsPassword()));
        params.add(new BasicNameValuePair("MessageContent", content));
        params.add(new BasicNameValuePair("UserNumber", mobile));
        params.add(new BasicNameValuePair("SerialNumber", String.valueOf(System.currentTimeMillis())));
        params.add(new BasicNameValuePair("ScheduleTime", ""));
        params.add(new BasicNameValuePair("f", "1"));
        return params;
    }

    /**
     * 获取时间
     */
    private static Date getMonthBefore(Date d, int month) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) - month);
        return now.getTime();
    }

    @RequestMapping(value = "GetOrgList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取机构列表")
    public String GetOrgList(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                             @RequestParam(value = "city", required = true) String city,
                             @ApiParam(name = "filter", value = "过滤条件", defaultValue = "")
                             @RequestParam(value = "filter", required = false) String filter,
                             @ApiParam(name = "type", value = "类型", defaultValue = "1")
                             @RequestParam(value = "type", required = false) String type,
                             @ApiParam(name = "pageIndex", value = "第几页", defaultValue = "")
                             @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                             @ApiParam(name = "pageSize", value = "每页记录数", defaultValue = "")
                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            List<Map<String, String>> list = getService(city).GetOrgList(city, filter, type, pageIndex, pageSize);
            return write(200, "获取机构列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }


    @RequestMapping(value = "GetOrgDepList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取科室接口")
    public String GetOrgDepList(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                                @RequestParam(value = "city", required = true) String city,
                                @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
                                @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                @ApiParam(name = "filter", value = "过滤条件", defaultValue = "")
                                @RequestParam(value = "filter", required = false) String filter,
                                @ApiParam(name = "pageIndex", value = "第几页", defaultValue = "")
                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                @ApiParam(name = "pageSize", value = "每页记录数", defaultValue = "")
                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            List<Map<String, String>> list = getService(city).GetOrgDepList(hospitalId, filter, pageIndex, pageSize);
            return write(200, "获取科室列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetDoctorList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取医生接口")
    public String GetDoctorList(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                                @RequestParam(value = "city", required = true) String city,
                                @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
                                @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "1040610")
                                @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
                                @ApiParam(name = "filter", value = "过滤条件", defaultValue = "")
                                @RequestParam(value = "filter", required = false) String filter,
                                @ApiParam(name = "pageIndex", value = "第几页", defaultValue = "")
                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                @ApiParam(name = "pageSize", value = "每页记录数", defaultValue = "")
                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            List<GuahaoDoctor> list = getService(city).GetDoctorList(hospitalId, hosDeptId, filter, pageIndex, pageSize);
            return write(200, "获取医生列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetDoctorArrange", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取医生排班接口（包含排班详细）")
    public String GetDoctorArrange(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                                   @RequestParam(value = "city", required = true) String city,
                                   @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211G1102")
                                   @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                   @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "3020001")
                                   @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
                                   @ApiParam(name = "doctorId", value = "医生ID", defaultValue = "AA2")
                                   @RequestParam(value = "doctorId", required = true) String doctorId) {
        try {
            List<Map<String, Object>> list = getService(city).GetDoctorArrange(hospitalId, hosDeptId, doctorId);
            return write(200, "获取医生排班成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetDoctorArrangeSimple", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取医生排班接口（一级）")
    public String GetDoctorArrangeSimple(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                                         @RequestParam(value = "city", required = true) String city,
                                         @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
                                         @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                         @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "1010210")
                                         @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
                                         @ApiParam(name = "doctorId", value = "医生ID", defaultValue = "03101")
                                         @RequestParam(value = "doctorId", required = true) String doctorId) {
        try {
            List<Map<String, String>> list = getService(city).GetDoctorArrangeSimple(hospitalId, hosDeptId, doctorId);
            return write(200, "获取医生排班成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetDoctorInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("根据医生编码获取医生详细信息")
    public String GetDoctorInfo(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                                @RequestParam(value = "city", required = true) String city,
                                @ApiParam(name = "doctorId", value = "医生id", defaultValue = "07101")
                                @RequestParam(value = "doctorId", required = true) String doctorId,
                                @ApiParam(name = "hospitalId", value = "医院id", defaultValue = "350211A1001")
                                @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                @ApiParam(name = "hosDeptId", value = "科室id", defaultValue = "1040610")
                                @RequestParam(value = "hosDeptId", required = true) String hosDeptId) {
        try {
            GuahaoDoctor doctor = getService(city).GetDoctorInfo(doctorId, hospitalId, hosDeptId);
            return write(200, "获取医生信息成功！", "data", doctor);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "CreateOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("创建挂号单")
    public String CreateOrder(@ApiParam(name = "city", value = "城市编码", defaultValue = "350200")
                              @RequestParam(value = "city", required = true) String city,
                              @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
                              @RequestParam(value = "hospitalId", required = true) String hospitalId,
                              @ApiParam(name = "hospitalName", value = "医院名称", defaultValue = "厦门大学附属第一医院")
                              @RequestParam(value = "hospitalName", required = true) String hospitalName,
                              @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "1040610")
                              @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
                              @ApiParam(name = "hosDeptName", value = "医院科室名称", defaultValue = "儿二科")
                              @RequestParam(value = "hosDeptName", required = true) String hosDeptName,
                              @ApiParam(name = "doctorId", value = "医生ID", defaultValue = "07101")
                              @RequestParam(value = "doctorId", required = true) String doctorId,
                              @ApiParam(name = "doctorName", value = "医生姓名", defaultValue = "林素莲")
                              @RequestParam(value = "doctorName", required = true) String doctorName,
                              @ApiParam(name = "arrangeDate", value = "排班信息", defaultValue = "{\"sectionType\":\"a\",\"startTime\":\"2016-09-02 08:20:00\",\"endTime\":\"2016-09-02 08:30:00\"}")
                              @RequestParam(value = "arrangeDate", required = true) String arrangeDate,
                              @ApiParam(name = "patient", value = "患者代码", defaultValue = "01954b2ebbb24a40a05da9d2f5c94795")
                              @RequestParam(value = "patient", required = true) String patient,
                              @ApiParam(name = "patientName", value = "患者姓名", defaultValue = "张锦川")
                              @RequestParam(value = "patientName", required = true) String patientName,
                              @ApiParam(name = "cardNo", value = "身份证号码", defaultValue = "35052419880511553X")
                              @RequestParam(value = "cardNo", required = true) String cardNo,
                              @ApiParam(name = "clinicCard", value = "市民卡号", defaultValue = "D57117706")
                              @RequestParam(value = "clinicCard", required = true) String clinicCard,
                              @ApiParam(name = "patientPhone", value = "患者手机", defaultValue = "13950116510")
                              @RequestParam(value = "patientPhone", required = true) String patientPhone) {
        try {
            if (StringUtils.isEmpty(patientName)) {
                return error(-1, "未设置姓名！");
            }
            if (StringUtils.isEmpty(cardNo)) {
                return error(-1, "未设置身份证号！");
            }
            if (StringUtils.isEmpty(clinicCard)) {
                return error(-1, "未设置社保卡号！");
            }
            if (StringUtils.isEmpty(patientPhone)) {
                return error(-1, "未设置手机号码！");
            }
            String orderCode = getService(city).CreateOrder(hospitalId, hospitalName, hosDeptId, hosDeptName, doctorId, doctorName, arrangeDate, patient, patientName, cardNo, clinicCard, patientPhone);

            //预约发送微信消息
            PatientReservation obj = patientReservationService.findByCode(orderCode);
            if (obj != null) {
                Patient p = patientService.findByCode(obj.getPatient());
                String msg = "您成功预约了 " + obj.getOrgName() + " " + obj.getDoctorName() + " " + obj.getStartTime() + " 的号源！";
                if (StringUtils.isNotEmpty(p.getOpenid())) {
                    // 推送消息给微信端
                    JSONObject json = new JSONObject();
                    json.put("first", "");
                    json.put("toUser", p.getCode());
                    json.put("id", obj.getCode());
                    json.put("date", obj.getStartTime());
                    json.put("orgCode", obj.getOrgCode());
                    json.put("orgName", obj.getOrgName());
                    json.put("doctorName", obj.getDoctorName());
                    json.put("deptName", obj.getDeptName());
                    json.put("remark", p.getName() + "，您好！" + msg);
                    PushMsgTask.getInstance().putWxMsg(getAccessToken(), 6, p.getOpenid(), p.getName(), json);
                }
                //发送短信小时
                // 调用总部发送信息的接口
                //String result = HttpClientUtil.post(SystemConf.getInstance().getSmsUrl(), buildSmsParams(msg, p.getMobile()), "GBK");
                //JSONObject json = toJson(result);
//				if (json == null) {
//					// 发送失败
//					throw new Exception("短信发送失败！");
//				} else if (json.getInt("result") != 0) {
//					return json.getString("description");
//				} else {
//					//发送成功，保存到数据库
//				}
                return write(200, "创建挂号单成功！");
            } else {
                return error(-1, "创建挂号单失败！");
            }
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }



    @RequestMapping(value = "CancelOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("取消挂号单")
    public String CancelOrder(@ApiParam(name = "orderId", value = "订单id", defaultValue = "9")
                              @RequestParam(value = "orderId", required = true) Long orderId) {
        try {
            //获取订单信息
            PatientReservation obj = patientReservationService.findById(orderId);
            boolean re = false;
            if (obj != null) {
                String type = obj.getType();
                String code = obj.getCode();
                if (type.equals("0")) {  //医护网接口
                    re = guahaoYihu.CancelOrder(code);
                } else if (type.equals("1"))   //厦门市民健康预约接口
                {
                    re = guahaoXM.CancelOrder(code, obj.getSsc());
                }
            }

            if (re) {
                //更新状态
                patientReservationService.updateStatus(orderId, 0);
                //微信消息
                Patient p = patientService.findByCode(obj.getPatient());
                if (StringUtils.isNotEmpty(p.getOpenid())) {
                    JSONObject json = new JSONObject();
                    json.put("first", "");
                    json.put("toUser", p.getCode());
                    json.put("name", obj.getName());
                    json.put("date", obj.getStartTime());
                    json.put("doctorName", obj.getDoctorName());
                    json.put("orgName", obj.getOrgName());
                    json.put("remark", obj.getName() + "，您好！\n您已取消了" + obj.getStartTime() + "的挂号！");
                    PushMsgTask.getInstance().putWxMsg(getAccessToken(), 7, p.getOpenid(), obj.getName(), json);
                }

                return write(200, "取消挂号单成功！");
            } else {
                return error(-1, "取消挂号单失败！");
            }
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }


    /****************************************** 内网预约 ***************************************************************/
    @RequestMapping(value = "CreateOrderByDoctor", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("(内网)转诊预约挂号")
    public String CreateOrderByDoctor(@ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
                                      @RequestParam(value = "hospitalId", required = true) String hospitalId,
                                      @ApiParam(name = "hospitalName", value = "医院名称", defaultValue = "厦门大学附属第一医院")
                                      @RequestParam(value = "hospitalName", required = true) String hospitalName,
                                      @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "1011610")
                                      @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
                                      @ApiParam(name = "hosDeptName", value = "医院科室名称", defaultValue = "内分泌糖尿病科门诊")
                                      @RequestParam(value = "hosDeptName", required = true) String hosDeptName,
                                      @ApiParam(name = "doctorId", value = "医生ID", defaultValue = "50108")
                                      @RequestParam(value = "doctorId", required = true) String doctorId,
                                      @ApiParam(name = "doctorName", value = "医生姓名", defaultValue = "杨叔禹")
                                      @RequestParam(value = "doctorName", required = true) String doctorName,
                                      @ApiParam(name = "arrangeDate", value = "排班信息", defaultValue = "{\"sectionType\":\"p\",\"startTime\":\"2016-12-29 14:00:00\",\"endTime\":\"2016-12-29 14:08:00\"}")
                                      @RequestParam(value = "arrangeDate", required = true) String arrangeDate,
                                      @ApiParam(name = "patient", value = "患者代码", defaultValue = "01954b2ebbb24a40a05da9d2f5c94795")
                                      @RequestParam(value = "patient", required = true) String patient,
                                      @ApiParam(name = "patientName", value = "患者姓名", defaultValue = "张锦川")
                                      @RequestParam(value = "patientName", required = true) String patientName,
                                      @ApiParam(name = "cardNo", value = "身份证号码", defaultValue = "35052419880511553X")
                                      @RequestParam(value = "cardNo", required = true) String cardNo,
                                      @ApiParam(name = "clinicCard", value = "市民卡号", defaultValue = "A0601003595X")
                                      @RequestParam(value = "clinicCard", required = true) String clinicCard,
                                      @ApiParam(name = "patientPhone", value = "患者手机", defaultValue = "13950116510")
                                      @RequestParam(value = "patientPhone", required = true) String patientPhone,
                                      @ApiParam(name = "dcode", value = "代预约医生编号", defaultValue = "test00000000005")
                                      @RequestParam(value = "dcode", required = true) String dcode,
                                      @ApiParam(name = "dname", value = "代预约医生名称", defaultValue = "组2全科医生")
                                      @RequestParam(value = "dname", required = true) String dname) {
        try {
            if (StringUtils.isEmpty(patientName)) {
                return error(-1, "未设置姓名！");
            }
            if (StringUtils.isEmpty(cardNo)) {
                return error(-1, "未设置身份证号！");
            }
            if (StringUtils.isEmpty(clinicCard)) {
                return error(-1, "未设置社保卡号！");
            }
            if (StringUtils.isEmpty(patientPhone)) {
                return error(-1, "未设置手机号码！");
            }
            String orderCode = guahaoXM.CreateOrderByDoctor(hospitalId, hospitalName, hosDeptId, hosDeptName, doctorId, doctorName, arrangeDate, patient, patientName, cardNo, clinicCard, patientPhone, dname, dcode);
            //获取预约信息查询是否挂号成功
            PatientReservation obj = patientReservationService.findByCode(orderCode);
            if (obj != null) {
                Patient p = patientService.findByCode(obj.getPatient());
                Doctor d = doctorService.findDoctorByCode(getUID());
                String msg = d.getName() + "医生已成功为您预约：" + obj.getStartTime() + "，" + obj.getOrgName() +
                        obj.getDeptName() + obj.getDeptName() + "医生的号源。您可直接前往医院就诊。";
                if (StringUtils.isNotEmpty(p.getOpenid())) {
                    // 推送消息给微信端
                    JSONObject json = new JSONObject();
                    json.put("first", "");
                    json.put("toUser", p.getCode());
                    json.put("id", obj.getCode());
                    json.put("date", obj.getStartTime());
                    json.put("orgName", obj.getOrgName());
                    json.put("orgCode", obj.getOrgCode());
                    json.put("doctorName", obj.getDeptName());
                    json.put("deptName", obj.getDeptName());
                    json.put("remark", p.getName() + "，您好！\n" + msg);
                    PushMsgTask.getInstance().putWxMsg(getAccessToken(), 6, p.getOpenid(), p.getName(), json);
                }
                //发送短信小时
                // 调用总部发送信息的接口
                String result = HttpClientUtil.post(SystemConf.getInstance().getSmsUrl(), buildSmsParams(msg, p.getMobile()), "GBK");

                return write(200, "创建挂号单成功！");
            } else {
                return error(-1, "创建挂号单失败！");
            }
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetDoctorArrangeTenDay", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("(内网)获取医生排班接口")
    public String GetDoctorArrangeTenDay(
            @ApiParam(name = "hospitalId", value = "医院ID", defaultValue = "350211A1001")
            @RequestParam(value = "hospitalId", required = true) String hospitalId,
            @ApiParam(name = "hosDeptId", value = "科室ID", defaultValue = "1011610")
            @RequestParam(value = "hosDeptId", required = true) String hosDeptId,
            @ApiParam(name = "doctorId", value = "医生ID", defaultValue = "50108")
            @RequestParam(value = "doctorId", required = true) String doctorId) {
        try {
            List<Map<String, Object>> list = guahaoXM.GetDoctorArrangeTenDay(hospitalId, hosDeptId, doctorId);
            return write(200, "获取医生排班成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    /********************************************* 医生端查询 **************************************************************************/
    @RequestMapping(value = "GetPatientReservationList", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    @ApiOperation("获取患者预约信息列表接口-医生端")
    public String GetPatientReservation(@ApiParam(name = "pageIndex", value = "第几页", defaultValue = "1")
                                        @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                        @ApiParam(name = "pageSize", value = "每页记录数", defaultValue = "10")
                                        @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                        @ApiParam(name = "patient", value = "患者编号", defaultValue = "4afdbce6194e412fbc770eb4dfbe7b00")
                                        @RequestParam(value = "patient", required = false) String patient,
                                        @ApiParam(name = "teamCode", value = "行政团队", defaultValue = "shiliuD20160926005")
                                        @RequestParam(value = "teamCode", required = false) Long teamCode) {
        try {
            List<PatientReservation> list = patientReservationService.getReservationByPatient(patient, teamCode, pageIndex, pageSize);

            return write(200, "获取患者预约信息列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }


    @RequestMapping(value = "GetDoctorReservationList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取医生代预约信息列表接口-医生端")
    public String GetDoctorReservation(@ApiParam(name = "pageIndex", value = "第几页", defaultValue = "1")
                                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                       @ApiParam(name = "pageSize", value = "每页记录数", defaultValue = "10")
                                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                       @ApiParam(name = "doctor", value = "医生编号", defaultValue = "shiliuD20160926005")
                                       @RequestParam(value = "doctor", required = false) String doctor) {
        try {
            List<Map<String,String>> list = patientReservationService.getReservationByDoctor(doctor, pageIndex, pageSize);

            return write(200, "获取患者预约信息列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }


    @RequestMapping(value = "GetPatientReservation", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取患者预约信息单条-医生端")
    public String GetPatientReservation(@ApiParam(name = "orderId", value = "订单id", defaultValue = "9")
                                        @RequestParam(value = "orderId", required = true) Long orderId) {
        try {
            PatientReservation obj = patientReservationService.findById(orderId);

            return write(200, "获取患者预约信息成功！", "data", obj);
        }
        catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "CountReservationByDoctorForPatient", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取医生为患者预约的总数")
    public String CountReservationByDoctorForPatient(@ApiParam(name = "doctor", value = "医生编号")
                                                     @RequestParam(value = "doctor", required = true) String doctor,
                                                     @ApiParam(name = "patient", value = "患者编号")
                                                     @RequestParam(value = "patient", required = true) String patient) {
        try {
            Long obj = patientReservationService.countReservationByDoctorForPatient(doctor, patient);
            return write(200, "获取患者预约信息成功！", "data", obj);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }
    /*************************************** 患者端查询 ******************************************************************/
    @RequestMapping(value = "GetRegList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取患者预约信息列表接口--患者端")
    public String GetRegList(@ApiParam(name = "patient", value = "患者编号", defaultValue = "0cc6e4562de2437ab2dbbf51a9fc3b49")
                             @RequestParam(value = "patient", required = false) String patient) {
        try {
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = new Date();
            Date oneMonthAfter = getMonthBefore(nowDate, -1);
            Date threeMonthBefore = getMonthBefore(nowDate, 3);        //三个月历史记录
            List<PatientReservation> list = guahaoXM.GetRegList(patient, sm.format(threeMonthBefore), sm.format(oneMonthAfter));
            return write(200, "获取患者预约信息列表成功！", "data", list);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "GetPatientReservationXm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("获取患者预约信息单条-患者端")
    public String GetPatientReservationXm(@ApiParam(name = "patientCode", value = "患者编号",defaultValue = "0cc6e4562de2437ab2dbbf51a9fc3b49")
                                          @RequestParam(value = "patientCode", required = true) String patientCode,
                                          @ApiParam(name = "orgCode", value = "机构编码",defaultValue = "350211A1002")
                                          @RequestParam(value = "orgCode", required = true) String orgCode,
                                          @ApiParam(name = "regCode", value = "挂号单号",defaultValue = "9c34e255-5984-43f0-8ecf-3bbf160e3c58")
                                          @RequestParam(value = "regCode", required = true) String regCode) {
        try {
            PatientReservation obj = guahaoXM.getRegDetail(patientCode, orgCode, regCode);
            return write(200, "获取患者预约信息成功！", "data", obj);
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }




}
