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

import io.swagger.annotations.Api;
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.ResponseBody;

import com.yihu.wlyy.entity.education.HealthNews;
import com.yihu.wlyy.service.app.health.HealthNewsService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;

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

	@Autowired
	private HealthNewsService healthNewsService;

	/**
	 * 查询资讯列表
	 * @param pagesize 分页大小
	 * @return 列表
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(long id, int pagesize) {
		try {
			Page<HealthNews> list = healthNewsService.findAll(id, pagesize);
			JSONArray jsonArray = new JSONArray();
			if (list != null) {
				for (HealthNews news : list) {
					JSONObject json = new JSONObject();
					json.put("id", news.getId());
					// 设置新闻图片
					json.put("photo", news.getPhoto());
					// 文章标题
					json.put("title", news.getTitle());
					// 文章查看URL
					json.put("url", news.getUrl());
					// 文章简介
					json.put("summary", news.getSummary());
					// 添加日期
					json.put("czrq", DateUtil.dateToStrLong(news.getCzrq()));
					jsonArray.put(json);
				}
			}
			return write(200, "查询成功", "list", jsonArray);
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "查询失败!");
		}
	}
}
