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

import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.config.vo.IotYfSipConfigPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.config.vo.IotYfSipConfigRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.config.vo.IotYfSipConfigSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.config.IotSipConfigDO;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.config.IotSipConfigService;
import lombok.extern.java.Log;
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-sip系统配置")
@RestController
@RequestMapping("/rq/iot-sip-config")
@Validated
public class IotYfSipConfigController {

    @Resource
    private IotSipConfigService iotSipConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建yf-sip系统配置")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:create')")
    public CommonResult<Long> createIotSipConfig(@Valid @RequestBody IotYfSipConfigSaveReqVO createReqVO) {
        return success(iotSipConfigService.createIotSipConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新yf-sip系统配置")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:update')")
    public CommonResult<Boolean> updateIotSipConfig(@Valid @RequestBody IotYfSipConfigSaveReqVO updateReqVO) {
        iotSipConfigService.updateIotSipConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除yf-sip系统配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:delete')")
    public CommonResult<Boolean> deleteIotSipConfig(@RequestParam("id") Long id) {
        iotSipConfigService.deleteIotSipConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得yf-sip系统配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:query')")
    public CommonResult<IotYfSipConfigRespVO> getIotSipConfig(@RequestParam("id") Long id) {
        IotSipConfigDO iotSipConfig = iotSipConfigService.getIotSipConfig(id);
        return success(BeanUtils.toBean(iotSipConfig, IotYfSipConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得yf-sip系统配置分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:query')")
    public CommonResult<PageResult<IotYfSipConfigRespVO>> getIotSipConfigPage(@Valid IotYfSipConfigPageReqVO pageReqVO) {
        PageResult<IotSipConfigDO> pageResult = iotSipConfigService.getIotSipConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotYfSipConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出yf-sip系统配置 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-sip-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSipConfigExcel(@Valid IotYfSipConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotSipConfigDO> list = iotSipConfigService.getIotSipConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "yf-sip系统配置.xls", "数据", IotYfSipConfigRespVO.class,
                        BeanUtils.toBean(list, IotYfSipConfigRespVO.class));
    }


    @Operation(summary = "批量删除sip系统配置")
    @DeleteMapping("/product/{productIds}")
    public CommonResult<Boolean> removeByProductId(@PathVariable Long[] productIds) {
        // 设备可能不存在通道，可以返回0
        int result = iotSipConfigService.deleteSipConfigByProductIds(productIds);
        return success(true);
    }

    /**
     * 获取产品下第一条sip系统配置详细信息
     */
    @Operation(summary = "获取产品下sip系统配置信息")
    //@PreAuthorize("@ss.hasPermi('iot:video:query')")
    @GetMapping(value = "/{productId}")
    public CommonResult<IotSipConfigDO> getInfo(@PathVariable("productId") Long productId) {
        IotSipConfigDO iotSipConfigDO = iotSipConfigService.selectSipConfigByProductId(productId);
        return success(iotSipConfigDO);
//        return AjaxResult.success(iotSipConfigService.selectSipConfigByProductId(productId));
    }
}
