package com.sptg.gateway.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.sptg.estate.api.entity.ApplyUser;
import com.sptg.estate.api.entity.Residential;
import com.sptg.permissions.api.dto.OrgDto;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
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 com.baomidou.mybatisplus.plugins.Page;
import com.sptg.ad.api.dto.AdTypeDto;
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.constant.SysLogConstant;
import com.sptg.common.core.util.DateUtil;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.dto.AccessRecordsDto;
import com.sptg.estate.api.dto.ApplyUserDto;
import com.sptg.estate.api.dto.GuestAuthorizeDto;
import com.sptg.estate.api.dto.MemberDto;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.dto.UnitDto;
import com.sptg.estate.api.dto.UserDto;
import com.sptg.estate.api.feigns.AccessRecordsFeign;
import com.sptg.estate.api.feigns.ApplyUserFeign;
import com.sptg.estate.api.feigns.GuestAuthorizeFeign;
import com.sptg.estate.api.feigns.MemberFeign;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.estate.api.feigns.UnitFeign;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.entity.Role;
import com.sptg.permissions.api.feigns.AccountFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.permissions.api.feigns.RoleFeign;
import com.sptg.statistics.api.dto.AppMemberLoginRecordsDto;
import com.sptg.statistics.api.dto.StatisticsAdPushDto;
import com.sptg.statistics.api.dto.StatisticsAdWatchDto;
import com.sptg.statistics.api.dto.SysDeviceLogDto;
import com.sptg.statistics.api.dto.SysOperationLogDto;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.AppMemberLoginRecordsFeign;
import com.sptg.statistics.api.feigns.StatisticsAdPushFeign;
import com.sptg.statistics.api.feigns.StatisticsAdWatchFeign;
import com.sptg.statistics.api.feigns.StatisticsCountFeign;
import com.sptg.statistics.api.feigns.SysDeviceLogFeign;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * title: HomeController
 * package: com.sptg.gateway.controller
 * description: 首页 控制器
 *
 *
 *
 */
@RestController
@RequestMapping(value = "/home")
@RequiredArgsConstructor
@Slf4j
public class HomeController {

    private final ConfFeign confFeign;
    private final RoleFeign roleFeign;
    private final AccountFeign accountFeign;
    private final DeviceAuthorizeFeign deviceAuthorizeFeign;
    private final ResidentialFeign residentialFeign;
    private final UnitFeign unitFeign;
    private final UserFeign userFeign;
    private final StatisticsAdPushFeign statisticsAdPushFeign;
    private final MemberFeign memberFeign;
    private final SysDeviceLogFeign sysDeviceLogFeign;
    private final StatisticsAdWatchFeign statisticsAdWatchFeign;
    private final AdTypeFeign adTypeFeign;
    private final StatisticsCountFeign statisticsCountFeign;
    private final GuestAuthorizeFeign guestAuthorizeFeign;
    private final AccessRecordsFeign accessRecordsFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final ApplyUserFeign applyUserFeign;
    private final AppMemberLoginRecordsFeign appMemberLoginRecordsFeign;
    private final OrgFeign orgFeign;

