package com.xjrsoft.module.room.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
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.room.dto.AddRoomBedDto;
import com.xjrsoft.module.room.dto.DistributeClassPageDto;
import com.xjrsoft.module.room.dto.DistributeRoomBedPageDto;
import com.xjrsoft.module.room.dto.RoomBedPageDto;
import com.xjrsoft.module.room.dto.UpdateRoomBedDto;
import com.xjrsoft.module.room.entity.RoomBed;
import com.xjrsoft.module.room.service.IRoomBedService;
import com.xjrsoft.module.room.vo.DistributeClassPageVo;
import com.xjrsoft.module.room.vo.DistributeRoomBedPageVo;
import com.xjrsoft.module.room.vo.RoomBedExcelVo;
import com.xjrsoft.module.room.vo.RoomBedPageVo;
import com.xjrsoft.module.room.vo.RoomBedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
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 org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
* @title: 寝室床位
* @Author dzx
* @Date: 2023-12-27
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomBed")
@Api(value = "/room"  + "/roomBed",tags = "寝室床位代码")
@AllArgsConstructor
public class RoomBedController {


    private final IRoomBedService roomBedService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室床位列表(分页)")
    @SaCheckPermission("roombed:detail")
    public RT<PageOutput<RoomBedPageVo>> page(@Valid RoomBedPageDto dto){

        Page<RoomBedPageVo> page = roomBedService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<RoomBedPageVo> pageOutput = ConventPage.getPageOutput(page, RoomBedPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/distribute-class-page")
    @ApiOperation(value="分配床位班级列表(分页)")
    @SaCheckPermission("roombed:detail")
    public RT<PageOutput<DistributeClassPageVo>> distributeClassPage(@Valid DistributeClassPageDto dto){
        Page<DistributeClassPageVo> page = roomBedService.getDistributeClassInfo(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<DistributeClassPageVo> pageOutput = ConventPage.getPageOutput(page, DistributeClassPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/distribute-roombed-page")
    @ApiOperation(value="分配床位寝室床位列表(分页)")
    @SaCheckPermission("roombed:detail")
    public RT<PageOutput<DistributeRoomBedPageVo>> distributeRoomBedPage(@Valid DistributeRoomBedPageDto dto){
        Page<DistributeRoomBedPageVo> page = roomBedService.getDistributeRoomBedInfo(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<DistributeRoomBedPageVo> pageOutput = ConventPage.getPageOutput(page, DistributeRoomBedPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询寝室床位信息")
    @SaCheckPermission("roombed:detail")
    public RT<RoomBedVo> info(@RequestParam Long id){
        RoomBed roomBed = roomBedService.getById(id);
        if (roomBed == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(roomBed, RoomBedVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增寝室床位")
    @SaCheckPermission("roombed:add")
    public RT<Boolean> add(@Valid @RequestBody AddRoomBedDto dto){
        RoomBed roomBed = BeanUtil.toBean(dto, RoomBed.class);
        boolean isSuccess = roomBedService.save(roomBed);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室床位")
    @SaCheckPermission("roombed:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateRoomBedDto dto){
        RoomBed roomBed = BeanUtil.toBean(dto, RoomBed.class);
        return RT.ok(roomBedService.updateById(roomBed));

    }
    @DeleteMapping
    @ApiOperation(value = "删除寝室床位")
    @SaCheckPermission("roombed:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(roomBedService.clearStudentInfo(ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<RoomBedPageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(RoomBedPageVo.class).sheet().doReadSync();
        Boolean result = roomBedService.saveBatch(BeanUtil.copyToList(savedDataList, RoomBed.class));
        return RT.ok(result);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid RoomBedPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<RoomBedExcelVo> customerList = roomBedService.getList(dto);
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, RoomBedExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

        return RT.fileStream(bot.toByteArray(), "RoomBed" + ExcelTypeEnum.XLSX.getValue());
    }
}