package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.security.annotation.InnerAuth;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.domain.SysReClassUser;
import com.vctgo.system.domain.dto.BindClassUserDto;
import com.vctgo.system.service.ISysReClassUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 班级配置人员信息Controller
 *
 * @author wanglonghua
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/allocation/class/user")
public class SysReClassUserController extends BaseController {
    @Autowired
    private ISysReClassUserService sysReClassUserService;

    /**
     * 查询班级配置人员信息分页列表
     */
    @RequiresPermissions("system:re_class_user:page")
    @GetMapping("/page")
    public AjaxResult page(SysReClassUser sysReClassUser) {
        IPage<SysReClassUser> pageList = sysReClassUserService.selectSysReClassUserPage(sysReClassUser);
        return AjaxResult.success(pageList);
    }

    /**
     * 获取班级配置人员信息列表
     */
    @GetMapping("/list")
    public AjaxResult getList(@SpringQueryMap SysReClassUser sysReClassUser) {
        List<SysReClassUser> sysReClassUserList = sysReClassUserService.selectSysReClassUserList(sysReClassUser);
        return AjaxResult.success(sysReClassUserList);
    }

    /**
     * 新增班级配置人员信息
     */
    @RequiresPermissions("system:allocation_teacher:add")
    @Transactional(rollbackFor = Exception.class)
    @PutMapping(value = "/teacher")
    public AjaxResult allocationTeacher(@RequestBody BindClassUserDto bindClassUserDto) {
        return AjaxResult.success(sysReClassUserService.allocationSysReClassUser(bindClassUserDto));
    }

    /**
     * 新增班级配置人员信息
     */
    @RequiresPermissions("system:allocation_childhood:add")
    @Transactional(rollbackFor = Exception.class)
    @PutMapping(value = "/childhood")
    public AjaxResult allocationChildhood(@RequestBody BindClassUserDto bindClassUserDto) {
        return AjaxResult.success(sysReClassUserService.allocationSysReClassUser(bindClassUserDto));
    }

    /**
     * 查看班级配置的人员明细
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/{classId}/detail")
    public AjaxResult getClassAllocationDetail(@PathVariable(name = "classId") Long classId,
                                               @RequestParam(name = "bindUserPosition", required = false) Integer bindUserPosition) {
        BindClassUserDto bindClassUserDto = new BindClassUserDto();
        bindClassUserDto.setClassId(classId);
        bindClassUserDto.setBindType(bindUserPosition);
        return AjaxResult.success(sysReClassUserService.getClassAllocationDetail(bindClassUserDto));
    }

    /**
     * 查看指定岗位的指定人员绑定的全部班级列表
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/list/{userId}/{bindUserPosition}")
    public AjaxResult getPointUserClassList(@PathVariable(name = "userId") Long userId,
                                            //1:主班教师，2：配班教师，3：保育员，4：幼儿，5：教师岗（主班教师、配班教师、保育员，按计划应更新为123）
                                            //12:教师岗（主班教师、配班教师），后续计划需要哪几个职位，直接拼接（1-4,6-9为系统固定存在的人员职位）
                                            @PathVariable(name = "bindUserPosition") Integer bindUserPosition) {
        return AjaxResult.success(sysReClassUserService.getPointUserClassList(userId, bindUserPosition));
    }

    /**
     * 查看指定岗位的指定人员绑定的全部班级列表
     */
    @InnerAuth
    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/external/list/{userId}/{bindUserPosition}")
    public AjaxResult getUserClassDataList(@PathVariable(name = "userId") Long userId,
                                           //1:主班教师，2：配班教师，3：保育员，4：幼儿，5：教师岗（主班教师、配班教师、保育员）
                                           @PathVariable(name = "bindUserPosition") Integer bindUserPosition) {
        return AjaxResult.success(sysReClassUserService.getPointUserClassList(userId, bindUserPosition));
    }

    /**
     * 获取班级配置人员信息详细信息
     */
    @RequiresPermissions("system:re_class_user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysReClassUserService.selectSysReClassUserById(id));
    }

    /**
     * 修改班级配置人员信息
     */
    @RequiresPermissions("system:re_class_user:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysReClassUser sysReClassUser) {
        return toAjax(sysReClassUserService.updateSysReClassUser(sysReClassUser));
    }

    /**
     * 逻辑删除班级配置人员信息
     */
    @RequiresPermissions("system:re_class_user:abandon")
    @PutMapping("/abandon/{ids}")
    public AjaxResult abandon(@PathVariable Long[] ids) {
        return toAjax(sysReClassUserService.logicalDeleteSysReClassUserByIds(ids));
    }

    /**
     * 删除班级配置人员信息
     */
    @RequiresPermissions("system:re_class_user:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysReClassUserService.deleteSysReClassUserByIds(ids));
    }
}