    /**
     * 功能描述:首页初始化
     * @param request
     * @return ModelAndView
     *
     *
     */
    @Security
    @GetMapping("/index")
    public ModelAndView index(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        Organization org = this.orgFeign.getOrgById(onLineUser.getOrgId());
        ModelAndView modelAndView;
       if(OperationConstant.TWO == org.getOrgType()){
            modelAndView = new ModelAndView("home/ad-home");
        }else if(OperationConstant.THREE == org.getOrgType()){
            modelAndView = new ModelAndView("home/estate-home");
        }else{
            modelAndView = new ModelAndView("home/home");
        }
        //  获取此账号的角色列表
        List<Role> roleList = this.roleFeign.getListByAccountId(onLineUser.getId());
        onLineUser.setRoleList(roleList);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:下属账户数量
     * @param request
     * @return ResultBean<Integer>
     *
     *
     */
    @GetMapping("/getSubordinateAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Integer> getSubordinateAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        return this.accountFeign.selectCountByPosition(onLineUser.getOrgPosition());
    }

    /**
     * 功能描述:设备区域覆盖数量
     * @param request
     * @return ResultBean<Map<String,Long>>
     *
     *
     */
    @GetMapping("/getDeviceCoverAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Long>> getDeviceCoverAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        DeviceAuthorizeDto deviceAuthorizeDto = new DeviceAuthorizeDto();
        deviceAuthorizeDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.deviceAuthorizeFeign.getDeviceCoverAccount(deviceAuthorizeDto);
    }

    /**
     * 功能描述:小区总数量/本月新增数量
     * @param request
     * @return ResultBean<Map<String,Integer>>
     *
     *
     */
    @GetMapping("/getResidentialAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Integer>> getResidentialAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        ResidentialDto residentialDto = new ResidentialDto();
        if (userOrg.getOrgType() == OperationConstant.THREE){//物业
            residentialDto.setEstatePosition(onLineUser.getOrgPosition());
        }else{//机构
            residentialDto.setOrgPosition(onLineUser.getOrgPosition());
        }
//        residentialDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.residentialFeign.getResidentialAccount(residentialDto);
    }

    /**
     * 功能描述:单元总数量/本月新增数量
     * @param request
     * @return ResultBean<Map<String,Integer>>
     *
     *
     */
    @GetMapping("/getUnitAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Integer>> getUnitAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        UnitDto unitDto = new UnitDto();
        unitDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.unitFeign.getUnitAccount(unitDto);
    }

    /**
     * 功能描述:住户总数量/本月新增数量
     * @param request
     * @return ResultBean<Map<String,Integer>>
     *
     *
     */
    @GetMapping("/getUserAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Integer>> getUserAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        UserDto userDto = new UserDto();
        userDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.userFeign.getUserAccount(userDto);
    }
    /**
     * 功能描述:待审核的住户认证记录数量
     * @param request
     * @return ResultBean<Map<String,Integer>>
     *
     *
     */
    @GetMapping("/getApplyUserAccountWithCheckState")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Integer>> getApplyUserAccountWithCheckState(HttpServletRequest request, @Param(value = "checkState") Integer checkState){
        HashMap<String, Integer> map = new HashMap();
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        ApplyUserDto dto = new ApplyUserDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setCheckState(checkState);
        List<ApplyUser> applyUserList = this.applyUserFeign.getApplyUserByCondition(dto);
        if (applyUserList != null && applyUserList.size() > 0){
            map.put("applyUserReadyCheckCount", applyUserList.size());
        }else{
            map.put("applyUserReadyCheckCount", 0);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,map);
    }

    /**
     * 功能描述:广告总投放/本月新增投放
     * @param request
     * @return ResultBean<Map<String,Long>>
     *
     *
     */
    @GetMapping("/getAdPushAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Long>> getAdPushAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        StatisticsAdPushDto statisticsAdPushDto = new StatisticsAdPushDto();
        statisticsAdPushDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.statisticsAdPushFeign.getAdPushAccount(statisticsAdPushDto);
    }

    /**
     * 功能描述:会员总数量/本月新增数量（小程序及APP）
     * @param request
     * @return ResultBean<Map<String,Integer>>
     *
     *
     */
    @GetMapping("/getMemberAccount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Integer>> getMemberAccount(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        MemberDto memberDto = new MemberDto();
        memberDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.memberFeign.getMemberAccount(memberDto);
    }

    /**
     * 功能描述:设备故障表格数量(含查询)
     * @param {
     *     request
     *     sysDeviceLogDto
     * }
     * @return ResultBean<List<Map<String,Object>>>
     *
     *
     */
    @PostMapping("/getDeviceFaultCountList")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> getDeviceFaultCountList(HttpServletRequest request, SysDeviceLogDto sysDeviceLogDto){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        sysDeviceLogDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.sysDeviceLogFeign.getDeviceFaultCountList(sysDeviceLogDto);
    }

    /**
     * 功能描述:广告观看次数表格数量(含查询)
     * @param {
     *     request
     *     statisticsAdWatchDto
     * }
     * @return ResultBean<List<Map<String,Object>>>
     *
     *
     */
    @PostMapping("/getAdWatchCountList")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> getAdWatchCountList(HttpServletRequest request, StatisticsAdWatchDto statisticsAdWatchDto){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        statisticsAdWatchDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.statisticsAdWatchFeign.getAdWatchCountList(statisticsAdWatchDto);
    }

    /**
     * 功能描述:设备故障省份排名
     * @param request
     * @return ResultBean<List<Map<String,Object>>>
     *
     *
     */
    @PostMapping("/deviceRankForArea")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> deviceRankForArea(HttpServletRequest request, SysDeviceLogDto sysDeviceLogDto){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        sysDeviceLogDto.setOrgPosition(onLineUser.getOrgPosition());
        Page page=new Page<>();
        page.setSize(8);
        sysDeviceLogDto.setPage(page);
        return this.sysDeviceLogFeign.deviceRankForArea(sysDeviceLogDto);
    }

    /**
     * 功能描述:广告观看省份排名
     * @param request
     * @return ResultBean<List<Map<String, Object>>
     *
     *
     */
    @PostMapping("/adWatchRankForArea")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String, Object>>> adWatchRankForArea(StatisticsAdWatchDto dto,HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页
        Page page=new Page<>();
        page.setSize(8);
        dto.setPage(page);
        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchRankForArea(dto);
        return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,maps);
    }

