package com.sooka.sponest.monitor.camera.controller;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.sooka.sponest.monitor.camera.domain.CentermonitorTCamerachannel;
import com.sooka.sponest.monitor.camera.service.ICentermonitorTCamerachannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 摄像头通道Controller
 *
 * @author ruoyi
 * @date 2022-06-10
 */
@Api(tags = "摄像头通道管理控制器")
@RestController
@RequestMapping("/camerachannel")
public class CentermonitorTCamerachannelController extends BaseController
{
    @Autowired
    private ICentermonitorTCamerachannelService centermonitorTCamerachannelService;

    /**
     * 查询摄像头通道列表
     */
    @ApiOperation(value = "查询摄像头通道列表", notes = "查询摄像头通道列表")
    @RequiresPermissions("camerachannel:camerachannel:list")
    @GetMapping("/list")
    @Log(title = "摄像头通道", businessType = BusinessType.OTHER)
    public TableDataInfo list(CentermonitorTCamerachannel centermonitorTCamerachannel)
    {
        startPage();
        List<CentermonitorTCamerachannel> list = centermonitorTCamerachannelService.selectCentermonitorTCamerachannelList(centermonitorTCamerachannel);
        return getDataTable(list);
    }

    /**
     * 查询摄像头通道列表远程调用
     */
    @ApiOperation(value = "查询摄像头通道列表远程调用", notes = "查询摄像头通道列表远程调用")
    @GetMapping("/selectCameraChannelList")
    @Log(title = "摄像头通道", businessType = BusinessType.OTHER)
    public R<List<CentermonitorTCamerachannel>> selectCameraChannelList(@RequestParam("cameraId") String cameraId)
    {
        startPage();
        CentermonitorTCamerachannel centermonitorTCamerachannel = new CentermonitorTCamerachannel();
        centermonitorTCamerachannel.setCameraId(cameraId);
        List<CentermonitorTCamerachannel> list = centermonitorTCamerachannelService.selectCentermonitorTCamerachannelList(centermonitorTCamerachannel);
        return R.ok(list);
    }

    /**
     * 导出摄像头通道列表
     */
    @ApiOperation(value = "导出摄像头通道列表", notes = "导出摄像头通道列表")
    @RequiresPermissions("camerachannel:camerachannel:export")
    @Log(title = "摄像头通道", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CentermonitorTCamerachannel centermonitorTCamerachannel)
    {
        List<CentermonitorTCamerachannel> list = centermonitorTCamerachannelService.selectCentermonitorTCamerachannelList(centermonitorTCamerachannel);
        ExcelUtil<CentermonitorTCamerachannel> util = new ExcelUtil<CentermonitorTCamerachannel>(CentermonitorTCamerachannel.class);
        util.exportExcel(response, list, "摄像头通道数据");
    }

    /**
     * 获取摄像头通道详细信息
     */
    @ApiOperation(value = "获取摄像头通道详细信息", notes = "获取摄像头通道详细信息")
    @RequiresPermissions("camerachannel:camerachannel:query")
    @GetMapping(value = "/{id}")
    @Log(title = "摄像头通道", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(centermonitorTCamerachannelService.selectCentermonitorTCamerachannelById(id));
    }

    /**
     * 新增摄像头通道
     */
    @ApiOperation(value = "新增摄像头通道", notes = "新增摄像头通道")
    @RequiresPermissions("camerachannel:camerachannel:add")
    @Log(title = "摄像头通道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CentermonitorTCamerachannel centermonitorTCamerachannel)
    {
        if (StringUtils.isNotEmpty(centermonitorTCamerachannel.getChannelCode())
                && UserConstants.NOT_UNIQUE.equals(centermonitorTCamerachannelService.checkChannelUnique(centermonitorTCamerachannel))) {
            return AjaxResult.error("新增摄像头通道失败，通道编码已存在");
        }
        centermonitorTCamerachannel.setId(IdUtils.simpleUUID());
        centermonitorTCamerachannel.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getUserId().toString());
        centermonitorTCamerachannel.setCreateTime(DateUtils.getNowDate());
        return toAjax(centermonitorTCamerachannelService.insertCentermonitorTCamerachannel(centermonitorTCamerachannel));
    }

    /**
     * 修改摄像头通道
     */
    @ApiOperation(value = "修改摄像头通道", notes = "修改摄像头通道")
    @RequiresPermissions("camerachannel:camerachannel:edit")
    @Log(title = "摄像头通道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CentermonitorTCamerachannel centermonitorTCamerachannel)
    {
        if (StringUtils.isNotEmpty(centermonitorTCamerachannel.getChannelCode())
                && UserConstants.NOT_UNIQUE.equals(centermonitorTCamerachannelService.checkChannelUnique(centermonitorTCamerachannel))) {
            return AjaxResult.error("修改摄像头通道失败，通道编码已存在");
        }

        return toAjax(centermonitorTCamerachannelService.updateCentermonitorTCamerachannel(centermonitorTCamerachannel));
    }

    /**
     * 删除摄像头通道
     */
    @ApiOperation(value = "删除摄像头通道", notes = "删除摄像头通道")
    @RequiresPermissions("camerachannel:camerachannel:remove")
    @Log(title = "摄像头通道", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(centermonitorTCamerachannelService.deleteCentermonitorTCamerachannelByIds(ids));
    }

    /**
     * 根据摄像头id查询通道编码
     * */
    @ApiOperation(value = "根据摄像头id查询通道编码", notes = "根据摄像头id查询通道编码")
    @GetMapping(value = "selectchannelCodeByCameraId/{cameraId}")
    @RequiresPermissions("camerachannel:camerachannel:selectchannel")
    @Log(title = "摄像头通道", businessType = BusinessType.OTHER)
    public AjaxResult selectchannelCodeByCameraId(@PathVariable String cameraId)
    {
        CentermonitorTCamerachannel centermonitorTCamerachannel=new CentermonitorTCamerachannel();
        centermonitorTCamerachannel.setCameraId(cameraId);
        return AjaxResult.success(centermonitorTCamerachannelService.selectCentermonitorTCamerachannelList(centermonitorTCamerachannel));
    }
}
