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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.kuaishou.modules.batch.entity.vo.KuaiShouVideoGetVo;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouReportDailyMaterialMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouReportDailyMaterialService;
import cn.com.ctop.kuaishou.modules.report.entity.KuaiShouReportDailyMaterial;
import cn.com.ctop.kuaishou.modules.report.entity.VideoGetvo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
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
 * @version V1.0
 * @date 2020-09-09
 */
@Slf4j
@Service
public class KuaiShouReportDailyMaterialServiceImpl extends ServiceImpl<KuaiShouReportDailyMaterialMapper, KuaiShouReportDailyMaterial> implements IKuaiShouReportDailyMaterialService {
    @Autowired
    private KuaiShouReportDailyMaterialMapper dailyMaterialMapper;

    @Override
    public List<KuaiShouVideoGetVo> getVideoList(Map<String, Object> requestMap) {
        return dailyMaterialMapper.getVideoList(requestMap);
    }

    /**
     * 获取素材数据
     *
     * @param accountId
     * @param startDate
     * @param endDate
     */
    @Override
    public void getMaterialReportByAccountIdAndStatDate(Long accountId, String token, String startDate, String endDate, Integer page) {
        String url = "https://ad.e.kuaishou.com/rest/openapi/v1/report/material_report";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token);
        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", "DAILY");
        param.put("view_type", 5);
        param.put("page_size", 1000);
        param.put("page", page);

        String result = HttpUtils.httpPostRequest(url, param, headers);
        JSONObject resultJson = JSONObject.parseObject(result);
    //    log.info("快手返回数据：{}",resultJson);
        if (Check.isNull(resultJson)) {
            return;
        }

        Integer total_count = resultJson.getInteger("total_count");
        if (total_count == 0) {
            return;
        }
        JSONArray details = resultJson.getJSONArray("details");
        if (Check.isNull(details)) {
            return;
        }

        List<KuaiShouReportDailyMaterial> addList = new ArrayList<>();
        long l1 = System.currentTimeMillis();
        for (int i = 0; i < details.size(); i++) {
            JSONObject detailJson = details.getJSONObject(i);
            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"));
            KuaiShouReportDailyMaterial material = JSONObject.toJavaObject(detailJson, KuaiShouReportDailyMaterial.class);
            VideoGetvo videoGetvo = dailyMaterialMapper.getVideoGetByPhotoId(material.getPhotoId());
            if (!Check.isNull(videoGetvo)) {
                material.setChannelType(videoGetvo.getChannelType());
                material.setSignature(videoGetvo.getSignature());
            }
            material.setAccountId(accountId);

            addList.add(material);
        }
  //      log.info("addList：{}",addList);
        dailyMaterialMapper.batchReplace(addList);
        getMaterialReportByAccountIdAndStatDate(accountId, token, startDate, endDate, page + 1);
    }

    @Override
    public List<KuaiShouReportDailyMaterial> getSignatureByDate(String date) {
        return dailyMaterialMapper.getSignatureByDate(date);
    }

    @Override
    public KuaiShouReportDailyMaterial getEtlDataByParams(String signature, String date,Long projectId) {
        return dailyMaterialMapper.getEtlDataByParams(signature,date,projectId);
    }

    @Override
    public List<KuaiShouReportDailyMaterial> getSignatureByDateGroupByProjectId(String date) {
        return dailyMaterialMapper.getSignatureByDateGroupByProjectId(date);
    }
}
