package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.QuestionnaireTopicDao;
import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityDto;
import com.ym.mec.biz.dal.entity.QuestionnaireTopic;
import com.ym.mec.biz.dal.entity.QuestionnaireUserResult;
import com.ym.mec.biz.dal.enums.QuestionnaireActiveTypeEnum;
import com.ym.mec.biz.service.QuestionnaireUserResultService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RequestMapping("questionnaireUserResult")
@Api(tags = "问题答案")
@RestController
public class QuestionnaireUserResultController extends BaseController {

    @Autowired
    private QuestionnaireUserResultService questionnaireUserResultService;
    @Autowired
    private QuestionnaireTopicDao questionnaireTopicDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "提交调查问卷")
    @PostMapping("/add")
    public Object add(@RequestBody ReplacementInstrumentActivityDto replacementInstrumentActivityDto) {
        questionnaireUserResultService.add(replacementInstrumentActivityDto);
        return succeed();
    }

    @ApiOperation(value = "获取用户答题详情")
    @RequestMapping("/queryUserResult")
    public Object queryUserResult(Integer topicId) {
        return succeed(questionnaireUserResultService.queryUserResult(topicId));
    }

    @ApiOperation(value = "是否可以提交问卷")
    @GetMapping("/commitFlag")
    public Object commitFlag(Integer cooperationId, QuestionnaireActiveTypeEnum activeType) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        HashMap<String, Object> paramMap = new HashMap<>(3);
        paramMap.put("activeId",cooperationId);
        paramMap.put("activeType",activeType.getCode());
        paramMap.put("userId",sysUser.getId());
        List<QuestionnaireUserResult> userResults = questionnaireUserResultService.findAll(paramMap);
        if(userResults == null || userResults.size() == 0){
            return succeed(true);
        }
        QuestionnaireUserResult result = userResults.get(0);
        QuestionnaireTopic topic = questionnaireTopicDao.get(result.getQuestionnaireTopicId());
        if(topic.getAgainCommitFlag() == 1){
            return succeed(true);
        }
        return succeed(false);
    }
}
