package com.tofly.scada.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.scada.common.FlowConstant;
import com.tofly.scada.entity.ScadaIoserverHis;
import com.tofly.scada.entity.vo.IOSever.IOServerDataInfoVo;
import com.tofly.scada.entity.vo.IOSever.IOServerParDto;
import com.tofly.scada.service.IOServerService;
import com.tofly.scada.service.ScadaIoserverHisService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "IOServer设备相关操作")
@RestController
@RequestMapping("/ioserver")
public class IOServerController {

    @Autowired
    private IOServerService ioServerService;
    @Autowired
    private ScadaIoserverHisService scadaIoserverHisService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param scadaIoserverHis IOServer监测设备数据记录表
     * @return
     */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询监测设备操作记录数据")
    public ResultRespone getScadaIoserverHisPage(Page page, @ModelAttribute ScadaIoserverHis scadaIoserverHis) {
        return ResultRespone.success(scadaIoserverHisService.page(page, new QueryWrapper<ScadaIoserverHis>().lambda().setEntity(scadaIoserverHis)
                .ge(StringUtil.isNotNull(scadaIoserverHis.getStartTime()), ScadaIoserverHis::getOperationTime, scadaIoserverHis.getStartTime())
                .le(StringUtil.isNotNull(scadaIoserverHis.getEndTime()), ScadaIoserverHis::getOperationTime, scadaIoserverHis.getEndTime())
        ));
    }

    @ApiOperation(value = "获取指定变量列表实时值")
    @GetMapping("/getBatchRealValue")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codes", value = "指标变量编码,多个以,分隔", required = true, dataType = "String")
    })
    public ResultRespone<List<IOServerDataInfoVo>> getBatchRealValue(String codes) {
        List<String> codeList = Arrays.stream(codes.split(FlowConstant.CSV)).map(String::trim)
                .collect(Collectors.toList());
        try {
            List<IOServerDataInfoVo> batchRealValue = ioServerService.getBatchRealValue(codeList);
            return ResultRespone.success(batchRealValue);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }

    }

    @ApiOperation(value = "实时数据回写")
    @PostMapping("/realVariables")
    public ResultRespone realVariables(@RequestBody IOServerParDto dto) {
        String success = "";
        try {
            success = ioServerService.realVariables(dto.getDtoList());
            return ResultRespone.success(success);
        } catch (IOException e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }

    }

}
