package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.util.DateUtil;
import com.fdkankan.ucenter.entity.SceneStatistics;
import com.fdkankan.ucenter.mapper.ISceneStatisticsMapper;
import com.fdkankan.ucenter.service.ISceneStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 百度统计场景流量量表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-10-31
 */
@Service
public class SceneStatisticsServiceImpl extends ServiceImpl<ISceneStatisticsMapper, SceneStatistics> implements ISceneStatisticsService {

    @Override
    public List<SceneStatistics> findByTime(Date startDate, Date endDate, String sceneNum, Integer type) {
        return this.list(new LambdaQueryWrapper<SceneStatistics>()
            .eq(SceneStatistics::getSceneNum, sceneNum)
            .eq(SceneStatistics::getType, type)
            .ge(SceneStatistics::getDateStr, startDate)
            .le(SceneStatistics::getDateStr, endDate));
    }

    @Override
    public void addData(Map<String, Map<String, Double>> map, String dateStr) {
        String sceneNum = "";
        Map<String, Double> numMap = null;
        SceneStatistics entity = null;
        for (Map.Entry<String, Map<String, Double>> entry : map.entrySet()) {
            sceneNum = entry.getKey();
            numMap = entry.getValue();
            entity = new SceneStatistics();
            entity.setPvCount(numMap.get("pv_count").intValue());
            entity.setVisitorCount(numMap.get("visit1_count").intValue());
            entity.setVisit1Count(numMap.get("visit1_count").intValue());
            entity.setOutwardCount(numMap.get("outward_count").intValue());
            entity.setExitCount(numMap.get("exit_count").intValue());
            entity.setAverageStayTime(numMap.get("average_stay_time").intValue());
            entity.setExitRatio(numMap.get("exit_ratio"));
            entity.setDateStr(DateUtil.string2Date(dateStr, DateUtil.YYYYMMDD_DATA_FORMAT));
            entity.setRecStatus("A");
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            if(sceneNum.contains("_tag")){
                entity.setType(2);
                entity.setSceneNum(sceneNum.replace("_tag", ""));
            } else if(sceneNum.contains("_video")){
                entity.setType(3);
                entity.setSceneNum(sceneNum.replace("_video", ""));
            }else {
                entity.setType(1);
                entity.setSceneNum(sceneNum);
            }
            this.save(entity);
        }
    }
}
