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

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.pldoor.PlDoorPointDTO;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorPoint;
import com.yuchen.saas.park.api.entity.pldoor.PlDoorPointGroupItem;
import com.yuchen.saas.park.api.vo.door.PlDoorPointVO;
import com.yuchen.saas.park.api.vo.pldoor.IotDeviceInfoVO;
import com.yuchen.saas.park.api.vo.request.PlDoorPointReq;
import com.yuchen.saas.park.service.service.pldoor.PlDoorPointGroupItemService;
import com.yuchen.saas.park.service.service.pldoor.PlDoorPointService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 2024-04-03
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plDoorPoint")
public class PlDoorPointController {

    private final PlDoorPointService plDoorPointService;

    private final PlDoorPointGroupItemService plDoorPointGroupItemService;

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

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

    /**
     * 根据主表id删除门禁点
     * @param plDoorPoint
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 门禁点 删除", notes = "根据主表id 门禁点 删除")
    public R removeId(@RequestBody PlDoorPoint plDoorPoint) {
        List<PlDoorPointGroupItem> list = plDoorPointGroupItemService.list(new LambdaQueryWrapper<PlDoorPointGroupItem>().eq(PlDoorPointGroupItem::getPlDoorPointId, plDoorPoint.getId()));
        if(Func.isNotEmpty(list)) {
            return R.error("该门禁点已绑定到分组，无法删除");
        }
        plDoorPointService.removeByIdParam(plDoorPoint);
        return R.success();
    }

    /**
     * 分页 门禁点
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 门禁点", notes = "分页 门禁点")
    public R<NestPage<PlDoorPointVO>> pageByparam(PlDoorPointReq req) {
        Page<PlDoorPointReq> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        IPage<PlDoorPointVO> pages = plDoorPointService.pageByparam(page, req);
        return  R.data(NestPage.of(pages));
    }

    /**
     * 列表 门禁点
     */
    @GetMapping("/listByparam")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "列表 门禁点", notes = "列表 门禁点")
    public R<List<PlDoorPoint>> listByparam(PlDoorPointReq req) {
        List<PlDoorPoint> list = plDoorPointService.list(new LambdaQueryWrapper<PlDoorPoint>().eq(PlDoorPoint::getParkId, req.getParkId()));
        return  R.data(list);
    }

    @PostMapping("/updateEnableState")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改启用状态", notes = "修改启用状态")
    public R updateEnableState(@Valid @RequestBody PlDoorPointDTO plDoorPointDTO) {
        if(Func.isEmpty(plDoorPointDTO) || Func.isEmpty(plDoorPointDTO.getEnableState())) {
            return R.error("启用状态不能为空");
        }
        PlDoorPoint plDoorPoint = new PlDoorPoint();
        plDoorPoint.setId(plDoorPointDTO.getId());
        plDoorPoint.setEnableState(plDoorPointDTO.getEnableState());
        return R.data(plDoorPointService.updateById(plDoorPoint));
    }


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

    /**
     * 根据iot接口查询设备列表
     * @return
     */
    @GetMapping("/getIotPageList")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据iot接口查询设备列表", notes = "根据iot接口查询设备列表")
    public R<List<IotDeviceInfoVO>> getIotPageList(Query query) {
        return R.data(plDoorPointService.getIotPageList(query));
    }

}
