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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.pldoor.PlVisitorPowerSet;
import com.yuchen.saas.park.api.request.PlVisitorParkingSpotRequest;
import com.yuchen.saas.park.service.service.pldoor.PlVisitorPowerSetService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 2024-05-28
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plVisitorPowerSet")
public class PlVisitorPowerSetController {

    private final PlVisitorPowerSetService plVisitorPowerSetService;

    /**
     * 访客权限设置 新增或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "访客权限设置 新增或修改", notes = "访客权限设置 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlVisitorPowerSet plVisitorPowerSet) {
        if(Func.isEmpty(plVisitorPowerSet)) {
            return R.error("访客权限设置");
        }
        if(Func.isEmpty(plVisitorPowerSet.getId())){
            PlVisitorPowerSet set = plVisitorPowerSetService.getOne(new LambdaQueryWrapper<PlVisitorPowerSet>()
                    .eq(PlVisitorPowerSet::getParkId, plVisitorPowerSet.getParkId()).last("limit 1"));
            if(Func.isNotEmpty(set)){
                return R.error("该项目访客权限设置已存在,不能重复添加");
            }
        }
        return R.data(plVisitorPowerSetService.saveOrUpdate(plVisitorPowerSet));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "访客权限设置 详情", notes = "访客权限设置 详情")
    public R detail(Long id) {
        return R.data(plVisitorPowerSetService.getById(id));
    }

    /**
     * 根据主表id删除访客权限设置
     * @param plVisitorPowerSet
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id删除访客权限设置", notes = "根据主表id删除访客权限设置")
    public R removeId(@RequestBody PlVisitorPowerSet plVisitorPowerSet) {
        if(Func.isEmpty(plVisitorPowerSet) || Func.isEmpty(plVisitorPowerSet.getId())) {
            return R.error("访客权限设置为空");
        }
        plVisitorPowerSetService.removeById(plVisitorPowerSet.getId());
        return R.success();
    }

    @PostMapping("/updateEnableState")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改启用状态", notes = "修改启用状态")
    public R updateEnableState(@Valid @RequestBody PlVisitorPowerSet PlVisitorPowerSet) {
        if(Func.isEmpty(PlVisitorPowerSet) || Func.isEmpty(PlVisitorPowerSet.getAutoSendState())) {
            return R.error("访客权限自动下发不能为空");
        }
        return R.data(plVisitorPowerSetService.updateById(PlVisitorPowerSet));
    }

    /**
     * 根据项目获取访客权限
     * @param PlVisitorPowerSet
     * @return
     */
    @GetMapping("/getByProjectId")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据项目获取访客权限", notes = "根据项目获取访客权限")
    public R getByProjectId(PlVisitorPowerSet PlVisitorPowerSet) {
        if(Func.isEmpty(PlVisitorPowerSet) || Func.isEmpty(PlVisitorPowerSet.getParkId())){
            return R.error("根据项目获取访客权限不能为空");
        }
        return R.data(plVisitorPowerSetService.getByProjectId(PlVisitorPowerSet.getParkId()));
    }

    /**
     * 根据项目id获取访客权限
     * @param parkId
     * @return
     */
    @GetMapping("/getPowerSetByProjectId")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据项目id获取访客权限", notes = "根据项目id获取访客权限")
    public R getPowerSetByProjectId(Long parkId) {
        if(Func.isEmpty(parkId)){
            return R.error("根据项目id不能为空");
        }
        return R.data(plVisitorPowerSetService.getByProjectId(parkId));
    }


    @GetMapping("/getParkingSpotByParam")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据项目id,停车场id,车牌号获取授权车位列表", notes = "根据项目id,停车场id,车牌号获取授权车位列表")
    public R getParkingSpotIdListByParam(PlVisitorParkingSpotRequest request) {
        if(Func.isEmpty(request)){
            return R.error("参数不能为空");
        }
        return R.data(plVisitorPowerSetService.getParkingSpotIdListByParam(request));
    }

}
