package cn.com.ctop.job.kuaishou.handler;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouStrategyService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouVideoRelateCreativesService;
import com.alibaba.fastjson.JSONArray;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 频库-视频关联创意数查询
 *
 * @author ZXA
 */
@Component
public class KuaishouVideoRelateCreativesJob {
    @Autowired
    private IKuaishouVideoRelateCreativesService kuaishouVideoRelateCreativesService;
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IKuaishouStrategyService kuaishouStrategyService;

    static ExecutorService executorService = Executors.newFixedThreadPool(5);

    /**
     * 视频库-更新视频关联创意数
     *
     * @throws Exception
     */
    @XxlJob("kuaishouVideoRelateCreativesJob")
    public void execute() throws Exception {
        List<CtopOauthToken> tokens = tokenService.selectKuaiShouToken();
        if (null == tokens || tokens.isEmpty()) {
            XxlJobHelper.log("未获取到可用的token");
            XxlJobHelper.handleFail();
            return;
        }
        //查询有效策略的账户ID
        List<Long> accountIds = kuaishouStrategyService.queryActiveAccount();
        if (Check.isNull(accountIds) || accountIds.size() == 0) {
            XxlJobHelper.log("未获取到有效策略账户信息");
            XxlJobHelper.handleFail();
            return;
        }
        //筛选出有效账户，获取该账户的组级人群报表数据
        List<CtopOauthToken> activeTokens = new ArrayList<>();
        for (Long accountId : accountIds) {
            for (CtopOauthToken token : tokens) {
                if (token.getAccountId() - accountId == 0) {
                    activeTokens.add(token);
                }
            }
        }
        if (!Check.isNull(activeTokens) && activeTokens.size() > 0) {
            activeTokens.forEach(token -> executorService.submit(() -> {
                videoRelateCreatives(token.getAccountId());
            }));
        }
    }

    /**
     * 逐个账户数据处理
     *
     * @param
     * @return void
     * @throws
     * @author ZHAOXA
     */
    private void videoRelateCreatives(Long accountId) {
        List<KuaiShouVideoGet> KuaiShouVideoGets = videoGetService.selectPhotoIdsByAccountId(accountId);
        if (!Check.isNull(KuaiShouVideoGets)) {
            JSONArray dateList = new JSONArray();
            JSONArray photoIds = new JSONArray();
            Map<String, KuaiShouVideoGet> statdateMap = new HashMap<>();
            int count = 0;
            for (KuaiShouVideoGet video : KuaiShouVideoGets) {
                count++;
                if (count < 19) {
                    photoIds.add(video.getPhotoId());
                    statdateMap.put(video.getPhotoId(), video);
                } else {
                    photoIds.add(video.getPhotoId());
                    statdateMap.put(video.getPhotoId(), video);
                    dateList.add(photoIds);
                    photoIds = new JSONArray();
                    count = 0;
                }
            }
            if (photoIds.size() > 0) {
                dateList.add(photoIds);
            }
            kuaishouVideoRelateCreativesService.videoRelateCreatives(accountId, dateList, statdateMap);
        }
    }
}
