package com.srs.web.controller.comprehensive;


import javax.servlet.http.HttpServletResponse;

import com.srs.comprehensive.domain.*;
import com.srs.comprehensive.service.IQuestionnaireAnswerService;
import com.srs.comprehensive.service.IQuestionnaireService;
import com.srs.system.service.ISysPostService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.*;

import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author srs
 * @date 2024-05-20
 */
@RestController
@RequestMapping("/comprehensive/answer")
@Api(value = "【请填写功能名称】管理", tags = "【请填写功能名称】管理")
public class QuestionnaireAnswerController extends BaseController {
    @Autowired
    private IQuestionnaireAnswerService answerService;
    @Autowired
    private IQuestionnaireService questionnaireService;
    @Autowired
    ISysPostService _postService;


    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询【请填写功能名称】列表
     */
//@PreAuthorize("@ss.hasPermi('system:answer:list')")
    @GetMapping("/list")
    @ApiOperation("查询【请填写功能名称】列表")
    public TableDataInfo list(QuestionnaireAnswer answer) {
        startPage();
        String stuNo = getUsername();
        //查询属于当前账户而且已尽填写完成的
        answer.setUserId(stuNo);
        answer.setStatus(1L);
        List<QuestionnaireAnswer> list = answerService.selectAnswerList(answer);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ApiOperation("导出【请填写功能名称】列表")
//    public void export(HttpServletResponse response, QuestionnaireAnswer tAnswer)
//    {
//        List<QuestionnaireAnswer> list = answerService.selectAnswerList(tAnswer);
//        ExcelUtil<QuestionnaireAnswer> util = new ExcelUtil<QuestionnaireAnswer>(QuestionnaireAnswer.class);
//        util.exportExcel(response, list, "【请填写功能名称】数据");
//    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取【请填写功能名称】详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(answerService.selectAnswerById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增【请填写功能名称】")
    public AjaxResult add(@RequestBody QuestionnaireAnswer tAnswer) {
        return toAjax(answerService.insertAnswer(tAnswer));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改【请填写功能名称】")
    public AjaxResult edit(@RequestBody QuestionnaireAnswer tAnswer) {
        return toAjax(answerService.updateAnswer(tAnswer));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除【请填写功能名称】")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(answerService.deleteAnswerByIds(ids));
    }


    @PostMapping("/queryAnswerList")
    public AjaxResult queryListByReleaseId(@RequestBody QuestionnaireAnswer answer) {
        //
        answer.setStatus(1L);
        List<QuestionnaireAnswer> answers = answerService.selectAnswerList(answer);
        return AjaxResult.success(answers);

    }

    @PostMapping("/getQuestionnaire")
    public AjaxResult getById(@RequestBody QuestionnaireAnswer answer) {

        Questionnaire questionnaire = questionnaireService.selectQuestionnaireById(answer.getQuestionnaireId());
        answer.setQuestionnaireId(questionnaire.getId());
        answer.setUserId(getUsername());
        answer.setQuestions(questionnaire.getQuestions());
        answer.setTitle(questionnaire.getTitle());
        QuestionnaireAnswer result = answerService.saveAnswer(answer);
        return success(result);
    }

    @PostMapping("/ratioScore")
    public TableDataInfo ratioScore(@RequestBody QuestionnaireResult result) {
        startPage();
        boolean isJwc = RoleBool.isJwc(getUserId(), _postService);
        List<QuestionnaireResult> list = null;
        if (isJwc) {
            list = answerService.ratioScore(result); //如果是教务除查询所有的
        } else {
            result.setDeptId(getDeptId());
            list = answerService.ratioScore(result);
        }

        return getDataTable(list);
    }


    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出【请填写功能名称】列表")
    public void export(HttpServletResponse response, QuestionnaireResult QuestionnaireResult) {

        boolean isJwc = RoleBool.isJwc(getUserId(), _postService);
        List<QuestionnaireResult> list = null;
        if (isJwc) {
            list = answerService.ratioScore(QuestionnaireResult); //如果是教务除查询所有的
        } else {

            QuestionnaireResult.setDeptId(getDeptId());
            list = answerService.ratioScore(QuestionnaireResult);
        }
        ExcelUtil<QuestionnaireResult> util = new ExcelUtil<QuestionnaireResult>(QuestionnaireResult.class);
        util.exportExcel(response, list, "问卷结果表");
    }


}
