package com.sptg.gateway.controller.statistics;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.dto.AdTypeDto;
import com.sptg.ad.api.entity.AdType;
import com.sptg.ad.api.feigns.AdOfflinePushRecordFeign;
import com.sptg.ad.api.feigns.AdTypeFeign;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.DateUtil;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.*;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.entity.Unit;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.dto.OrgDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.feigns.AreaFeign;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.statistics.api.constant.StatisticsSecurityToken;
import com.sptg.statistics.api.dto.StatisticsAdPushDto;
import com.sptg.statistics.api.dto.StatisticsAdWatchDto;
import com.sptg.statistics.api.entity.StatisticsAdWatch;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.api.feigns.StatisticsAdPushFeign;
import com.sptg.statistics.api.feigns.StatisticsAdWatchFeign;
import com.sptg.statistics.api.feigns.StatisticsCountFeign;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * title: DataCockpitController
 * package: com.sptg.gateway.controller.statistics
 * description: 数据驾驶舱-网关控制器
 *
 *                
 *                
 */
@RestController
@RequestMapping(value = "/statistics/data/cockpit")
@AllArgsConstructor
@Slf4j
public class DataCockpitController {
    private final StatisticsAdPushFeign statisticsAdPushFeign;
    private final StatisticsAdWatchFeign statisticsAdWatchFeign;
    private final StatisticsCountFeign statisticsCountFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final AreaFeign areaFeign;
    private final AdTypeFeign adTypeFeign;
    private final OrgFeign orgFeign;
    private final DeviceAuthorizeFeign deviceAuthorizeFeign;
    private final ResidentialFeign residentialFeign;
    private final UnitFeign unitFeign;
    private final GuestAuthorizeFeign guestAuthorizeFeign;
    private final AccessRecordsFeign accessRecordsFeign;
    private final ApplyUserFeign applyUserFeign;
    private final UserFeign userFeign;
    private final ServerUserFeign serverUserFeign;
    private final MemberFeign memberFeign;
    private final AdOfflinePushRecordFeign adOfflinePushRecordFeign;

