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.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.door.DoorDeviceGroupItem;
import com.yuchen.saas.park.api.entity.door.DoorDevicePoint;
import com.yuchen.saas.park.service.service.door.DoorDeviceGroupItemService;
import com.yuchen.saas.park.service.service.door.DoorDevicePointService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.List;

/**
 * <p>
 * 门禁点 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorDevicePoint")
public class DoorDevicePointController {

    private final DoorDevicePointService doorDevicePointService;
    private final DoorDeviceGroupItemService doorDeviceGroupItemService;

    /**
     * 门禁点 批量新增
     */
    @PostMapping("/batchSave")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "门禁点 批量新增", notes = "门禁点 批量新增")
    public R batchSave(@Valid @RequestBody List<DoorDevicePoint> doorDevicePointList) {
        if(Func.isEmpty(doorDevicePointList)) {
            return R.error("门禁点不能为空");
        }
        return R.data(doorDevicePointService.batchSave(doorDevicePointList));
    }

    /**
     * 门禁点 编辑
     */
    @PostMapping("/updateById")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "门禁点 编辑", notes = "门禁点 编辑")
    public R updateById(@Valid @RequestBody DoorDevicePoint doorDevicePoint) {
        if(Func.isEmpty(doorDevicePoint)) {
            return R.error("门禁点不能为空");
        }
        return R.data(doorDevicePointService.updateById(doorDevicePoint));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "门禁点 详情", notes = "门禁点详情")
    public R detail(Long id) {
        return R.data(doorDevicePointService.getById(id));
    }

    /**
     * 根据主表id删除门禁点
     * @param id
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据主表id 门禁点 删除", notes = "根据主表id 门禁点 删除")
    public R removeId(Long id) {
        List<DoorDeviceGroupItem> list = doorDeviceGroupItemService.list(new LambdaQueryWrapper<DoorDeviceGroupItem>().eq(DoorDeviceGroupItem::getDevicePointId, id));
        if(Func.isNotEmpty(list)) {
            return R.error("该门禁点已绑定到分组，无法删除");
        }
        doorDevicePointService.removeById(id);
        return R.success();
    }

    @PostMapping("/batchRemoveId")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public R batchRemoveId(@RequestBody List<Long> idList) {
        doorDevicePointService.removeByIds(idList);
        return R.success();
    }

    /**
     * 分页 门禁点
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "分页 门禁点", notes = "分页 门禁点")
    public R<IPage<DoorDevicePoint>> list(DoorDevicePoint doorDevicePoint, Query query) {
        IPage<DoorDevicePoint> pages = doorDevicePointService.pageByparam(doorDevicePoint, query);
        return  R.data(pages);
    }

    /**
     * 获取已绑定门禁点的设备Ids
     */
    @GetMapping("/getListBindId")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取已绑定门禁点的设备Ids", notes = "获取已绑定门禁点的设备Ids")
    public List<Long> getListBindId() {
        return doorDevicePointService.getListBindId();
    }

    /**
     * 获取门禁点 列表
     * @param doorDevicePoint
     * @return
     */
    @GetMapping("/getListByparam")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取门禁点 列表", notes = "获取门禁点 列表")
    public R getListByparam(DoorDevicePoint doorDevicePoint) {
        return R.data(doorDevicePointService.listByparam(doorDevicePoint));
    }

    /**
     * 获取门禁点 列表(包含已删除)
     * @return
     */
    @GetMapping("/getListByparamAll")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取门禁点 列表(包含已删除)", notes = "获取门禁点 列表(包含已删除)")
    public R getListByparamAll() {
        return R.data(doorDevicePointService.getListByparamAll());
    }

}
