package com.psdc.controller.control;

import com.alibaba.fastjson2.JSONObject;
import com.psdc.core.controller.BaseController;
import com.psdc.core.domain.AjaxResult;
import com.psdc.core.domain.entity.SysUser;
import com.psdc.core.page.TableDataInfo;
import com.psdc.domain.SysConfig;
import com.psdc.entity.PsdcControlLog;
import com.psdc.entity.vo.DeviceStatusVo;
import com.psdc.service.IPsdcControlLogService;
import com.psdc.service.IPsdcDeviceService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/control/manual")
public class ManualControl extends BaseController {


    @Autowired
    private IPsdcDeviceService psdcDeviceService;

    @Autowired
    private IPsdcControlLogService psdcControlLogService;

    /**
     * 查询设备状态列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('control:manual:devlist')")
    @GetMapping("/deviceStatusList")
    public AjaxResult getControlDeviceList() {
        List<DeviceStatusVo> deviceStatusVos = psdcDeviceService.queryDeviceStatus();
        return AjaxResult.success("设备状态列表",deviceStatusVos);
    }

    /**
     * 控制设备启停
     * @param jsonObject
     * @return
     */
    @PreAuthorize("@ss.hasPermi('control:manual:startstop')")
    @PostMapping("/startAndStop")
    public AjaxResult deviceStartAndStop(@RequestBody JSONObject jsonObject){
        int i = psdcDeviceService.controlDeviceStartAndStop(jsonObject.getInteger("deviceId"), jsonObject.getInteger("runStatus"));
        if ( i == 1){
            return AjaxResult.success("控制成功");
        } else {
            return AjaxResult.error("控制失败，请联系管理员");
        }
    }

    /**
     * 获取调控日志列表
     */
    @PreAuthorize("@ss.hasPermi('control:timer:controlLogList')")
    @GetMapping("/controlLogList")
    public TableDataInfo controlLogList(PsdcControlLog psdcControlLog) {
        startPage();
        List<PsdcControlLog> list = psdcControlLogService.query(psdcControlLog);
        return getDataTable(list);
    }
}
