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

import com.yihu.wlyy.entity.education.FoodComp;
import com.yihu.wlyy.service.app.health.FoodCompService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
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;

import java.util.List;

/**
 * 患者端：食物热量查询
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/common/health/food", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "患者端-食物热量查询")
public class FoodCompController extends BaseController {

	@Autowired
	private FoodCompService foodCompService;

	/**
	 * 查询食物热量列表
	 * @param pid 父id
	 * @return 列表
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public String list(@RequestParam("pid")long pid) {
		try {
			List<FoodComp> foodComps = foodCompService.getByPid(pid);
			return write(200, "查询成功", "list", foodComps);
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "查询失败!");
		}
	}

	/**
	 * 查询食物热量查询
	 * @param name 查询名称
	 * @return 列表
	 */
	@RequestMapping(value = "search")
	@ResponseBody
	public String list(@RequestParam("name")String name) {
		try {
			List<FoodComp> foodComps = foodCompService.getByName(name);
			return write(200, "查询成功", "list", foodComps);
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "查询失败!");
		}
	}
	/**
	 * 查询食物热量详情
	 * @param id 查询名称
	 * @return 列表
	 */
	@RequestMapping(value = "detail")
	@ResponseBody
	public String list(@RequestParam("id")Long id) {
		try {
			FoodComp foodComp = foodCompService.getById(id);
			return write(200, "查询成功", "list", foodComp);
		} catch (Exception ex) {
			error(ex);
			return invalidUserException(ex, -1, "查询失败!");
		}
	}

}
