package com.srs.web.controller.teacher;

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

import com.srs.common.core.domain.entity.SysUser;
import com.srs.system.service.ISysUserService;
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 org.springframework.security.access.prepost.PreAuthorize;

import com.srs.teacher.domain.SysTeacherContest;
import com.srs.teacher.service.ISysTeacherContestService;
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;

/**
 * contestController
 *
 * @author srs
 * @date 2024-06-29
 */
@RestController
@RequestMapping("/teacher/contest")
@Api(value = "contest管理", tags = "contest管理")
public class SysTeacherContestController extends BaseController {
    @Autowired
    private ISysTeacherContestService sysTeacherContestService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询contest列表
     */
@PreAuthorize("@ss.hasPermi('teacher:contest:list')")
@GetMapping("/list")
@ApiOperation("查询contest列表")
    public TableDataInfo list(SysTeacherContest sysTeacherContest)
    {
        startPage();
        List<SysTeacherContest> list = sysTeacherContestService.selectSysTeacherContestList(sysTeacherContest);
        return getDataTable(list);
    }

    /**
     * 导出contest列表
     */
    @PreAuthorize("@ss.hasPermi('teacher:contest:export')")
    @Log(title = "contest", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出contest列表")
    public void export(HttpServletResponse response, SysTeacherContest sysTeacherContest)
    {
        List<SysTeacherContest> list = sysTeacherContestService.selectSysTeacherContestList(sysTeacherContest);
        //10.16 导出信息补充
        SysUser u = userService.selectUserByUserName(sysTeacherContest.getTeacherId().toString());
        for(int i=0;i<list.size();i++){
            list.get(i).setTeacherName(u.getUserName());
            list.get(i).setTeacherCollege(u.getDept().getDeptName());
        }
        ExcelUtil<SysTeacherContest> util = new ExcelUtil<SysTeacherContest>(SysTeacherContest.class);
        util.exportExcel(response, list, "contest数据");
    }

    /**
     * 获取contest详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:contest:query')")
    @GetMapping(value = "/{contestId}")
    @ApiOperation("获取contest详细信息")
    public AjaxResult getInfo(@PathVariable("contestId") Long contestId)
    {
        return success(sysTeacherContestService.selectSysTeacherContestByContestId(contestId));
    }

    /**
     * 新增contest
     */
    @PreAuthorize("@ss.hasPermi('teacher:contest:add')")
    @Log(title = "contest", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增contest")
    public AjaxResult add(@RequestBody SysTeacherContest sysTeacherContest)
    {
        return toAjax(sysTeacherContestService.insertSysTeacherContest(sysTeacherContest));
    }

    /**
     * 修改contest
     */
    @PreAuthorize("@ss.hasPermi('teacher:contest:edit')")
    @Log(title = "contest", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改contest")
    public AjaxResult edit(@RequestBody SysTeacherContest sysTeacherContest)
    {
        return toAjax(sysTeacherContestService.updateSysTeacherContest(sysTeacherContest));
    }

    /**
     * 删除contest
     */
    @PreAuthorize("@ss.hasPermi('teacher:contest:remove')")
    @Log(title = "contest", businessType = BusinessType.DELETE)
    @PostMapping("/{contestIds}")
    @ApiOperation("删除contest")
    public AjaxResult remove(@PathVariable Long[] contestIds)
    {
        return toAjax(sysTeacherContestService.deleteSysTeacherContestByContestIds(contestIds));
    }
}
