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

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.KuaiShouAdvertiserBaseInfo;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouAdvertiserBaseInfoService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.batch.service.ISyncPullMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 异步获取素材信息
 */

@Service
public class SyncPullMaterialServiceImpl implements ISyncPullMaterialService {
    @Autowired
    private IKuaiShouAdvertiserBaseInfoService KuaiShouAdvertiserBaseInfoService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IKuaishouInterfaceService iKuaishouInterfaceService;
    static ExecutorService videoExecutorService = Executors.newFixedThreadPool(10);
    static ExecutorService suZhaoExecutorService = Executors.newFixedThreadPool(10);


    /**
     * 获取一户多开所有视频信息
     *
     * @param accountId
     * @param startDate
     * @param endDate
     */
    @Override
    public void getSyncVideoList(Long accountId, String startDate, String endDate) {
        KuaiShouAdvertiserBaseInfo baseInfo = KuaiShouAdvertiserBaseInfoService.getBaseInfo(accountId);
        if (!Check.isNull(baseInfo)) {
            List<KuaiShouAdvertiserBaseInfo> baseInfoList = KuaiShouAdvertiserBaseInfoService.getAccountIdList(baseInfo.getUserId());
            if (!Check.isNull(baseInfoList)) {
                for (KuaiShouAdvertiserBaseInfo advertiserBaseInfo : baseInfoList) {
                    CtopOauthToken token = oauthTokenService.getTokenByAccountId(advertiserBaseInfo.getAccountId());
                    if (!Check.isNull(token)) {
                        videoExecutorService.submit(new Runnable() {
                            @Override
                            public void run() {
                                iKuaishouInterfaceService.getVideoList(token, startDate, endDate);
                            }
                        });
                    }
                }
            }
        }
    }


}
