package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuchen.saas.park.api.entity.park.PmgBatParkBuildingInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkFloorInfo;
import com.yuchen.saas.park.api.entity.park.PmgBatParkRoomInfo;
import com.yuchen.saas.park.api.entity.park.PmgLogPeopleCount;
import com.yuchen.saas.park.api.vo.park.*;
import com.yuchen.saas.park.api.vo.requestvo.requestparam.RequestParamBase;
import com.yuchen.saas.park.service.config.PeopleConfig;
import com.yuchen.saas.park.service.mapper.PeopleCountMapper;
import com.yuchen.saas.park.service.mapper.PmgBatParkVisitorMapper;
import com.yuchen.saas.park.service.service.*;
import com.yuchen.saas.park.service.util.EnergyDateUtils;
import com.yuchen.saas.park.service.util.ParkDateUtils;
import com.yuchen.saas.park.service.util.PieChartUtils;
import com.yuchen.saas.park.service.util.WsDateUtil;
import org.apache.commons.lang.StringUtils;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.utils.DateUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 吕海泉
 * @create 2020/12/23 17:50
 */
@Service
public class PeopleCountServiceImpl
        extends BaseServiceImpl<PeopleCountMapper, PmgLogPeopleCount>
        implements IPeopleCountService {
    @Resource
    private PeopleConfig peopleConfig;

    @Resource
    private PmgBatParkVisitorMapper pmgBatParkVisitorMapper;

    @Resource
    private PmgBatParkBuildingInfoService buildingInfoService;

    @Resource
    private PmgBatParkFloorInfoService floorInfoService;

    @Resource
    private PmgBatParkRoomInfoService roomInfoService;

    @Resource(name = "pmgBatPointLocationInfoServiceImpl")
    private PmgBatPointLocationInfoService locationInfoService;

    @Override
    public Map<String, Object> getCountTime24() {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        List<GetCountTime24RespVo> returnR = baseMapper.getCountTime24();

        if(Func.isNotEmpty(returnR) && returnR.size() > 0) {
            List<String> countArr = new ArrayList<String>();
            List<String> timeArr = new ArrayList<String>();
            for(GetCountTime24RespVo getVo: returnR) {
                countArr.add(getVo.getCount());
                timeArr.add(getVo.getHour());
            }
            returnMap.put("countArr", countArr);
            returnMap.put("timeArr", timeArr);
        }
        return returnMap;
    }

    @Override
    public Map<String, Object> getCountTime24(Long parkId, Long buildingId, Long floorId, Long roomId) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        List<GetCountTime24RespVo> returnR = baseMapper.getCountTime24ByParkId(parkId, buildingId, floorId, roomId);

        if(Func.isNotEmpty(returnR) && returnR.size() > 0) {
            List<String> countArr = new ArrayList<String>();
            List<String> timeArr = new ArrayList<String>();
            for(GetCountTime24RespVo getVo: returnR) {
                countArr.add(getVo.getCount());
                timeArr.add(getVo.getHour());
            }
            returnMap.put("countArr", countArr);
            returnMap.put("timeArr", timeArr);
        }
        return returnMap;
    }

    /**
     * 24小时访客(包含陌生人)趋势
     * @param parkId
     * @return
     */
    @Override
    public Map<String, Object> getVisitorCountTime24(String parkId) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        List<GetCountTime24RespVo> returnR = baseMapper.getVisitorCountTime24(parkId, peopleConfig.getCode10002(), peopleConfig.getCode10003());
        if(Func.isNotEmpty(returnR) && returnR.size() > 0) {
            List<String> countArr = new ArrayList<String>();
            List<String> timeArr = new ArrayList<String>();
            for(GetCountTime24RespVo getVo: returnR) {
                countArr.add(getVo.getCount());
                timeArr.add(getVo.getHour());
            }
            returnMap.put("countArr", countArr);
            returnMap.put("timeArr", timeArr);
        }
        return returnMap;
    }

    /**
     * 人员统计
     *  10000 VIP
     *  10001 园内(内部)人员
     *  10002 访客
     *  10003 陌生人
     * @return
     *  todayVisitor（今日访客）
     *  totalParkPerson（总入园园内）
     *  totalVisitor（总入园访客）
     *  totalVip（总入园VIP）
     *  totalPerson（vip+园内+访客+陌生人）
     *  todayInCount 今日入园人数
     *  yesterDayInCount 昨日入园人数
     *  insiderStaffCount 内部人数（今日入园）
     *  normalPeopleCount 正常体温（今日入园）
     *  totalCount 今日陌生人总数
     *
     */
    @Override
    public Map<String, Long> getPersonResult() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        List<PmgLogPeopleCount> pmgLogPeopleCounts = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda()
                .ge(PmgLogPeopleCount::getCreateTime,zero).eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10002()));
        List<PmgLogPeopleCount> pmgLogPeopleCountsVip = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10000()));
        List<PmgLogPeopleCount> pmgLogPeopleCountsPark = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10001()));
        List<PmgLogPeopleCount> pmgLogPeopleCountsVisitor = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10002()));
        int pmgLogPeopleCountsStranger = count(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10003()));
        new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 今日入园人数
        Long todayInCount = baseMapper.getTodayInCount();

        // 昨日入园人数
        Long yesterDayInCount = baseMapper.getTesterDayInCount();

        // 内部人数（今日入园）
        Long insiderStaffCount = getToDayCountByCode(peopleConfig.getCode10001());
        // 今日陌生人总数
        Long totalCount = getToDayCountByCode(peopleConfig.getCode10003());
        // 正常体温（今日入园）
        Long normalPeopleCount = getNormalPeopleCount(peopleConfig.getTemperature());
        // 异常体温（今日入园）
        Long abnormalPeopleCount = getAbnormalPeopleCount(peopleConfig.getTemperature());

