package com.yida.data.device.camera.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.camera.EduCamera;
import com.yida.data.common.core.entity.camera.EduCameraAreaGroup;
import com.yida.data.common.core.entity.camera.EduCameraAreaGroupCamera;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.device.camera.service.EduCameraAreaGroupCameraService;
import com.yida.data.device.camera.service.EduCameraAreaGroupService;
import com.yida.data.device.vo.camera.EduCameraAreaGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ccl
 */
@Api(tags = "监控-区域分组")
@Slf4j
@Validated
@RestController
@RequestMapping("/camera/groupType")
@RequiredArgsConstructor
public class WebEduCameraGroupTypeController {

    private final EduCameraAreaGroupService areaGroupService;
    private final EduCameraAreaGroupCameraService groupCameraService;

    @ApiOperation("新增或修改区域分组")
    @PostMapping("/saveOrUpAreaGroup")
    public ResultBean saveOrUpAreaGroup(@RequestBody EduCameraAreaGroup areaGroup){

        areaGroupService.saveOrUpdate(areaGroup);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除区域分组")
    @GetMapping("/removeAreaGroup")
    public ResultBean saveOrUpCamera(@RequestParam @ApiParam("区域分组id") Long id){
        List<EduCameraAreaGroupCamera> list = groupCameraService.list(Wrappers.query(new EduCameraAreaGroupCamera()).lambda().eq(EduCameraAreaGroupCamera::getGroupId, id));
        if (CollectionUtil.isNotEmpty(list)){
            return ResultBean.buildError("该区域中含有设备，请先移除设备");
        }
        areaGroupService.removeById(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询区域分组详情")
    @GetMapping("/findAreaGroup")
    public ResultBean<EduCameraAreaGroup> findAreaGroup(@RequestParam @ApiParam("区域分组id") Long id){

        return ResultBean.buildSuccess(areaGroupService.getById(id));
    }

    @ApiOperation("查询树状区域分组")
    @GetMapping("/findAreaGroupList")
    public ResultBean<List<EduCameraAreaGroup>> findAreaGroupList(){

        return ResultBean.buildSuccess(areaGroupService.findAreaGroupList(FebsUtil.getCurrentUser().getDeptId()));
    }

    @ApiOperation("修改分组")
    @GetMapping("/updateAreaGroupList")
    public ResultBean updateAreaGroupList(@RequestParam (required = false) String name,
                                          @RequestParam(required = false) Integer upOrDown,
                                          @RequestParam Long groupId){
        areaGroupService.updateAreaGroupList(name,upOrDown,groupId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询区域设备列表")
    @GetMapping("/findGroupCamera")
    public ResultBean<List<EduCamera>> findGroupCamera(@RequestParam @ApiParam("区域分组id") Long id){

        return ResultBean.buildSuccess(areaGroupService.findGroupCamera(id));
    }

    @ApiOperation("查询树状区域分组")
    @GetMapping("/findTreeGroup")
    public ResultBean<List<EduCameraAreaGroupVO>> findTreeGroup(){

        return ResultBean.buildSuccess(areaGroupService.findTreeGroup(FebsUtil.getCurrentUser().getDeptId()));
    }

}
