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

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.patient.PatientHealthGuidance;
import com.yihu.wlyy.service.app.health.PatientHealthGuidanceService;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.web.WeixinBaseController;

/**
 * 医生端：健康指导控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/doctor/health/guidance")
@Api(description = "健康指导")
public class DoctorHealthGuidanceController extends WeixinBaseController {

	@Autowired
	private PatientHealthGuidanceService patientHealthGuidanceService;

	/**
	 * 添加患者健康指导
	 * @param patient 患者标识
	 * @param content 指导内容
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public String add(String patient, String content, @RequestParam(required = false) String images, @RequestParam(required = false) String voice) {
		try {
			PatientHealthGuidance guidance = new PatientHealthGuidance();
			guidance.setContent(content);
			guidance.setDoctor(getUID());
			guidance.setPatient(patient);

			if (StringUtils.isNotEmpty(images)) {
				images = CommonUtil.copyTempImage(images);
			}
			guidance.setImages(images);

			if (StringUtils.isNotEmpty(voice)) {
				images = CommonUtil.copyTempVoice(voice);
			}
			guidance.setVoice(voice);

			// 保存
			if (patientHealthGuidanceService.add(guidance, getAccessToken()) != null) {
				return success("保存成功！");
			} else {
				return error(-1, "保存失败！");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "保存失败！");
		}
	}

	/**
	 * 查询患者的健康指导
	 * @param patient 患者标识
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(@RequestParam(required = false) String patient, @RequestParam(required = false) String begin, @RequestParam(required = false) String end, long id, int pagesize) {
		try {
			if (StringUtils.isNotEmpty(begin)) {
				begin = begin + " 00:00:00";
			}
			if (StringUtils.isNotEmpty(end)) {
				end = end + " 23:59:59";
			}
			JSONArray array = patientHealthGuidanceService.findPatientGuidanceByDoctor(getUID(), patient, begin, end, id, pagesize);
			return write(200, "查询成功！", "list", array);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

}
