package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.service.IBaseClassService;
import com.xjrsoft.module.room.dto.AdjustBedPageDto;
import com.xjrsoft.module.room.dto.AdjustClassPageDto;
import com.xjrsoft.module.room.dto.AdjustStudentBedDto;
import com.xjrsoft.module.room.service.IRoomBedService;
import com.xjrsoft.module.room.vo.AdjustBedClassPageVo;
import com.xjrsoft.module.room.vo.AdjustBedStudentPageVo;
import com.xjrsoft.module.room.vo.NoBedStudentPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 调整床位
* @Author dzx
* @Date: 2023-12-30
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomBedAdjust")
@Api(value = "/room"  + "/roomBedAdjust",tags = "调整床位代码")
@AllArgsConstructor
public class RoomBedAdjustController {


    private final IRoomBedService roomBedService;
    private final IBaseClassService baseClassService;

    @GetMapping(value = "/class-student")
    @ApiOperation(value="需要分配/调整床位的学生")
    @SaCheckPermission("roomBedAdjust:detail")
    public RT<List<AdjustBedClassPageVo>> classStudent(@Valid AdjustClassPageDto dto){
        List<String> roleList = StpUtil.getRoleList();

        long teacherId = StpUtil.getLoginIdAsLong();
        List<BaseClass> classList = baseClassService.list(
            new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getTeacherId, teacherId)
        );
        if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
            if(classList != null && !classList.isEmpty()){
                dto.setClassId(classList.get(0).getId());
            }
        }else{
            if(classList != null && !classList.isEmpty() &&  dto.getClassId() == null){
                dto.setClassId(classList.get(0).getId());
            }else if(ObjectUtil.isNull(dto.getClassId()) && ObjectUtil.isNull(dto.getGradeId())){
                return RT.ok(new ArrayList<>());
            }
        }

        List<AdjustBedClassPageVo> result = roomBedService.getClassStudetBed(dto);
        if (result == null) {
            return RT.ok(new ArrayList<>());
        }
        return RT.ok(result);
    }

    @GetMapping(value = "/bed-student")
    @ApiOperation(value="床位学生列表")
    @SaCheckPermission("roomBedAdjust:detail")
    public RT<List<AdjustBedStudentPageVo>> distributeClassPage(@Valid AdjustBedPageDto dto){
        List<String> roleList = StpUtil.getRoleList();

        long teacherId = StpUtil.getLoginIdAsLong();
        List<BaseClass> classList = baseClassService.list(
                new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getTeacherId, teacherId)
        );
        if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
            if(classList != null && !classList.isEmpty()){
                dto.setClassId(classList.get(0).getId());
            }
        }
        List<AdjustBedStudentPageVo> result = roomBedService.getBedStudetInfo(dto);
        if (result == null) {
            return RT.ok(new ArrayList<>());
        }
        return RT.ok(result);
    }

    @GetMapping(value = "/is-class-teacher")
    @ApiOperation(value="判断登录者是否是班主任")
    @SaCheckPermission("roomBedAdjust:detail")
    public RT<Integer> getLoginRole(@Valid AdjustBedPageDto dto){
        List<String> roleList = StpUtil.getRoleList();

        if(roleList.size() == 2 && roleList.contains("CLASSTE") && roleList.contains("TEACHER")){
            return RT.ok(1);
        }
        return RT.ok(0);
    }

    @GetMapping(value = "/no-bed-student")
    @ApiOperation(value="未分配床位的学生(分页)")
    @SaCheckPermission("roomBedAdjust:detail")
    public RT<PageOutput<NoBedStudentPageVo>> noBedStudent(@Valid AdjustBedPageDto dto){
        Page<NoBedStudentPageVo> page = roomBedService.getNoBedStudent(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<NoBedStudentPageVo> pageOutput = ConventPage.getPageOutput(page, NoBedStudentPageVo.class);
        return RT.ok(pageOutput);
    }


    @DeleteMapping
    @ApiOperation(value = "批量移出学生")
    @SaCheckPermission("roomBedAdjust:delete")
    public RT<Boolean> delete(@Valid @RequestBody Long id){
        return RT.ok(roomBedService.clearStudentInfoByRoomId(id));
    }

    @DeleteMapping(value="/delete-one")
    @ApiOperation(value = "单个移出学生")
    @SaCheckPermission("roomBedAdjust:delete")
    public RT<Boolean> deleteOne(@Valid @RequestBody Long id){
        return RT.ok(roomBedService.clearStudentInfoByBedId(id));
    }

    @PutMapping
    @ApiOperation(value = "修改学生的寝室床位")
    @SaCheckPermission("roomBedAdjust:edit")
    public RT<Boolean> adjustBed(@Valid @RequestBody AdjustStudentBedDto dto){
        Boolean result = roomBedService.adjustBed(dto);
        return RT.ok(result);
    }

    @PutMapping("/adjustBedBatch")
    @ApiOperation(value = "批量修改学生的寝室床位")
    @SaCheckPermission("roomBedAdjust:edit")
    public RT<Boolean> adjustBedBatch(@Valid @RequestBody List<AdjustStudentBedDto> dto){
        Boolean result = roomBedService.adjustBedBatch(dto);
        return RT.ok(result);
    }
}