package com.yihu.figure.controller.health;

import com.yihu.figure.controller.BaseController;
import com.yihu.figure.service.health.HealthIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * Created by yeshijie on 2017/3/6.
 */
@Controller
@RequestMapping(value = "/health/")
@Api(description = "自动生成体征数据接口")
public class HealthIndexController extends BaseController {

    @Autowired
    private HealthIndexService healthIndexService;

    /**
     * 自动生成体征数据
     * @param patient
     * @param type
     * @return
     */
    @RequestMapping(value="autoHealth" ,method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "自动生成体征数据")
    public String autoHealth(@ApiParam(name="patient",value="居民",defaultValue = "915d1169-5b1d-11e6-8344-fa163e8aee56")
                             @RequestParam(value="patient",required = true) String patient,
                             @ApiParam(name="type",value="类型1血糖、2血压、3体重、4腰围",defaultValue = "4")
                             @RequestParam(value="type",required = false) String type){
        try{
            healthIndexService.autoHealthIndex(patient,type);
            return write(200,"生成成功");
        }catch (Exception e){
            e.printStackTrace();
            return error(-1, "生成失败");
        }
    }

    /**
     * 统计居民体征数据
     * @param patient
     * @return
     */
    @RequestMapping(value="staticsHealth" ,method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "统计居民体征数据")
    public String staticsHealth(@ApiParam(name="patient",value="居民",defaultValue = "915d1169-5b1d-11e6-8344-fa163e8aee56")
                                @RequestParam(value="patient",required = true) String patient){
        try{
            healthIndexService.statistics(patient);
            return write(200,"统计成功");
        }catch (Exception e){
            e.printStackTrace();
            return error(-1, "统计失败");
        }

    }

    /**
     * 自动生成体征数据
     * @param patient
     * @param type
     * @return
     */
    @RequestMapping(value="healthIndex" ,method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取居民体征数据")
    public String healthIndex(@ApiParam(name="patient",value="居民",defaultValue = "915d1169-5b1d-11e6-8344-fa163e8aee56")
                              @RequestParam(value="patient",required = true) String patient,
                              @ApiParam(name="type",value="类型1血糖、2血压",defaultValue = "1")
                              @RequestParam(value="type",required = true) String type,
                              @ApiParam(name="timeType",value="时间类型（1近1个月，2近半年，3近1年）",defaultValue = "1")
                              @RequestParam(value="timeType",required = true) String timeType,
                              @ApiParam(name="bloodType",value="1空腹血糖 2早餐后血糖 3午餐前血糖 4午餐后血糖 5晚餐前血糖 6晚餐后血糖 7睡前血糖，8舒张压，9收缩压，10心率）",defaultValue = "1")
                              @RequestParam(value="bloodType",required = true) String bloodType){
        try{
            JSONObject json = healthIndexService.healthIndex(patient,type,bloodType,timeType);
            return write(200,"获取成功","data",json);
//            return "{\"msg\":\"获取成功\",\"data\":{\"healths\":[{\"recordDate\":\"2017-3-06 15:42:48\",\"value\":\"8.3\"},{\"recordDate\":\"2017-3-05 15:42:48\",\"value\":\"8.7\"},{\"recordDate\":\"2017-3-04 15:42:48\",\"value\":\"8.1\"},{\"recordDate\":\"2017-3-03 15:42:48\",\"value\":\"4.1\"},{\"recordDate\":\"2017-3-02 15:42:48\",\"value\":\"4.2\"},{\"recordDate\":\"2017-3-01 15:42:48\",\"value\":\"7.8\"},{\"recordDate\":\"2017-2-28 15:42:48\",\"value\":\"4.7\"},{\"recordDate\":\"2017-2-27 15:42:48\",\"value\":\"8.2\"},{\"recordDate\":\"2017-2-26 15:42:48\",\"value\":\"4.5\"},{\"recordDate\":\"2017-2-25 15:42:48\",\"value\":\"4.6\"},{\"recordDate\":\"2017-2-24 15:42:48\",\"value\":\"7.3\"},{\"recordDate\":\"2017-2-23 15:42:48\",\"value\":\"7.9\"},{\"recordDate\":\"2017-2-22 15:42:48\",\"value\":\"8.9\"},{\"recordDate\":\"2017-2-21 15:42:48\",\"value\":\"8.2\"},{\"recordDate\":\"2017-2-20 15:42:48\",\"value\":\"8.6\"},{\"recordDate\":\"2017-2-19 15:42:48\",\"value\":\"8.9\"},{\"recordDate\":\"2017-2-18 15:42:48\",\"value\":\"6.2\"},{\"recordDate\":\"2017-2-17 15:42:48\",\"value\":\"8.1\"},{\"recordDate\":\"2017-2-16 15:42:48\",\"value\":\"5.1\"},{\"recordDate\":\"2017-2-15 15:42:48\",\"value\":\"6.6\"},{\"recordDate\":\"2017-2-14 15:42:48\",\"value\":\"6.0\"},{\"recordDate\":\"2017-2-13 15:42:48\",\"value\":\"6.2\"},{\"recordDate\":\"2017-2-12 15:42:48\",\"value\":\"6.0\"},{\"recordDate\":\"2017-2-11 15:42:48\",\"value\":\"8.9\"},{\"recordDate\":\"2017-2-10 15:42:48\",\"value\":\"4.4\"},{\"recordDate\":\"2017-2-09 15:42:48\",\"value\":\"7.4\"},{\"recordDate\":\"2017-2-08 15:42:48\",\"value\":\"7.3\"},{\"recordDate\":\"2017-2-07 15:42:48\",\"value\":\"7.6\"},{\"recordDate\":\"2017-2-06 15:42:48\",\"value\":\"5.7\"},{\"recordDate\":\"2017-2-05 15:42:48\",\"value\":\"6.3\"}],\"statistics\":{\"normal\":30,\"high\":10,\"low\":20}},\"status\":200}";
        }catch (Exception e){
            e.printStackTrace();
            return error(-1, "获取失败");
        }
    }
}
