package com.zcxk.charge.web.controller;

import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.dto.generalview.WaterSupplyChart;
import com.zcxk.charge.api.vo.ComdisplayInfoVo;
import com.zcxk.charge.web.service.*;
import com.zcxk.charge.web.util.OrgInfoUtil;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.utils.BigDecimalUtils;
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.util.CollectionUtils;
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;


/**
 * @author makejava
 */
@RestController
@RequestMapping("/comDisplay")
@Api(tags = "综合展示")
public class ComDisplayController extends BaseController{


    @Autowired
    private MonitorDataReportService monitorDataReportService;

    @Autowired
    private SceneService sceneService;

    @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 AjaxMessage.success( monitorDataReportService.waterSupplyChartsByMonth(WATER_WORKS.getMessage(), 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, WATER_WORKS.getMessage(), 3, 2);
        BigDecimal powerUsage = monitorDataReportService.getAmountCount(companyOrgId, null, WATER_WORKS.getMessage(), 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 AjaxMessage.success(sceneUsageDto);
    }

    @RequestMapping(value="selectMapForWholeCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "二供供水排行--总公司")
    public AjaxMessage<List<SecDataCollectDto>> selectMapForWholeCompany() {
        MonitorDataCollectDto monitorDataCollectDto = new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName(PUMPING_STATION.getMessage());
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsByCom(null));
        List<SecDataCollectDto> result = onlineMonitorService.selectMapForWholeCompany(monitorDataCollectDto);
        return AjaxMessage.success( result);
    }

    @RequestMapping(value="selectMapForCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "二供供水排行--分公司")
    public AjaxMessage selectMapForCompany(@RequestParam Integer companyOrgId) {
        MonitorDataCollectDto monitorDataCollectDto = new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName(PUMPING_STATION.getMessage());
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsByCom(Long.valueOf(companyOrgId)));
        return AjaxMessage.success(onlineMonitorService.selectMapSuspensionForMonth(monitorDataCollectDto));
    }


    @RequestMapping(value="selectMapDataForCompany" , method = RequestMethod.GET)
    @ApiOperation(value = "综合展示--地图详细信息展示(按子公司查询)")
    public AjaxMessage<ComdisplayInfoVo> selectMapDataForSinggleCompany(@RequestParam Integer companyOrgId) {
        List<ComdisplayInfoVo> receFee = comdisplayMapLocationService.selectMapDataForSingleCompany(companyOrgId);
        if (CollectionUtils.isEmpty(receFee)) {
            return AjaxMessage.success(null);
        }
        double defaultDouble = 0D;
        int scale = 3;
        String divideNum = "10000";
        receFee.get(0).setCompanyOrgName(orgInfoUtil.getOrgName(receFee.get(0).getCompanyOrgId()));
        // 售水量
        Double waterUsage = receFee.get(0).getWaterUsage() != null ? receFee.get(0).getWaterUsage() : 0d;
        // 查询子公司的取水量、制水量 产销差(制水量-售水量)/制水量  X 100%
        BigDecimal intakeWaterUsage = monitorDataReportService.getAmountCount(companyOrgId, null, WATER_SOURCE.getMessage(), 4, 2);
        BigDecimal yieldWaterUsage  = monitorDataReportService.getAmountCount(companyOrgId, null, WATER_WORKS.getMessage(), 3, 2);
        Double rate = receFee.get(0).getMeterReadingArrivalRate() == null ? 0.00:
                BigDecimal.valueOf(receFee.get(0).getMeterReadingArrivalRate()).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
        receFee.get(0).setIntakeWaterUsage(BigDecimalUtils.divide(intakeWaterUsage, divideNum, scale).doubleValue());
        receFee.get(0).setYieldWaterUsage(BigDecimalUtils.divide(yieldWaterUsage, divideNum, scale).doubleValue());
        receFee.get(0).setMeterReadingArrivalRate(rate);
        double amount = receFee.get(0).getYieldWaterUsage() - waterUsage;
        if (receFee.get(0).getYieldWaterUsage() == defaultDouble) {
            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());
        }
        return AjaxMessage.success(receFee.get(0));
    }

    @RequestMapping(value="selectPipeNetLayer" , method = RequestMethod.GET)
    @ApiOperation(value = "查询管网地图图层及设备")
    public AjaxMessage selectPipeNetLayer(
            @ApiParam(value = "公司机构id")@RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "一级场景类型名称") @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)));
        return AjaxMessage.success(onlineMonitorService.selectPipeNetLayer(onlineDataDto));
    }

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

    @RequestMapping(value="selectMapSuspension" , method = RequestMethod.GET)
    @ApiOperation(value = "查询地图悬浮数据")
    public AjaxMessage selectMapSuspension(
            @ApiParam(value = "公司机构id")@RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "一级场景类型名称") @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)));
        return AjaxMessage.success(onlineMonitorService.selectMapSuspension(monitorDataCollectDto));
    }

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

}
