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

import com.yihu.wlyy.service.survey.PatientQuestionnaireService;
import com.yihu.wlyy.web.BaseController;
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 Reece on 2017/3/14.
 */
@Controller
@RequestMapping(value = "/patient/questionnaire")
@Api(description = "居民端问卷调查")
public class PatientQuestionnaireController extends BaseController {
    @Autowired
    private PatientQuestionnaireService patientQuestionnaireService;

    @RequestMapping(value = "getSurveySummary", method = RequestMethod.GET)
    @ApiOperation(value = "居民端获取问卷概述")
    @ResponseBody
    public String getSurveySummary(
            @ApiParam(value = "问卷编码")
            @RequestParam String id) {
        try {
            String patient = getRepUID();
//            String patient = getUID();
            JSONObject jsonObject = patientQuestionnaireService.getSurveySummary(patient, id);
            return write(200, "查询成功!", "data", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return write(-1, "查询失败!");
        }
    }

    @RequestMapping(value = "getQuestions", method = RequestMethod.GET)
    @ApiOperation(value = "按升序获取问卷的问题")
    @ResponseBody
    public String getQuestions(
            @ApiParam(value = "问卷编码")
            @RequestParam String id,
            @ApiParam(value = "当前问题的sort（初始为1）")
            @RequestParam Integer sort) {
        try {
            JSONObject jsonObject = patientQuestionnaireService.getQuestions(id, sort);
            return write(200, "查询成功！", "data", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            return write(-1, "查询失败!");
        }
    }

    @RequestMapping(value = "saveAnswer", method = RequestMethod.POST)
    @ApiOperation(value = "保存用户答案")
    @ResponseBody
    public String saveAnswer(
            @RequestParam String jsonData) {
        try {
            String patient = getRepUID();
//            String patient = getUID();
            JSONObject json = new JSONObject(jsonData);
            patientQuestionnaireService.saveAnswer(patient, json);
            return write(200, "保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return write(-1, "保存失败！");
        }

    }
}
