 package com.huatek.busi.controller;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.huatek.busi.entity.TransformerPd;
import com.huatek.busi.service.BusiGeneratorAmplService;
import com.huatek.busi.service.BusiGeneratorPressureService;
import com.huatek.busi.service.cable.BusiCableCirculationService;
import com.huatek.busi.service.cable.CableCirculationService;
import com.huatek.busi.service.gis.Sf6EnvService;
import com.huatek.busi.service.transformer.*;
import com.huatek.busi.vo.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.common.BusiConstant.HomePagePart;
import com.huatek.busi.common.SiteConstants.ArresterTreeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.busi.constant.ApiConstant.Tags;
import com.huatek.busi.dto.BusiEqmWarningDto;
import com.huatek.busi.dto.CapacitanceEquipmentImCacheDto;
import com.huatek.busi.dto.GisPdCacheDto;
import com.huatek.busi.dto.ReadTableCurrentDto;
import com.huatek.busi.dto.RptAretMonitorDto;
import com.huatek.busi.dto.RptGisMonitorSf6Dto;
import com.huatek.busi.dto.RptTermperRecordDto;
import com.huatek.busi.dto.TransformerEpaDataCacheDto;
import com.huatek.busi.dto.TransformerIcgeCacheDto;
import com.huatek.busi.dto.TransformerInfraredMonitoringCacheDto;
import com.huatek.busi.dto.TransformerPdCacheDto;
import com.huatek.busi.dto.TransformerRunStatusDto;
import com.huatek.busi.service.BusiHomePageService;
import com.huatek.busi.service.ReadTableVisualizationService;
import com.huatek.busi.service.arrester.RptAretMonitorService;
import com.huatek.busi.service.capacitance.CapacitanceEquipmentImCacheService;
import com.huatek.busi.service.gis.GisPdCacheService;
import com.huatek.busi.service.gis.RptGisMonitorSf6Service;
import com.huatek.busi.service.temperature.RptTermperRecordService;
import com.huatek.busi.vo.monitor.BusiRptArresterMonitorVo;
import com.huatek.busi.vo.warning.BusiCapaVo;
import com.huatek.busi.vo.warning.BusiHomePageWarningVo;
import com.huatek.busi.vo.warning.BusiIcgeVo;
import com.huatek.busi.vo.warning.BusiOilVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * APP首页接口
 * @author buck_guo
 * @date 2019/08/19
 */
@Api(tags = {Tags.APP_INDEX_DATA})
@Slf4j
@RestController
@RequestMapping("/app/appHomePage")
public class AppHomePageController {
	
    @Reference(check = false)
    private BusiHomePageService busiHomePageService;
    @Reference(check = false)
    private RptTermperRecordService rptTermperRecordService;
    @Reference(check = false)
    private RptGisMonitorSf6Service rptGisMonitorSf6Service;
    @Reference(check = false)
    private TransformerPdCacheService transformerPdCacheService;
    @Reference(check = false)
    private GisPdCacheService gisPdCacheService;
    @Reference(check = false)
    private ReadTableVisualizationService readTableVisualizationService;
    @Reference(check = false)
    private TransformerRunStatusService transformerRunStatusService;
    @Reference(check = false)
    private TransformerInfraredMonitoringCacheService transformerInfraredMonitoringCacheService;
    @Reference(check = false)
    private TransformerEpaDataCacheService transformerEpaDataCacheService;
    @Reference(check = false)
    private TransformerIcgeCacheService transformerIcgeCacheService;
    @Reference(check = false)
    private RptAretMonitorService rptAretMonitorService;
    @Reference(check = false)
    private CapacitanceEquipmentImCacheService capacitanceEquipmentImCacheService;

    @Reference(check = false)
    private Sf6EnvService sf6EnvService;

    @Reference(check = false)
    private BusiGeneratorPressureService busiGeneratorPressureService;

    @Reference(check = false)
    private BusiGeneratorAmplService busiGeneratorAmplService;

