package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.room.dto.AddRoomStudentAppointDto;
import com.xjrsoft.module.room.dto.RoomStudentAppointPageDto;
import com.xjrsoft.module.room.dto.UpdateRoomStudentAppointDto;
import com.xjrsoft.module.room.entity.RoomStudentAppoint;
import com.xjrsoft.module.room.service.IRoomStudentAppointService;
import com.xjrsoft.module.room.vo.AppointPageRoomBedVo;
import com.xjrsoft.module.room.vo.HeadTeaRoomCadreAppointPageVo;
import com.xjrsoft.module.room.vo.RoomStudentAppointVo;
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.PostMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
* @title: 寝室长任命
* @Author dzx
* @Date: 2023-12-30
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomStudentAppoint")
@Api(value = "/room"  + "/roomStudentAppoint",tags = "寝室干部任命代码")
@AllArgsConstructor
public class RoomStudentAppointController {


    private final IRoomStudentAppointService roomStudentAppointService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室干部任命列表(分页)")
    @SaCheckPermission("roomstudentappoint:detail")
    public RT<PageOutput<HeadTeaRoomCadreAppointPageVo>> page(@Valid RoomStudentAppointPageDto dto){

        IPage<HeadTeaRoomCadreAppointPageVo> page = roomStudentAppointService.getPage(dto);
        PageOutput<HeadTeaRoomCadreAppointPageVo> pageOutput = ConventPage.getPageOutput(page, HeadTeaRoomCadreAppointPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/getStuByRoomId")
    @ApiOperation(value="根据寝室id获取该寝室的所有已入住人员(不分页)")
    @SaCheckPermission("roomstudentappoint:detail")
    public RT<List<AppointPageRoomBedVo>> getStuByRoomId(@Valid RoomStudentAppointPageDto dto){
        List<AppointPageRoomBedVo> appointPageRoomBedVoList = roomStudentAppointService.getStuByRoomId(dto);
        return RT.ok(appointPageRoomBedVoList);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询寝室干部信息")
    @SaCheckPermission("roomstudentappoint:detail")
    public RT<RoomStudentAppointVo> info(@RequestParam Long id){
        RoomStudentAppoint roomStudentAppoint = roomStudentAppointService.getById(id);
        if (roomStudentAppoint == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(roomStudentAppoint, RoomStudentAppointVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增寝室干部任命")
    @SaCheckPermission("roomstudentappoint:add")
    public RT<Boolean> add(@Valid @RequestBody AddRoomStudentAppointDto dto){
        RoomStudentAppoint roomStudentAppoint = BeanUtil.toBean(dto, RoomStudentAppoint.class);
        boolean isSuccess = roomStudentAppointService.save(roomStudentAppoint);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室干部任命")
    @SaCheckPermission("roomstudentappoint:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateRoomStudentAppointDto dto){

        RoomStudentAppoint roomStudentAppoint = BeanUtil.toBean(dto, RoomStudentAppoint.class);
        return RT.ok(roomStudentAppointService.updateById(roomStudentAppoint));

    }

    @PutMapping("/appoint")
    @ApiOperation(value = "任命寝室干部，寝室干部记录中存在更新，不存在新增")
    @SaCheckPermission("roomstudentappoint:edit")
    public RT<Boolean> appoint(@Valid @RequestBody UpdateRoomStudentAppointDto dto){
        LambdaQueryWrapper<RoomStudentAppoint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(RoomStudentAppoint::getRoomId,dto.getRoomId())
                .eq(RoomStudentAppoint::getPostId,dto.getPostId());
        RoomStudentAppoint roomStudentAppoint = roomStudentAppointService.getOne(queryWrapper);
        //为空，表示该寝室该职位没有安排人
        if(ObjectUtil.isNull(roomStudentAppoint)){
            dto.setId(null);
            RoomStudentAppoint roomStudentAppointInsert = BeanUtil.toBean(dto, RoomStudentAppoint.class);
            boolean isSuccess = roomStudentAppointService.save(roomStudentAppointInsert);
            return RT.ok(isSuccess);
        }else{
            RoomStudentAppoint roomStudentAppointUpdate = BeanUtil.toBean(dto, RoomStudentAppoint.class);
            roomStudentAppointUpdate.setId(roomStudentAppoint.getId());
            return RT.ok(roomStudentAppointService.updateById(roomStudentAppointUpdate));
        }
    }

    @DeleteMapping
    @ApiOperation(value = "删除寝室干部任命")
    @SaCheckPermission("roomstudentappoint:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(roomStudentAppointService.removeBatchByIds(ids));

    }

}