package org.jeecg.modules.ctop.job;

import cn.com.ctop.common.module.entity.Project;
import cn.com.ctop.common.module.service.IProjectService;
import cn.com.ctop.common.module.utils.BigDecimalUtil;
import cn.com.ctop.common.module.utils.Check;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.ctop.service.ICpaBidWarningService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * DONE
 * 深度转化出价预警
 */
public class AccountCpaBidJob implements Job {
    @Autowired
    private ICpaBidWarningService cpaBidWarningService;
    static ExecutorService executorService = null;
    @Autowired
    private IProjectService projectService;

    /**
     * 出价预警
     *
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                executorService = Executors.newFixedThreadPool(4);
                QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
                List<Project> projects = projectService.list(queryWrapper);
                if (!Check.isNull(projects)) {
                    for (Project project : projects) {
                        if (BigDecimalUtil.compareToDecimal(project.getMaxDeepCpaBid(), new BigDecimal(0)) <= 0) {
                            continue;
                        }
                        executorService.submit(new Runnable() {
                            @Override
                            public void run() {
                                cpaBidWarningService.cpaBidWarning(project);
                            }

                        });
                    }
                    executorService.shutdown();
                }
            }
        };
        thread.start();

    }
}