//        // 累计人数：历史所有访问/vip/内部人数，根据人员类型(访客/vip/内部)和ID(去重)
//        Long vipCount = getCountBycode(peopleConfig.getCode10000());
//        // 内部人员 根据用户id去重
//        Long insiderCount = getCountBycode(peopleConfig.getCode10001());
//        // 访客 根据用户id 去重
//        Long visitorCount = getCountBycode(peopleConfig.getCode10002());
//        // 今日访客，根据id去重
//        Long todayVisitorCount = getTodayCount(peopleConfig.getCode10002());

        Map<String, Long>  total = new HashMap<>();
        int sum = pmgLogPeopleCountsVip.size() + pmgLogPeopleCountsPark.size() + pmgLogPeopleCountsVisitor.size() + pmgLogPeopleCountsStranger;
        total.put("todayVisitor",(long) pmgLogPeopleCounts.size());
        total.put("totalPerson", (long) sum);
        total.put("totalVip",(long) pmgLogPeopleCountsVip.size());
        total.put("totalParkPerson",(long) pmgLogPeopleCountsPark.size());
        total.put("totalVisitor",(long) pmgLogPeopleCountsVisitor.size());
        total.put("todayInCount", todayInCount);
        total.put("yesterDayInCount", yesterDayInCount);
        total.put("insiderStaffCount", insiderStaffCount);
        total.put("totalCount", totalCount);
        total.put("normalPeopleCount", normalPeopleCount);
        total.put("abnormalPeopleCount", abnormalPeopleCount);

//        total.put("vipCount", vipCount);
//        total.put("insiderCount", insiderCount);
//        total.put("visitorCount", visitorCount);
//        total.put("todayVisitorCount", todayVisitorCount);
        return total;
    }

    @Override
    public Map<String, Long> getPersonResult(String parkId) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        List<PmgLogPeopleCount> pmgLogPeopleCounts = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda()
                .ge(PmgLogPeopleCount::getCreateTime,zero).eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10002()).eq(PmgLogPeopleCount::getParkId,parkId));
        List<PmgLogPeopleCount> pmgLogPeopleCountsVip = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10000()).eq(PmgLogPeopleCount::getParkId,parkId));
        List<PmgLogPeopleCount> pmgLogPeopleCountsPark = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10001()).eq(PmgLogPeopleCount::getParkId,parkId));
        List<PmgLogPeopleCount> pmgLogPeopleCountsVisitor = baseMapper.selectList(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10002()).eq(PmgLogPeopleCount::getParkId,parkId));
        int pmgLogPeopleCountsStranger = count(Wrappers.<PmgLogPeopleCount>query().lambda().eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10003()).eq(PmgLogPeopleCount::getParkId,parkId));
        new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 今日入园人数
        Long todayInCount = baseMapper.getTodayInCountByParkId(parkId);

        // 昨日入园人数
        Long yesterDayInCount = baseMapper.getTesterDayInCountByParkId(parkId);

        // 内部人数（今日入园）
        Long insiderStaffCount = getToDayCountByCode(peopleConfig.getCode10001(), parkId);
        // 今日陌生人总数
        Long totalCount = getToDayCountByCode(peopleConfig.getCode10003(), parkId);
        // 正常体温（今日入园）
        Long normalPeopleCount = getNormalPeopleCount(peopleConfig.getTemperature(), parkId);
        // 异常体温（今日入园）
        Long abnormalPeopleCount = getAbnormalPeopleCount(peopleConfig.getTemperature(), parkId);

//        // 累计人数：历史所有访问/vip/内部人数，根据人员类型(访客/vip/内部)和ID(去重)
//        Long vipCount = getCountBycode(peopleConfig.getCode10000());
//        // 内部人员 根据用户id去重
//        Long insiderCount = getCountBycode(peopleConfig.getCode10001());
//        // 访客 根据用户id 去重
//        Long visitorCount = getCountBycode(peopleConfig.getCode10002());
//        // 今日访客，根据id去重
//        Long todayVisitorCount = getTodayCount(peopleConfig.getCode10002());

        Map<String, Long>  total = new HashMap<>();
        int sum = pmgLogPeopleCountsVip.size() + pmgLogPeopleCountsPark.size() + pmgLogPeopleCountsVisitor.size() + pmgLogPeopleCountsStranger;
        total.put("todayVisitor",(long) pmgLogPeopleCounts.size());
        total.put("totalPerson", (long) sum);
        total.put("totalVip",(long) pmgLogPeopleCountsVip.size());
        total.put("totalParkPerson",(long) pmgLogPeopleCountsPark.size());
        total.put("totalVisitor",(long) pmgLogPeopleCountsVisitor.size());
        total.put("todayInCount", todayInCount);
        total.put("yesterDayInCount", yesterDayInCount);
        total.put("insiderStaffCount", insiderStaffCount);
        total.put("totalCount", totalCount);
        total.put("normalPeopleCount", normalPeopleCount);
        total.put("abnormalPeopleCount", abnormalPeopleCount);

