package com.yihu.figure.controller.patient;

import com.yihu.figure.controller.BaseController;
import com.yihu.figure.model.patient.PatientInfo;
import com.yihu.figure.service.PatientInfoService;
import com.yihu.figure.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenweida on 2017/3/7.
 */
@RestController
@RequestMapping(value = "/patient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "患者信息")
public class PatientController extends BaseController {
    @Autowired
    private PatientInfoService patientInfoService;

    @ApiOperation(value = "获取患者信息")
    @RequestMapping(value = "getPatientInfo", method = RequestMethod.GET)
    public String getPatientInfo(
            @ApiParam(name = "patientCode", value = "患者code", required = true) @RequestParam(value = "patientCode", required = true) String patientCode) {
        try {
            PatientInfo patientInfo = patientInfoService.getPatientInfo(patientCode);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", patientInfo.getName());
            jsonObject.put("code", patientInfo.getCode());
            jsonObject.put("sex", patientInfo.getSex());

            jsonObject.put("xbs", patientInfo.getXbs());
            jsonObject.put("jws", patientInfo.getJws());
            jsonObject.put("grs", patientInfo.getGrs());
            jsonObject.put("jzs", patientInfo.getJzs());
            jsonObject.put("hys", patientInfo.getHys());

            jsonObject.put("age", DateUtil.getAgeByBirthday(patientInfo.getBirthday()));
            jsonObject.put("userPortraits", patientInfo.getUserPortraits());
            jsonObject.put("suggests", patientInfo.getSuggests());
            return write(200, "获取成功！", "patient", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    @ApiOperation(value = "获取示例患者信息列表")
    @RequestMapping(value = "getPatientList", method = RequestMethod.GET)
    public String getPatientInfo() {
        try {
            JSONArray ja=new JSONArray();
            List<PatientInfo> patientInfos = patientInfoService.getPatientList();

            patientInfos.stream().forEach( patientInfo->{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", patientInfo.getName());
                jsonObject.put("code", patientInfo.getCode());
                //jsonObject.put("sex", patientInfo.getSex());

                //jsonObject.put("xbs", patientInfo.getXbs());
                //jsonObject.put("jws", patientInfo.getJws());
               // jsonObject.put("grs", patientInfo.getGrs());
               // jsonObject.put("jzs", patientInfo.getJzs());
               // jsonObject.put("hys", patientInfo.getHys());

              //  jsonObject.put("age", DateUtil.getAgeByBirthday(patientInfo.getBirthday()));
              //  jsonObject.put("userPortraits", patientInfo.getUserPortraits());
              //  jsonObject.put("suggests", patientInfo.getSuggests());
                ja.put(jsonObject);
            });
            return write(200, "获取成功！", "patient", ja);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    public static void main(String[] args) {
        List<Integer> values=new ArrayList<>();
        values.add(0);
        values.add(3);
        values.add(5);
        values.add(11);
        values.add(33);
        values.add(22);
        values.add(31);


        values.stream().filter( n-> n > 5).forEach( n-> System.out.println(n));
    }
}