    /**
     * 功能描述:广告数据驾驶舱
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping("/adDataCockpit")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE)
    public ModelAndView adDataCockpit(){
        ModelAndView modelAndView = new ModelAndView("statistics/ad-data-cockpit");
        return modelAndView;
    }

    /**
     * 功能描述:广告数据驾驶舱统计数量
     * @param regionProvince
     * @return ResultBean<Map<String,Object>>
     *                
     *                      
     */
    @PostMapping("/adDataCockpitCount")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> adDataCockpitCount(String regionProvince){
        Map<String,Object> resultMap=new HashMap<>(OperationConstant.FIVE);
        Account onLineUser = securityServiceInterface.getOnLineUser();
        //广告类别数量
        int adTypeCount=0;
        //广告投放
        long adExposureCount=0L;
        //广告观看次数
        long adWatchCount=0L;
        //上周观看次数
        Long adWatchCountDayOfLastWeek =0L;
        //本周观看次数
        Long adWatchCountDayOfWeek=0L;

        StatisticsAdWatchDto statisticsAdWatchDto=new StatisticsAdWatchDto();
        statisticsAdWatchDto.setRegionProvince(regionProvince);
        statisticsAdWatchDto.setOrgPosition(onLineUser.getOrgPosition());
        if(StringUtils.isNotBlank(regionProvince)){
            StatisticsAdPushDto statisticsAdPushDto=new StatisticsAdPushDto();
            statisticsAdPushDto.setDeviceRegionProvince(regionProvince);
            statisticsAdPushDto.setOrgPosition(onLineUser.getOrgPosition());
            adExposureCount= statisticsAdPushFeign.statisticsAdPushCount(statisticsAdPushDto);
            adWatchCount=statisticsAdWatchFeign.getAdWatchCount(statisticsAdWatchDto);
        }else{
            ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(onLineUser.getOrgPosition());
            if(statisticsCountResultBean!=null){
                StatisticsCount statisticsCount = statisticsCountResultBean.getData();
                if(statisticsCount!=null){
                    //广告观看次数
                    adWatchCount = statisticsCount.getAdWatchCount();
                    //广告投放
                    adExposureCount=statisticsCount.getAdExposureCount();
                }
            }
        }

        //本周一0点
        Date beginDayOfWeek = DateUtil.getBeginDayOfWeek();
        //上周一0点
        Date beginDayOfLastWeek = DateUtil.getBeginDayOfLastWeek();
        statisticsAdWatchDto.setStartDate(beginDayOfLastWeek);
        statisticsAdWatchDto.setEndDate(beginDayOfWeek);
        adWatchCountDayOfLastWeek = statisticsAdWatchFeign.getAdWatchCount(statisticsAdWatchDto);
        statisticsAdWatchDto.setStartDate(beginDayOfWeek);
        statisticsAdWatchDto.setEndDate(null);
        adWatchCountDayOfWeek =  statisticsAdWatchFeign.getAdWatchCount(statisticsAdWatchDto);

        //广告分类数量
        AdTypeDto adTypeDto=new AdTypeDto();
        adTypeDto.setRootOrgId(onLineUser.getRootOrgId());
        Integer adTypeCountI=adTypeFeign.getAdTypeCount(adTypeDto);
        adTypeCount=adTypeCountI==null?0:adTypeCountI;
        resultMap.put("adTypeCount",adTypeCount);
        resultMap.put("adExposureCount",adExposureCount);
        resultMap.put("adWatchCount",adWatchCount);
        resultMap.put("adWatchCountDayOfWeek",adWatchCountDayOfWeek);
        resultMap.put("adWatchCountDayOfLastWeek",adWatchCountDayOfLastWeek);

        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:广告观看量TOP10城市
     * @param dto
     * @return ResultBean<List<Map<String, Object>>>
     *                
     *                      
     */
    @PostMapping("/statisticsAdWatchRankForArea")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdWatchRankForArea(StatisticsAdWatchDto dto){
        //登录用户
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页 默认大小10 第1页
        Page page=new Page<>();
        dto.setPage(page);
        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchRankForArea(dto);
        return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,maps);
    }

    /**
     * 功能描述:广告分布区域
     * @param dto
     * @return ResultBean<List<Map<String, Object>>>
     *                
     *                      
     */
    @PostMapping("/statisticsAdPushAndExposureByArea")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdPushAndExposureByArea(StatisticsAdPushDto dto){
        //登录用户
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        List<Map<String, Object>> maps = statisticsAdPushFeign.statisticsAdPushAndExposureByArea(dto);
        //转换省市编号换成省市中文
        if(maps!=null){
            List<Map<String, Object>>  resultList=new ArrayList<Map<String, Object>>(maps.size());
            maps.forEach(map->{
                String code=(String)map.get("regionProvince");
                if(StringUtils.isNotBlank(code)) {
                    Area area = areaFeign.selectByCode(code);
                    if (null != area) {
                        //缩写的
                        String areaName = area.getShortName();
                        Map<String, Object> resultMap = new HashMap<String, Object>(OperationConstant.TWO);
                        resultMap.put("regionProvince", code);
                        resultMap.put("area", areaName);
                        resultMap.put("adExposureCount", map.get("adExposureCount"));
                        resultList.add(resultMap);
                    }
                }
            });
            return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,resultList);
        }
        return  new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS);
    }

    /**
     * 功能描述:广告区域线上线下分布
     *
     * @param dto
     * @return ResultBean<List < Map < String, Object>>>
     */
    @PostMapping("/statisticsAdPushAndExposureByAreaAndOffline")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdPushAndExposureByAreaAndOffline(StatisticsAdPushDto dto) {
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        List<Map<String, Object>> maps = statisticsAdPushFeign.statisticsAdPushAndExposureByArea(dto);
        if (maps != null) {
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(maps.size());
            maps.forEach(map -> {
                String code = (String) map.get("regionProvince");
                if (StringUtils.isNotBlank(code)) {
                    Area area = areaFeign.selectByCode(code);
                    if (null != area) {
                        String areaName = area.getShortName();
                        Integer offlineCount = adOfflinePushRecordFeign.getAdOfflinePushRecordByProvinceName(areaName, onLineUser.getOrgPosition());
                        Map<String, Object> resultMap = new HashMap<String, Object>(OperationConstant.TWO);
                        resultMap.put("regionProvince", code);
                        resultMap.put("area", areaName);
                        resultMap.put("adExposureCount", map.get("adExposureCount"));
                        if(offlineCount != null){
                            resultMap.put("offlineCount",offlineCount);
                        }else {
                            resultMap.put("offlineCount",0);
                        }
                        resultList.add(resultMap);
                    }
                }
            });
            return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS, resultList);
        }
        return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS);
    }
