package org.jeecg.modules.adweb.marketing.googleads.vo;

import lombok.Data;

import org.jeecg.modules.adweb.common.constant.NumConstant;
import org.jeecg.modules.adweb.common.util.NumberUtil;
import org.jeecg.modules.adweb.marketing.googleads.dto.MetricsDTO;

import java.util.Map;

/**
 * @author wfansh
 */
@Data
public class DailyStatsVO {

    private String date; // 日期

    private long impressions; // 展示数

    private long clicks; // 点击数

    private double ctr; // 点击率

    private double conversions; // 转化数

    private double cost; // 花费

    public static DailyStatsVO fromMetricsDTO(Map.Entry<String, MetricsDTO> metricsDTOEntry) {
        DailyStatsVO dailyStatsVO = new DailyStatsVO();

        dailyStatsVO.setDate(metricsDTOEntry.getKey());

        MetricsDTO metricsDTO = metricsDTOEntry.getValue();
        dailyStatsVO.setImpressions(metricsDTO.getImpressions());
        dailyStatsVO.setClicks(metricsDTO.getClicks());
        dailyStatsVO.setCtr(NumberUtil.formatDecimal(metricsDTO.getCtr(), 4).doubleValue());
        dailyStatsVO.setConversions(
                NumberUtil.formatDecimal(metricsDTO.getConversions(), 2).doubleValue());
        dailyStatsVO.setCost(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        metricsDTO.getCostMicros(), NumConstant.ONE_MILLION),
                                2)
                        .doubleValue());

        return dailyStatsVO;
    }
}
