package com.qiuguo.iot.user.api.controller;

import cn.hutool.core.util.ObjectUtil;
import com.qiuguo.iot.data.entity.user.UserRoomEntity;
import com.qiuguo.iot.data.request.user.UserRoomRequest;
import com.qiuguo.iot.data.service.user.UserRoomService;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/family")
/*
  房间管理
 */
public class RoomAdminController {

    @Resource
    private UserRoomService roomService;

    //新增
    @PostMapping("/save/room")
    public Mono<Void> roomSave(@RequestBody UserRoomEntity room) {
        room.setCreateTime(new Date());
        room.setIsDelete(1);
        room.setModifyTime(new Date());
        return roomService.insertUserRoom(room).thenEmpty(Mono.empty());
    }

    //更新
    @PostMapping("/edit/room")
    public Mono<Integer> editRoom(@RequestBody UserRoomEntity room) {
        return roomService.updateUserRoomById(room).map( integer -> integer);
    }

    //删除
    @GetMapping("/del/room")
    public Mono<Integer> delFamily(@RequestParam Long id) {
        if (ObjectUtil.isNull(id)){
            throw new RuntimeException("传入id为空");
        }
       return roomService.deleteUserRoomById(id);
    }

    //查询
    @PostMapping("/get/room")
    public Mono<PagerResult<UserRoomEntity>> getFamily(@RequestBody UserRoomRequest resp) {
       return roomService.selectUserRoomsByRequest(resp);
    }
}
