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

import com.yihu.wlyy.util.SendP2PUtil;
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.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", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "健康指导")
public class DoctorHealthGuidanceController extends WeixinBaseController {

	@Autowired
	private PatientHealthGuidanceService patientHealthGuidanceService;

	/**
	 * 获取某个健康指导
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/id")
	@ResponseBody
	public String getHealthGuidance(Long id){
		try{
			if(id == null || id < 1){
				return error(-1,"参数错误");
			}

			JSONObject guidance = patientHealthGuidanceService.findById(id);
			return write(200,"查询成功","data",guidance);
		}catch (Exception e){
			return error(-1,"查询失败");
		}
	}

	/**
	 * 添加患者健康指导
	 * @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) {
				SendP2PUtil.sendP2Pmsg(getUID(), patient, "1",content);
				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, "查询失败！");
		}
	}


	/**
	 * 查询患者的健康指导
	 *
	 * @param patient
	 * @param teamCode
	 * @param page
	 * @param pagesize
	 * @return
	 */
	@RequestMapping(value = "list_by_team", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	@ApiOperation(value = "查询居民健康指导记录")
	public String list(@RequestParam @ApiParam(value = "居民code") String patient,
					   @RequestParam @ApiParam(value = "团队Code") Long teamCode,
					   @RequestParam @ApiParam(value = "第几页") int page,
					   @RequestParam @ApiParam(value = "页大小") int pagesize) {
		try {
			if (StringUtils.isEmpty(patient)) {
				return error(-1, "请输入需查询的居民");
			}
			if (teamCode == null || teamCode < 1) {
				return error(-1, "请输入需查询的居民的团队");
			}
			JSONArray result = patientHealthGuidanceService.findByPatientAndGuidanceAndTeam(patient,teamCode,page,pagesize);
			return write(200, "查询成功！", "list", result);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

}