    /**
     * 功能描述:用户活跃
     * @param appMemberLoginRecordsDto
     * @return ResultBean<List<Map<String,Object>>>
     *
     *
     */
    @PostMapping("/getMemberLoginCountList")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<List<Map<String,Object>>> getMemberLoginCountList(HttpServletRequest request, AppMemberLoginRecordsDto appMemberLoginRecordsDto){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        appMemberLoginRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.appMemberLoginRecordsFeign.getMemberLoginCountList(appMemberLoginRecordsDto);
    }

    /**
     * 功能描述:小区统计
     * @param ResidentialDto
     * @return ResultBean<List<Residential>>
     *
     *
     */
    @PostMapping("/getResidentialData")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String, Object>> getResidentialData(HttpServletRequest request, ResidentialDto residentialDto){
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> orgMap = new HashMap<>();       //机构小区数据
        List<Object> orgTitle = new ArrayList<>();
        List<Object> orgMonthAccount = new ArrayList<>();
        List<Object> orgTotalMonthAccount = new ArrayList<>();

        Map<String, Object> estMap = new HashMap<>();
        List<Object> estTitle = new ArrayList<>();          //物业小区数据
        List<Object> estMonthAccount = new ArrayList<>();
        List<Object> estTotalMonthAccount = new ArrayList<>();

        OrgDto orgDto = new OrgDto();
        //常运智慧集团position为"0/1/"
        orgDto.setPosition("0/1/");
        //只查机构，去掉小区
        orgDto.setFlag(1);
        List<Organization> orgList = this.orgFeign.selectListByCondition(orgDto);
        orgList.forEach(org -> {
            ResidentialDto rdto = new ResidentialDto();
            if(org.getOrgType() == OperationConstant.ONE){//机构
                rdto.setOrgPosition(org.getPosition());
                ResultBean<Map<String, Integer>> residentialAccount = this.residentialFeign.getResidentialAccount(rdto);
                Map<String, Integer> data = residentialAccount.getData();
                String name = org.getName();
                orgTitle.add(name);
                orgMonthAccount.add(data.get("monthCount"));
                orgTotalMonthAccount.add(data.get("totalCount"));
            }else if(org.getOrgType() == OperationConstant.THREE){//物业
                rdto.setEstatePosition(org.getPosition());
                ResultBean<Map<String, Integer>> residentialAccount = this.residentialFeign.getResidentialAccount(rdto);
                Map<String, Integer> data = residentialAccount.getData();
                String name = org.getName();
                estTitle.add(name);
                estMonthAccount.add(data.get("monthCount"));
                estTotalMonthAccount.add(data.get("totalCount"));
            }
        });
        orgMap.put("title", orgTitle);
        orgMap.put("monthCount", orgMonthAccount);
        orgMap.put("totalCount", orgTotalMonthAccount);
        estMap.put("title", estTitle);
        estMap.put("monthCount", estMonthAccount);
        estMap.put("totalCount", estTotalMonthAccount);
        map.put("orgData", orgMap);
        map.put("estData", estMap);
        return new ResultBean<>(ResultEnum.SUCCESS, map);
    }

