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


import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouPlanCreateService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

import static org.jeecg.common.util.DateUtils.getEndDate;
import static org.jeecg.common.util.DateUtils.getStartDate;


@Component
@Slf4j
public class KuaishouCleanNews {
    @Autowired
    private IUserAllocationService userAllocationService;
    static ExecutorService cleanService = Executors.newFixedThreadPool(15);
    @Autowired
    private IKuaishouPlanCreateService planCreateService;

    @XxlJob("cleanNews")
    public void cleanNews() {
        String nowDate = DateUtils.getDate("yyyy-MM-dd");
        String statDate = DateUtils.getAnotherDay("yyyy-MM-dd", nowDate, -1);
        Date startDate = getStartDate(statDate);
        Date endDate = getEndDate(statDate);
        List<UserAllocation> userAllocations = userAllocationService.listByMediaId("2", 0);
        if (!Check.isNull(userAllocations)) {
            for (UserAllocation allocation : userAllocations) {
                cleanService.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            planCreateService.cleanCreateDetail(allocation.getProjectId(), allocation.getUserId(), allocation.getUserName(), allocation.getAccountId(), statDate, startDate, endDate);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
}

