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.AddRoomDto;
import com.xjrsoft.module.room.dto.RoomPageDto;
import com.xjrsoft.module.room.dto.UpdateRoomDto;
import com.xjrsoft.module.room.entity.Room;
import com.xjrsoft.module.room.service.IRoomService;
import com.xjrsoft.module.room.vo.RoomExcelVo;
import com.xjrsoft.module.room.vo.RoomPageVo;
import com.xjrsoft.module.room.vo.RoomVo;
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.ArrayList;
import java.util.List;

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


    private final IRoomService roomService;

    @GetMapping(value = "/page")
    @ApiOperation(value="寝室列表(分页)")
    @SaCheckPermission("room:detail")
    public RT<PageOutput<RoomPageVo>> page(@Valid RoomPageDto dto){

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

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


    @PostMapping
    @ApiOperation(value = "新增寝室")
    @SaCheckPermission("room:add")
    public RT<String> add(@Valid @RequestBody AddRoomDto dto){
        String message = roomService.saveBatch(dto);
        if("ok".equals(message)){
            return RT.ok("true");
        }
        return RT.ok(message);
    }

    @PutMapping
    @ApiOperation(value = "修改寝室")
    @SaCheckPermission("room:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateRoomDto dto){

        Room room = BeanUtil.toBean(dto, Room.class);
        return RT.ok(roomService.updateById(room));

    }

    @DeleteMapping
    @ApiOperation(value = "删除寝室")
    @SaCheckPermission("room:delete")
    public RT<Boolean> delete(@Valid @RequestBody Long id){
        String result = roomService.deleteReachBed(id);
        if("ok".equals(result)){
            return RT.ok(true);
        }
        return RT.error(result);

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

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

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