package com.yihu.wlyy.web.third;

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.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.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.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
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.util.List;
import java.util.Map;

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

	@Autowired
	private GuahaoXMService guahaoXM;

	@Autowired
	private GuahaoYihuService guahaoYihu;

	@Autowired
	private PatientReservationService patientReservationService;

	@Autowired
	private PatientService patientService;

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

	/**
	 * 获取机构列表
	 */
	@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("id",obj.getId());
					json.put("date", obj.getStartTime());
					json.put("orgName", obj.getOrgName());
					json.put("doctorName", obj.getDoctorName());
					json.put("deptName", obj.getDeptName());
					json.put("remark", msg);
					PushMsgTask.getInstance().putWxMsg(getAccessToken(), 6, p.getOpenid(), p.getName(), json);
				}
				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) String 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(obj.getCode(),0);
				//微信消息
				Patient p = patientService.findByCode(obj.getPatient());
				if (StringUtils.isNotEmpty(p.getOpenid())) {
					JSONObject json = new JSONObject();
					json.put("first", "取消预约挂号提醒");
					json.put("name", obj.getName());
					json.put("date", obj.getStartTime());
					json.put("doctorName", obj.getDoctorName());
					json.put("orgName", obj.getOrgName());
					json.put("remark", "您好，您已取消了" + 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 = "GetPatientReservationList",method = RequestMethod.POST)
	@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) {
		try {
			List<PatientReservation> list =  patientReservationService.getReservationByPatient(getUID(),pageIndex,pageSize);

			//遍历更新预约状态
			for(PatientReservation item :list)
			{
				String type = item.getType();
				String code = item.getCode();
				if (type.equals("0")) {  //医护网接口

				}
				else if (type.equals("1"))   //厦门市民健康预约接口
				{
					Integer status = guahaoXM.GetOrderStatus(item.getOrgCode(),code,item.getSsc());
					//更新状态
					if(status!=null) {
						patientReservationService.updateStatus(item.getCode(), 0);
						item.setStatus(status);
					}
				}
			}
			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) String orderId) {
		try {
			PatientReservation obj =  patientReservationService.findById(orderId);
			if(obj!=null) {
				String type = obj.getType();
				String code = obj.getCode();
				Integer status = null;
				if (type.equals("0")) {  //医护网接口

				}
				else if (type.equals("1"))   //厦门市民健康预约接口
				{
					status = guahaoXM.GetOrderStatus(obj.getOrgCode(), code, obj.getSsc());
				}
				//更新状态
				if (status != null) {
					patientReservationService.updateStatus(obj.getCode(), 0);
					obj.setStatus(status);
				}
				return write(200, "获取患者预约信息成功！", "data", obj);
			}
			else{
				return error(-1,"不存在该条预约信息！");
			}

		} catch (Exception e) {
			return error(-1,e.getMessage());
		}
	}
}
