package com.yihu.wlyy.web.doctor.followup;

import java.util.*;

import com.yihu.wlyy.entity.followup.Followup;
import com.yihu.wlyy.service.app.followup.FollowUpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
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 com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.service.common.account.DoctorService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;

/**
 * 医生端：随访接口
 * 
 * @author hzp add 2016-12-07
 *
 */
@Controller
@RequestMapping(value = "/doctor/followup", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生端-随访接口")
public class DoctorFollowUpController extends BaseController {

	@Autowired
	private FollowUpService followUpService;

	/*************************************** 随访计划 ****************************************************************************/
	@ApiOperation("获取随访列表")
	@RequestMapping(value = "/list", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getList(@ApiParam(name="startTime",value="开始时间",defaultValue = "2016-12-07 00:00:00")
							  @RequestParam(value="startTime",required = true) String startTime,
							  @ApiParam(name="endTime",value="结束时间",defaultValue = "2016-12-14 00:00:00")
							  @RequestParam(value="endTime",required = true) String endTime,
						      @ApiParam(name="page",value="第几页",defaultValue = "1")
							  @RequestParam(value="page",required = true) String page,
							  @ApiParam(name="pageSize",value="每页几行",defaultValue = "10")
							  @RequestParam(value="pageSize",required = true) String pageSize) {
		try {
			List<Map<String,String>> result = followUpService.getListByDoctor(getUID(),startTime,endTime,page,pageSize);     //"64de9952-5b15-11e6-8344-fa163e8aee56"

			return write(200, "获取随访列表成功！", "data", result);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取随访列表失败！"+e.getMessage());
		}
	}

	@ApiOperation("获取随访列表(创建者)")
	@RequestMapping(value = "/createrList", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getCreaterList(@ApiParam(name="startTime",value="开始时间",defaultValue = "2016-12-07 00:00:00")
						  @RequestParam(value="startTime",required = true) String startTime,
						  @ApiParam(name="endTime",value="结束时间",defaultValue = "2016-12-14 00:00:00")
						  @RequestParam(value="endTime",required = true) String endTime,
						  @ApiParam(name="page",value="第几页",defaultValue = "1")
						  @RequestParam(value="page",required = true) String page,
						  @ApiParam(name="pageSize",value="每页几行",defaultValue = "10")
						  @RequestParam(value="pageSize",required = true) String pageSize) {
		try {
			List<Map<String,String>> result = followUpService.getListByCreater(getUID(),startTime,endTime,page,pageSize);     //"64de9952-5b15-11e6-8344-fa163e8aee56"

			return write(200, "获取随访列表成功！", "data", result);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取随访列表失败！"+e.getMessage());
		}
	}

	@ApiOperation("新增随访计划（批量）")
	@RequestMapping(value = "/addFollowupPlan", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String addFollowupPlan(@ApiParam(name="patient",value="患者代码",defaultValue = "443a196ef8744536a531260eb26c05d7")
						  @RequestParam(value="patient",required = true) String patient,
						  @ApiParam(name="data",value="随访计划列表json",defaultValue = "[{\"date\":\"2016-12-16 20:00:00\",\"type\":\"10\",\"doctor\":\"64de9952-5b15-11e6-8344-fa163e8aee56\"},{\"date\":\"2016-12-17 15:00:00\",\"type\":\"3\",\"doctor\":\"64de9952-5b15-11e6-8344-fa163e8aee56\"}]")
						  @RequestParam(value="data",required = true) String data) {
		try {
			followUpService.addFollowupPlan(getUID(),patient,data);

			return write(200, "新增随访计划成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "新增随访计划失败！"+e.getMessage());
		}
	}


	@ApiOperation("开始随访记录")
	@RequestMapping(value = "/startFollowup", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String startFollowup(@ApiParam(name="id",value="随访记录ID",defaultValue = "")
								 @RequestParam(value="id",required = true) String id,
								 @ApiParam(name="date",value="随访时间",defaultValue = "2016-12-14 20:00:00")
								 @RequestParam(value="date",required = true) String date,
								 @ApiParam(name="followupType",value="随访方式【字典FOLLOWUP_WAY_DICT】",defaultValue = "12")
								 @RequestParam(value="followupType",required = true) String followupType,
								 @ApiParam(name="followupClass",value="随访类别【1.高血压 2.糖尿病】",defaultValue = "1")
								 @RequestParam(value="followupClass",required = true) String followupClass,
								 @ApiParam(name="followupManagerStatus",value="随访管理状态【字典FOLLOWUP_MANAGER_STATUS】",defaultValue = "1")
								 @RequestParam(value="followupManagerStatus",required = false) String followupManagerStatus) {
		try {
			followUpService.startFollowup(id,date,followupType,followupClass,followupManagerStatus);

			return write(200, "开始随访记录成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "开始随访记录失败！"+e.getMessage());
		}
	}

	@ApiOperation("新增临时随访记录（返回ID）")
	@RequestMapping(value = "/addFollowup", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String addFollowup(@ApiParam(name="patient",value="患者代码",defaultValue = "443a196ef8744536a531260eb26c05d7")
								@RequestParam(value="patient",required = true) String patient,
								@ApiParam(name="date",value="下次随访时间",defaultValue = "2016-12-15 20:00:00")
								@RequestParam(value="date",required = true) String date,
								@ApiParam(name="followupType",value="随访方式【字典FOLLOWUP_WAY_DICT】",defaultValue = "12")
								@RequestParam(value="followupType",required = true) String followupType,
								@ApiParam(name="followupClass",value="随访类别【1.高血压 2.糖尿病】",defaultValue = "1")
								@RequestParam(value="followupClass",required = true) String followupClass,
								@ApiParam(name="followupManagerStatus",value="随访管理状态【字典FOLLOWUP_MANAGER_STATUS】",defaultValue = "1")
								@RequestParam(value="followupManagerStatus",required = false) String followupManagerStatus) {
		try {
			String response = followUpService.addFollowup(getUID(),patient,date,followupType,followupClass,followupManagerStatus);

			return write(200, "新增临时随访记录成功！","data",response);
		} catch (Exception e) {
			return invalidUserException(e, -1, "新增临时随访记录失败！"+e.getMessage());
		}
	}

	@ApiOperation("取消随访计划")
	@RequestMapping(value = "/cancelFollowupPlan", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String cancelFollowupPlan(@ApiParam(name="id",value="随访记录ID",defaultValue = "")
								      @RequestParam(value="id",required = true) String id)
	{
		try {
			followUpService.cancelFollowupPlan(id);

			return write(200, "取消随访计划成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "取消随访计划失败！"+e.getMessage());
		}
	}

	@ApiOperation("完成随访记录")
	@RequestMapping(value = "/finishFollowup", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String finishFollowup(@ApiParam(name="id",value="随访记录ID",defaultValue = "")
									 @RequestParam(value="id",required = true) String id)
	{
		try {
			followUpService.finishFollowup(id);

			return write(200, "完成随访记录成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "完成随访记录失败！"+e.getMessage());
		}
	}
	/*********************************** 随访详情 *******************************************************************/
	@ApiOperation("获取面访信息")
	@RequestMapping(value = "/getFollowup", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getFollowup(@ApiParam(name="id",value="随访记录ID",defaultValue = "4")
									 @RequestParam(value="id",required = true) String id)
	{
		try {
			Map<String,String> response = followUpService.getFollowup(id);

			return write(200, "获取面访信息成功！","data",response);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取面访信息失败！"+e.getMessage());
		}
	}

	@ApiOperation("获取面访项目列表")
	@RequestMapping(value = "/getFollowupProject", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getFollowupProject(@ApiParam(name="id",value="随访记录ID",defaultValue = "4")
									 @RequestParam(value="id",required = true) String id)
	{
		try {
			List<Map<String,String>> list = followUpService.getFollowupProject(id);

			return write(200, "获取面访项目列表成功！","data",list);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取面访项目列表失败！"+e.getMessage());
		}
	}

	@ApiOperation("获取面访项目数据")
	@RequestMapping(value = "/getFollowupProjectData", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getFollowupProjectData(@ApiParam(name="id",value="随访记录ID",defaultValue = "4")
									 @RequestParam(value="id",required = true) String id,
									 @ApiParam(name="followupProject",value="面访项目",defaultValue = "2")
									 @RequestParam(value="followupProject",required = true) String followupProject)
	{
		try {
			Map<String,String> response = followUpService.getFollowupProjectData(id,followupProject);

			return write(200, "获取面访项目数据成功！","data",response);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取面访项目数据失败！"+e.getMessage());
		}
	}


	@ApiOperation("保存面访项目数据")
	@RequestMapping(value = "/saveFollowupProjectData", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String saveFollowupProjectData(@ApiParam(name="id",value="随访记录ID",defaultValue = "4")
								   @RequestParam(value="id",required = true) String id,
								   @ApiParam(name="followupProject",value="面访项目",defaultValue = "2")
								   @RequestParam(value="followupProject",required = true) String followupProject,
								   @ApiParam(name="followupProjectData",value="面访项目数据",defaultValue = "{\"WEIGHT\":\"76\",\"WEIGHT_EXP\":\"60\",\"BMI\":\"11\"}")
								   @RequestParam(value="followupProjectData",required = true) String followupProjectData)
	{
		try {
			followUpService.saveFollowupProjectData(id,followupProject,followupProjectData);

			return write(200, "保存面访项目数据成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "保存面访项目数据失败！"+e.getMessage());
		}
	}


	/*********************************** 电话随访 *****************************************************************/
	@ApiOperation("获取电话随访内容")
	@RequestMapping(value = "/getFollowupPhone", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String getFollowupPhone(@ApiParam(name="id",value="随访记录ID",defaultValue = "")
									@RequestParam(value="id",required = true) String id) {
		try {
			String response = followUpService.getFollowupPhone(id);

			return write(200, "获取电话随访内容成功！","data",response);
		} catch (Exception e) {
			return invalidUserException(e, -1, "获取电话随访内容失败！"+e.getMessage());
		}
	}

	@ApiOperation("记录电话随访内容")
	@RequestMapping(value = "/saveFollowupPhone", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String saveFollowupPhone(@ApiParam(name="id",value="随访记录ID",defaultValue = "")
								  @RequestParam(value="id",required = true) String id,
								  @ApiParam(name="content",value="电话随访内容",defaultValue = "")
								  @RequestParam(value="content",required = true) String content) {
		try {
			followUpService.saveFollowupPhone(id,content);

			return write(200, "记录电话随访内容成功！");
		} catch (Exception e) {
			return invalidUserException(e, -1, "记录电话随访内容失败！"+e.getMessage());
		}
	}

}
