package com.fjhx.iot.controller.tda;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.iot.constants.RedisConstant;
import com.fjhx.iot.entity.tda.dto.TdaDeviceDto;
import com.fjhx.iot.entity.tda.dto.TdaDeviceSelectDto;
import com.fjhx.iot.entity.tda.po.TdaDevice;
import com.fjhx.iot.entity.tda.vo.TdaDeviceVo;
import com.fjhx.iot.service.tda.TdaDeviceService;
import com.ruoyi.common.core.domain.BaseSelectDto;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 设备管理 前端控制器
 * </p>
 *
 * @author zlj
 * @since 2023-03-21
 */
@DS(SourceConstant.IOT)
@RestController
@RequestMapping("/tdaDevice")
public class TdaDeviceController {

    @Autowired
    private TdaDeviceService tdaDeviceService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 设备管理分页
     */
    @PostMapping("/page")
    public Page<TdaDeviceVo> page(@RequestBody TdaDeviceSelectDto dto) {
        return tdaDeviceService.getPage(dto);
    }

    /**
     * 获取上位机设备列表
     */
    @PostMapping("computerList")
    public List<JSONObject> computerList(@RequestBody TdaDeviceSelectDto dto) {
        return tdaDeviceService.getComputerList(dto);
    }

    /**
     * 设备数据
     */
    @PostMapping("/data")
    public Object getData(@RequestBody TdaDevice tdaDevice) {
        return redisCache.getCacheObject(RedisConstant.DEVICE_PREFIX + tdaDevice.getDeviceId());
    }

    /**
     * 设备管理明细
     */
    @PostMapping("/detail")
    public TdaDeviceVo detail(@RequestBody BaseSelectDto dto) {
        return tdaDeviceService.detail(dto.getId());
    }

    /**
     * 设备管理新增
     */
    @PostMapping("/add")
    public void add(@RequestBody TdaDeviceDto tdaDeviceDto) {
        tdaDeviceService.add(tdaDeviceDto);
    }

    /**
     * 设备管理删除
     */
    @PostMapping("/delete")
    public void delete(@RequestBody BaseSelectDto dto) {
        tdaDeviceService.delete(dto.getId());
    }

}
