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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.protobuf.ServiceException;
import com.yuchen.saas.park.api.dto.irrigate.BindDTO;
import com.yuchen.saas.park.api.dto.irrigate.ControlIrrigateDTO;
import com.yuchen.saas.park.api.vo.park.irrigate.AllDeviceInfoVO;
import com.yuchen.saas.park.api.vo.park.security.DeviceListVO;
import com.yuchen.saas.park.api.vo.park.security.OnlineStatisticVO;
import com.yuchen.saas.park.service.service.PmgBatIrrigateBindService;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author 颜艺波
 * @date 2022/6/30 11:51
 * @Description
 */
@RestController
@RequestMapping("/irrigate")
@Api(value = "浇灌接口", tags = "浇灌接口")
public class IrrigateController {

    @Autowired
    PmgBatSmartEquipmentService equipmentService;

    @Autowired
    PmgBatIrrigateBindService irrigateBindService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "浇灌设备在线统计")
    @GetMapping("/onlineStatistic/{parkId}")
    public R<OnlineStatisticVO> onlineStatistic(@PathVariable("parkId") Long parkId) {
        return R.data(equipmentService.onlineStatistic(parkId, 8));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "浇灌设备列表查询")
    @GetMapping("/deviceList/{parkId}")
    public R<List<DeviceListVO>> deviceList(@PathVariable("parkId") Long parkId) {
        return R.data(equipmentService.deviceList(parkId, 8, null));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "浇灌设备绑定")
    @PostMapping("/bind")
    public R bind(@RequestBody @Valid BindDTO bindDTO) throws ServiceException {
        return irrigateBindService.bind(bindDTO);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "浇灌设备取消绑定")
    @PostMapping("/unBind")
    public R unBind(@RequestBody @Valid BindDTO bindDTO) {
        return irrigateBindService.unBind(bindDTO);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "所有设备信息查询")
    @GetMapping("/allDeviceInfo/{parkId}")
    public R<List<AllDeviceInfoVO>> allDeviceInfo(@PathVariable("parkId") Long parkId) {
        return R.data(irrigateBindService.allDeviceInfo(parkId));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "开启浇灌")
    @GetMapping("/start")
    public R start(@Valid ControlIrrigateDTO controlIrrigateDTO) {
        return R.data(irrigateBindService.start(controlIrrigateDTO));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "关闭浇灌")
    @GetMapping("/stop")
    public R stop(@Valid ControlIrrigateDTO controlIrrigateDTO) {
        return R.data(irrigateBindService.stop(controlIrrigateDTO));
    }
}
