package com.ho.business.controller;

import com.github.pagehelper.PageInfo;
import com.ho.business.service.InverterService;
import com.ho.business.vo.InverterCurveReqVo;
import com.ho.business.vo.req.MonitorQuery;
import com.ho.business.vo.req.DeviceAttrbuteReqVO;
import com.ho.business.vo.resp.InverterResp.*;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author gyan
 * @desc: 逆变器
 * @DateTime: 2023/1/6 9:15
 */
@RestController
@RequestMapping(ContextConstant.BUSINESS + "inverter")
@Api(tags = "逆变器模块")
public class InverterController {

    @Autowired
    InverterService inverterService;


    @Autowired
    BigDecimalUtil bigDecimalUtil;


    @GetMapping("/list/{stationId}")
    @ApiOperation(value = "根据电站Id查询逆变器")
    public DataResult<List<InverterTopologyRespVO>> selectByStationId(@PathVariable("stationId") Integer stationId) {
        List<InverterTopologyRespVO> list = inverterService.selectByStationId(stationId);
        return DataResult.success(list);
    }


    @PostMapping("pvData")
    @ApiOperation(value = "获取PV值")
    public DataResult<List<InverterPvValueRespVO>> getPvDate(@RequestBody MonitorQuery monitorQuery) {
        List<InverterPvValueRespVO> list = inverterService.getPvDate(monitorQuery);
        //返回值的BigDecimal类型保留两位小数
        for (InverterPvValueRespVO inverterPvValueRespVO : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(inverterPvValueRespVO);
            bigDecimalUtil.ifIsNUll(inverterPvValueRespVO);
        }
        return DataResult.success(list);
    }


    @PostMapping("panelData")
    @ApiOperation(value = "获取面板数据")
    public DataResult<InverterPanelData>   panelData(@RequestBody MonitorQuery monitorQuery) {
        InverterPanelData inverterPanelData = inverterService.getPanelData(monitorQuery);
        //返回值的BigDecimal类型保留两位小数
        bigDecimalUtil.keepTwoDecimalPlaces(inverterPanelData);
        bigDecimalUtil.ifIsNUll(inverterPanelData);
        return DataResult.success(inverterPanelData);
    }


    @PostMapping("curve")
    @ApiOperation(value = "获取电流/电压/有功功率曲线")
    public DataResult<List<InverterCurveRespVo>> getPvDate(@RequestBody InverterCurveReqVo vo) {
        List<InverterCurveRespVo> list = inverterService.getCurve(vo);
        //返回值的BigDecimal类型保留两位小数
        for (InverterCurveRespVo curveRespVo : list) {
            bigDecimalUtil.keepTwoDecimalPlaces(curveRespVo);
            bigDecimalUtil.ifIsNUll(curveRespVo);
        }
        return DataResult.success(list);
    }

    @PostMapping("attribute")
    @ApiOperation(value = "获取设备属性")
    public DataResult<PageResult<DeviceAttributeRespVO>> getAttribute(@RequestBody DeviceAttrbuteReqVO vo) {
       List<DeviceAttributeRespVO> respVOS = inverterService.getAttr(vo);
       //对数据进行分页处理
        PageResult pageResult = new PageResult<>();
        if (!respVOS.isEmpty()) {
            List list = PageUtils.dealList(respVOS, vo.getPageNum(), vo.getPageSize());
            pageResult = PageUtils.getPageResult(new PageInfo<>(list));
            pageResult.setTotalRows(respVOS.size());
            pageResult.setTotalPages(respVOS.size() / vo.getPageSize() + 1);
        } else {
            pageResult = PageUtils.getPageResult(new PageInfo<>(new ArrayList<>()));
        }
        return DataResult.success(pageResult);
    }
}