/**
     * 功能描述:广告月度(每日)观看趋势
     *
     * @param dto
     * @return ResultBean<List < Map < String, Object>>>
     */

    @PostMapping("/statisticsAdWatchCountChartByDateType")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdWatchCountChartByDateType(StatisticsAdWatchDto dto){
        //登录用户
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setWatchDate(new Date());

        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchCountChartByDateType(dto);
        return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,maps);
    }

   /**
     * 功能描述:广告月度线上线下投放折线图
     *
     * @param year
     * @return ResultBean<List < Map < String, Object>>>
     */
    @PostMapping("/statisticsAdCountByDateType")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> statisticsAdCountByDateType(String year,String month) {
        Account onLineUser = securityServiceInterface.getOnLineUser();
        List<Map<String,Object>> maps = new ArrayList<>();
        if(month != null && month != ""){//某年某月每一天的数据
        }else{//某年每月数据
            for(int i =1;i<=12;i++){
                Map<String, Object> resultMap = new HashMap<String, Object>(OperationConstant.TWO);
                Integer onlineCount = 0;
                Integer offlineCount = this.adOfflinePushRecordFeign.getAdOfflinePushRecordByYearMonth(year, String.valueOf(i));
                resultMap.put("onlineCount",onlineCount);
                resultMap.put("offlineCount",offlineCount);
                maps.add(resultMap);
            }
        }
        return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS, maps);
    }
