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.DoorDeviceGroupDTO;
import com.yuchen.saas.park.api.entity.door.*;
import com.yuchen.saas.park.service.service.door.DoorDeviceEventService;
import com.yuchen.saas.park.service.service.door.DoorDeviceGroupItemService;
import com.yuchen.saas.park.service.service.door.DoorDeviceGroupService;
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.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;
import java.util.List;

/**
 * <p>
 * 门禁分组 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorDeviceGroup")
public class DoorDeviceGroupController {

    private final DoorDeviceGroupService doorDeviceGroupService;

    private final DoorDeviceGroupItemService deviceGroupItemService;

    /**
     * 门禁分组 新增/修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "门禁分组 新增/修改", notes = "门禁分组 新增/修改")
    public R saveOrUpdate(@Valid @RequestBody DoorDeviceGroupDTO doorDeviceGroupDTO) {
        if(Func.isEmpty(doorDeviceGroupDTO)) {
            return R.error("门禁分组不能为空");
        }
        return R.data(doorDeviceGroupService.saveOrUpdateByParam(doorDeviceGroupDTO));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "门禁分组详情", notes = "门禁分组详情")
    public R detail(Long id) {
        return R.data(doorDeviceGroupService.detailById(id));
    }

    /**
     * 根据主表id删除门禁分组
     * @param id
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id门禁分组删除", notes = "根据主表id门禁分组删除")
    public R removeId(Long id) throws Exception {
        String msg = doorDeviceGroupService.deleteById(id);
        if(Func.isNotEmpty(msg)) {
            return R.fail(msg);
        }else{
            return R.success();
        }
    }

    /**
     * 批量删除-根据主表id删除门禁分组
     * @param idList
     * @return
     */
    @PostMapping("/batchRemoveId")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量删除-根据主表id删除门禁分组", notes = "批量删除-根据主表id删除门禁分组")
    public R batchRemoveId(@RequestBody List<Long> idList) throws Exception {
        String msg = doorDeviceGroupService.batchRemoveId(idList);
        if(Func.isNotEmpty(msg)) {
            return R.fail(msg);
        }else{
            return R.success();
        }
    }

    /**
     * 分页 门禁分组
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "分页 门禁分组", notes = "分页 门禁分组")
    public R<IPage<DoorDeviceGroup>> page(DoorDeviceGroup doorDevicePoint, Query query) {
        IPage<DoorDeviceGroup> pages = doorDeviceGroupService.page(Condition.getPage(query),
                new LambdaQueryWrapper<DoorDeviceGroup>()
                        .like(Func.isNotEmpty(doorDevicePoint.getGroupName()), DoorDeviceGroup::getGroupName,
                                doorDevicePoint.getGroupName())
                        .orderByDesc(DoorDeviceGroup::getCreateTime)
        );
        return  R.data(pages);
    }

    /**
     * 批量移除门禁分组 详情 门禁点绑定关系
     * @param itemIdList
     * @return
     */
    @PostMapping("/batchRemovePointByParam")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "批量移除门禁分组 详情 门禁点绑定关系", notes = "批量移除门禁分组 详情 门禁点绑定关系")
    public R batchRemovePointByParam(@RequestBody List<Long> itemIdList) {
        deviceGroupItemService.removeByIds(itemIdList);
        return R.success();
    }

    /**
     * 获取详情-门禁点列表分页
     * @param req
     * @return
     */
    @PostMapping("/devicePointPage")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取详情-门禁点列表分页", notes = "获取详情-门禁点列表分页")
    public R<NestPage<DoorDevicePoint>> devicePointPage(@Valid @RequestBody PageDeviceGroupItemReqDTO req) {
        Page<DoorDeviceGroupItem> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        return R.data(doorDeviceGroupService.devicePointPage(page, req));
    }

    /**
     * 门禁分组 基础信息新增/修改
     */
    @PostMapping("/basicsSaveOrUpdate")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "门禁分组 基础信息新增/修改", notes = "门禁分组 基础信息新增/修改")
    public R basicsSaveOrUpdate(@Valid @RequestBody DoorDeviceGroup doorDeviceGroup) {
        if(Func.isEmpty(doorDeviceGroup)) {
            return R.error("设备分组不能为空");
        }
        return R.data(doorDeviceGroupService.saveOrUpdate(doorDeviceGroup));
    }

    /**
     * 门禁分组 设置门禁点数量
     */
    @PostMapping("/setPointNumber")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "门禁分组 设置门禁点数量", notes = "门禁分组 设置门禁点数量")
    public R setPointNumber(@RequestBody DoorDeviceGroup doorDeviceGroup) {
        if(Func.isEmpty(doorDeviceGroup.getId())){
            return R.error("id不能为空");
        }
        if(Func.isEmpty(doorDeviceGroup.getNumber())){
            return R.error("门禁点数量不能为空");
        }
        doorDeviceGroupService.updateById(doorDeviceGroup);
        return  R.success();
    }

    /**
     * 门禁分组 详情 门禁点列表新增子项
     */
    @PostMapping("/infoSaveItem")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "门禁分组 详情 门禁点列表新增子项", notes = "门禁分组 详情 门禁点列表新增子项")
    public R infoSaveItem(@Valid @RequestBody List<DoorDeviceGroupItem> doorDeviceGroupItemList) {
        if(Func.isEmpty(doorDeviceGroupItemList)) {
            return R.error("门禁点不能为空");
        }
        return R.data(deviceGroupItemService.saveBatch(doorDeviceGroupItemList));
    }

    /**
     * 获取门禁分组 列表
     * @param doorDeviceGroup
     * @return
     */
    @GetMapping("/getListByparam")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "获取门禁分组 列表", notes = "获取门禁分组 列表")
    public R getListByparam(DoorDeviceGroup doorDeviceGroup) {
        return R.data(doorDeviceGroupService.listByparam(doorDeviceGroup));
    }

}
