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

import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCommentService;
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.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class KuaiShouDeleteCommentJob {

    @Autowired
    private IUserAllocationService allocationService;

    @Autowired
    private IKuaiShouCommentService kuaiShouCommentService;

    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    static CountDownLatch countDownLatch = null;

    //TODO 待删除
    @XxlJob("kuaiShouDeleteCommentJob")
    public void kuaiShouDeleteComment(){
        Long start = System.currentTimeMillis();
        //查询需要删除的账户
        List<Long> accountIds = allocationService.getAccountsByDeleteComment();
        if (!accountIds.isEmpty()) {
            countDownLatch = new CountDownLatch(accountIds.size());
            accountIds.forEach(accountId -> executorService.submit(() -> {
                try {
                    kuaiShouCommentService.shieldComment(accountId);
                } catch (Exception e) {
                    XxlJobHelper.log(e.getMessage(), e);
                } finally {
                    countDownLatch.countDown();
                }
            }));
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Long end = System.currentTimeMillis();
            XxlJobHelper.log("快手删评论所用时长：{}毫秒", end - start);
        }
    }
}
