package com.srs.web.controller.comprehensive;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.srs.comprehensive.domain.QuestionnaireAnswer;
import com.srs.comprehensive.domain.QuestionnaireRelease;
import com.srs.comprehensive.service.IQuestionnaireAnswerService;
import com.srs.comprehensive.service.IQuestionnaireReleaseService;
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;

/**
 * 【请填写功能名称】Controller
 *
 * @author srs
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/comprehensive/release")
@Api(value = "【请填写功能名称】管理", tags = "【请填写功能名称】管理")
public class QuestionnaireReleaseController extends BaseController {
    @Autowired
    private IQuestionnaireReleaseService releaseService;
    @Autowired
    private IQuestionnaireAnswerService answerService;

    @Autowired
    ISysPostService _postService;

    @Autowired
    private ISysRoleService roleService;
    /**
     * 查询【请填写功能名称】列表
     */
//@PreAuthorize("@ss.hasPermi('system:release:list')")
@GetMapping("/list")
@ApiOperation("查询【请填写功能名称】列表")
    public TableDataInfo list(QuestionnaireRelease release)
    {

        boolean isJwc = RoleBool.isJwc(getUserId(),_postService);
        boolean isStu = RoleBool.isStu(getUserId(), roleService);
        release.setStatus("1");//查询已经开放填写的问卷
        release.setUserName(getUsername()); //用来判断是否已经填写过了
        List<QuestionnaireRelease> list = null;
        if(isJwc){
            startPage();
            list = releaseService.selectReleaseList(release); //如果是教务除查询所有的
        }
        else if(isStu){
            startPage();
            release.setStuNo(getUsername()); //用户筛选学生要填的
            list = releaseService.selectReleaseList(release);
        }else{
            startPage();
            release.setDeptId(getDeptId());
            list = releaseService.selectReleaseList(release);
        }

        return getDataTable(list);
    }

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

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

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

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

    /**
     * 删除【请填写功能名称】
     */

    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    @ApiOperation("删除【请填写功能名称】")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(releaseService.deleteTReleaseByIds(ids));
    }


    @GetMapping("/get/{id}")
    public AjaxResult getById(@PathVariable("id") Long id)
    {
        QuestionnaireRelease questionnaire= releaseService.selectTReleaseById(id);
        QuestionnaireAnswer answer = new QuestionnaireAnswer();

        answer.setUserId(getUsername());
        answer.setQuestions(questionnaire.getQuestions());
        answer.setTitle(questionnaire.getTitle());
        QuestionnaireAnswer result = answerService.saveAnswer(answer);


        return success(result);
    }
}
