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.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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 = 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(
            @ApiParam(allowMultiple = true, defaultValue = "{\n" +
                    "    \"surveyCode\": \"问卷id\",\n" +
                    "    \"questions\": [\n" +
                    "        {\n" +
                    "            \"qstCode\": \"问题id\",\n" +
                    "            \"sort\": \"1\",\n" +
                    "            \"type\": \"0\",\n" +
                    "            \"options\": [\n" +
                    "                {\n" +
                    "                    \"optionCode\": \"选项id\",\n" +
                    "                    \"sort\": \"2\"\n" +
                    "                }\n" +
                    "            ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"qstCode\": \"问题id\",\n" +
                    "            \"type\": \"1\",\n" +
                    "            \"sort\": \"2\",\n" +
                    "            \"options\": [\n" +
                    "                {\n" +
                    "                    \"optionCode\": \"选项id\",\n" +
                    "                    \"sort\": \"1\",\n" +
                    "                    \"comment\": \"选项说明可为null\"\n" +
                    "                },\n" +
                    "                {\n" +
                    "                    \"optionCode\": \"选项id\",\n" +
                    "                    \"sort\": \"3\",\n" +
                    "                    \"comment\": \"选项说明可为null\"\n" +
                    "                },\n" +
                    "                {\n" +
                    "                    \"optionCode\": \"选项id\",\n" +
                    "                    \"sort\": \"5\",\n" +
                    "                    \"comment\": \"选项说明可为null\"\n" +
                    "                }\n" +
                    "            ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"qstCode\": \"问答题题目ID\",\n" +
                    "            \"content\": \"问答题答案\",\n" +
                    "            \"sort\": \"3\",\n" +
                    "            \"type\": \"2\"\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}")
            @RequestParam String jsonData) {
        try {
            String patient = getUID();
            JSONObject json = new JSONObject(jsonData);
            patientQuestionnaireService.saveAnswer(patient, json);
            return write(200, "保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return write(-1, "保存失败！");
        }

    }
}
