package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.BindAccountLogin;
import cn.com.ctop.common.module.service.IBindAccountLoginService;
import cn.com.ctop.kuaishou.modules.graphql.service.IKuaishouWebInterfaceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.List;

@Slf4j
public class KuaishouCommentAutoDelete implements Job {
    @Value("${chrome.script.clean}")
    private String cleanUrl;
    @Autowired
    private IKuaishouWebInterfaceService kuaishouWebInterfaceService;
    @Autowired
    private IBindAccountLoginService bindAccountLoginService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Thread thread = new Thread() {
            @Override
            public void run() {
                QueryWrapper<BindAccountLogin> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("login_type", "kuaishou");
                queryWrapper.eq("status", 1);
                List<BindAccountLogin> list = bindAccountLoginService.list(queryWrapper);
                if (list != null && list.size() > 0) {
                    for (BindAccountLogin account : list) {
                        try {
                            log.info("正在删除评论：{}", account.getAccountName());
                            kuaishouWebInterfaceService.adkuaishouWebLogin(account.getAccountName(), account.getPassword());
                            kuaishouWebInterfaceService.deleteAllComment(new HashMap<>());
                            Runtime runtime = Runtime.getRuntime();
                            Process pro = runtime.exec(cleanUrl);
                            int status = pro.waitFor();
                            if (status == 0) {
                                log.info("success，删除评论调用shell脚本执行成功");
                            } else {
                                log.info("error，删除评论调用shell脚本执行失败");
                            }
                            Thread.sleep(5 * 1000L);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        thread.start();

    }
}
