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

import cn.com.ctop.common.module.utils.Check;
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.batch.entity.KuaiShouCreative;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyImage;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyImageMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportDailyImageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.K;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快手图片报表
 * @author jeecg-boot
 * @date   2020-09-28
 * @version V1.0
 */
@Slf4j
@Service
public class KuaishouReportDailyImageServiceImpl extends ServiceImpl<KuaishouReportDailyImageMapper, KuaishouReportDailyImage> implements IKuaishouReportDailyImageService {

    @Autowired
    KuaishouReportDailyImageMapper kuaishouReportDailyImageMapper;

    @Override
    public void getMaterialImageReport(Long accountId, String accessToken, String startDate, String endDate, Integer page) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", " application/json");
        headers.put("Access-Token", accessToken);

        JSONObject param = new JSONObject();
        param.put("advertiser_id", accountId);
        param.put("start_date",startDate);
        param.put("end_date",endDate);
        param.put("view_type",7);
        param.put("temporal_granularity","DAILY");
        param.put("page_size", 500);
        param.put("page", page);

        JSONObject resultJson = JSONObject.parseObject(HttpUtils.kuaiShouhttpPostRequest("https://ad.e.kuaishou.com/rest/openapi/v1/report/material_report", param.toJSONString(), headers));
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (Check.isNull(resultJson)) {
            log.error("获取素材图片报表返回结果为空,advertiserId:{}", accountId);
            return;
        }

        JSONArray details = resultJson.getJSONArray("details");
        if (null == details || details.size() <= 0) {
            log.info(resultJson.toJSONString());
            return;
        }
        List<KuaishouReportDailyImage> imageReports = new ArrayList<>();
        details.forEach(detail->{
            JSONObject detailJSON = (JSONObject) detail;
            if (!Check.isNull(detailJSON)) {
                detailJSON.put("photo_like", detailJSON.getInteger("like"));
                detailJSON.put("photo_share", detailJSON.getInteger("share"));
                detailJSON.put("photo_comment", detailJSON.getInteger("comment"));
                detailJSON.put("photo_show", detailJSON.getInteger("show"));
                KuaishouReportDailyImage kuaishouReportDailyImage = JSONObject.toJavaObject(detailJSON,KuaishouReportDailyImage.class);
                kuaishouReportDailyImage.setAccountId(accountId);
                imageReports.add(kuaishouReportDailyImage);
            }
        });
        this.saveBatch(imageReports);
        if(resultJson.getInteger("total_count")>(1000*page)){
            getMaterialImageReport(accountId, accessToken, startDate, endDate, page + 1);
        }
    }

}