//        total.put("vipCount", vipCount);
//        total.put("insiderCount", insiderCount);
//        total.put("visitorCount", visitorCount);
//        total.put("todayVisitorCount", todayVisitorCount);
        return total;
    }


    @Override
    public AbnormalPeopleVo getAbnormalPeople() {
        AbnormalPeopleVo count = baseMapper.getAbnormalPeople();
        return count;
    }

    @Override
    public AbnormalPeopleVo getAbnormalPeopleByParkId(String parkId) {
        AbnormalPeopleVo count = baseMapper.getAbnormalPeopleByParkId(parkId);
        return count;
    }

    @Override
    public List<Map<String, Object>> visitorBuildingSpread(String code) {
        return baseMapper.visitorBuildingSpread(code);
    }

    @Override
    public List<Map<String, Object>> visitorBuildingSpread(String code, String parkId) {
        return baseMapper.visitorBuildingSpreadByParkId(code, parkId);
    }

    @Override
    public Long getToDayCountByCode(String code) {
        return baseMapper.getToDayCountByCode(code);
    }

    public Long getToDayCountByCode(String code, String parkId) {
        return baseMapper.getToDayCountByCodeAndParkId(code, parkId);
    }

    @Override
    public Long getNormalPeopleCount(String temperature) {
        return baseMapper.getNormalPeopleCount(temperature);
    }

    public Long getNormalPeopleCount(String temperature, String parkId) {
        return baseMapper.getNormalPeopleCountByParkId(temperature, parkId);
    }

    @Override
    public List<PmgLogPeopleCount> getListAbnormalAndStranger() {
        return baseMapper.getListAbnormalAndStranger();
    }

    @Override
    public Long getAbnormalPeopleCount(String temperature) {
        return baseMapper.getAbnormalPeopleCount(temperature);
    }

    public Long getAbnormalPeopleCount(String temperature, String parkId) {
        return baseMapper.getAbnormalPeopleCountByParkId(temperature, parkId);
    }
    /**
     * 累计人数：历史所有访问/vip/内部人数，根据人员类型(访客/vip/内部)和ID(去重)
     * @param code
     * @return
     */
    @Override
    public Long getCountBycode(String code) {
        return baseMapper.getCountBycode(code);
    }

    /**
     * 累计人数：历史所有访问/vip/内部人数，根据人员类型(访客/vip/内部)和ID(去重),增加parkid
     * @param code
     * @return
     */
    public Long getCountBycodeAndParkId(String code, String parkId) {
        return baseMapper.getCountBycodeAndParkId(code, parkId);
    }

    /**
     * 今日访客/vip/内部，更加人员id去重
     * @param code
     * @return
     */
    @Override
    public Long getTodayCount(String code) {
        return baseMapper.getTodayCount(code);
    }

    /**
     * 今日访客/vip/内部，更加人员id去重,增加路径参数parkid
     * @param code
     * @return
     */
    public Long getTodayCountAndParkId(String code, String parkId) {
        return baseMapper.getTodayCountAndParkId(code, parkId);
    }

    /**
     * 首页-人员图像接口返回数据
     * @return
     */
    @Override
    public Map<String, Long> getPersonImage() {
        // VIP人员，根据用户id去重
        Long vipCount = getCountBycode(peopleConfig.getCode10000());
        // 内部人员，根据用户id去重
        Long insiderCount = getCountBycode(peopleConfig.getCode10001());
        // 访客人员，根据用户id 去重
        Long visitorCount = getCountBycode(peopleConfig.getCode10002());
        // 今日访客，根据id去重
        Long todayVisitorCount = getTodayCount(peopleConfig.getCode10002());
        // 累计访问人数
        Long count = vipCount + insiderCount + visitorCount;
        // 添加进返回集合
        Map<String, Long>  result = new HashMap<>();
        result.put("count", count);
        result.put("vipCount", vipCount);
        result.put("insiderCount", insiderCount);
        result.put("visitorCount", visitorCount);
        result.put("todayVisitorCount", todayVisitorCount);
        return result;
    }

    @Override
    public Map<String, Long> getPersonImage(String parkId) {
        // VIP人员，根据用户id去重
        Long vipCount = getCountBycodeAndParkId(peopleConfig.getCode10000(), parkId);
        // 内部人员，根据用户id去重
        Long insiderCount = getCountBycodeAndParkId(peopleConfig.getCode10001(), parkId);
        // 访客人员，根据用户id 去重
        Long visitorCount = getCountBycodeAndParkId(peopleConfig.getCode10002(), parkId);
        // 今日访客，根据id去重
        Long todayVisitorCount = getTodayCountAndParkId(peopleConfig.getCode10002(), parkId);
        // 累计访问人数
        Long count = vipCount + insiderCount + visitorCount;
        // 添加进返回集合
        Map<String, Long>  result = new HashMap<>();
        result.put("count", count);
        result.put("vipCount", vipCount);
        result.put("insiderCount", insiderCount);
        result.put("visitorCount", visitorCount);
        result.put("todayVisitorCount", todayVisitorCount);
        return result;
    }

    /**
     * 获取指定园区本月入园人次、本月可疑人员人次、本月外部访客人次
     * @param parkId 园区ID
     * @return
     */
    @Override
    public MonthPeopleCountVo getMonthPeopleCount(String parkId) {
        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        // 定义返回对象
        MonthPeopleCountVo peopleCountVo = new MonthPeopleCountVo();
        // 查询封装内部人员总数
        peopleCountVo.setInsiderMonthTotal(count(peopleWrapper.eq(PmgLogPeopleCount::getParkId, parkId)
                .eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10001()).ge(PmgLogPeopleCount::getTimeStamp, ParkDateUtils
                        .getThisMonthFirstDay().getTime())));
        // 清空查询条件
        peopleWrapper.clear();
        // 查询封装VIP人员总数
        peopleCountVo.setVipMonthPerTotal(count(peopleWrapper.eq(PmgLogPeopleCount::getParkId, parkId)
                .eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10000()).ge(PmgLogPeopleCount::getTimeStamp, ParkDateUtils.getThisMonthFirstDay().getTime())));
        // 清空查询条件
        peopleWrapper.clear();
        // 查询封装访客人员总数(这里发访客包含陌生人)
        peopleCountVo.setVisitorMonthTotal(count(peopleWrapper.eq(PmgLogPeopleCount::getParkId, parkId)
                .ge(PmgLogPeopleCount::getTimeStamp, ParkDateUtils.getThisMonthFirstDay().getTime()).and(wrapper -> wrapper.eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10002()).or()
                        .eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10003()))));
        return peopleCountVo;
    }

    /**
     * 近七天入园人次统计
     * @param parkId
     * @return
     */
    @Override
    public PieChartVo getSevenDayPeopleCount(String parkId) {
        return PieChartUtils.getPicChart(baseMapper.getSevenDayPeopleCount(parkId));
    }

    /**
     * 近七天访客人次(包含陌生人)
     * @param parkId
     * @return
     */
    @Override
    public PieChartVo getSevenDayVisitorCount(String parkId) {
        return PieChartUtils.getPicChart(baseMapper.getSevenDayVisitorCount(peopleConfig.getCode10002(), peopleConfig.getCode10003(), parkId));
    }

    /**
     * 近七天园内人员人次
     * @param parkId
     * @return
     */
    @Override
    public PieChartVo getSevenDayInsiderCount(String parkId) {
        return PieChartUtils.getPicChart(baseMapper.getSevenDayByCodeCount(peopleConfig.getCode10001(), parkId));
    }

    /**
     * 近七天VIP人员人次
     * @param parkId
     * @return
     */
    @Override
    public PieChartVo getSevenDayVipCount(String parkId) {
        return PieChartUtils.getPicChart(baseMapper.getSevenDayByCodeCount(peopleConfig.getCode10000(), parkId));
    }

    /**
     * 通过人员类型获取今日该类型的数量
     * @param parkId 园区ID
     * @return
     */
    @Override
    public PeopleTodayCountVo getTodayPeopleCount(String parkId) {
        // 定义返回对象
        PeopleTodayCountVo peopleTodayCountVo = new PeopleTodayCountVo();
        // 获取封装今日vip人次
        peopleTodayCountVo.setVipCount(baseMapper.getTodayPeopleCountByCode(peopleConfig.getCode10000(), parkId));
        // 获取封装今日内部人员人次
        peopleTodayCountVo.setInsiderCount(baseMapper.getTodayPeopleCountByCode(peopleConfig.getCode10001(), parkId));
        // 获取封装今日访客人员人次
        peopleTodayCountVo.setVisitorCount(baseMapper.getTodayPeopleCountByCode(peopleConfig.getCode10002(), parkId));
        // 获取封装今日陌生人人员人次
        peopleTodayCountVo.setStrangerCount(baseMapper.getTodayPeopleCountByCode(peopleConfig.getCode10003(), parkId));

        return peopleTodayCountVo;
    }

    /******************* 3d/bi start ****************/

    /**
     * 人员入园信息折线图（入园人次、访客人次、可疑人员）２４小时
     * @param paramBase
     * @return
     */
    @Override
    public GetCountTime24InfoRespVo GetCountTime24InfoRespVo(RequestParamBase paramBase) {

        //定义返回结果
        GetCountTime24InfoRespVo getCountTime24InfoRespVo = new GetCountTime24InfoRespVo();

        //查询--今日入园人次(三个参数指定一个)
        List<GetEnvironmentInfo> enterParkCountList = baseMapper.GetCountTime24InfoRespVo(paramBase.getParkId(),paramBase.getBuildingId(),paramBase.getFloorId(),null);

        //查询--访客人次(三个参数指定一个)
        List<GetEnvironmentInfo> visitorCountList = baseMapper.GetCountTime24InfoRespVo(paramBase.getParkId(),paramBase.getBuildingId(),paramBase.getFloorId(),peopleConfig.getCode10002());

        //查询--可疑人员人次(三个参数指定一个) 后期需要改 此处为模拟
        List<GetEnvironmentInfo> dubiousCountList = baseMapper.GetCountTime24InfoRespVo(paramBase.getParkId(),paramBase.getBuildingId(),paramBase.getFloorId(),peopleConfig.getCode10000());

        getCountTime24InfoRespVo.setEnterParkCount(enterParkCountList);
        getCountTime24InfoRespVo.setVisitorCount(visitorCountList);
        getCountTime24InfoRespVo.setDubiousCount(dubiousCountList);
        return getCountTime24InfoRespVo;
    }

    @Override
    public GetCountTimeMonthInfoRespVo getCountTimeMonthInfo(RequestParamBase paramBase) {

        //定义返回结果
        GetCountTimeMonthInfoRespVo countTimeMonthInfoRespVo = new GetCountTimeMonthInfoRespVo();


        //入园人次统计(key时间、value统计数)
        List<GetEnvironmentInfo> countTimeMonthInfo = baseMapper.getCountTimeMonthInfo(paramBase.getParkId(), paramBase.getBuildingId(), paramBase.getFloorId(), null);


        //查询--访客人次(三个参数指定一个)
        List<GetEnvironmentInfo> visitorCountList = baseMapper.getCountTimeMonthInfo(paramBase.getParkId(),paramBase.getBuildingId(),paramBase.getFloorId(),peopleConfig.getCode10002());


        //查询--可疑人员人次(三个参数指定一个) 后期需要改 此处为模拟
        List<GetEnvironmentInfo> dubiousCountList = baseMapper.getCountTimeMonthInfo(paramBase.getParkId(),paramBase.getBuildingId(),paramBase.getFloorId(),peopleConfig.getCode10000());



        countTimeMonthInfoRespVo.setEnterParkCount(countTimeMonthInfo);
        countTimeMonthInfoRespVo.setVisitorCount(visitorCountList);
        countTimeMonthInfoRespVo.setDubiousCount(dubiousCountList);
        return countTimeMonthInfoRespVo;
    }

    @Override
    public HashMap<String, Long> getPeopleTypeTime24(RequestParamBase requestParamBase) {

        //返回对象
        HashMap<String,Long> peopleType = new HashMap(4);

        //今日-内部人员总数
        Long interiorTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10001(), "%Y-%m-%d", DateUtil.formatDate(new Date()));

        //今日-可疑人员总数
        Long dubiousTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10003(), "%Y-%m-%d", DateUtil.formatDate(new Date()));

        //今日-访客人员总数
        Long visitorTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10002(), "%Y-%m-%d", DateUtil.formatDate(new Date()));

        //今日-vip总数
        Long vipTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10000(), "%Y-%m-%d", DateUtil.formatDate(new Date()));

        peopleType.put("interiorTotal",interiorTotal);
        peopleType.put("dubiousTotal",dubiousTotal);
        peopleType.put("visitorTotal",visitorTotal);
        peopleType.put("vipTotal",vipTotal);
        return peopleType;
    }

    @Override
    public HashMap<String, Long> getPeopleTypeTimeMonth(RequestParamBase requestParamBase) {

        //返回对象
        HashMap<String,Long> peopleType = new HashMap(4);

        //获取当前年月格式
        Date time = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String yearMonth = df.format(time);

        //本月-内部人员总数
        Long interiorTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10001(), "%Y-%m", yearMonth);

        //本月-可疑人员总数
        Long dubiousTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10003(), "%Y-%m", yearMonth);

        //本月-访客人员总数
        Long visitorTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10002(), "%Y-%m", yearMonth);

        //本月-vip总数
        Long vipTotal = baseMapper.getPeopleTypeTime(requestParamBase.getParkId(), requestParamBase.getBuildingId(), requestParamBase.getFloorId(), peopleConfig.getCode10000(), "%Y-%m", yearMonth);

        peopleType.put("interiorTotal",interiorTotal);
        peopleType.put("dubiousTotal",dubiousTotal);
        peopleType.put("visitorTotal",visitorTotal);
        peopleType.put("vipTotal",vipTotal);
        return peopleType;
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForParkMonth(String parkId) {

        Date time = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String yearMonth = df.format(time);
        return pmgBatParkVisitorMapper.getPeopleMakeForPark(parkId, "%Y-%m", yearMonth);
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForParkDay(String parkId) {
        return pmgBatParkVisitorMapper.getPeopleMakeForPark(parkId, "%Y-%m-%d", DateUtil.formatDate(new Date()));
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForBuildingMonth(String parkId,String buildingId) {
        Date time = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String yearMonth = df.format(time);
        return pmgBatParkVisitorMapper.getPeopleMakeForBuilding(parkId,buildingId, "%Y-%m", yearMonth);
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForBuildingDay(String parkId,String buildingId) {
        return pmgBatParkVisitorMapper.getPeopleMakeForBuilding(parkId,buildingId, "%Y-%m-%d", DateUtil.formatDate(new Date()));
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForRoomMonth(String parkId,String buildingId,String floorId) {
        Date time = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String yearMonth = df.format(time);
        return pmgBatParkVisitorMapper.getPeopleMakeForRoom(parkId,buildingId,floorId, "%Y-%m", yearMonth);
    }

    @Override
    public List<Map<String, String>> getPeopleMakeForRoomDay(String parkId,String buildingId,String floorId) {
        return pmgBatParkVisitorMapper.getPeopleMakeForRoom(parkId,buildingId,floorId, "%Y-%m-%d", DateUtil.formatDate(new Date()));
    }

    @Override
    public Map<String, String> getPeopleInfo(Long parkId, Long buildingId, Long floorId) {
        Map<String, String> peopleInfo = new HashMap<>();
        if (Func.isNotEmpty(parkId) && Func.isNotEmpty(buildingId) && Func.isNotEmpty(floorId)) {
            // 楼层场景：获取楼层内部员工、楼层企业员工
            peopleInfo.put("insiderNumber", "10");
            peopleInfo.put("employeesNumber", "15");
        } else {
            //非楼层场景：获取今日入园人次、今日可疑人次、今日访客人次
            List<Map<String, String>> list = baseMapper.getPeopleInfo(parkId,buildingId,floorId);
            for (int i = 0; i < list.size(); i++) {
                peopleInfo.put(String.valueOf(list.get(i).get("PEOPLETOTLE")),String.valueOf(list.get(i).get("count")));
            }
        }

        return peopleInfo;
    }

    @Override
    public IPage<PmgLogPeopleCount> pagePeople(PmgLogPeopleCount pmgLogPeopleCount, Query query) {

        IPage<PmgLogPeopleCount> pages = this.page(Condition.getPage(query),
                new LambdaQueryWrapper<PmgLogPeopleCount>()
                        .eq(Func.isNotEmpty(pmgLogPeopleCount.getParkId()), PmgLogPeopleCount::getParkId, pmgLogPeopleCount.getParkId())
                        .eq(Func.isNotEmpty(pmgLogPeopleCount.getCode()), PmgLogPeopleCount::getCode,
                                pmgLogPeopleCount.getCode())
                        .like(Func.isNotEmpty(pmgLogPeopleCount.getEquipmentName()),
                                PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                        .like(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()), PmgLogPeopleCount::getTemperature,
                                formatTemperature(pmgLogPeopleCount.getTemperature()))
                        .orderByDesc(PmgLogPeopleCount::getTimeStamp)
        );
        return pages;
    }

    @Override
    public IPage<PmgLogPeopleCount> pagePeopleAbnormal(PmgLogPeopleCount pmgLogPeopleCount, Query query) {

        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        IPage<PmgLogPeopleCount> pages = this.page(Condition.getPage(query),
                peopleWrapper
                        .like(StringUtils.isNotBlank(pmgLogPeopleCount.getEquipmentName()),
                                PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                        .like(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()), PmgLogPeopleCount::getTemperature,
                                formatTemperature(pmgLogPeopleCount.getTemperature()))
                        .gt(PmgLogPeopleCount::getTemperature, peopleConfig.getTemperature())
                        .orderByDesc(PmgLogPeopleCount::getTimeStamp));
        return pages;
    }

    @Override
    public IPage<PmgLogPeopleCount> getHighFrequencyAndStranger(PmgLogPeopleCount pmgLogPeopleCount, Query query) {

        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        peopleWrapper.like(StringUtils.isNotBlank(pmgLogPeopleCount.getEquipmentName()),
                PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                .and(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()), wrapper -> wrapper.like(PmgLogPeopleCount::getTemperature,
                                formatTemperature(pmgLogPeopleCount.getTemperature())))
                .and(wrapper -> wrapper.eq(PmgLogPeopleCount::getCode,peopleConfig.getCode10003()).or().eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10004()))
                .orderByDesc(PmgLogPeopleCount::getTimeStamp);
        IPage<PmgLogPeopleCount> pages = this.page(Condition.getPage(query),
                peopleWrapper);
        return pages;
    }

    @Override
    public IPage<PmgLogPeopleCount> getNormalDayPage(PmgLogPeopleCount pmgLogPeopleCount, Query query) {

        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        IPage<PmgLogPeopleCount> iPage = this.page(Condition.getPage(query),
                peopleWrapper
                        .le(PmgLogPeopleCount::getTemperature, peopleConfig.getTemperature())
                        .like(StringUtils.isNotBlank(pmgLogPeopleCount.getEquipmentName()),
                                PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                        .eq(PmgLogPeopleCount::getParkId, pmgLogPeopleCount.getParkId())
                        .and(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()),
                                wrapper -> wrapper.like(PmgLogPeopleCount::getTemperature,
                                        formatTemperature(pmgLogPeopleCount.getTemperature())))
                        .between(PmgLogPeopleCount::getTimeStamp, startOfDay().getTime(), endOfDay().getTime())
                        .orderByDesc(PmgLogPeopleCount::getTimeStamp));
        return iPage;
    }

    @Override
    public IPage<PmgLogPeopleCount> getAbnormalDayPage(String parkId, PmgLogPeopleCount pmgLogPeopleCount,
                                                       Query query) {
        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        peopleWrapper.gt(PmgLogPeopleCount::getTemperature, peopleConfig.getTemperature())
                .eq(PmgLogPeopleCount::getParkId, parkId)
                .like(StringUtils.isNotBlank(pmgLogPeopleCount.getEquipmentName()),
                        PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                .between(PmgLogPeopleCount::getTimeStamp,
                        startOfDay().getTime(), endOfDay().getTime())
                .and(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()),
                        wrapper -> wrapper.like(PmgLogPeopleCount::getTemperature,
                                formatTemperature(pmgLogPeopleCount.getTemperature())))
                .orderByDesc(PmgLogPeopleCount::getTimeStamp);
        IPage<PmgLogPeopleCount> iPage = this.page(Condition.getPage(query), peopleWrapper);
        return iPage;
    }

    @Override
    public IPage<PmgLogPeopleCount> getStrangerDayPage(PmgLogPeopleCount pmgLogPeopleCount, Query query) {
        LambdaQueryWrapper<PmgLogPeopleCount> peopleWrapper = Wrappers.lambdaQuery();
        IPage<PmgLogPeopleCount> iPage = this.page(Condition.getPage(query)
                , peopleWrapper
                        .eq(PmgLogPeopleCount::getParkId, pmgLogPeopleCount.getParkId())
                        .like(StringUtils.isNotBlank(pmgLogPeopleCount.getEquipmentName()),
                                PmgLogPeopleCount::getEquipmentName, pmgLogPeopleCount.getEquipmentName())
                        .eq(PmgLogPeopleCount::getCode, peopleConfig.getCode10003())
                        .and(Func.isNotEmpty(pmgLogPeopleCount.getTemperature()),
                                wrapper -> wrapper.like(PmgLogPeopleCount::getTemperature,
                                        formatTemperature(pmgLogPeopleCount.getTemperature())))
                        .between(PmgLogPeopleCount::getTimeStamp,
                                startOfDay().getTime(), endOfDay().getTime())
                        .orderByDesc(PmgLogPeopleCount::getTimeStamp));
        return iPage;
    }

    @Override
    public List<GetEnvironmentInfo> getHourSecurityAbnormalToday(Long parkId, Long buildingId, Long floorId) {
        //获取今日日期
        String dateToday = EnergyDateUtils.getDateToday();
        //获取安防所有productKey
        List<String> productKeyList = locationInfoService.getProductKeyList(parkId, buildingId, floorId, null,"5");
        //获取今日异常行为趋势(小时-例折线图)
        List<GetEnvironmentInfo> list = new ArrayList<>();
        if (Func.isNotEmpty(productKeyList)) {
            list = baseMapper.getHourSecurityAbnormalDay(productKeyList, dateToday);
        }

        //获取今日几点
        int nowHour = EnergyDateUtils.getHourOfToday();
        //没有的小时给0
        for (int i = 1; i <= nowHour + 1; i++) {
            String hourNum = String.valueOf(i);
            if (!list.stream().anyMatch(e -> hourNum.equals(e.getKey()))) {
                GetEnvironmentInfo info = new GetEnvironmentInfo();
                info.setKey(hourNum);
                info.setValue("0");
                list.add(info);
            }
        }
        //升序排序
        list.sort((a, b) -> Integer.parseInt(a.getKey()) - Integer.parseInt(b.getKey()));
        return list;
    }

    @Override
    public List<GetEnvironmentInfo> getDaySecurityAbnormalMonth(Long parkId, Long buildingId, Long floorId) {
        //获取本月日期
        String dateMonth = EnergyDateUtils.getDateMonth();
        //获取安防所有productKey
        List<String> productKeyList = locationInfoService.getProductKeyList(parkId, buildingId, floorId, null,"5");
        //获取本月异常行为趋势(天-小时折线图)
        List<GetEnvironmentInfo> list = new ArrayList<>();
        if (Func.isNotEmpty(productKeyList)) {
            list = baseMapper.getDaySecurityAbnormalMonth(productKeyList, dateMonth);
        }
        //获取今日几号
        int nowDay = WsDateUtil.getDayOfCurrentMonth();
        //获取本月1号日期
        String beginDateMonth = WsDateUtil.getBeginDateOfMonth(0);
        for(int i = 0; i < nowDay; i++) {
            String offsetDate = WsDateUtil.offsetDay(beginDateMonth, i);
            if (!list.stream().anyMatch(e -> offsetDate.equals(e.getKey()))) {
                GetEnvironmentInfo info = new GetEnvironmentInfo();
                info.setKey(offsetDate);
                info.setValue("0");
                list.add(info);
            }
        }
        //升序排序
        list.sort((a, b) -> cn.hutool.core.date.DateUtil
                .parse(a.getKey()).compareTo(cn.hutool.core.date.DateUtil.parse(b.getKey())));
        return list;
    }

    @Override
    public Map<String, List<GetEnvironmentInfo>> getSecurityAbnormalDistribution(Long parkId, Long buildingId, Long floorId) {

        Map<String, List<GetEnvironmentInfo>> rspMap = new HashMap<>();
        List<GetEnvironmentInfo> listToday = new ArrayList<>();
        List<GetEnvironmentInfo> listMonth = new ArrayList<>();
        //获取今日日期
        String dateToday = EnergyDateUtils.getDateToday();
        //获取本月日期
        String dateMonth = EnergyDateUtils.getDateMonth();
        //获取所有点位设备
        List<PmgEquipmentPointVo> equipmentPointList = locationInfoService.selectByAllEquipmentPoint(parkId, buildingId
                , floorId, null);
        //过滤安防的所有设备
        List<PmgEquipmentPointVo> newEquipmentPointList =
                equipmentPointList.stream().filter(e -> "5".equals(e.getCategoryCode())).collect(Collectors.toList());
        //园区场景:查询楼栋分布
        if (Func.isNotEmpty(parkId) && Func.isEmpty(buildingId) && Func.isEmpty(floorId)) {
            LambdaQueryWrapper<PmgBatParkBuildingInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PmgBatParkBuildingInfo::getParkId, parkId);
            List<PmgBatParkBuildingInfo> buildingList = buildingInfoService.list(lambdaQueryWrapper);
            //循环获取楼栋的异常行为数
            for(PmgBatParkBuildingInfo buildingInfo : buildingList) {
                Long id = buildingInfo.getId();
                String name = buildingInfo.getBuildingName();
                //获取该楼栋的productKey
                List<String> pList =
                        newEquipmentPointList.stream().filter(p -> id.equals(p.getBuildingId())).map(PmgEquipmentPointVo::getProductKey).collect(
                                Collectors.toList());
                addInfoToList(name, pList, listToday, listMonth);
            }
        }
        //楼栋场景:查询楼层分布
        if (Func.isNotEmpty(parkId) && Func.isNotEmpty(buildingId) && Func.isEmpty(floorId)) {
            LambdaQueryWrapper<PmgBatParkFloorInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PmgBatParkFloorInfo::getBuildingId, buildingId);
            List<PmgBatParkFloorInfo> floorList = floorInfoService.list(lambdaQueryWrapper);
            //循环获取楼层的异常行为数
            for(PmgBatParkFloorInfo floorInfo : floorList) {
                Long id = floorInfo.getId();
                String name = floorInfo.getFloorName();
                //获取该楼层的productKey
                List<String> pList =
                        newEquipmentPointList.stream().filter(p -> id.equals(p.getFloorId())).map(PmgEquipmentPointVo::getProductKey).collect(
                                Collectors.toList());
                addInfoToList(name, pList, listToday, listMonth);
            }
        }
        //楼层场景:查询房间分布
        if (Func.isNotEmpty(parkId) && Func.isNotEmpty(buildingId) && Func.isNotEmpty(floorId)) {

            LambdaQueryWrapper<PmgBatParkRoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PmgBatParkRoomInfo::getFloorId, floorId);
            List<PmgBatParkRoomInfo> roomList = roomInfoService.list(lambdaQueryWrapper);
            //循环获取楼层的异常行为数
            for(PmgBatParkRoomInfo roomInfo : roomList) {
                Long id = roomInfo.getId();
                String name = roomInfo.getRoomName();
                //获取该楼层的productKey
                List<String> pList =
                        newEquipmentPointList.stream().filter(p -> id.equals(p.getRoomId())).map(PmgEquipmentPointVo::getProductKey).collect(
                                Collectors.toList());
                addInfoToList(name, pList, listToday, listMonth);
            }
        }
        //今日的异常行为分布
        rspMap.put("distributionToday", listToday);
        //本月的异常行为分布
        rspMap.put("distributionMonth", listMonth);
        return rspMap;
    }

    private void addInfoToList(String key, List<String> productKeyList, List<GetEnvironmentInfo> listToday,
                               List<GetEnvironmentInfo> listMonth) {

        int countDay = 0;
        int countMonth = 0;
        if (Func.isNotEmpty(productKeyList)) {
            //获取该楼栋今日的异常行为数
            LambdaQueryWrapper<PmgLogPeopleCount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(PmgLogPeopleCount::getProductKey, productKeyList);
            queryWrapper.between(PmgLogPeopleCount::getTimeStamp,
                    WsDateUtil.getBeginTimeMillisDay(System.currentTimeMillis(), 0),
                    WsDateUtil.getEndTimeMillisDay(System.currentTimeMillis(), 0));
            countDay = this.list(queryWrapper).size();

            countMonth =
                    this.list(new LambdaQueryWrapper<PmgLogPeopleCount>()
                            .eq(PmgLogPeopleCount::getProductKey, productKeyList)
                            .between(PmgLogPeopleCount::getTimeStamp,
                                    WsDateUtil.getBeginTimeMillisMonth(System.currentTimeMillis(), 0),
                                    WsDateUtil.getEndTimeMillisMonth(System.currentTimeMillis(), 0))).size();
        }
        //今日统计数据添加到集合
        GetEnvironmentInfo info = new GetEnvironmentInfo();
        info.setKey(key);
        info.setValue(String.valueOf(countDay));
        listToday.add(info);
        //本月统计数据添加到集合
        GetEnvironmentInfo info1 = new GetEnvironmentInfo();
        info1.setKey(key);
        info1.setValue(String.valueOf(countMonth));
        listMonth.add(info1);
    }

    @Override
    public List<AbnormalListVO> getAbnormalList(String categoryCode, Long parkId, Long buildingId, Long floorId) {

        List<AbnormalListVO> abnormalList = new ArrayList<>();
        //获取安防所有productKey
        List<String> productKeyList = locationInfoService.getProductKeyList(parkId, buildingId, floorId, null,"5");
        if (Func.isNotEmpty(productKeyList)) {
            abnormalList = baseMapper.getAbnormalList(productKeyList);
        }
        return abnormalList;
    }


    /**
     * 如果查询条件温度传了整数，转为Float类型会自动加上.0,这里是把温度值小数位后面的0去掉
     * @param temperature 温度
     * @return
     */
    private String formatTemperature(Float temperature) {
        String temperatureStr = "";
        if (Func.isNotEmpty(temperature)) {
            temperatureStr =
                    new BigDecimal(String.valueOf(temperature)).stripTrailingZeros().toPlainString();
        }
        return temperatureStr;
    }

    /**
     * 私有方法 获取今日最早时间
     *
     * @return
     */
    private Date startOfDay() {
        Calendar todayStart = Calendar.getInstance();
        int year = todayStart.get(Calendar.YEAR);
        int month = todayStart.get(Calendar.MONTH);
        int day = todayStart.get(Calendar.DATE);
        todayStart.set(year, month, day, 0, 0, 0);
        return todayStart.getTime();
    }

    /**
     * 私有方法 获取今日最晚时间
     *
     * @return
     */
    private Date endOfDay() {
        Calendar todayEnd = Calendar.getInstance();
        int year = todayEnd.get(Calendar.YEAR);
        int month = todayEnd.get(Calendar.MONTH);
        int day = todayEnd.get(Calendar.DATE);
        todayEnd.set(year, month, day, 23, 59, 59);
        return todayEnd.getTime();
    }
}																										