/**
     * 功能描述:广告观看明细
     *
     * @param dto
     * @return ResultBean<List < StatisticsAdWatch>>
     */

    @PostMapping("/statisticsAdWatchRank")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<StatisticsAdWatch>> statisticsAdWatchRank(StatisticsAdWatchDto dto){
        //登录用户
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页
        Page page=new Page<>();
        //大小15条记录
        page.setSize(15);
        dto.setPage(page);
        ResultBean<Page<StatisticsAdWatch>> resultBean = statisticsAdWatchFeign.showStatisticsAdWatchRankList(dto);
        Page<StatisticsAdWatch> page2 = resultBean.getData();
        if(null!=page2){
             page2.getRecords().forEach(adWatch->{
                //获取广告分类名称并赋值
                Long adTypeId=adWatch.getAdTypeId();
                if(adTypeId!=null){
                    AdType adType=adTypeFeign.getAdTypeById(adTypeId);
                    String name=adType!=null?adType.getName():"";
                    adWatch.setAdTypeName(name);
                }
            });
            return new ResultBean<List<StatisticsAdWatch>>(ResultEnum.SUCCESS,page2.getRecords());
        }
        return  new ResultBean<List<StatisticsAdWatch>>(ResultEnum.SUCCESS);
    }

    /**
     * 功能描述:广告类别观看分布
     * @param dto
     * @return ResultBean<List<Map<String, Object>>>
     *                
     *                      
     */
    @PostMapping("/statisticsAdWatchRankForAdType")
    @Security(token = StatisticsSecurityToken.STATISTICS_AD_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public  ResultBean<List<Map<String, Object>>> statisticsAdWatchRankForAdType(StatisticsAdWatchDto dto) {
        //登录用户
        Account onLineUser = securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页
        Page page=new Page<>();
        //大小15条记录
        page.setSize(20);
        dto.setPage(page);
        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchRankForAdType(dto);
        return  new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,maps);
    }

    /**--------------------------------------------首页数据驾驶舱---------------------------------------------------**/
    /**
     * 功能描述:首页数据驾驶舱
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping("/homeDataCockpit")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE)
    public ModelAndView homeDataCockpit(){
        ModelAndView modelAndView = new ModelAndView("statistics/home-data-cockpit");
        return modelAndView;
    }

    /**
     * 功能描述:首页数据驾驶舱广告统计数量
     * @return ResultBean<Map<String,Object>>
     *                
     *                      
     */
    @PostMapping("/homeAdDataCockpitCount")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeAdDataCockpitCount(){
        Map<String,Object> resultMap=new HashMap<>(OperationConstant.THREE);
        Account onLineUser = securityServiceInterface.getOnLineUser();
        //广告类别数量
        int adTypeCount=0;
        //广告投放
        long adExposureCount=0L;
        //广告观看次数
        long adWatchCount=0L;
        StatisticsAdWatchDto statisticsAdWatchDto=new StatisticsAdWatchDto();
        statisticsAdWatchDto.setOrgPosition(onLineUser.getOrgPosition());

        ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(onLineUser.getOrgPosition());
        if(statisticsCountResultBean!=null){
            StatisticsCount statisticsCount = statisticsCountResultBean.getData();
            if(statisticsCount!=null){
                //广告投放
                adExposureCount=statisticsCount.getAdExposureCount();
                //广告观看次数
                adWatchCount=statisticsCount.getAdWatchCount();
            }
        }
        //广告分类数量
        AdTypeDto adTypeDto=new AdTypeDto();
        adTypeDto.setRootOrgId(onLineUser.getRootOrgId());
        Integer adTypeCountI=adTypeFeign.getAdTypeCount(adTypeDto);
        adTypeCount=adTypeCountI==null?0:adTypeCountI;
        resultMap.put("adTypeCount",adTypeCount);
        resultMap.put("adExposureCount",adExposureCount);
        resultMap.put("adWatchCount",adWatchCount);
        return  new ResultBean<Map<String,Object>>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:首页数据驾驶舱物业统计数量
     * @param
     * @return ResultBean<Map<String,Object>>
     *                
     *                      
     */
    @PostMapping("/homeEstateDataCockpitCount")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeEstateDataCockpitCount(){
        Map<String,Object> resultMap=new HashMap<>(6);
        Account onLineUser = securityServiceInterface.getOnLineUser();

        //物业数量
        OrgDto orgDto=new OrgDto();
        orgDto.setPosition(onLineUser.getOrgPosition());
        orgDto.setOrgType(OperationConstant.THREE);
        orgDto.setFlag(OperationConstant.ONE);
        int orgCount = orgFeign.selectCount(orgDto);
        resultMap.put("orgCount",orgCount);
       //小区数量
        ResidentialDto residentialDto = new ResidentialDto();
        residentialDto.setOrgPosition(onLineUser.getOrgPosition());
        Integer residentialCount = this.residentialFeign.getResidentialCount(residentialDto);
        resultMap.put("residentialCount",residentialCount);
        //单元数量
        UnitDto unitDto=new UnitDto();
        unitDto.setOrgPosition(onLineUser.getOrgPosition());
        int unitCount =this.unitFeign.getUnitCount(unitDto);
        resultMap.put("unitCount",unitCount);
        //设备数量
        DeviceAuthorizeDto deviceAuthorizeDto = new DeviceAuthorizeDto();
        deviceAuthorizeDto.setOrgPosition(onLineUser.getOrgPosition());
        long deviceCount = this.deviceAuthorizeFeign.getDeviceCount(deviceAuthorizeDto);
        resultMap.put("deviceCount",deviceCount);
        //在线设备数量
        deviceAuthorizeDto.setOnlineStatus(OperationConstant.ONE);
        long onlineDeviceCount = this.deviceAuthorizeFeign.getDeviceCount(deviceAuthorizeDto);
        resultMap.put("onlineDeviceCount",onlineDeviceCount);
        //住户数量
        UserDto userDto=new UserDto();
        userDto.setOrgPosition(onLineUser.getOrgPosition());
        int userCount=this.userFeign.getUserCount(userDto);

        resultMap.put("userCount",userCount);
       //访客数量
        GuestAuthorizeDto guestAuthorizeDto=new GuestAuthorizeDto();
        guestAuthorizeDto.setOrgPosition(onLineUser.getOrgPosition());
        long guestCount = this.guestAuthorizeFeign.getGuestCount(guestAuthorizeDto);
        resultMap.put("guestCount",guestCount);
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:广告每日观看趋势(广告观看次数，设备开门次数)
     * @return ResultBean<Map<String,Object>>
     *                
     *                      
     */
    @PostMapping("/homeAdWatchDay")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeAdWatchDay(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Map<String,Object> resultMap=new HashMap<>(OperationConstant.TWO);
        //广告观看人数
        StatisticsAdWatchDto statisticsAdWatchDto=new StatisticsAdWatchDto();
        statisticsAdWatchDto.setWatchDate(new Date());
        statisticsAdWatchDto.setOrgPosition(onLineUser.getOrgPosition());
        List<Map<String, Object>> adWatchPersonForDay = this.statisticsAdWatchFeign.statisticsAdWatchPersonForDay(statisticsAdWatchDto);
        resultMap.put("adWatchPersonForDay",adWatchPersonForDay);
        //设备开门次数
        AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
        accessRecordsDto.setAccessDate(new Date());
        accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        accessRecordsDto.setGroupType(OperationConstant.ONE);
        List<Map<String, Object>> accessCountForDay = this.accessRecordsFeign.accessCountListForGroupType(accessRecordsDto);
        resultMap.put("accessCountForDay",accessCountForDay);
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:设备分布区域
     * @return ResultBean<List<Map<String,Object>>>
     *                
     *                      
     */
    @PostMapping("/homeDeviceCountDistribution")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> homeDeviceCountDistribution(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        DeviceAuthorizeDto dto = new DeviceAuthorizeDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        List<Map<String, Object>> deviceCountForArea = this.deviceAuthorizeFeign.getDeviceCountForArea(dto);
        //转换省市编号换成省市中文
        if(deviceCountForArea!=null) {
            deviceCountForArea.forEach(map -> {
                String code = "";
                code = (String) map.get("regionProvince");
                if(StringUtils.isNotBlank(code)){
                    Area area = areaFeign.selectByCode(code);
                    if (null != area) {
                        //缩写的
                        String areaName = area.getShortName();
                        map.put("regionProvince", areaName);
                    }
                }
            });
        }
        return new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,deviceCountForArea);
    }

    /**
     * 功能描述:城市物业明细
     * @return ResultBean<List<Map<String,Object>>>
     *                
     *                      
     */
    @PostMapping("/homeCityEstateDetail")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> homeCityEstateDetail(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResidentialDto residentialDto = new ResidentialDto();
        residentialDto.setOrgPosition(onLineUser.getOrgPosition());
        List<Map<String, Object>> cityCountList = this.residentialFeign.statisticsCityCountList(residentialDto);
        //转换省市编号换成省市中文
        if(cityCountList!=null) {
            cityCountList.forEach(map -> {
                String code = "";
                code = (String) map.get("regionCity");
                if(StringUtils.isNotBlank(code)){
                    Area area = areaFeign.selectByCode(code);
                    if (null != area) {
                        //缩写的
                        String areaName = area.getShortName();
                        map.put("regionCity", areaName);
                    }
                }
            });
        }
        return new ResultBean<List<Map<String,Object>>>(ResultEnum.SUCCESS,cityCountList);
    }

    /**
     * 功能描述:开门方式
     * @return  ResultBean<Map<String,Object>>
     *                
     *                     
     */
    @PostMapping("/homeOpenWay")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Long>> homeOpenWay(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Map<String, Long> openWayMap=new HashMap<String, Long>(OperationConstant.FIVE);
        //获取统计数量
        ResultBean<StatisticsCount> statisticsCountResultBean = this.statisticsCountFeign.selectCountOne(onLineUser.getOrgPosition());
        StatisticsCount statisticsCount = statisticsCountResultBean.getData();
        long cardOpenCount=0;
        long telnetOpenCount=0;
        long faceOpenCount=0;
        long passWordOpenCount=0;
        if(statisticsCount!=null){
            //远程开门
            telnetOpenCount = statisticsCount.getAccessRecordTelnetCount();
            //刷卡开门
            cardOpenCount = statisticsCount.getAccessRecordCardCount();
            //人脸识别
            faceOpenCount = statisticsCount.getAccessRecordFaceCount();
            //密码开门
            passWordOpenCount = statisticsCount.getAccessRecordPasswordCount();
        }
        openWayMap.put("cardOpenCount",cardOpenCount);
        openWayMap.put("telnetOpenCount",telnetOpenCount);
        openWayMap.put("faceOpenCount",faceOpenCount);
        openWayMap.put("passWordOpenCount",passWordOpenCount);
        return  new ResultBean<Map<String,Long>>(ResultEnum.SUCCESS,openWayMap);
    }

    /**
     * 功能描述:住户认证
     * @return ResultBean<Map<String,Object>>
     *                
     *                     
     */
    @PostMapping("/homeAuthWay")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeAuthWay(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ApplyUserDto applyUserDto = new ApplyUserDto();
        applyUserDto.setOrgPosition(onLineUser.getOrgPosition());
        Map<String, Object> applyUserCountForCheckStateMap = this.applyUserFeign.applyUserCountForCheckState(applyUserDto);
        return new ResultBean<Map<String,Object>>(ResultEnum.SUCCESS,applyUserCountForCheckStateMap);
    }

    /**
     * 功能描述:用户类型
     * @return ResultBean<Map<String,Object>>
     *                
     *                     
     */
    @PostMapping("/homeUserType")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeUserType(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        UserDto userDto = new UserDto();
        userDto.setOrgPosition(onLineUser.getOrgPosition());
        //获取业主,家庭成员人数
        Map<String, Object> userCountForTypeMap = this.userFeign.userCountForType(userDto);
        if(userCountForTypeMap !=null){
            //获取服务人员人数
            ServerUserDto serverUserDto=new ServerUserDto();
            serverUserDto.setOrgPosition(onLineUser.getOrgPosition());
            int serverUserCount = this.serverUserFeign.getServerUserCount(serverUserDto);
            userCountForTypeMap.put("serverUserCount",serverUserCount);
        }

        return new ResultBean<Map<String,Object>>(ResultEnum.SUCCESS,userCountForTypeMap);
    }

    /**
     * 功能描述:移动端用户
     * @return Map<String,Object>
     *                
     *                     
     */
    @PostMapping("/homeMobileUser")
    @Security(token = StatisticsSecurityToken.STATISTICS_HOME_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> homeMobileUser(){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        MemberDto dto = new MemberDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        return this.memberFeign.getMemberTypeCount(dto);
    }

    /**--------------------------------------------物业数据驾驶舱----------------------------------------------------**/

    /**
     * 功能描述:物业数据驾驶舱
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping("/estateDataCockpit")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE)
    public ModelAndView estateDataCockpit(){
        ModelAndView modelAndView = new ModelAndView("statistics/estate-data-cockpit");
        return modelAndView;
    }

    /**
     * 功能描述:物业数据驾驶舱数量统计
     * @return ResultBean<Map<String,Object>>
     *                
     *                      
     */
    @PostMapping("/estateDataCockpitCount")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> estateDataCockpitCount(){
        Map<String,Object> resultMap=new HashMap<>(6);
        Account onLineUser = securityServiceInterface.getOnLineUser();
        //小区数量
        ResidentialDto residentialDto = new ResidentialDto();
        residentialDto.setOrgPosition(onLineUser.getOrgPosition());
        Integer residentialCount = this.residentialFeign.getResidentialCount(residentialDto);
        resultMap.put("residentialCount",residentialCount);
        //单元数量
        UnitDto unitDto=new UnitDto();
        unitDto.setOrgPosition(onLineUser.getOrgPosition());
        int unitCount =this.unitFeign.getUnitCount(unitDto);
        resultMap.put("unitCount",unitCount);
        //住户数量
        UserDto userDto=new UserDto();
        userDto.setOrgPosition(onLineUser.getOrgPosition());
        int userCount=this.userFeign.getUserCount(userDto);
        resultMap.put("userCount",userCount);
        //待审核住户
        ApplyUserDto applyUserDto=new ApplyUserDto();
        applyUserDto.setCheckState(OperationConstant.ZERO);
        applyUserDto.setOrgPosition(onLineUser.getOrgPosition());
        Long applyUserCount = applyUserFeign.getApplyUserCount(applyUserDto);
        resultMap.put("uncheckApplyUserCount",applyUserCount);
        //访客数量
        GuestAuthorizeDto guestAuthorizeDto=new GuestAuthorizeDto();
        guestAuthorizeDto.setOrgPosition(onLineUser.getOrgPosition());
        long guestCount = this.guestAuthorizeFeign.getGuestCount(guestAuthorizeDto);
        resultMap.put("guestCount",guestCount);
        //服务人员
        ServerUserDto serverUserDto=new ServerUserDto();
        serverUserDto.setOrgPosition(onLineUser.getOrgPosition());
        int serverUserCount = this.serverUserFeign.getServerUserCount(serverUserDto);
        resultMap.put("serverUserCount",serverUserCount);
        //设备数量
        DeviceAuthorizeDto deviceAuthorizeDto = new DeviceAuthorizeDto();
        deviceAuthorizeDto.setOrgPosition(onLineUser.getOrgPosition());
        long deviceCount = this.deviceAuthorizeFeign.getDeviceCount(deviceAuthorizeDto);
        resultMap.put("deviceCount",deviceCount);
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:开门方式
     * @param selectDateType 查询时间类型   0 按天 1 按周 2 按月 3 按年
     * @return  ResultBean<Map<String,Object>>
     *                
     *                     
     */
    @PostMapping("/estateOpenWay")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> estateOpenWay(Integer selectDateType){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        //获取统计数量
        AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
        accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        //查询时间
        accessRecordsDto.setSelectDate(new Date());
        //查询类型
        accessRecordsDto.setSelectDateType(selectDateType);
        Map<String, Object> resultMap = this.accessRecordsFeign.accessCountForOpenWay(accessRecordsDto);
        long passWordOpenCount=0;
        long telnetOpenCount=0;
        long faceOpenCount=0;
        long cardOpenCount=0;
        long openDoorCount=0;
        if(resultMap!=null){
            passWordOpenCount = (Integer)resultMap.get("passWordOpenCount");
            telnetOpenCount = (Integer)resultMap.get("telnetOpenCount");
            faceOpenCount = (Integer)resultMap.get("faceOpenCount");
            cardOpenCount = (Integer)resultMap.get("cardOpenCount");
            openDoorCount=passWordOpenCount+telnetOpenCount+faceOpenCount+cardOpenCount;
            resultMap.put("openDoorCount",openDoorCount);
            //当数字大的时候用K表示
            if(passWordOpenCount>=1000){
                resultMap.put("passWordOpenCount", String.format("%.1f",passWordOpenCount/1000.0)+"k");
            }
            if(telnetOpenCount>=1000){
                resultMap.put("telnetOpenCount", String.format("%.1f",telnetOpenCount/1000.0)+"k");
            }
            if(faceOpenCount>=1000){
                resultMap.put("faceOpenCount", String.format("%.1f",faceOpenCount/1000.0)+"k");
            }
            if(cardOpenCount>=1000){
                resultMap.put("cardOpenCount", String.format("%.1f",cardOpenCount/1000.0)+"k");
            }
            if(openDoorCount>=1000){
                resultMap.put("openDoorCount", String.format("%.1f",openDoorCount/1000.0)+"k");
            }
        }
        return  new ResultBean<Map<String,Object>>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:开门数据统计图
     * @param groupType 数据分组类型  0 按时  1按日 2按周  3按月
     * @return ResultBean<List<Map<String, Object>>>
     *                
     *                      
     */
    @PostMapping("/estateOpenCountLineChart")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> estateOpenCountLineChart(Integer groupType){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
        accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        accessRecordsDto.setGroupType(groupType);
        accessRecordsDto.setSelectDate(new Date());
        List<Map<String, Object>> accessCountList = this.accessRecordsFeign.accessCountListForGroupType(accessRecordsDto);
        return  new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,accessCountList);
    }

    /**
     * 功能描述:查询所有设备开门次数
     * @param selectDateType 查询时间类型   0 按天 1 按周 2 按月 3 按年
     * @return ResultBean<List<Map<String, Object>>>
     *                
     *                      
     */
    @PostMapping("/getDeviceOpenCountList")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> getDeviceOpenCountList(Integer selectDateType){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
        accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        accessRecordsDto.setSelectDateType(selectDateType);
        accessRecordsDto.setSelectDate(new Date());
        //查询前20条数据
        Page page=new Page(0,19);
        accessRecordsDto.setPage(page);
        //查询所有设备开门次数
        ResultBean<Page<Map<String, Object>>>resultBean = this.accessRecordsFeign.getDeviceOpenCountList(accessRecordsDto);
        Page<Map<String, Object>> pageResult = resultBean.getData();
        List<Map<String, Object>> records = (pageResult!=null?pageResult.getRecords():null);
        return  new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,records);
    }

    /**
     * 功能描述:获取最新开门记录
     * @param beginDate 查询时间
     * @return ResultBean<List<AccessRecords>>
     *                
     *                      
     */
    @PostMapping("/getNewAccessRecordsList")
    @Security(token = StatisticsSecurityToken.STATISTICS_ESTATE_SKY_EYE_MONITORING_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<List<AccessRecords>> getNewAccessRecordsList(String beginDate,Long residentialId){
        //用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
        accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        accessRecordsDto.setResidentialId(residentialId);
        try {
            //查询最新时间条件
            if(StringUtils.isNotBlank(beginDate)){
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date parse = simpleDateFormat.parse(beginDate);
                //加1秒处理，查询到等于当前时间的数据问题
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(parse);
                calendar.add(Calendar.SECOND,1);
                parse= calendar.getTime();
                accessRecordsDto.setBeginDate(parse);
            }
            Page page=new Page(0,20);
            accessRecordsDto.setPage(page);
        } catch (ParseException e) {
           log.error(e.getMessage(),e);
        }
        ResultBean<Page<AccessRecords>> pageResultBean = this.accessRecordsFeign.showAccessRecordsList(accessRecordsDto);
        Page<AccessRecords> pageResult = pageResultBean.getData();
        List<AccessRecords> records = (pageResult!=null?pageResult.getRecords():null);
        if(records!=null&&!records.isEmpty()){
            for(AccessRecords accessRecords:records){
                //小区
                if(accessRecords.getResidentialId()!=null) {
                    Residential residential = residentialFeign.findResidentialById(accessRecords.getResidentialId());
                    accessRecords.setResidentialName(residential!=null?residential.getName():"");
                }
                //单元
                if(accessRecords.getUnitId()!=null){
                    Unit unit = unitFeign.findUnitById(accessRecords.getUnitId());
                    accessRecords.setUnitName(unit!=null?unit.getName():"");
                }

            }
        }
        return  new ResultBean<List<AccessRecords>>(ResultEnum.SUCCESS,records);
    }
    /*--------------------------------------------------------内部方法----------------------------------------------------------------------*/
    /*--------------------------------------------------------------------------------------------------------------------------------------*/
}
