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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.device.api.dto.deviceCenter.SyncDeviceDataSubmitReqDTO;
import com.yuchen.saas.device.api.entity.deviceCenter.SaasDeviceVideoMonitor;
import com.yuchen.saas.device.api.feign.deviceCenter.DeviceCenterFeign;
import com.yuchen.saas.manage.system.api.param.CommonIdParam;
import com.yuchen.saas.park.api.dto.security.SaasParkSurveillanceCameraQryDto;
import com.yuchen.saas.park.api.entity.security.SaasParkSurveillanceCamera;
import com.yuchen.saas.park.service.service.security.ISaasParkSurveillanceCameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 视频监控设备表 前端控制器
 * </p>
 *
 * @author 洪铭铭
 * @since 2024-09-12
 */
@Api(tags = "智慧安防视频监控管理")
@RestController
@RequestMapping("/security/camera")
public class SaasParkSurveillanceCameraController {

    @Autowired
    private ISaasParkSurveillanceCameraService saasParkSurveillanceCameraService;

    @Resource
    private DeviceCenterFeign deviceCenterFeign;

    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "视频监控新增")
    public R save(@Valid @RequestBody SaasParkSurveillanceCamera saasParkSurveillanceCamera) {
        SyncDeviceDataSubmitReqDTO reqDTO = new SyncDeviceDataSubmitReqDTO();
        reqDTO.setBroadCategory("1");
        SaasDeviceVideoMonitor saasDeviceVideoMonitor = new SaasDeviceVideoMonitor();
        BeanUtils.copyProperties(saasParkSurveillanceCamera, saasDeviceVideoMonitor);
        saasDeviceVideoMonitor.setProjectId(saasParkSurveillanceCamera.getParkId());
        saasDeviceVideoMonitor.setDeviceName(saasParkSurveillanceCamera.getProductKey());
        saasDeviceVideoMonitor.setDeviceAlias(saasParkSurveillanceCamera.getDeviceName());
        saasDeviceVideoMonitor.setDeviceImages(saasParkSurveillanceCamera.getDevicePhoto());
        saasDeviceVideoMonitor.setDeviceState(saasParkSurveillanceCamera.getDeviceStatus());
        saasDeviceVideoMonitor.setLastConnectionTime(saasParkSurveillanceCamera.getLastConnectTime());
        saasDeviceVideoMonitor.setLastBreakTime(saasParkSurveillanceCamera.getLastDisconnectTime());
        saasDeviceVideoMonitor.setDeviceAddress(saasParkSurveillanceCamera.getAddress());
        saasDeviceVideoMonitor.setRemark(saasParkSurveillanceCamera.getDeviceDesc());
        saasDeviceVideoMonitor.setSpaceId(Long.parseLong(saasParkSurveillanceCamera.getInstallLocation()));
        saasDeviceVideoMonitor.setMonitorType(Long.parseLong(saasParkSurveillanceCamera.getCameraType()));
        reqDTO.setSaasDeviceVideoMonitor(saasDeviceVideoMonitor);
        deviceCenterFeign.syncSubmitDeviceData(reqDTO);
        return R.status(saasParkSurveillanceCameraService.saveOrUpdate(saasParkSurveillanceCamera));
    }

    @PostMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "列表", notes = "视频监控列表")
    public R<List<SaasParkSurveillanceCamera>> list() {
        return R.data(saasParkSurveillanceCameraService.getAll());
    }

    @PostMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页列表", notes = "分页 视频监控列表")
    public R<IPage<SaasParkSurveillanceCamera>> page(@RequestBody SaasParkSurveillanceCameraQryDto qryDto) {
        IPage<SaasParkSurveillanceCamera> pages = saasParkSurveillanceCameraService.selectPage(qryDto);
        return R.data(pages);
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改", notes = "视频监控修改")
    public R update(@Valid @RequestBody SaasParkSurveillanceCamera saasParkSurveillanceCamera) {
        return R.status(saasParkSurveillanceCameraService.saveOrUpdate(saasParkSurveillanceCamera));
    }

    @PostMapping("/delete")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除", notes = "视频监控删除")
    public R delete(@Valid @RequestBody CommonIdParam param) {
        return R.data(saasParkSurveillanceCameraService.removeById(param.getId()));
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "详情", notes = "视频监控详情")
    public R<SaasParkSurveillanceCamera> detail(@RequestParam("id") Long id) {
        return R.data(saasParkSurveillanceCameraService.getById(id));
    }

    @GetMapping("/deviceAction")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "详情", notes = "视频监控设备控制")
    public R<SaasParkSurveillanceCamera> deviceAction(@RequestParam("id") Long id) {
        return R.data(saasParkSurveillanceCameraService.getById(id));
    }

    @GetMapping("/channelAction")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "详情", notes = "视频监控防区控制")
    public R<SaasParkSurveillanceCamera> channelAction(@RequestParam("id") Long id) {
        return R.data(saasParkSurveillanceCameraService.getById(id));
    }


//    @PostMapping("/page")
//    @ApiOperationSupport(order = 6)
//    @ApiOperation(value = "分页列表", notes = "应急预案分页列表")
//    public R<Page<SaasParkSurveillanceCamera>> page(@RequestBody SaasParkElectronicFenceQueryDto saasParkElectronicFenceQueryDto) {
//        return R.data(saasParkSurveillanceCameraService.page(saasParkElectronicFenceQueryDto));
//    }


}
