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

import com.yihu.wlyy.service.app.health.PatientHealthGuidanceService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 患者端：健康指导控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/patient/health/guidance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "患者端-健康指导")
public class PatientHealthGuidanceController extends BaseController {

	@Autowired
	private PatientHealthGuidanceService patientHealthGuidanceService;

	/**
	 * 居民端一键已读
	 * @param
	 * @return
	 */
	@RequestMapping(value = "modifyStatus")
	@ResponseBody
	public String modifyStatus() {
		try {
			patientHealthGuidanceService.modifyStatus(getRepUID(),"1");
//			patientHealthGuidanceService.modifyStatus(getUID(),"1");
			return write(200, "设置成功！");
		} catch (Exception e) {
			error(e);
			return error(-1, "设置失败！");
		}
	}


	/**
	 * 查询患者的健康指导
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(@RequestParam String id, @RequestParam String pagesize) {
		try {
			Long idl = Long.parseLong(id);
			int pageSize = Integer.parseInt(pagesize);
//			JSONArray array = patientHealthGuidanceService.findGuidanceByPatient(getUID(), idl, pageSize);
			JSONArray array = patientHealthGuidanceService.findGuidanceByPatient(getRepUID(), idl, pageSize);
			return write(200, "查询成功！", "list", array);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

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

			JSONObject guidance = patientHealthGuidanceService.findById(idl);

//			修改医生给居民指导的阅读状态,改为已读
			patientHealthGuidanceService.modifyStatus(id,"0");

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