package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportHourlyGroup;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportHourlyGroupMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportHourlyGroupService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class KuaishouReportHourlyGroupServiceImpl extends ServiceImpl<KuaishouReportHourlyGroupMapper, KuaishouReportHourlyGroup> implements IKuaishouReportHourlyGroupService {
    @Autowired
    private KuaishouReportHourlyGroupMapper groupMapper;


    @Override
    public void getAdvertiserGroupReportHourly(Long accountId, String accessToken, String startDate, String endDate) {
        getGroupHourlyReportByPage(accountId, accessToken, startDate, endDate, 1);
    }

    private void getGroupHourlyReportByPage(Long accountId, String accessToken, String startDate, String endDate, int page) {
        String url = PropertiesUtils.getConfig("kuaishou_api_url") + KuaishouInterfaceConstant.GTOUP_REPORT;
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", accessToken);
        Map<String, Object> param = new HashMap<>();
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("advertiser_id", accountId);
        param.put("temporal_granularity", "HOURLY");
        param.put("page_size", 2000);
        param.put("page", page);

        String result = HttpUtils.httpPostRequest(url, param, headers);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JSONObject resultJson = JSONObject.parseObject(result);
        Integer code = resultJson.getInteger("code");
        String message = resultJson.getString("message");
        if (null == code || code != 0) {
            log.error("获取快手广告组时报表异常:{},accountId:{}", message, accountId);
            return;
        }
        JSONArray details = resultJson.getJSONObject("data").getJSONArray("details");
        if (null == details || details.size() <= 0) {
            log.info("快手广告组时报表信息为空=》accountId:{}", accountId);
            return;
        }
        List<KuaishouReportHourlyGroup> groups = new ArrayList<>();
        for (int i = 0; i < details.size(); i++) {
            JSONObject detailJson = details.getJSONObject(i);
            var show = detailJson.getLong("show");
            detailJson.put("photo_show", show);
            var like = detailJson.getLong("like");
            detailJson.put("photo_like", like);
            var hourlyGroup = JSONObject.toJavaObject(detailJson, KuaishouReportHourlyGroup.class);
            hourlyGroup.setAccountId(accountId);
            hourlyGroup.setId("" + hourlyGroup.getAccountId() + hourlyGroup.getUnitId() + hourlyGroup.getStatDate() + hourlyGroup.getStatHour());
            hourlyGroup.setCreateTime(new Date());
            hourlyGroup.setUpdateTime(new Date());
            groups.add(hourlyGroup);
        }
        groupMapper.replaceBatch(groups);

        getGroupHourlyReportByPage(accountId, accessToken, startDate, endDate, page + 1);
    }


    @Override
    public void replaceBatch(List<KuaishouReportHourlyGroup> groups) {
        groupMapper.replaceBatch(groups);
    }
}
