package com.yuchen.saas.park.service.controller.door;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.door.DoorDeviceEventDTO;
import com.yuchen.saas.park.api.dto.door.DoorUserInOutEventDTO;
import com.yuchen.saas.park.api.entity.door.DoorDeviceEvent;
import com.yuchen.saas.park.api.entity.door.DoorUserInOutEvent;
import com.yuchen.saas.park.service.service.door.DoorDeviceEventService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 设备事件 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorDeviceEvent")
public class DoorDeviceEventController {

    private final DoorDeviceEventService doorDeviceEventService;

    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "设备事件 新增/修改", notes = "设备事件 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody DoorDeviceEvent doorDeviceEvent) {
        if(Func.isEmpty(doorDeviceEvent)) {
            return R.error("设备事件不能为空");
        }
        return R.data(doorDeviceEventService.saveOrUpdate(doorDeviceEvent));
    }
    
    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "设备事件 详情", notes = "设备事件详情")
    public R detail(Long id) {
        return R.data(doorDeviceEventService.getById(id));
    }

    /**
     * 根据主表id删除
     * @param id
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 设备事件 删除", notes = "根据主表id 设备事件 删除")
    public R removeId(Long id) {
        doorDeviceEventService.removeById(id);
        return R.success();
    }

    /**
     * 设备事件 分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "设备事件 分页", notes = "设备事件 分页")
    public R<IPage<DoorDeviceEvent>> page(DoorDeviceEvent doorDeviceEvent, Query query) {
        IPage<DoorDeviceEvent> pages = doorDeviceEventService.page(Condition.getPage(query),
                new LambdaQueryWrapper<DoorDeviceEvent>()
                        .eq(DoorDeviceEvent::getDevicePointId, doorDeviceEvent.getDevicePointId())
                        .eq(DoorDeviceEvent::getParkId, doorDeviceEvent.getParkId())
                        .eq(DoorDeviceEvent::getEventType, doorDeviceEvent.getEventType())
                        .orderByDesc(DoorDeviceEvent::getCreateTime)
        );
        return  R.data(pages);
    }

    /**
     * 人员出入事件 分页
     */
    @PostMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "人员出入事件 分页", notes = "人员出入事件 分页")
    public R<NestPage<DoorDeviceEvent>> page(@Valid @RequestBody DoorDeviceEventDTO req) {
        Page<DoorDeviceEvent> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        return  R.data(doorDeviceEventService.paramPage(page, req));
    }


    @GetMapping("/receiveIotDoorEventInfo")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "接收iot设备事件", notes = "接收iot设备事件")
    public R receiveIotDoorEventInfo(@RequestParam("doorEventInfoJson") String doorEventInfoJson) {
        if(Func.isEmpty(doorEventInfoJson)) {
            return R.error("设备事件不能为空");
        }
        return R.data(doorDeviceEventService.receiveIotDoorEventInfo(doorEventInfoJson));
    }
}
