package com.ho.business.controller;

import com.ho.business.entity.VirtualDeviceCol;
import com.ho.business.service.VirtualDeviceColService;
import com.ho.business.vo.req.VirtualDeviceColReq;
import com.ho.business.vo.req.deviceModel.SynchronousReqVo;
import com.ho.business.vo.resp.VirtualDeviceColResp;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
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.validation.Valid;
import java.util.List;

/**
 * @author gyan
 * @desc: 虚拟设备关系controller
 * @DateTime: 2023/6/29 9:45
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "virtual")
@Api(tags = "虚拟设备关系模块")
public class VirtualTypeController {
    @Autowired
    VirtualDeviceColService virtualDeviceColService;

    // 增
    @PostMapping("add")
    @ApiOperation(value = "新增虚拟设备关联接口")
    @LogAnnotation(title = "虚拟设备关系模块", action = "新增虚拟设备关联接口")
    public DataResult<VirtualDeviceCol> add(@RequestBody @Valid List<VirtualDeviceCol> list) {
        if(list.isEmpty()){
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        DataResult<VirtualDeviceCol> result = new DataResult<>();
        virtualDeviceColService.insertList(list);
        return result;
    }
    // 改
    @PostMapping("update")
    @ApiOperation(value = "更新虚拟设备关系")
    @LogAnnotation(title = "虚拟设备关系模块", action = "更新虚拟设备关系")
    public DataResult update(@RequestBody @Valid VirtualDeviceCol vo) {
        virtualDeviceColService.updateVirtualDeviceCol(vo);
        return DataResult.success();
    }

    // 查
    @PostMapping("/page")
    @ApiOperation(value = "分页查接口")
    public DataResult<PageResult<VirtualDeviceColResp>> page(@RequestBody @Valid VirtualDeviceColReq vo) {
        DataResult<PageResult<VirtualDeviceColResp>> result = DataResult.success();
        PageResult<VirtualDeviceColResp> pageList = virtualDeviceColService.getPageList(vo);
        result.setData(pageList);
        return result;
    }

    // 删
    @DeleteMapping("id")
    @ApiOperation(value = "删除接口")
    @LogAnnotation(title = "虚拟设备关系模块", action = "删除接口")
    public DataResult deleteByIds(@RequestBody List<Integer> ids) {
        //删除电站数据及其先关数据
        if (ids.isEmpty()){
            throw new BusinessException(BaseResponseCode.NON_DATA);
        }
        virtualDeviceColService.deleteByList(ids);
        return DataResult.success();
    }

    //一键同步功能
    @PostMapping("/getSynchronousV")
    @ApiOperation(value = "一键同步虚拟设备关系功能")
    @LogAnnotation(title = "虚拟设备关系模块", action = "一键同步虚拟设备关系功能")
    //deviceType
    public DataResult getSynchronousV(@RequestBody SynchronousReqVo vo) {
        virtualDeviceColService.getSynchronousV(vo);
        return DataResult.success();
    }

}