    /**
     * 功能描述:开门方式
     * @return  ResultBean<Map<String,Object>>
     *
     *
     */
    @PostMapping("/openWay")
    public ResultBean<Map<String,Long>> openWay(HttpServletRequest request){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        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){
            //刷卡开门
             cardOpenCount = statisticsCount.getAccessRecordCardCount();
            //远程开门
            telnetOpenCount = statisticsCount.getAccessRecordTelnetCount();
            //人脸识别
            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<>(ResultEnum.SUCCESS,openWayMap);
    }

    /**
     * 功能描述:观看类别占比
     * @param dto
     * @param request
     * @return ResultBean<Map<String, Object>>
     *
     *
     */
    @PostMapping("/adWatchProportionForAdType")
    public   ResultBean<Map<String, Object>> adWatchProportionForAdType(StatisticsAdWatchDto dto,HttpServletRequest request) {
        Map<String, Object> resultMap=new HashMap<>(OperationConstant.TWO);
        //登录用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页
        Page page=new Page<>();
        //大小10条记录
        page.setSize(6);
        dto.setPage(page);
        //统计观看类别
        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchRankForAdType(dto);
        //获取统计数量
        ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(onLineUser.getOrgPosition());
        StatisticsCount statisticsCount=statisticsCountResultBean.getData();
        if(statisticsCount!=null){
            //总观看数量
            long adWatchCountTotal = statisticsCount.getAdWatchCount();
            if(adWatchCountTotal>0){
                long currentAdWatchCountTotal=0L;
                if(null!=maps) {
                    for(Map<String, Object> map:maps) {
                        int adWatchCount = (int)map.get("adWatchCount");
                        currentAdWatchCountTotal+=adWatchCount;
                    }
                }
                //其他类别观看数量
                long otherAdWatchCount=adWatchCountTotal-currentAdWatchCountTotal;
                if(otherAdWatchCount>0){
                    Map<String,Object> otherMap=new HashMap<String,Object>(OperationConstant.TWO);
                    otherMap.put("adTypeName", "其他");
                    otherMap.put("adWatchCount", otherAdWatchCount);
                    maps.add(otherMap);
                }

            }
        }
        resultMap.put("adWatchCountList",maps);
        return  new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:会员占比（根据会员类型）
     * @return ResultBean<Map<String,Object>>
     *
     *
     */
    @PostMapping("/getMemberTypeCount")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Map<String,Object>> getMemberTypeCount(HttpServletRequest request,MemberDto memberDto){
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        memberDto.setOrgPosition(onLineUser.getOrgPosition());
        return this.memberFeign.getMemberTypeCount(memberDto);
    }
    /**
     * 功能描述:广告首页数据概览
     * @param request
     * @return ResultBean<Map<String,Object>>
     *
     *
     */
    @PostMapping("/getAdHomeDataCount")
    public ResultBean<Map<String,Object>> getAdHomeDataCount(HttpServletRequest request){
        Map<String,Object> resultMap=new HashMap<String,Object>(6);
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        AdTypeDto adTypeDto=new AdTypeDto();
        adTypeDto.setRootOrgId(onLineUser.getRootOrgId());
        //广告类别
        Integer adTypeCount = this.adTypeFeign.getAdTypeCount(adTypeDto);
        resultMap.put("adTypeCount",adTypeCount==null?0:adTypeCount);
        //本月广告类别
        adTypeDto.setSelectMonth(new Date());
        Integer currentMonthAdTypeCount = this.adTypeFeign.getAdTypeCount(adTypeDto);
        resultMap.put("currentMonthAdTypeCount",currentMonthAdTypeCount==null?0:currentMonthAdTypeCount);

        //获取统计数量
        ResultBean<StatisticsCount> statisticsCountResultBean = statisticsCountFeign.selectCountOne(onLineUser.getOrgPosition());
        StatisticsCount statisticsCount=statisticsCountResultBean.getData();
        long adPushCount=0L;
        Long currentMonthAdPushCount=0L;
        long adWatchCount=0L;
        long currentMonthAdWatchCount=0L;
        if(statisticsCount!=null){
            //总投放
            adPushCount = statisticsCount.getAdExposureCount();
            //本月投放
            StatisticsAdPushDto statisticsAdPushDto=new StatisticsAdPushDto();
            statisticsAdPushDto.setIsAllType(OperationConstant.TWO);
            statisticsAdPushDto.setOrgPosition(onLineUser.getOrgPosition());
            Long currentMonthAdPushCountL = statisticsAdPushFeign.statisticsAdPushCount(statisticsAdPushDto);
            currentMonthAdPushCount=currentMonthAdPushCountL==null?0L:currentMonthAdPushCountL;
            //总观看
            adWatchCount = statisticsCount.getAdWatchCount();
            //本月观看
            StatisticsAdWatchDto statisticsAdWatchDto=new StatisticsAdWatchDto();
            statisticsAdWatchDto.setSelectMonth(new Date());
            statisticsAdWatchDto.setOrgPosition(onLineUser.getOrgPosition());
            Long currentMonthAdWatchCountL = statisticsAdWatchFeign.getAdWatchCount(statisticsAdWatchDto);
            currentMonthAdWatchCount=currentMonthAdWatchCountL==null?0L:currentMonthAdWatchCountL;
        }
        resultMap.put("adPushCount",adPushCount);
        resultMap.put("currentMonthAdPushCount",currentMonthAdPushCount);
        resultMap.put("adWatchCount",adWatchCount);
        resultMap.put("currentMonthAdWatchCount",currentMonthAdWatchCount);
        return new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:观看排行
     * @param dto
     * @param request
     * @return ResultBean<List<Map<String, Object>>>
     *
     *
     */
    @PostMapping("/adWatchRankForAdType")
    public  ResultBean<List<Map<String, Object>>> adWatchRankForAdType(StatisticsAdWatchDto dto,HttpServletRequest request) {
        //登录用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //分页
        Page page=new Page<>();
        //大小15条记录
        page.setSize(7);
        dto.setPage(page);
        //统计观看类别
        List<Map<String, Object>> maps = statisticsAdWatchFeign.statisticsAdWatchRankForAdType(dto);
        return  new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,maps);
    }

    /**
     * 功能描述:访客数量
     * @param request
     * @return ResultBean<Map<String, Object>>
     *
     *
     */
    @PostMapping("/getGuestCount")
    public  ResultBean<Map<String, Object>> getGuestCount(HttpServletRequest request) {
        Map<String, Object> resultMap=new HashMap<>(OperationConstant.TWO);
        //登录用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        GuestAuthorizeDto dto=new GuestAuthorizeDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //访客数量
        long guestCount = guestAuthorizeFeign.getGuestCount(dto);
        resultMap.put("guestCount",guestCount);
        dto.setSelectMonth(new Date());
        //当月访客数量
        long currentMonthGuestCount = guestAuthorizeFeign.getGuestCount(dto);
        resultMap.put("currentMonthGuestCount",currentMonthGuestCount);
        return new ResultBean<Map<String, Object>>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:开门次数
     * @param request
     * @return ResultBean<Map<String, Object>>
     *
     *
     */
    @PostMapping("/getAccessRecordsCount")
    public  ResultBean<Map<String, Object>> getAccessRecordsCount(HttpServletRequest request) {
        Map<String, Object> resultMap=new HashMap<>(OperationConstant.TWO);
        //登录用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        AccessRecordsDto dto=new AccessRecordsDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //开门次数
        long accessRecordsCount = accessRecordsFeign.getAccessRecordsCount(dto);
        resultMap.put("accessRecordsCount",accessRecordsCount);
        dto.setSelectMonth(new Date());
        //当月开门次数
        long currentMonthAccessRecordsCount = accessRecordsFeign.getAccessRecordsCount(dto);
        resultMap.put("currentMonthAccessRecordsCount",currentMonthAccessRecordsCount);
        return new ResultBean<Map<String, Object>>(ResultEnum.SUCCESS,resultMap);
    }

    /**
     * 功能描述:工作动态
     * @param request
     * @return ResultBean<List<SysOperationLog>>
     *
     *
     */
    @PostMapping("/workState")
    public ResultBean<List<SysOperationLog>> workState(HttpServletRequest request){
        //用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        SysOperationLogDto solDto=new SysOperationLogDto();
        solDto.setOrgPosition(onLineUser.getOrgPosition());
        Page page=new Page<>();
        page.setSize(7);
        solDto.setPage(page);
        //只查增删改事件
        solDto.setSelectEvents(OperationConstant.TWO);
        ResultBean<Page<SysOperationLog>> resultBean = this.sysOperationLogFeign.showSysOperationLogList(solDto);
        Page<SysOperationLog> pageData = resultBean.getData();
        if(pageData!=null){
            List<SysOperationLog> sysOperationLogList = pageData.getRecords();
            if(sysOperationLogList!=null){
                sysOperationLogList.forEach(sysOperationLog->{
                    Long operator = sysOperationLog.getOperator();
                    if(operator!=null){
                        Account account = accountFeign.getById(operator);
                        sysOperationLog.setOperatorAccountName(account.getUserName());
                        if(sysOperationLog.getOperationDate()!=null){
                            String beforeDateMsg= DateUtil.calculateTime(sysOperationLog.getOperationDate());
                            sysOperationLog.setBeforeDateMsg(beforeDateMsg);
                            sysOperationLog.setUserPortraitUri(this.confFeign.getFileServerHost()+account.getUserPortraitUri());
                        }
                    }
                    //如果为空，系统自动填充
                    String  message=sysOperationLog.getMessage();
                    if(StringUtils.isBlank(message)){
                        //事件
                        int event=sysOperationLog.getOperationEvent();
                        String tableId = sysOperationLog.getTableId();
                        //是否批量
                        boolean batchOperation=false;
                        if(StringUtils.isNotBlank(tableId)){
                            batchOperation=tableId.contains(",")?true:false;
                        }

                        if(event== SysLogConstant.ADD_EVENT){
                            message="新增一条记录";
                        }else if(event== SysLogConstant.DELETE_EVENT){
                            if(batchOperation){
                                message ="批量删除记录";
                            }else{
                                message ="删除一条记录";
                            }
                        }else if(event== SysLogConstant.ALTER_EVENT){
                            if(batchOperation) {
                                message = "批量修改记录";
                            }else{
                                message = "修改一条记录";
                            }
                        }
                    sysOperationLog.setMessage(message);
                    }
                });
            }
            return new ResultBean<List<SysOperationLog>>(ResultEnum.SUCCESS,sysOperationLogList);
        }
        return  new ResultBean<List<SysOperationLog>>(ResultEnum.SUCCESS);
    }

    /**
     * 功能描述:认证用户数量
     * @param request
     * @return Long
     *
     *
     */
    @PostMapping("/getApplyUserCount")
    public Long getApplyUserCount(HttpServletRequest request) {
        //用户
        Account onLineUser = (Account)request.getAttribute("onLineUser");
        //查询待审核认证用户
        ApplyUserDto applyUserDto=new ApplyUserDto();
        applyUserDto.setCheckState(0);
        applyUserDto.setOrgPosition(onLineUser.getOrgPosition());
        Long applyUserCount = applyUserFeign.getApplyUserCount(applyUserDto);
        return applyUserCount;
    }

    /**
     * 功能描述:开门趋势折线图
     * @param request
     * @param selectDateType  0今日(默认)  1本周 2本月 3全年 4选年 5选月 6选日
     * @return List<Map<String, Object>>
     *
     *
     */
    @PostMapping("/estateOpenCountLineChart")
    public ResultBean<List<Map<String, Object>>> estateOpenCountLineChart(HttpServletRequest request,int selectDateType,String selectDate){
        try {
            //用户
            Account onLineUser = (Account)request.getAttribute("onLineUser");
            //设备开门次数
            AccessRecordsDto accessRecordsDto=new AccessRecordsDto();
            if(selectDateType==6){
                //按小时分组统计
                accessRecordsDto.setGroupType(OperationConstant.ZERO);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                accessRecordsDto.setSelectDate(simpleDateFormat.parse(selectDate));
            }else if(selectDateType==OperationConstant.FIVE){
                //按日分组统计
                accessRecordsDto.setGroupType(OperationConstant.ONE);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
                accessRecordsDto.setSelectDate(simpleDateFormat.parse(selectDate));
            }else if(selectDateType==OperationConstant.FOUR){
                //按月份分组统计
                accessRecordsDto.setGroupType(OperationConstant.THREE);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
                accessRecordsDto.setSelectDate(simpleDateFormat.parse(selectDate));
            }else if(selectDateType==OperationConstant.THREE){
                //按月份分组统计
                accessRecordsDto.setGroupType(OperationConstant.THREE);
                accessRecordsDto.setSelectDate(new Date());
            }else if(selectDateType==OperationConstant.TWO){
                //按日分组统计
                accessRecordsDto.setGroupType(OperationConstant.ONE);
                accessRecordsDto.setSelectDate(new Date());
            }else if(selectDateType==OperationConstant.ONE){
                //按星期分组统计
                accessRecordsDto.setGroupType(OperationConstant.TWO);
                accessRecordsDto.setSelectDate(new Date());
            }else{
                //按小时分组统计
                accessRecordsDto.setGroupType(OperationConstant.ZERO);
                accessRecordsDto.setSelectDate(new Date());
            }
            accessRecordsDto.setOrgPosition(onLineUser.getOrgPosition());
            List<Map<String, Object>> accessCountList = this.accessRecordsFeign.accessCountListForGroupType(accessRecordsDto);
            return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,accessCountList);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
            return  new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
        }
    }

    /**
     * 功能描述:访客趋势折线图
     * @param request
     * @param selectDateType  0今日(默认)  1本周 2本月 3全年 4选年 5选月 6选日
     * @return List<Map<String, Object>>
     *
     *
     */
    @PostMapping("/estateGuestCountLineChart")
    public ResultBean<List<Map<String, Object>>> estateGuestCountLineChart(HttpServletRequest request,int selectDateType,String selectDate){
        try {
            //用户
            Account onLineUser = (Account)request.getAttribute("onLineUser");
            //统计访客人数
            GuestAuthorizeDto dto=new GuestAuthorizeDto();
            //6选日时间
            if(selectDateType==6){
                //按小时分组统计
                dto.setGroupType(OperationConstant.ZERO);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                dto.setCreateDate(simpleDateFormat.parse(selectDate));
                //5选月
            }else if(selectDateType==OperationConstant.FIVE){
                //按日分组统计
                dto.setGroupType(OperationConstant.ONE);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
                dto.setCreateDate(simpleDateFormat.parse(selectDate));
                //5选年
            }else if(selectDateType==OperationConstant.FOUR){
                //按月份分组统计
                dto.setGroupType(OperationConstant.THREE);
                if(StringUtils.isBlank(selectDate)){
                    new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
                dto.setCreateDate(simpleDateFormat.parse(selectDate));
            }else if(selectDateType==OperationConstant.THREE){
                //按月份分组统计
                dto.setGroupType(OperationConstant.THREE);
                dto.setCreateDate(new Date());
            }else if(selectDateType==OperationConstant.TWO){
                //按日分组统计
                dto.setGroupType(OperationConstant.ONE);
                dto.setCreateDate(new Date());
            }else if(selectDateType==OperationConstant.ONE){
                //按星期分组统计
                dto.setGroupType(OperationConstant.TWO);
                dto.setCreateDate(new Date());
                dto.setSelectEndDate(new Date());
            }else{
                //按小时分组统计
                dto.setGroupType(OperationConstant.ZERO);
                dto.setCreateDate(new Date());
            }
            dto.setOrgPosition(onLineUser.getOrgPosition());
            List<Map<String, Object>> guestAuthorizeCountList = this.guestAuthorizeFeign.guestAuthorizeCountListForGroupType(dto);
            return new ResultBean<List<Map<String, Object>>>(ResultEnum.SUCCESS,guestAuthorizeCountList);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
            return  new ResultBean<List<Map<String, Object>>>(ResultEnum.ERROR);
        }
    }

    public static void main(String[] args){
        char [] pardStore = new char[62];
        for (int i = 0; i < 26; i++) {
            pardStore[i]=(char)('A'+i);
        }
        for (int i = 26; i < 52; i++) {
            pardStore[i]=(char)('a'+(i-26));
        }
        for (int i = 52; i < 62; i++) {
            pardStore[i]=(char)('0'+(i-52));
        }
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        for(int j = 1; j <= 1011; j++){
            for (int i = 0; i < 12; i++) {
                int n = r.nextInt(62);
//                System.out.print(pardStore[n]);
                sb.append(pardStore[n]);
            }
            System.out.println("原始密码：" + sb.toString() + " >>>>> 加密后：" + DigestUtils.sha256Hex(sb.toString()));
            sb = new StringBuffer();
        }
    }
}
