package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaBuilding;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.entity.TaPersonVisitRecord;
import com.huiju.estateagents.entity.TdWxDict;
import com.huiju.estateagents.excel.IntentionUser;
import com.huiju.estateagents.excel.StatsBuilding;
import com.huiju.estateagents.excel.StatsTimeBuilding;
import com.huiju.estateagents.mapper.*;
import com.huiju.estateagents.po.PersonPO;
import com.huiju.estateagents.service.IStatisticalService;
import com.huiju.estateagents.statistic.entity.TsPersonFromStatistic;
import com.huiju.estateagents.statistic.mapper.TsPersonFromStatisticMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数据统计
 */
@Service
@Slf4j
public class StatisticalServiceImpl implements IStatisticalService {

    @Autowired
    private TaPersonMapper taPersonMapper;

    @Autowired
    private TaBuildingMapper taBuildingMapper;


    @Autowired
    private TaPersonVisitRecordMapper taPersonVisitRecordMapper;

    @Autowired
    private TaBuildingDynamicMapper taBuildingDynamicMapper;

    @Autowired
    private TaNewsMapper taNewsMapper;

    @Autowired
    private TaPersonIntentionRecordMapper taPersonIntentionRecordMapper;

    @Autowired
    private TsPersonFromStatisticMapper tsPersonFromStatisticMapper;

    @Autowired
    private TdWxDictMapper tdWxDictMapper;


    @Override
    public ResponseBean indexStatistical(Integer orgId) {

        ResponseBean responseBean = new ResponseBean();
        Map<String, Object> map = new HashMap<>();

        // 总用户数
        Integer selectUserCount = taPersonMapper.selectUserCount(orgId);

        // 注册数
        Integer selectRegisteredCount = taPersonMapper.selectRegisteredCount(orgId);

        // 最近七天--> 调整为今日新增
        Integer selectRecentlyCount = taPersonMapper.selectTodayRecentlyCount(orgId, LocalDateTime.now());

        // -------  用户来源 end ------------
        //今日访问次数
        Integer selectVisitCount = taPersonVisitRecordMapper.selectTodayVisitCount(orgId, LocalDateTime.now());

        map.put("selectUserCount", selectUserCount);
        map.put("selectRegisteredCount", selectRegisteredCount);
        map.put("selectRecentlyCount", selectRecentlyCount);
        map.put("selectVisitCount", selectVisitCount);

        responseBean.addSuccess(map);

        return responseBean;
    }

