package com.srs.web.controller.routine;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.comprehensive.domain.CphTeacher;
import com.srs.comprehensive.domain.SrsClass;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.CphTeacherMapper;
import com.srs.comprehensive.mapper.SrsClassMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.routine.service.IRtClassMettingStuUploadService;
import com.srs.system.service.ISysRoleService;
import com.srs.web.controller.common.RoleBool;
import liquibase.pro.packaged.A;
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.routine.domain.RtClassMettingContent;
import com.srs.routine.service.IRtClassMettingContentService;
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 static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 班会内容Controller
 *
 * @author srs
 * @date 2024-05-18
 */
@RestController
@RequestMapping("/classmetting/classMettingContent")
@Api(value = "班会内容管理", tags = "班会内容管理")
public class RtClassMettingContentController extends BaseController {
    @Autowired
    private IRtClassMettingContentService rtClassMettingContentService;

    @Autowired
    private IRtClassMettingStuUploadService rtClassMettingStuUploadService;

    @Autowired
    ISysRoleService _roleService;

    @GetMapping("/listStuFdyClass")
    @ApiOperation("学生查询自己的辅导员带的班级")
    public AjaxResult listStuFdyClass() {
        return rtClassMettingContentService.listStuFdyClass();
    }


    @GetMapping("/listStuByClassId/{classId}")
    @ApiOperation("通过班级id查询学生")
    public AjaxResult listStuByClassId(@PathVariable Long classId) {
        boolean hasRole = RoleBool.notStu(getUserId(), _roleService);
        if (hasRole) {
            return rtClassMettingContentService.listStuByClassId(classId);
        } else {
            return AjaxResult.error(401, "你无权做这些");
        }
    }

    @Autowired
    private SrsStudentMapper _stuMapper;

    @Autowired
    private SrsClassMapper _classMapper;

    @Autowired
    private CphTeacherMapper _fdyMapper;

    @GetMapping("/listStu")
    @ApiOperation("学生查询辅导员班会内容列表")
    public TableDataInfo listStu(RtClassMettingContent rtClassMettingContent) {
        List<Map<String, Object>> list1 = rtClassMettingStuUploadService.listStuClass(getUsername());
        if (list1.size() == 0) {
            throw new RuntimeException("未查询到您的学生信息,请联系管理员");
        }
        QueryWrapper<SrsStudent> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("stu_no", getUsername());
        studentQueryWrapper.last("limit 1");
        SrsStudent student = _stuMapper.selectOne(studentQueryWrapper);
        if (student == null) {
            return getDataTable(new ArrayList<Map<String, Object>>());
        }
        Long classId = student.getClassId();
        if (classId == null) {
            return getDataTable(new ArrayList<Map<String, Object>>());
        }
        SrsClass srsClass = _classMapper.selectSrsClassByClassId(classId);
        if (srsClass == null) {
            return getDataTable(new ArrayList<Map<String, Object>>());
        }
        Long teacherId = srsClass.getTeacherId();

        CphTeacher fdy = _fdyMapper.selectById(teacherId);
        if (fdy == null) {
            return getDataTable(new ArrayList<Map<String, Object>>());
        }
        rtClassMettingContent.setCrTeacherNo(fdy.getEmployeeId());
        startPage();
        List<RtClassMettingContent> list = rtClassMettingContentService.selectRtClassMettingContentList(rtClassMettingContent);
        //list.add(1);
        return getDataTable(list);
    }


    @GetMapping("/listFdy")
    @ApiOperation("查询辅导员班会内容列表")
    public TableDataInfo listFdy(RtClassMettingContent rtClassMettingContent) {
        rtClassMettingContent.setCrTeacherNo(getUsername());
        startPage();
        List<RtClassMettingContent> list = rtClassMettingContentService.selectRtClassMettingContentList(rtClassMettingContent);
        //list.add(1);
        return getDataTable(list);
    }


    /**
     * 查询班会内容列表
     */
    //@PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:list')")
    @GetMapping("/list")
    @ApiOperation("查询班会内容列表")
    public TableDataInfo list(RtClassMettingContent rtClassMettingContent) {
        startPage();
        List<RtClassMettingContent> list = rtClassMettingContentService.selectRtClassMettingContentList(rtClassMettingContent);
        //list.add(1);
        return getDataTable(list);
    }

    /**
     * 导出班会内容列表
     */
    //@PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:export')")
    @Log(title = "班会内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出班会内容列表")
    public void export(HttpServletResponse response, RtClassMettingContent rtClassMettingContent) {
        List<RtClassMettingContent> list = rtClassMettingContentService.selectRtClassMettingContentList(rtClassMettingContent);
        ExcelUtil<RtClassMettingContent> util = new ExcelUtil<RtClassMettingContent>(RtClassMettingContent.class);
        util.exportExcel(response, list, "班会内容数据");
    }

    /**
     * 获取班会内容详细信息
     */
    //@PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:query')")
    @GetMapping(value = "/{crId}")
    @ApiOperation("获取班会内容详细信息")
    public AjaxResult getInfo(@PathVariable("crId") Long crId) {
        return success(rtClassMettingContentService.selectRtClassMettingContentByCrId(crId));
    }

    @Log(title = "班会内容", businessType = BusinessType.INSERT)
    @PostMapping("/stuAdd")
    @ApiOperation("学生新增班会内容")
    public AjaxResult stuAdd(@RequestBody RtClassMettingContent rtClassMettingContent) {
        List<Map<String, Object>> list = rtClassMettingStuUploadService.listStuClass(getUsername());
        if (list.size() == 0) {
            throw new RuntimeException("您的班级为空,请联系管理员");
        }
        Map<String, Object> map = list.get(0);

        rtClassMettingContent.setCrTeacherNo(map.get("t_no").toString());
        rtClassMettingContent.setCreateBy(getUsername());
        return rtClassMettingContentService.insertRtClassMettingContent(rtClassMettingContent);
    }

    /**
     * 新增班会内容
     */
    // @PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:add')")
    @Log(title = "班会内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增班会内容")
    public AjaxResult add(@RequestBody RtClassMettingContent rtClassMettingContent) {
        rtClassMettingContent.setCrTeacherNo(getUsername());
        rtClassMettingContent.setCreateBy(getUsername());
        return rtClassMettingContentService.insertRtClassMettingContent(rtClassMettingContent);
    }

    /**
     * 修改班会内容
     */
    //@PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:edit')")
    @Log(title = "班会内容", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改班会内容")
    public AjaxResult edit(@RequestBody RtClassMettingContent rtClassMettingContent) {
        rtClassMettingContent.setUpdateBy(getUsername());
        return toAjax(rtClassMettingContentService.updateRtClassMettingContent(rtClassMettingContent));
    }

    /**
     * 删除班会内容
     */
    //@PreAuthorize("@ss.hasPermi('classmetting:classMettingContent:remove')")
    @Log(title = "班会内容", businessType = BusinessType.DELETE)
    @PostMapping("/{crIds}")
    @ApiOperation("删除班会内容")
    public AjaxResult remove(@PathVariable Long[] crIds) {
        return toAjax(rtClassMettingContentService.deleteRtClassMettingContentByCrIds(crIds));
    }
}
