package cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device;

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device.vo.YfSipDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device.vo.YfSipDeviceRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device.vo.YfSipDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.device.YfSipDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.device.channel.YfSipDeviceChannelDO;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.device.YfSipDeviceService;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.model.BaseTree;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - yf监控设备")
@RestController
@RequestMapping("/rq/yf-sip-device")
@Validated
public class YfSipDeviceController {

    @Resource
    private YfSipDeviceService yfSipDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建yf监控设备")
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:create')")
    public CommonResult<Long> createYfSipDevice(@Valid @RequestBody YfSipDeviceSaveReqVO createReqVO) {
        return success(yfSipDeviceService.createYfSipDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf监控设备")
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:update')")
    public CommonResult<Boolean> updateYfSipDevice(@Valid @RequestBody YfSipDeviceSaveReqVO updateReqVO) {
        yfSipDeviceService.updateYfSipDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf监控设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:delete')")
    public CommonResult<Boolean> deleteYfSipDevice(@RequestParam("id") Long id) {
        yfSipDeviceService.deleteYfSipDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf监控设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:query')")
    public CommonResult<YfSipDeviceRespVO> getYfSipDevice(@RequestParam("id") Long id) {
        YfSipDeviceDO yfSipDevice = yfSipDeviceService.getYfSipDevice(id);
        return success(BeanUtils.toBean(yfSipDevice, YfSipDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf监控设备分页")
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:query')")
    public CommonResult<PageResult<YfSipDeviceRespVO>> getYfSipDevicePage(@Valid YfSipDevicePageReqVO pageReqVO) {
        PageResult<YfSipDeviceDO> pageResult = yfSipDeviceService.getYfSipDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, YfSipDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf监控设备 Excel")
    @PreAuthorize("@ss.hasPermission('rq:yf-sip-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportYfSipDeviceExcel(@Valid YfSipDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<YfSipDeviceDO> list = yfSipDeviceService.getYfSipDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf监控设备.xls", "数据", YfSipDeviceRespVO.class,
                        BeanUtils.toBean(list, YfSipDeviceRespVO.class));
    }


    @Operation(summary = "查询监控设备树结构")
    //@PreAuthorize("@ss.hasPermi('iot:video:list')")
    @GetMapping("/listchannel/{deviceId}")
    public CommonResult<List<BaseTree<YfSipDeviceChannelDO>>> listchannel(@PathVariable("deviceId") String deviceId) {
        List<BaseTree<YfSipDeviceChannelDO>> baseTrees = yfSipDeviceService.selectSipDeviceChannelList(deviceId);
        return success(baseTrees);
    }

    @Operation(summary = "根据sipId删除")
    //@PreAuthorize("@ss.hasPermi('iot:video:remove')")
    @DeleteMapping("/sipid/{sipId}")
    public CommonResult<Integer> remove(@PathVariable String sipId) {
        return success(yfSipDeviceService.deleteSipDeviceBySipId(sipId));
    }
}
