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

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yihu.wlyy.entity.follow.FollowUpPlanRemind;
import com.yihu.wlyy.service.app.followup.FollowUpPlanRemindService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;

/**
 * 患者端：随访提醒控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/patient/followup/remind")
public class FollowUpPlanRemindController extends BaseController {

	@Autowired
	private FollowUpPlanRemindService followUpPlanRemindService;

	/**
	 * 查询患者随访提醒列表
	 * @param id
	 * @param pagesize
	 * @return
	 */
	public String list(long id, int pagesize) {
		try {
			JSONArray array = new JSONArray();
			Page<FollowUpPlanRemind> page = followUpPlanRemindService.findByPatient(getUID(), id, pagesize);
			for (FollowUpPlanRemind remind : page) {
				if (remind == null) {
					continue;
				}
				JSONObject json = new JSONObject();
				json.put("id", remind.getId());
				// 设置医生标识
				json.put("doctor", remind.getDoctor());
				// 设置医生姓名
				json.put("doctorName", remind.getDoctorName());
				// 设置随访计划标识
				json.put("plan", remind.getPlan());
				// 设置提醒内容
				json.put("content", remind.getContent());
				// 设置患者查看标识：1未读，0已读
				json.put("read", remind.getRead());
				// 设置提醒时间
				json.put("czrq", DateUtil.dateToStrLong(remind.getCzrq()));
				array.put(json);
			}
			return write(200, "查询成功！", "list", array);
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败！");
		}
	}

	/**
	 * 更新为已读
	 * @param id
	 * @return
	 */
	public String read(long id) {
		try {
			followUpPlanRemindService.read(id);
			return success("操作成功！");
		} catch (Exception e) {
			error(e);
			return error(-1, "更新已读失败！");
		}
	}

}
