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.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.AddRoomBedCheckRecordDto;
import com.xjrsoft.module.room.dto.RoomBedCheckRecordPageDto;
import com.xjrsoft.module.room.dto.UpdateRoomBedCheckRecordDto;
import com.xjrsoft.module.room.entity.RoomBedCheckRecord;
import com.xjrsoft.module.room.service.IRoomBedCheckRecordService;
import com.xjrsoft.module.room.vo.RoomBedCheckRecordEchoVo;
import com.xjrsoft.module.room.vo.RoomBedCheckRecordPageVo;
import com.xjrsoft.module.room.vo.RoomBedCheckRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 查寝记录
* @Author szs
* @Date: 2024-01-02
* @Version 1.0
*/
@RestController
@RequestMapping("/room" + "/roomBedCheckRecord")
@Api(value = "/room"  + "/roomBedCheckRecord",tags = "查寝记录代码")
@AllArgsConstructor
public class RoomBedCheckRecordController {


    private final IRoomBedCheckRecordService roomBedCheckRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="查寝记录列表(分页)")
    @SaCheckPermission("roombedcheckrecord:detail")
    public RT<PageOutput<RoomBedCheckRecordPageVo>> page(@Valid RoomBedCheckRecordPageDto dto){
        IPage<RoomBedCheckRecordPageVo> page = roomBedCheckRecordService.getPage(dto);
        PageOutput<RoomBedCheckRecordPageVo> pageOutput = ConventPage.getPageOutput(page, RoomBedCheckRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/page-mobile")
    @ApiOperation(value="查寝记录列表(分页-移动)")
    @SaCheckPermission("roombedcheckrecord:detail")
    public RT<PageOutput<RoomBedCheckRecordPageVo>> pageMobile(@Valid RoomBedCheckRecordPageDto dto){
        IPage<RoomBedCheckRecordPageVo> page = roomBedCheckRecordService.getPageMobile(dto);
        PageOutput<RoomBedCheckRecordPageVo> pageOutput = ConventPage.getPageOutput(page, RoomBedCheckRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询查寝记录信息")
    @SaCheckPermission("roombedcheckrecord:detail")
    public RT<RoomBedCheckRecordVo> info(@RequestParam Long id){
        RoomBedCheckRecordVo roomBedCheckRecordVo = roomBedCheckRecordService.getInfo(id);
        if (roomBedCheckRecordVo == null) {
           return RT.error("找不到此数据！");
        }

        return RT.ok(roomBedCheckRecordVo);
    }

    @GetMapping(value = "/echo")
    @ApiOperation(value="查寝记录回显")
    @SaCheckPermission("roombedcheckrecord:detail")
    public RT<RoomBedCheckRecordEchoVo> echo(){
        RoomBedCheckRecordEchoVo roomBedCheckRecordEchoVo = roomBedCheckRecordService.getEcho();
        return RT.ok(roomBedCheckRecordEchoVo);
    }

    @PostMapping
    @ApiOperation(value = "新增查寝记录")
    @SaCheckPermission("roombedcheckrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddRoomBedCheckRecordDto dto){
        RoomBedCheckRecord roomBedCheckRecord = BeanUtil.toBean(dto, RoomBedCheckRecord.class);
        boolean isSuccess = roomBedCheckRecordService.add(roomBedCheckRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改查寝记录")
    @SaCheckPermission("roombedcheckrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateRoomBedCheckRecordDto dto){

        RoomBedCheckRecord roomBedCheckRecord = BeanUtil.toBean(dto, RoomBedCheckRecord.class);
        return RT.ok(roomBedCheckRecordService.update(roomBedCheckRecord));

    }

    @DeleteMapping
    @ApiOperation(value = "删除查寝记录")
    @SaCheckPermission("roombedcheckrecord:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(roomBedCheckRecordService.delete(ids));

    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid RoomBedCheckRecordPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<RoomBedCheckRecordPageVo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<RoomBedCheckRecordPageVo>) page(dto).getData()).getList();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, RoomBedCheckRecordPageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

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