package com.willalp.web.controller.control;

import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.control.domain.HsControlDeviceInfo;
import com.willalp.control.domain.HsControlTiming;
import com.willalp.control.domain.vo.OpenOrCloseVo;
import com.willalp.control.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author hs
 */
@RestController
@RequestMapping("/api/control")
public class HsControlAppController extends BaseController {

    @Resource
    private IHsControlDeviceInfoService deviceInfoService;

    @Resource
    private IHsControlTimingService controlTimingService;

    @Resource
    private IHsControlUsenoConfigService controlUseNoConfigService;

    @Resource
    private IHsControlStatService statService;

    @Resource
    private IHsControlLogService logService;

    @Resource
    private ControlService controlService;

    @ApiOperation("设备状态信息列表")
    @GetMapping("/list")
    public AjaxResult getControlDeviceList(@RequestParam("userId") Long userId) {
        List<HsControlDeviceInfo> hsControlDeviceInfos = deviceInfoService.appSelectHsControlDeviceInfoList(userId);
        return AjaxResult.success(hsControlDeviceInfos);
    }

    @ApiOperation("可添加设备列表")
    @GetMapping("/useNoList")
    public AjaxResult getControlUseNoList() {
        return AjaxResult.success(controlUseNoConfigService.canAddUseNoListList());
    }

    @ApiOperation("添加设备")
    @PostMapping("/add")
    public AjaxResult addDevice(@RequestBody HsControlDeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.addDeviceInfo(deviceInfo));
    }

    @ApiOperation("删除设备")
    @GetMapping("/delete")
    public AjaxResult deleteDevice(String deviceInfoId) {
        return toAjax(deviceInfoService.removeById(deviceInfoId));
    }

    @ApiOperation("获取年-月统计列表")
    @GetMapping("/statList")
    public AjaxResult getStatList(@RequestParam("useNoId") String useNoId) {
        return AjaxResult.success(statService.appControlStatList(useNoId));
    }

    @ApiOperation("获取日统计列表")
    @GetMapping("/statDayList")
    public AjaxResult getStatList1(@RequestParam("useNoId") String useNoId,
                                  @RequestParam("yearMonth") String yearMonth) {
        return AjaxResult.success(statService.appControlStatList(useNoId, yearMonth));
    }

    @GetMapping("/useLogList")
    public AjaxResult getUseLog(@RequestParam("useNoId") String useNoId,
                                @RequestParam("date") String date) {
        return AjaxResult.success(logService.selectLogListByUseNoIdAndDate(useNoId, date));
    }

    @ApiOperation("修改设备名称")
    @PutMapping("/editDevice")
    public AjaxResult updateDeviceName(@RequestBody HsControlDeviceInfo deviceInfo) {
        return toAjax(deviceInfoService.updateById(deviceInfo));
    }

    @ApiOperation("开或关")
    @PostMapping("/openOrClose")
    public AjaxResult open(@RequestBody OpenOrCloseVo openOrCloseVo) {
        return toAjax(controlService.openOrClose(openOrCloseVo));
    }

    /**
     * 获取定时任务信息
     *
     * @param useNoId
     * @return
     */
    @ApiOperation("获取定时任务信息")
    @GetMapping("/timing")
    public AjaxResult getTimingInfo(@RequestParam("useNoId") String useNoId) {
        return AjaxResult.success(controlTimingService.getTimingInfo(useNoId));
    }

    /**
     * 修改设备定时任务
     */
    @PutMapping("/timing")
    public AjaxResult editTiming(@RequestBody HsControlTiming hsControlTiming) {
        return toAjax(controlTimingService.saveOrUpdateTiming(hsControlTiming));
    }

}