    @Reference(check = false)
    private TransformerWindingTemperatureService transformerWindingTemperatureService;

    @Reference(check = false)
    private BusiGeneratorPdService busiGeneratorPdService;

    @Reference(check = false)
    private CwzzTemperatureService cwzzTemperatureService;

    @Reference(check = false)
    private BusiCableCirculationService busiCableCirculationService;


    @Reference(check = false)
    private CableCirculationService cableCirculationService;


    @Reference(check = false)
    private TransformerPdService transformerPdService;


    
    @ApiOperation(value = "变压器及容性设备数据列表", response = BusiHomePageVo.class)
    @ApiImplicitParam(name = "type", value = "类型：YSP--油色谱 , TX--铁芯, TG--套管, RX--容性设备, JJ--夹件")
    @GetMapping(value = "/transfromerList")
    public Map<String, Object> transfromerList(@RequestParam("type") String type) {
        List<BusiHomePageVo> list = Collections.emptyList();
        if ("YSP".equals(type)) {
            list = busiHomePageService.findTransfromerlist(TransfromerTreeEnum.YSP_EQM.getValue());
        } else if ("TX".equals(type)) {
            list = busiHomePageService.findTransfromerlist(TransfromerTreeEnum.TX_EQM.getValue());
        } else if ("TG".equals(type)) {
            list = busiHomePageService.findTransfromerlist(TransfromerTreeEnum.TG_EQM.getValue());
        } else if ("RX".equals(type)) {
            list = busiHomePageService.findArresterlist(ArresterTreeEnum.RX_EQM.getValue());
        } else if ("BLQ".equals(type)) {
            list = busiHomePageService.findArresterlist(ArresterTreeEnum.JY_EQM.getValue());
        } else if ("JJ".equals(type)) {
            list = busiHomePageService.findTransfromerlist(TransfromerTreeEnum.JJ_EQM.getValue());
        }
        log.debug("type: {}, list: {}", type, list);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }

    @ApiOperation(value = "变压器及容性设备数据信息", response = BusiHomeInfoVo.class)
    @PostMapping(value = "/transfromerInfo")
    public Map<String, Object> transfromerInfo(@RequestBody BusiHomePageVo busiHP) {
        SiteTreeEnum siteTreeEnum = null;
        HomePagePart part = HomePagePart.from(busiHP.getType());
        switch (part) {
            case BLQ:
            case RX:
                siteTreeEnum = SiteTreeEnum.BLQ;
                break;
            case JJ:
            case TG:
            case TX:
            case YSP:
                siteTreeEnum = SiteTreeEnum.BYQ;
                break;
        }
        BusiHomeInfoVo info = busiHomePageService.findTransfromerInfo(busiHP.getMainTreeId(), busiHP.getSubTreeId(),
            busiHP.getSubEquipmentId(), siteTreeEnum, busiHP.getEquipmentType(), busiHP.getSensorId());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", info);
    }

