package com.yuchen.saas.park.service.controller.energy;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.energy.WaterAddDTO;
import com.yuchen.saas.park.api.entity.park.PmgBatWaterData;
import com.yuchen.saas.park.api.vo.park.energy.DeviceStatisticVO;
import com.yuchen.saas.park.api.vo.park.energy.EnergyStatisticVO;
import com.yuchen.saas.park.api.vo.park.energy.SevenTrendVO;
import com.yuchen.saas.park.api.vo.park.energy.TotalRankingVO;
import com.yuchen.saas.park.service.service.PmgBatWaterDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 颜艺波
 * @date 2022/6/28 18:12
 * @Description
 */
@RestController
@AllArgsConstructor
@RequestMapping("/energy")
@Api(value = "水表", tags = "水表")
public class EnergyWaterController {

    @Autowired
    PmgBatWaterDataService waterDataService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "耗水量统计", notes = "耗水量统计")
    @GetMapping("/water/energyStatistic/{parkId}")
    public R<EnergyStatisticVO> energyStatistic(@PathVariable("parkId") Long parkId) {
        return R.data(waterDataService.energyStatistic(parkId));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "七日用量趋势", notes = "七日用量趋势")
    @GetMapping("/water/sevenTrend/{parkId}")
    public R<List<SevenTrendVO>> sevenTrend(@PathVariable("parkId") Long parkId) {
        return R.data(waterDataService.sevenTrend(parkId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "累积耗水量排名", notes = "累积耗水量排名")
    @GetMapping("/water/totalRanking/{parkId}")
    public R<List<TotalRankingVO>> totalRanking(@PathVariable("parkId") Long parkId) {
        return R.data(waterDataService.totalRanking(parkId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "能耗设备统计", notes = "能耗设备统计")
    @GetMapping("/deviceStatistic/{parkId}")
    public R<DeviceStatisticVO> deviceStatistic(@PathVariable("parkId") Long parkId) {
        return R.data(waterDataService.deviceStatistic(parkId));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "新增水表数据", notes = "新增水表数据")
    @PostMapping("/water/add")
    public R<Boolean> add(@RequestBody WaterAddDTO waterAddDTO) {
        waterAddDTO.setReportTime(System.currentTimeMillis());
        PmgBatWaterData data = waterDataService.getOne(Wrappers.<PmgBatWaterData>lambdaQuery()
                .eq(PmgBatWaterData::getProductKey, waterAddDTO.getProductKey())
                .eq(PmgBatWaterData::getDeviceName, waterAddDTO.getDeviceName())
                .orderByDesc(PmgBatWaterData::getReportTime)
                .last("limit 1"));
        // 上报数值在原来基础上增加2.55
        BigDecimal b = data.getValue() != null ? new BigDecimal(data.getValue()) : BigDecimal.ZERO;
        b = b.add(new BigDecimal("2.5"));
        waterAddDTO.setValue(b.toString());
        return R.data(waterDataService.add(waterAddDTO));
    }

}
