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

import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.PatientHealthRecordDiet;
import com.yihu.wlyy.entity.patient.PatientHealthRecordMedication;
import com.yihu.wlyy.entity.patient.PatientHealthRecordSports;
import com.yihu.wlyy.service.app.health.PatientHealthRecordService;
import com.yihu.wlyy.service.common.account.PatientService;
import com.yihu.wlyy.service.third.jw.JwArchivesService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;
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.data.domain.Page;
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;

/**
 * 医生端：健康记录控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/doctor/health_record", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "健康记录")
public class DoctorHealthRecordController extends BaseController {

	@Autowired
	private PatientHealthRecordService patientHealthRecordService;
	@Autowired
	private JwArchivesService jwArchivesService;
	@Autowired
	private PatientService patientService;

	/**
	 * 患者最近填写的运动、用药、饮食内容
	 * @param patient 患者标识
	 * @return
	 */
	@RequestMapping(value = "recent")
	@ResponseBody
	@ApiOperation("根据患者标志获取最近的保健记录")
	public String recent(@ApiParam(name="patient",value="患者代码",defaultValue = "P20160901001")
									 @RequestParam(value="patient",required = true) String patient) {
		try {
			JSONObject json = patientHealthRecordService.findRecentByPatient(patient);
			if (json != null) {
				return write(200, "查询成功", "data", json);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}
	
	/**
	 * 运动记录查询接口
	 * @param patient 患者标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_sports")
	@ResponseBody
	@ApiOperation("获取患者运动记录")
	public String sports(@ApiParam(name="patient",value="患者代码",defaultValue = "P20160901001")
							 @RequestParam(value="patient",required = true)String patient,
						 @ApiParam(name="start",value="开始时间",defaultValue = "2017-05-15 00:00:00")
						 @RequestParam(value="start",required = true)String start,
						 @ApiParam(name="end",value="结束时间",defaultValue = "2017-05-15 12:00:00")
							 @RequestParam(value="end",required = true) String end,
						 @ApiParam(name="page",value="第几页",defaultValue = "1")
							 @RequestParam(value="page",required = true) int page,
						 @ApiParam(name="pagesize",value="每页记录数",defaultValue = "10")
							 @RequestParam(value="pagesize",required = true) int pagesize) {
		try {
			//Page<PatientHealthRecordSports> data = patientHealthRecordService.findSportsByPatient(patient, DateUtil.strToDateLong(sortDate), pagesize);
			Page<PatientHealthRecordSports> data = patientHealthRecordService.findSportsByPatientPage(patient, start,end,page, pagesize);
			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordSports record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 设置运动时长（分钟）
					json.put("sports_time", record.getSportsTime());
					// 设置运动强度
					json.put("sports_type", record.getSportsTypeName());
					// 设置运动类型
					json.put("sports", record.getSportsName());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}

	/**
	 * 用药记录查询接口
	 * @param patient 患者标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_medication")
	@ResponseBody
	@ApiOperation("获取患者用药记录")
	public String medication(@ApiParam(name="patient",value="患者代码",defaultValue = "P20160901001")
								 @RequestParam(value="patient",required = true)String patient,
							 @ApiParam(name="start",value="开始")
							 	 @RequestParam(required = false) String start,
							 @ApiParam(name="end",value="结束")
								 @RequestParam(required = false) String end,
							 @ApiParam(name="page",value="第几页",defaultValue = "1")
								 @RequestParam(required = false) Integer page,
							 @ApiParam(name="sortDate",value="日期")
							 	@RequestParam(required = false) String sortDate,
							 @ApiParam(name="pagesize",value="每页记录数",defaultValue = "10")
								 @RequestParam(required = true)int pagesize) {
		try {
			Page<PatientHealthRecordMedication> data = null;
			if(!StringUtils.isEmpty(sortDate)) {
				data = patientHealthRecordService.findMedicationByPatient(patient, DateUtil.strToDateLong(sortDate),page, pagesize);
			} else {
				data = patientHealthRecordService.findMedicalByPatientPage(patient, start, end, page, pagesize);
			}

			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordMedication record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 药品名称
					json.put("medicines", record.getMedicinesName());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}

	/**
	 * 饮食记录查询接口
	 * @param patient 患者标识
	 * @param page 页码
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_diet")
	@ResponseBody
	public String diet(@ApiParam(name = "patient", value = "患者代码", defaultValue = "P20160901001")
						   @RequestParam(value="patient",required = true)String patient,
					   @ApiParam(name="start",value="开始")
					   @RequestParam(required = true) String start,
					   @ApiParam(name="end",value="结束")
						   @RequestParam(required = false) String end,
					   @ApiParam(name="page",value="第几页",defaultValue = "1")
						   @RequestParam(required = true)int page,
					   @ApiParam(name="pagesize",value="每页记录数",defaultValue = "10")
						   @RequestParam(required = true)int pagesize) {
		try {
			Page<PatientHealthRecordDiet> data = patientHealthRecordService.findDietByPatientPage(patient, start,end, page, pagesize);
			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordDiet record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 设置饮食内容
					json.put("content", record.getContent());
					// 设置图片URL,多图以逗号分隔
					json.put("images", record.getImages());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}

	@RequestMapping(value = "list_medical", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation("查询居民健康体检列表")
	public String medical(@ApiParam(name = "patient", value = "患者代码", defaultValue = "P20160901001")
						    @RequestParam(value="patient",required = true) String patient ,
						    @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{
			Patient p = patientService.findByCode(patient);
			if(p != null){
				JSONArray re = jwArchivesService.getEhrSickMedicalList(p.getIdcard(),pageIndex,pageSize);
				return write(200,"查询成功","list",re);
			}else{
				return error(-1,"患者不存在");
			}
		}catch (Exception e){
			error(e);
			return error(-1,"查询失败");
		}
	}

	@RequestMapping(value = "medical_detail", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation("查询居民健康体检详情信息")
	public String medical_detail(@ApiParam(name = "medicalNo", value = "体检ID", defaultValue = "1249652")
								  @RequestParam(value="medicalNo",required = true) String medicalNo){
		try{
			JSONObject json = jwArchivesService.getEhrSickMedicalRecord(medicalNo);
			return write(200,"查询成功","medical_detail",json);
		}catch (Exception e){
			error(e);
			return error(-1,"查询失败");
		}
	}

}
