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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuchen.saas.park.api.dto.response.PmgBatDeviceDataResponse;
import com.yuchen.saas.park.api.dto.response.PmgBatDeviceTreeResponse;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.vo.request.DeviceCategoryAddRequest;
import com.yuchen.saas.park.api.vo.request.DeviceCategoryDelRequest;
import com.yuchen.saas.park.api.vo.request.DeviceCategoryReOperationRequest;
import com.yuchen.saas.park.api.vo.request.DeviceCategoryUpdateRequest;
import com.yuchen.saas.park.service.service.PmgBatDeviceCategoryService;
import com.yuchen.saas.park.service.service.impl.PmgBatDeviceCategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * @ClassName PmgBatDeviceCategory
 * @Description 园区设备分类控制器类
 * @Author 肖祥
 * @Date 2022/10/5 14:50
 * @Version 1.0。0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/deviceCategory")
@Api(value = "设备分类", tags = "设备分类接口")
public class DeviceCategoryController {


    @Resource
    private PmgBatDeviceCategoryService pmgBatDeviceCategoryService;

    /**
     * 根据园区、租户id查询其下所有设备的树形结构图
     */
    @GetMapping("/getDeviceCategoryTree")
    @ApiOperation(value = "根据园区、租户id查询其下所有设备的树形结构图:可视化使用", notes = "传入查询信息")
    public R<List<PmgBatDeviceTreeResponse>> getParkingTree(@RequestParam(value = "parkId")Long parkId) {
        return pmgBatDeviceCategoryService.getParkingTree(parkId);
    }

    /**
     * 根据园区、租户id查询其下所有设备的树形结构图
     */
    @GetMapping("/getSmartEquipmentList")
    @ApiOperation(value = "获取已关联设备树摄像头", notes = "传入查询信息")
    public R<IPage<PmgBatSmartEquipment>> getSmartEquipmentList(@RequestParam(value = "parkId")Long parkId,@RequestParam(value = "name",required = false)String name,@RequestParam(value = "categoryId",required = false)String categoryId, int current, int size) {
        return pmgBatDeviceCategoryService.getSmartEquipmentList(parkId,"384rQ13KlTUvrx3q",name, categoryId,current, size);
    }



    /**
     * 新增分类接口
     */
    @PostMapping("/add")
    @ApiOperation(value = "分类新增接口", notes = "传入查询信息")
    public R addCategory(@Valid @RequestBody DeviceCategoryAddRequest addRequest) {
        return pmgBatDeviceCategoryService.addDeviceCategory(addRequest);
    }

    /**
     * 分类编辑接口
     */
    @PostMapping("/update")
    @ApiOperation(value = "分类编辑接口", notes = "传入查询信息")
    public R updateCategory(@Valid @RequestBody DeviceCategoryUpdateRequest updateRequest) {
        return pmgBatDeviceCategoryService.updateDeviceCategory(updateRequest);
    }

    /**
     * 分类删除接口
     */
    @PostMapping("/delete")
    @ApiOperation(value = "分类删除接口", notes = "传入查询信息")
    public R deleteCategory(@Valid @RequestBody DeviceCategoryDelRequest deleteRequest) {
        return pmgBatDeviceCategoryService.deleteDeviceCategory(deleteRequest);
    }

    /**
     * 待关联设备列表接口
     */
    @GetMapping("/wait/relDevice")
    @ApiOperation(value = "分类删除接口", notes = "传入查询信息")
    public R<List<PmgBatDeviceDataResponse>> waitRelDevice(@RequestParam(value = "parkId")Long parkId) {
        return pmgBatDeviceCategoryService.waitRelDevice(parkId);
    }

    /**
     * 分类已经关联的设备列表
     */
    @GetMapping("/already/relDevice")
    @ApiOperation(value = "分类已经关联的设备列表", notes = "传入查询信息")
    public R alreadyRelDevice(@RequestParam("parkId") @NotNull Long parkId,
                              @RequestParam("categoryId") @NotNull Long categoryId) {
        return pmgBatDeviceCategoryService.alreadyRelDevice(parkId,categoryId);
    }

    /**
     * 设备分类关联操作:0是解关联 1是关联
     */
    @PostMapping("/relation/operation")
    @ApiOperation(value = "设备分类关联操作接口", notes = "设备分类关联操作")
    public R relationOperation(@Valid @RequestBody DeviceCategoryReOperationRequest operationRequest) {
        return pmgBatDeviceCategoryService.deviceCategoryLinkOperation(operationRequest);
    }

    /**
     * 根据园区、租户id查询其下所有设备的树形结构图
     */
    @GetMapping("/getDeviceAreaTree")
    @ApiOperation(value = "根据园区、租户id查询其下所有设备的树形结构图:可视化使用", notes = "传入查询信息")
    public R<List<PmgBatDeviceTreeResponse>> getDeviceAreaTree(@RequestParam(value = "parkId",required=false)Long parkId) {
        return pmgBatDeviceCategoryService.getParkingAreaTree(parkId);
    }

    /**
     * clearDeviceCategoryCache
     */
    @GetMapping("/cache/clear")
    public R clearDeviceCategoryCache() {
        PmgBatDeviceCategoryServiceImpl.deviceMap.clear();
        return R.success();
    }

    /**
     * clearDeviceCategoryCache
     */
    @GetMapping("/data/count")
    public R<Integer> parkDeviceDataCount(@RequestParam("parkId") Long parkId) {
        return pmgBatDeviceCategoryService.parkDeviceDataCount(parkId);
    }

    @GetMapping("/data/scopeValue")
    public R<Set<String>> parkDeviceDataCategory(@RequestParam("scopeValue") String scopeValue){
        return pmgBatDeviceCategoryService.parkDeviceDataScopeValue(scopeValue);
    }
}