    @Override
    public ResponseBean selectActiveUserCount(Integer orgId, String dateType, LocalDate startDate, LocalDate endDate) {
        ResponseBean responseBean = new ResponseBean();

        String type = null;
        if (CommConstant.DAY.equals(dateType)) {
            type = CommConstant.DAY;
        } else if (CommConstant.WEEK.equals(dateType)) {
            type = CommConstant.WEEK;
        } else if (CommConstant.MONTH.equals(dateType)) {
            type = CommConstant.MONTH;
        } else {
            responseBean.addError("无此查询类型！");
        }

        // 用户活跃数
        List<Map<String, Object>> selectActiveUserCount = taPersonMapper.selectActiveUserCount(orgId, CommConstant.PERSON_REALTY_CONSULTANT, type, null, null);
        Map<String, Object> map = new HashMap<>();
        map.put("selectActiveUserCount", selectActiveUserCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectNewsUserCount(Integer orgId, LocalDate startDate, LocalDate endDate) {
        ResponseBean responseBean = new ResponseBean();

        // 新增用户数
        List<Map<String, Object>> selectNewsUserCount = taPersonMapper.selectNewsUserCount(orgId, CommConstant.PERSON_REALTY_CONSULTANT, startDate, endDate);

        Map<String, Object> map = new HashMap<>();
        map.put("selectNewsUserCount", selectNewsUserCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectUserBehavior(Integer pageNum,
                                           Integer pageSize,
                                           Integer orgId,
                                           LocalDate startDate,
                                           LocalDate endDate,
                                           String buildingId,
                                           String eventType,
                                           String event,
                                           String activity) {
        ResponseBean responseBean = new ResponseBean();

        // 用户行为
        List<PersonPO> selectUserBehavior = taPersonMapper.selectUserBehavior(orgId, CommConstant.PERSON_REALTY_CONSULTANT, startDate, endDate, buildingId, eventType, event, activity);

        IPage<TaPersonVisitRecord> page = new Page<>(pageNum, pageSize);
        IPage<TaPersonVisitRecord> visitRecordIPage = taPersonVisitRecordMapper.selectAll(page, startDate, endDate, buildingId, eventType, event, activity, orgId);

        Map<String, Object> map = new HashMap<>();
        map.put("selectUserBehavior", selectUserBehavior);
        map.put("data", visitRecordIPage);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectEventAll(Integer orgId, String event, String personId) {
        ResponseBean responseBean = new ResponseBean();

        List<TaPersonVisitRecord> taPersonVisitRecords = taPersonVisitRecordMapper.selectEventAll(event, personId, orgId);
        responseBean.addSuccess(taPersonVisitRecords);
        return responseBean;
    }

    @Override
    public ResponseBean selectConversion(Integer orgId, String conversion) {
        ResponseBean responseBean = new ResponseBean();
        Map<String, Object> result = new HashMap<>();

        switch (conversion) {
            case CommConstant.BUILDING_SAVE:
                Map<String, Object> selectBuildingStatisticalSave = taBuildingMapper.selectBuildingStatistical(orgId, "save");
                result.put("data_count", selectBuildingStatisticalSave);
                break;
            case CommConstant.BUILDING_SHARE:
                Map<String, Object> selectBuildingStatisticalShare = taBuildingMapper.selectBuildingStatistical(orgId, "share");
                result.put("data_count", selectBuildingStatisticalShare);
                break;
            case CommConstant.ACTIVITY_SAVE:
                Map<String, Object> selectBuildingDynamicStatisticalSave = taBuildingDynamicMapper.selectBuildingDynamicStatistical(orgId, "save");
                result.put("data_count", selectBuildingDynamicStatisticalSave);
                break;
            case CommConstant.ACTIVITY_SHARE:
                Map<String, Object> selectBuildingDynamicStatisticalShare = taBuildingDynamicMapper.selectBuildingDynamicStatistical(orgId, "share");
                result.put("data_count", selectBuildingDynamicStatisticalShare);
                break;
            case CommConstant.NEWS_SAVE:
                Map<String, Object> selectNewsStatisticalSave = taNewsMapper.selectNewsStatistical(orgId, "save");
                result.put("data_count", selectNewsStatisticalSave);
                break;

            case CommConstant.NEWS_SHARE:
                Map<String, Object> selectNewsStatisticalShare = taNewsMapper.selectNewsStatistical(orgId, "share");
                result.put("data_count", selectNewsStatisticalShare);
                break;

            case CommConstant.ACTIVITY_SIGN:
                Map<String, Object> selectBuildingDynamicEnlistStatistical = taBuildingDynamicMapper.selectBuildingDynamicEnlistStatistical(orgId);
                result.put("data_count", selectBuildingDynamicEnlistStatistical);
                break;

            case CommConstant.AUTHORIZATION_PHONE:
                // 总用户数
                Integer selectUserCount = taPersonMapper.selectUserCount(orgId);

                // 注册数
                Integer selectRegisteredCount = taPersonMapper.selectRegisteredCount(orgId);
                Map<String, Object> userMap = new HashMap<>();
                userMap.put("pvNum", selectUserCount);
                userMap.put("registeredCount", selectRegisteredCount);
                result.put("data_count", userMap);
                break;
        }

        responseBean.addSuccess(result);
        return responseBean;
    }


    @Override
    public ResponseBean selectUserResource(Integer orgId, LocalDate startDate, LocalDate endDate) {
        ResponseBean responseBean = new ResponseBean();
        // 用户来源 柱状
//        List<Map<String, Object>> mapList = taPersonMapper.selectUserSourceColumnar(orgId, CommConstant.PERSON_REALTY_CONSULTANT, startDate, endDate);

        // 用户来源饼状
        // 来源置业顾问, 来源客户，其他
        Integer total = taPersonMapper.selectFromNum(orgId, null);
        Integer person_realty_consultant = taPersonMapper.selectFromNum(orgId, CommConstant.PERSON_REALTY_CONSULTANT);
        Integer person_estate_agent = taPersonMapper.selectFromNum(orgId, CommConstant.PERSON_ESTATE_CUSTOMER);
        Integer person_null = total - person_realty_consultant - person_estate_agent;
        Map<String, Object> map = new HashMap<>();
        map.put("person_realty_consultant", person_realty_consultant);
        map.put("person_estate_agent", person_estate_agent);
        map.put("person_null", person_null);

        // 用户来源 数据列表
        List<Map<String, Object>> selectUserSourceData = taPersonMapper.selectUserSourceData(orgId, CommConstant.PERSON_ESTATE_AGENT, startDate, endDate);

        Map<String, Object> result = new HashMap<>();
        result.put("columnar", new ArrayList<Map<String, Object>>());
        result.put("pie", map);
        result.put("data", selectUserSourceData);

        responseBean.addSuccess(result);
        return responseBean;
    }


    @Override
    public ResponseBean selectUserCount(Integer orgId) {
        ResponseBean responseBean = new ResponseBean();
        // 总用户数
        Integer selectUserCount = taPersonMapper.selectUserCount(orgId);
        Map<String, Object> map = new HashMap<>();
        map.put("selectUserCount", selectUserCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectRegisteredCount(Integer orgId) {
        ResponseBean responseBean = new ResponseBean();
        // 注册数
        Integer selectRegisteredCount = taPersonMapper.selectRegisteredCount(orgId);
        Map<String, Object> map = new HashMap<>();
        map.put("selectRegisteredCount", selectRegisteredCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectRecentlyCount(Integer orgId, LocalDate startDate, LocalDate endDate) {
        ResponseBean responseBean = new ResponseBean();
        // 最近七天
        Integer selectRecentlyCount = taPersonMapper.selectRecentlyCount(orgId, CommConstant.PERSON_REALTY_CONSULTANT, startDate, endDate);
        Map<String, Object> map = new HashMap<>();
        map.put("selectRecentlyCount", selectRecentlyCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectSexUser(Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        // 性别比例
        // 1 男 2 女
        Integer selectSexMale = taPersonMapper.selectSexUser(orgId, CommConstant.SEX_MALE);
        Integer selectSexFemale = taPersonMapper.selectSexUser(orgId, CommConstant.SEX_FEMALE);
        Map<String, Object> selectSexMaleMap = new HashMap<>();
        selectSexMaleMap.put("name", "男");
        selectSexMaleMap.put("value", selectSexMale);
        Map<String, Object> selectSexFemaleMap = new HashMap<>();
        selectSexFemaleMap.put("name", "女");
        selectSexFemaleMap.put("value", selectSexFemale);
        List<Map<String, Object>> selectSexUser = new ArrayList<>();
        selectSexUser.add(selectSexMaleMap);
        selectSexUser.add(selectSexFemaleMap);

        Map<String, Object> map = new HashMap<>();
        map.put("selectSexUser", selectSexUser);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectCityUser(Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        // 城市比例
        List<Map<String, Object>> selectCityUser = taPersonMapper.selectCityUser(orgId);
        Map<String, Object> map = new HashMap<>();
        map.put("selectCityUser", selectCityUser);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public ResponseBean selectIntentionUser(Integer pageNum, Integer pageSize, Integer orgId, String buildingId, List<TaPersonBuilding> taPersonBuildingListByUserId) {
        ResponseBean responseBean = new ResponseBean();

        IPage<Map<String, Object>> pg = new Page<>(pageNum, pageSize);
        IPage<Map<String, Object>> page = taPersonIntentionRecordMapper.selectIntentionUser(pg, orgId, buildingId, taPersonBuildingListByUserId);

        responseBean.addSuccess(page);
        return responseBean;
    }

    /**
     * 用户来源数据 首页
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseBean selectPersonFrom(Integer orgId, LocalDate startDate, LocalDate endDate) {
        ResponseBean responseBean = new ResponseBean();
        List<TsPersonFromStatistic> list = tsPersonFromStatisticMapper.selectPersonFrom(orgId, startDate, endDate);
        QueryWrapper<TdWxDict> tdWxDictQueryWrapper = new QueryWrapper<>();
        tdWxDictQueryWrapper.groupBy("scene_type");
        List<TdWxDict> tdWxDicts = tdWxDictMapper.selectList(tdWxDictQueryWrapper);
        tdWxDicts.forEach(e -> {
            int fromSum = list.stream().filter(l -> e.getSceneType().equals(l.getSceneType())).mapToInt(TsPersonFromStatistic::getFromNum).sum();
            int registerSum = list.stream().filter(l -> e.getSceneType().equals(l.getSceneType())).mapToInt(TsPersonFromStatistic::getRegisteredNum).sum();
            e.setFromData(fromSum);
            e.setRegisterSum(registerSum);
        });
        Map<String, Object> map = new HashMap<>();
        map.put("tdWxDicts", tdWxDicts);
        List<TsPersonFromStatistic> tableList = tsPersonFromStatisticMapper.selectPersonFromGroupByData(orgId, startDate, endDate);
        map.put("list", tableList);
        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 用户来源数据统计折线图
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseBean selectPersonFromGroupByDay(Integer orgId, LocalDate startDate, LocalDate endDate) {
        List<TsPersonFromStatistic> dayList = getDayList(startDate, endDate);
        List<TsPersonFromStatistic> list = tsPersonFromStatisticMapper.selectPersonFromGroupByDay(orgId, startDate, endDate);
        dayList.forEach(e -> {
            list.forEach(l -> {
                if (e.getCreateTime().equals(l.getCreateTime())) {
                    e.setFromNum(l.getFromNum());
                    e.setRegisteredNum(l.getRegisteredNum());
                }
            });
        });
        return ResponseBean.success(dayList);
    }

    /**
     * 返回时间区间格式
     *
     * @param startDate
     * @param endDate
     * @return
     */
    private List<TsPersonFromStatistic> getDayList(LocalDate startDate, LocalDate endDate) {
        if (null == startDate || null == endDate) {
            endDate = LocalDate.now();
            startDate = LocalDate.now().plusDays(-7);
        }

        //获取时间区间
        List<LocalDate> dates = Stream.iterate(startDate, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(startDate, endDate))
                .collect(Collectors.toList());

        //格式化时间
        List<TsPersonFromStatistic> dataList = new ArrayList<>();
        DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        dates.forEach(e -> {
            TsPersonFromStatistic tsPersonFromStatistic = new TsPersonFromStatistic();
            tsPersonFromStatistic.setRegisteredNum(0);
            tsPersonFromStatistic.setFromNum(0);
            tsPersonFromStatistic.setCreateTime(dtf3.format(e));
            dataList.add(tsPersonFromStatistic);
        });
        return dataList;
    }

    /**
     * 获取楼盘的折线图统计
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param personBuildingList
     * @return
     */
    @Override
    public ResponseBean getStatsBarList(Integer orgId, LocalDate startDate, LocalDate endDate, List<TaPersonBuilding> personBuildingList) {
        Map<String,Object> map = new HashMap<>();
        //项目公客排行折线图获取
        Map<String,Object> gkBarMap = getGkBarData(orgId,startDate,endDate,personBuildingList);
        map.put("gkBarMap",gkBarMap);

        //项目私客排行折线图获取
        Map<String,Object> skBarMap = getSkBarData(orgId,startDate,endDate,personBuildingList);
        map.put("skBarMap",skBarMap);

        //项目访问人数排行折线图获取
        Map<String,Object> wgBarMap = getWgBarData(orgId,startDate,endDate,personBuildingList);
        map.put("wgBarMap",wgBarMap);

        //项目访问次数排行折线图获取
        Map<String,Object> fwBarMap = getFwBarData(orgId,startDate,endDate,personBuildingList);
        map.put("fwBarMap",fwBarMap);

        return ResponseBean.success(map);
    }

    /**
     * 获取楼盘统计所有数据
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param buildingId
     * @param personBuildingList
     * @return
     */
    @Override
    public ResponseBean getStatsTableList(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, List<TaPersonBuilding> personBuildingList,String sortField,String sortOrder) {
        String orderType = "asc";
        if ("descend".equals(sortOrder)){
            orderType = "desc";
        }else if ("ascend".equals(sortOrder)){
            orderType = "asc";
        }
        if(StringUtils.isEmpty(sortField)){
            sortField = "kh_num";
            orderType = "desc";
        }
        IPage<StatsBuilding> pg = new Page<>(pageNum, pageSize);
        IPage<StatsBuilding> page = taBuildingMapper.getStatsTableList(pg, orgId, startDate, endDate, buildingId, personBuildingList,sortField,orderType);
        return ResponseBean.success(page);
    }

    /**
     * 获取需要导出的数据
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param buildingId
     * @param personBuildingList
     * @return
     */
    @Override
    public List<StatsBuilding> getExportTableList(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, List<TaPersonBuilding> personBuildingList) {
        return taBuildingMapper.getExportTableList(orgId, startDate, endDate, buildingId, personBuildingList);
    }

    /**
     * 按时间获取此楼盘的数据折线图
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param buildingId
     * @param taPersonBuildingListByUserId
     * @return
     */
    @Override
    public ResponseBean getStatsTimeBarList(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, List<TaPersonBuilding> taPersonBuildingListByUserId) {
        Map<String,Object> map = new HashMap<>();
        TaBuilding taBuilding = taBuildingMapper.selectById(buildingId);
        List<StatsTimeBuilding> list = taBuildingMapper.getStatsTimeBarList(orgId,startDate,endDate,buildingId,taBuilding.getBuildingName());
        //日期维度
        List<String> timeList = list.stream().map(StatsTimeBuilding::getCreateDate).collect(Collectors.toList());
        //公客维度
        List<Integer> gkList = list.stream().map(StatsTimeBuilding::getGkNum).collect(Collectors.toList());
        //私客维度
        List<Integer> skList = list.stream().map(StatsTimeBuilding::getSkNum).collect(Collectors.toList());
        //新增客户维度
        List<Integer> khList = list.stream().map(StatsTimeBuilding::getKhNum).collect(Collectors.toList());
        //访问人数维度
        List<Integer> uvList = list.stream().map(StatsTimeBuilding::getUvNum).collect(Collectors.toList());
        map.put("list",list);
        map.put("timeList",timeList);
        map.put("gkList",gkList);
        map.put("skList",skList);
        map.put("khList",khList);
        map.put("uvList",uvList);
        return ResponseBean.success(map);
    }

    /**
     * 分页统计楼盘详情数据
     *
     * @param pageNum
     * @param pageSize
     * @param orgId
     * @param startDate
     * @param endDate
     * @param buildingId
     * @param taPersonBuildingListByUserId
     * @param sortField
     * @param sortOrder
     * @return
     */
    @Override
    public ResponseBean getStatsTimeTableList(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, List<TaPersonBuilding> taPersonBuildingListByUserId, String sortField, String sortOrder) {
        String orderType = "asc";
        if ("descend".equals(sortOrder)){
            orderType = "desc";
        }else if ("ascend".equals(sortOrder)){
            orderType = "asc";
        }
        IPage<StatsTimeBuilding> pg = new Page<>(pageNum, pageSize);
        IPage<StatsTimeBuilding> page = taBuildingMapper.getStatsTimeTableList(pg, orgId, startDate, endDate, buildingId,sortField,orderType);
        return ResponseBean.success(page);
    }

    /**
     * 项目详情统计
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @param buildingId
     * @param taPersonBuildingListByUserId
     * @return
     */
    @Override
    public List<StatsTimeBuilding> getExportTimeTableList(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, List<TaPersonBuilding> taPersonBuildingListByUserId) {
        TaBuilding taBuilding = taBuildingMapper.selectById(buildingId);
        return taBuildingMapper.getStatsTimeBarList(orgId,startDate,endDate,buildingId,taBuilding.getBuildingName());
    }

    /**
     * 意向客户导出
     *
     * @param orgId
     * @param buildingId
     * @param taPersonBuildingListByUserId
     * @return
     */
    @Override
    public List<IntentionUser> getExportIntentionUser(Integer orgId, String buildingId, List<TaPersonBuilding> taPersonBuildingListByUserId) {
        return taPersonIntentionRecordMapper.selectExportIntentionUser(orgId, buildingId, taPersonBuildingListByUserId);
    }

    /**
     * 新调整--新增用户
     *
     * @param orgId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseBean changeNewsUserCount(Integer orgId, LocalDate startDate, LocalDate endDate) {
        List<TsPersonFromStatistic> tableList = tsPersonFromStatisticMapper.selectNewLinePersonFromGroupByData(orgId, startDate, endDate);
        return ResponseBean.success(tableList);
    }

    /**
     * 人员城市分布
     *
     * @param orgId
     * @return
     */
    @Override
    public ResponseBean selectCityPerson(Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        // 城市比例
        List<Map<String, Object>> selectCityUser = taPersonMapper.selectCityPerson(orgId);
        Map<String, Object> map = new HashMap<>();
        map.put("selectCityUser", selectCityUser);
        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 项目公客排行折线图获取
     * @param orgId
     * @param startDate
     * @param endDate
     * @param personBuildingList
     * @return
     */
    private Map<String, Object> getGkBarData(Integer orgId, LocalDate startDate, LocalDate endDate, List<TaPersonBuilding> personBuildingList) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> gkBarData = taBuildingMapper.getGkBarData(orgId, startDate, endDate, personBuildingList);
        if (gkBarData.size() > 0){
            map.put("data",gkBarData);
            List<Object> buildingNameList = gkBarData.stream().map(x -> x.get("buildingName")).collect(Collectors.toList());
            map.put("buildingNameList",buildingNameList);
            List<Object> gkNumList = gkBarData.stream().map(x -> x.get("gkNum")).collect(Collectors.toList());
            map.put("numList",gkNumList);
        }
        return map;
    }

    /**
     * 项目私客排行折线图获取
     * @param orgId
     * @param startDate
     * @param endDate
     * @param personBuildingList
     * @return
     */
    private Map<String, Object> getSkBarData(Integer orgId, LocalDate startDate, LocalDate endDate, List<TaPersonBuilding> personBuildingList) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> skBarData = taBuildingMapper.getSkBarData(orgId, startDate, endDate, personBuildingList);
        if (skBarData.size() > 0){
            map.put("data",skBarData);
            List<Object> buildingNameList = skBarData.stream().map(x -> x.get("buildingName")).collect(Collectors.toList());
            map.put("buildingNameList",buildingNameList);
            List<Object> gkNumList = skBarData.stream().map(x -> x.get("skNum")).collect(Collectors.toList());
            map.put("numList",gkNumList);
        }
        return map;
    }


    /**
     * 项目访问人数排行折线图获取
     * @param orgId
     * @param startDate
     * @param endDate
     * @param personBuildingList
     * @return
     */
    private Map<String, Object> getWgBarData(Integer orgId, LocalDate startDate, LocalDate endDate, List<TaPersonBuilding> personBuildingList) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> wgBarData = taBuildingMapper.getWgBarData(orgId, startDate, endDate, personBuildingList);
        if (wgBarData.size() > 0){
            map.put("data",wgBarData);
            List<Object> buildingNameList = wgBarData.stream().map(x -> x.get("buildingName")).collect(Collectors.toList());
            map.put("buildingNameList",buildingNameList);
            List<Object> gkNumList = wgBarData.stream().map(x -> x.get("wgNum")).collect(Collectors.toList());
            map.put("numList",gkNumList);
        }
        return map;
    }

    /**
     * 项目访问次数排行折线图获取
     * @param orgId
     * @param startDate
     * @param endDate
     * @param personBuildingList
     * @return
     */
    private Map<String, Object> getFwBarData(Integer orgId, LocalDate startDate, LocalDate endDate, List<TaPersonBuilding> personBuildingList) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String, Object>> fwBarData = taBuildingMapper.getFwBarData(orgId, startDate, endDate, personBuildingList);
        if (fwBarData.size() > 0){
            map.put("data",fwBarData);
            List<Object> buildingNameList = fwBarData.stream().map(x -> x.get("buildingName")).collect(Collectors.toList());
            map.put("buildingNameList",buildingNameList);
            List<Object> gkNumList = fwBarData.stream().map(x -> x.get("pvNum")).collect(Collectors.toList());
            map.put("numList",gkNumList);
        }
        return map;
    }
}
