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.CampaignStatsDTO;
import org.jeecg.modules.adweb.marketing.googleads.dto.MetricsDTO;

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

    private String name; // 广告系列名称

    private String status; // 状态

    private String advertisingChannelType; // 类型

    private String biddingStrategyType; // 出价策略

    private double budget; // 每日预算

    private long impressions; // 展示数

    private long clicks; // 点击数

    private double ctr; // 点击率

    private double averageCpc; // 平均点击费用

    private double averageCpm; // 千次展示费用

    private double conversions; // 转化数

    private double cost; // 花费

    public static CampaignStatsVO fromCampaignStatsDTO(CampaignStatsDTO campaignStatsDTO) {
        CampaignStatsVO campaignStatsVO = new CampaignStatsVO();

        campaignStatsVO.setName(campaignStatsDTO.getName());
        campaignStatsVO.setStatus(campaignStatsDTO.getStatus());
        campaignStatsVO.setAdvertisingChannelType(campaignStatsDTO.getAdvertisingChannelType());
        campaignStatsVO.setBiddingStrategyType(campaignStatsDTO.getBiddingStrategyType());
        campaignStatsVO.setBudget(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        campaignStatsDTO.getBudgetMicros(),
                                        NumConstant.ONE_MILLION),
                                2)
                        .doubleValue());

        MetricsDTO metricsDTO = campaignStatsDTO.getMetrics();
        campaignStatsVO.setImpressions(metricsDTO.getImpressions());
        campaignStatsVO.setClicks(metricsDTO.getClicks());
        campaignStatsVO.setCtr(NumberUtil.formatDecimal(metricsDTO.getCtr(), 4).doubleValue());
        campaignStatsVO.setAverageCpc(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        metricsDTO.getAverageCpc(), NumConstant.ONE_MILLION),
                                2)
                        .doubleValue());
        // Note: The original 'averageCpm' value is invalid and will be recalculated in place.
        campaignStatsVO.setAverageCpm(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        metricsDTO.getCostMicros(),
                                        Long.valueOf(metricsDTO.getImpressions()).intValue()
                                                * NumConstant.ONE_THOUSAND),
                                2)
                        .doubleValue());
        campaignStatsVO.setConversions(
                NumberUtil.formatDecimal(metricsDTO.getConversions(), 2).doubleValue());
        campaignStatsVO.setCost(
                NumberUtil.formatDecimal(
                                NumberUtil.safeDivide(
                                        metricsDTO.getCostMicros(), NumConstant.ONE_MILLION),
                                2)
                        .doubleValue());

        return campaignStatsVO;
    }
}
