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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.entity.KuaiShouProjectData;
import cn.com.ctop.kuaishou.modules.report.service.IKuaiShouProjectDataService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouReportHourlyGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 获取 广告组分时数据
 *
 * @author syh
 */
@Component
public class KuaishouHourlyGroupReportLoadJob {
    @Autowired
    private ICtopOauthTokenService tokenService;
    static ExecutorService executorService = Executors.newFixedThreadPool(5);
    @Autowired
    private IKuaiShouProjectDataService kuaiShouProjectDataService;
    @Autowired
    private IUserAllocationService allocationService;
    @Autowired
    private IKuaishouReportHourlyGroupService hourlyGroupService;


    /**
     * 广告组时报
     * @throws Exception
     */
    @XxlJob("kuaishouHourlyGroupReport")
    public void execute() throws Exception {
        //增加接口的开始和结束日期
        String nowDate = DateUtils.getNowDate("yyyy-MM-dd");
        String yesterday = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        //1:查询当日数据
        QueryWrapper<KuaiShouProjectData> queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_status", 0);
        List<KuaiShouProjectData> list = kuaiShouProjectDataService.list(queryWrapper);
        if (Check.isNull(list)) {
            return;
        }
        for (KuaiShouProjectData projectData : list) {
            Long projectId = projectData.getProjectId();
            if (Check.isNull(projectId)) {
                continue;
            }
            QueryWrapper<UserAllocation> allocationQueryWrapper = new QueryWrapper<>();
            allocationQueryWrapper.eq("project_id", projectId);
            allocationQueryWrapper.eq("account_status", 0);
            List<UserAllocation> accountIds = allocationService.list(allocationQueryWrapper);
            if (Check.isNull(accountIds)) {
                continue;
            }
            for (UserAllocation allocation : accountIds) {
                CtopOauthToken token = tokenService.getTokenByAccountId(allocation.getAccountId());
                if (Check.isNull(token)) {
                    continue;
                }

                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        hourlyGroupService.getAdvertiserGroupReportHourly(token.getAccountId(), token.getAccessToken(), yesterday, yesterday);
                    }
                });
            }
        }
    }
}
