package com.zcxk.controller;

import com.zcxk.dto.*;
import com.zcxk.dto.generalView.WaterSupplyChart;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.*;
import com.zcxk.util.OrgInfoUtil;
import com.zcxk.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/comDisplay")
@Api(tags = "综合展示")
public class ComDisplayController {
    @Autowired
    private MonitorDataReportService monitorDataReportService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private SceneService sceneService;
    @Autowired
    private MonitorInfoService monitorInfoService;
    @Autowired
    private OnlineMonitorService onlineMonitorService;
    @Autowired
    private ComdisplayMapLocationService comdisplayMapLocationService;
    @Autowired
    private OrgInfoUtil orgInfoUtil;

    @RequestMapping(value = "waterSupplyCharts", method = RequestMethod.GET)
    @ApiOperation(value = "综合展示--制水电耗对比曲线图(总分)")
    public AjaxMessage<List<WaterSupplyChart>> waterSupplyCharts(@RequestParam(required = false) Integer companyOrgId) {
        return new AjaxMessage<>(ResultStatus.OK, monitorDataReportService.waterSupplyChartsByMonth("水厂", companyOrgId));
    }

    @RequestMapping(value = "yieldPowerUsage", method = RequestMethod.GET)
    @ApiOperation(value = "综合展示--吨水电耗(总分)")
    public AjaxMessage<ComUsageDto> yieldPowerUsage(@RequestParam(required = false) Integer companyOrgId) {
        ComUsageDto sceneUsageDto = new ComUsageDto();
        BigDecimal yieldWaterUsage = monitorDataReportService.getAmountCount(companyOrgId, null, "水厂", 3, 2);
        BigDecimal powerUsage = monitorDataReportService.getAmountCount(companyOrgId, null, "水厂", 5, 2);
        sceneUsageDto.setYieldWaterUsage(yieldWaterUsage.divide(BigDecimal.valueOf(10000), 3, BigDecimal.ROUND_HALF_UP).doubleValue());
        sceneUsageDto.setPowerUsage(powerUsage.divide(BigDecimal.valueOf(10000), 3, BigDecimal.ROUND_HALF_UP).doubleValue());
        if (sceneUsageDto.getPowerUsage() != null && sceneUsageDto.getYieldWaterUsage() != null && sceneUsageDto.getPowerUsage() != 0) {
            sceneUsageDto.setYieldPowerUsage(powerUsage.divide(yieldWaterUsage, 3, BigDecimal.ROUND_HALF_UP).doubleValue());
        } else {
            sceneUsageDto.setYieldPowerUsage(0d);
        }
        return new AjaxMessage<>(ResultStatus.OK, sceneUsageDto);
    }
    @RequestMapping(value="selectMapForWholeCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "二供供水排行--总公司")
    public AjaxMessage<List<SecDataCollectDto>> selectMapForWholeCompany() {
        MonitorDataCollectDto monitorDataCollectDto = new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName("泵站");
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsByCom(null));
        List<SecDataCollectDto> result = onlineMonitorService.selectMapForWholeCompany(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    @RequestMapping(value="selectMapForCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "二供供水排行--分公司")
    public AjaxMessage<List<MonitorDataCollectDto>> selectMapForCompany(@RequestParam(required = true) Integer companyOrgId) {
        MonitorDataCollectDto monitorDataCollectDto = new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName("泵站");
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsByCom(Long.valueOf(companyOrgId)));
        List<MonitorDataCollectDto> result = onlineMonitorService.selectMapSuspensionForMonth(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    @RequestMapping(value="selectMapDataForCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "综合展示--地图详细信息展示(按子公司查询)")
    public AjaxMessage<ComdisplayInfoDto> selectMapDataForSinggleCompany(@RequestParam(required = true) Integer companyOrgId) {
        List<ComdisplayInfoDto> receFee = comdisplayMapLocationService.selectMapDataForSingleCompany(companyOrgId);
        if (receFee.size() > 0) {
            receFee.get(0).setCompanyOrgName(orgInfoUtil.getOrgName(receFee.get(0).getCompanyOrgId()));
            Double waterUsage = receFee.get(0).getWaterUsage() != null ? receFee.get(0).getWaterUsage() : 0d;//售水量
            //查询子公司的取水量、制水量 产销差(制水量-售水量)/制水量  X 100%
            receFee.get(0).setIntakeWaterUsage(monitorDataReportService.getAmountCount(companyOrgId, null, "水源", 4, 2).divide(new BigDecimal("10000"), 3, BigDecimal.ROUND_HALF_UP).doubleValue());
            receFee.get(0).setYieldWaterUsage(monitorDataReportService.getAmountCount(companyOrgId, null, "水厂", 3, 2).divide(new BigDecimal("10000"), 3, BigDecimal.ROUND_HALF_UP).doubleValue());
            receFee.get(0).setMeterReadingArrivalRate(receFee.get(0).getMeterReadingArrivalRate() != null ? BigDecimal.valueOf(receFee.get(0).getMeterReadingArrivalRate()).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP).doubleValue() : 0.00);
            double amount = receFee.get(0).getYieldWaterUsage() - waterUsage;
            if (receFee.get(0).getYieldWaterUsage() == 0d) {
                receFee.get(0).setWaterFeeRecoveryRate(BigDecimal.valueOf(0).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else {
                receFee.get(0).setWaterFeeRecoveryRate(BigDecimal.valueOf(amount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(receFee.get(0).getYieldWaterUsage()), 2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        }
        if (receFee.size() > 0) {
            return new AjaxMessage<>(ResultStatus.OK, receFee.get(0));
        } else {
            return new AjaxMessage<>(ResultStatus.OK, null);
        }
    }

    @RequestMapping(value="selectPipeNetLayer" , method = RequestMethod.GET)
    @ApiOperation(value = "查询管网地图图层及设备")
    public AjaxMessage<List<PipeNetLayerDto>> selectPipeNetLayer(
            @ApiParam(value = "公司机构id", required = false)@RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "一级场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setFlag(1);
        onlineDataDto.setSceneIds(companyOrgId==null?sceneService.findByParentIdsLike(null):sceneService.findByParentIdsByCom(Long.valueOf(companyOrgId)));
        List<PipeNetLayerDto> result=onlineMonitorService.selectPipeNetLayer(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectDeviceMapParam" , method = RequestMethod.GET)
    @ApiOperation(value = "查询设备地图参数数据")
    public AjaxMessage<List<MonitorDataCollectDto>> selectDeviceMapParam(
            @ApiParam(value = "设备id", required = true) @RequestParam Long deviceId){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setDeviceId(deviceId);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectDeviceMapParam(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectMapSuspension" , method = RequestMethod.GET)
    @ApiOperation(value = "查询地图悬浮数据")
    public AjaxMessage<List<MonitorDataCollectDto>> selectMapSuspension(
            @ApiParam(value = "公司机构id", required = false)@RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "一级场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName(sceneTypeName);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(companyOrgId==null?sceneService.findByParentIdsLike(null):sceneService.findByParentIdsByCom(Long.valueOf(companyOrgId)));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectMapSuspension(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectMapParam" , method = RequestMethod.GET)
    @ApiOperation(value = "查询地图参数数据")
    public AjaxMessage<List<MonitorDataCollectDto>> selectMapParam(
            @ApiParam(value = "一级场景id", required = true) @RequestParam Long sceneId){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneId(sceneId);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectMapParam(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

}