    /**
     * 获取最近的告警列表
     * 
     * @param eqType
     *            告警大类
     * @param gzType
     *            告警小类
     * @param page
     *            当前页
     * @param limit
     *            每页大小
     * @return 告警列表
     */
    @ApiOperation(value = "获取最近的告警列表", response = BusiHomePageWarningVo.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "eqType", value = "告警大类", required = false),
        @ApiImplicitParam(name = "gzType", value = "告警小类", required = false),
        @ApiImplicitParam(name = "page", value = "当前页", required = false),
        @ApiImplicitParam(name = "limit", value = "每页大小", required = false)
    })
    @GetMapping(value = "/getLastWarning")
    public Map<String, Object> getLastWarning(@RequestParam(value = "eqType", required = false) String eqType,
        @RequestParam(value = "gzType", required = false) String gzType,
        @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit) {
        BusiEqmWarningDto busiEqmWarningDto = new BusiEqmWarningDto();
        busiEqmWarningDto.setEqType(eqType);
        busiEqmWarningDto.setGzType(gzType);
        busiEqmWarningDto.setPage(page == null ? 1 : page);
        busiEqmWarningDto.setLimit(limit == null ? BusiConstant.MAX_WARNING_PAGE_SIZE : limit);
        PageInfo<BusiHomePageWarningVo> pageInfo = busiHomePageService.findCurrentWarningList(busiEqmWarningDto);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "获取最近的告警列表", response = BusiHomePageWarningVo.class)
    @PostMapping(value = "/getLastWarning")
    public Map<String, Object> getLastWarning(@RequestBody BusiEqmWarningDto param) {
        param.setPage(param.getPage() == null ? 1 : param.getPage());
        param.setLimit(param.getLimit() == null ? BusiConstant.MAX_WARNING_PAGE_SIZE : param.getLimit());
        PageInfo<BusiHomePageWarningVo> pageInfo = busiHomePageService.findCurrentWarningList(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }
    @ApiOperation(value = "油色谱实时数据查询", response = TransformerEpaDataCacheVo.class)
    @PostMapping(value = "/epaDataCacheCurrent")
    public Map<String, Object> epaDataCacheCurrent(@RequestBody TransformerEpaDataCacheDto param) {
    	BusiOilVo epaInfo = transformerEpaDataCacheService.findTransformerEpaDataCacheRecordPage(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",epaInfo);
    }
    
    @ApiOperation(value = "铁芯、夹件实时数据查询", response = BusiIcgeVo.class)
    @PostMapping(value = "/coreClipDataCacheCurrent")
    public Map<String, Object> coreClipDataCacheCurrent(@RequestBody TransformerIcgeCacheDto param) {
    	BusiIcgeVo epaInfo = transformerIcgeCacheService.getFirstTransformerIcgeCacheInfo(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",epaInfo);
    }
    
    @ApiOperation(value = "避雷器实时数据查询", response = BusiRptArresterMonitorVo.class)
    @PostMapping(value = "/aretMonitorDataCacheCurrent")
    public Map<String, Object> LightningrodImDataCacheCurrent(@RequestBody RptAretMonitorDto param) {
    	BusiRptArresterMonitorVo epaInfo = rptAretMonitorService.getUptoDateRptAretMonitor(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",epaInfo);
    }
    
    @ApiOperation(value = "避雷器容性实时数据查询", response = BusiCapacitanceMonitorVo.class)
    @PostMapping(value = "/capacitanceDataCacheCurrent")
    public Map<String, Object> capacitanceDataCacheCurrent(@RequestBody CapacitanceEquipmentImCacheDto param) {
    	BusiCapaVo epaInfo = capacitanceEquipmentImCacheService.getUptoDateCapacitanceMonitor(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",epaInfo);
    }
    
    @ApiOperation(value = "测温柜实时数据分页查询", response = RptTermperRecordVo.class)
    @PostMapping(value = "/cwgCurrent")
    public Map<String, Object> cwgCurrent(@RequestBody RptTermperRecordDto param) {
        PageInfo<RptTermperRecordVo> pageInfo = rptTermperRecordService.findRptTermperRecordPage(param);
        
        if(pageInfo != null)
        {
            DecimalFormat df = new DecimalFormat("0.00%");
            pageInfo.getList().stream().forEach(e -> {
                if(e.getDailyGrowthRate()!=null)
                {
                    e.setDailyGrowthRate(df.format(Double.parseDouble(e.getDailyGrowthRate())).replace("%",""));
                }
                if(e.getMonthlyGrowthRate()!=null)
                {
                    e.setMonthlyGrowthRate(df.format(Double.parseDouble(e.getMonthlyGrowthRate())).replace("%",""));
                }
            });
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "SF6温度、压力、微水实时数据分页查询", response = RptGisMonitorSf6Vo.class)
    @PostMapping(value = "/gisSf6Current")
    public Map<String, Object> gisSf6Current(@RequestBody RptGisMonitorSf6Dto param) {
        PageInfo<RptGisMonitorSf6Vo> pageInfo = rptGisMonitorSf6Service.findRptGisMonitorSf6Page(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "变压器局放实时数据分页查询", response = TransformerPdCacheVo.class)
    @PostMapping(value = "/transPdCurrent")
    public Map<String, Object> transPdCurrent(@RequestBody TransformerPdCacheDto param) {
        PageInfo<TransformerPdCacheVo> pageInfo = transformerPdCacheService.findTransformerPdCachePage(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "GIS局放实时数据分页查询", response = GisPdCacheVo.class)
    @PostMapping(value = "/gisPdCurrent")
    public Map<String, Object> gisPdCurrent(@RequestBody GisPdCacheDto param) {
        PageInfo<GisPdCacheVo> pageInfo = gisPdCacheService.findGisPdCachePage(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }
    
    @ApiOperation(value = "图像实时数据分页查询", response = ReadTableVisualizationVo.class)
    @PostMapping(value = "/pictureCurrent")
    public Map<String, Object> pictureCurrent(@RequestBody ReadTableCurrentDto param) {
        if(param.getDTimeEnd() != null) {
            param.setDTimeEnd(param.getDTimeEnd().plusDays(1));
        }
        PageInfo<ReadTableCurrentVo> pageInfo = readTableVisualizationService.findReadTableCurrentPageData(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "变压器运行工况时数据分页查询", response = TransformerRunStatusVo.class)
    @PostMapping(value = "/transformerRunStatusCurrent")
    public Map<String, Object> transformerRunStatusCurrent(@RequestBody TransformerRunStatusDto param) {
        PageInfo<TransformerRunStatusVo> pageInfo = transformerRunStatusService.transformerRunStatusCurrent(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "红外热成像时数据分页查询", response = TransformerInfraredMonitoringCacheVo.class)
    @PostMapping(value = "/findInfraredThermalImag")
    public Map<String, Object> findInfraredThermalImag(@RequestBody TransformerInfraredMonitoringCacheDto param) {
        PageInfo<TransformerInfraredMonitoringCacheVo> pageInfo =
            transformerInfraredMonitoringCacheService.findTransformerInfraredMonitoringCachePage(param);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    @ApiOperation(value = "SF6Env实时数据分页查询", response = HomeActualVo.class)
    @PostMapping(value = "/gisSf6EnvCurrent")
    public Map<String, Object> gisSf6EnvCurrent() {
        List<HomeActualVo> pageInfo = sf6EnvService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }

    @ApiOperation(value = "变压器绕组测温查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfoRZCWCurrent")
    public Map<String, Object> findInfoRZCWCurrent() {
        List<HomeActualVo> pageInfo = transformerWindingTemperatureService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }
    @ApiOperation(value = "发电机振动查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfGenzdCurrent")
    public Map<String, Object> findInfGenzdCurrent() {
        List<HomeActualVo> pageInfo = busiGeneratorAmplService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }

    @ApiOperation(value = "发电机压力查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfGenylCurrent")
    public Map<String, Object> findInfGenylCurrent() {
        List<HomeActualVo> pageInfo = busiGeneratorPressureService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }

    @ApiOperation(value = "发电机局放查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfGenjfCurrent")
    public Map<String, Object> findInfGenjfCurrent() {
        List<HomeActualVo> pageInfo = busiGeneratorPdService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }

    @ApiOperation(value = "电缆测温装置查询查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfoCWZZCurrent")
    public Map<String, Object> findInfoCWZZCurrent() {
        List<HomeActualVo> pageInfo = cwzzTemperatureService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }
    @ApiOperation(value = "电缆环流查询查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfoDLHLCurrent")
    public Map<String, Object> findInfoDLHLCurrent() {
        List<HomeActualVo> pageInfo = cableCirculationService.queryActuals();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }

    @ApiOperation(value = "电缆局放查询查询", response = HomeActualVo.class)
    @PostMapping(value = "/findInfoDLJFCurrent")
    public Map<String, Object> findInfoDLJFCurrent() {
        List<HomeActualVo> pageInfo = transformerPdService.queryActualsCable();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS,  "", pageInfo);
    }
}
