package com.tzy.controller.appuser;

import com.tzy.annotation.ApiLimitRule;
import com.tzy.common.config.handle.ApiVersion;
import com.tzy.common.constant.MsgConstants;
import com.tzy.common.dto.OutDTO;
import com.tzy.common.dto.UserInfo;
import com.tzy.common.exception.AppAssert;
import com.tzy.common.utils.UserUtils;
import com.tzy.dto.QuestionParam;
import com.tzy.framework.web.domain.LimitRule;
import com.tzy.pojo.question.Questionnaire;
import com.tzy.service.question.QuestionnaireService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(value = "问卷")
@RestController
@RequestMapping("/api/{version}/questionnaire")
@Slf4j
public class QuestionnaireController {

    @Resource
    private QuestionnaireService questionnaireService;

    private static final String QUESTIONNAIRE_SUCCESS_MSG =  "【感谢您的参与！您的消费额度已提升】";
    private static final String QUESTIONNAIRE_FAIL_MSG =   "【您的消费额度提升失败！如有问题请联系平台客服】";

    /**
     * 查询问卷
     */
    @ApiVersion(1.0)
    @PostMapping(value = "/{id}")
    @ResponseBody
    public OutDTO getQuestionnaireDetail(@PathVariable("id") Long id){
        Questionnaire questionnaire = questionnaireService.getQuestionnaireDetail(id);
        return OutDTO.ok().put("questionnaire", questionnaire);
    }

    /**
     * 查询问卷
     */
    @ApiVersion(1.0)
    @ApiLimitRule(seconds = 2,limitCount=1,type= LimitRule.USER,msg="操作频繁,请稍后再试!")
    @PostMapping(value = "/submit")
    @ResponseBody
    public OutDTO submitQuestionnaire(@RequestBody QuestionParam questionParam) {
        AppAssert.notNull(questionParam, MsgConstants.PARAM_EMPTY);
        AppAssert.notNull(questionParam.getQuestionnaireId(), MsgConstants.PARAM_EMPTY);
        AppAssert.notEmpty(questionParam.getQuestionAnswers(), MsgConstants.PARAM_EMPTY);
        UserInfo user = UserUtils.getSimpleUserInfo(true);
        AppAssert.notNull(user, MsgConstants.NEED_LOGIN);
        questionParam.setUserId(user.getId());
        boolean result = questionnaireService.submitQuestionnaire(questionParam);
        return OutDTO.ok(result ? QUESTIONNAIRE_SUCCESS_MSG : QUESTIONNAIRE_FAIL_MSG).put("result", result);
    }
}
