package com.awspaas.user.apps.donenow_ivt.job;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.donenow_ivt.cache.formStatusCache;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;
import java.util.List;

public class clearOrderJob implements Job {
    /**
     *
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<RowMap> list = DBSql.getMaps("SELECT * FROM BO_EU_DNIVT_ORDER WHERE (VENDOR_ACCOUNT_ID IS NULL OR VENDOR_ACCOUNT_ID='') AND (PURCHASE_ACCOUNT_ID IS NULL OR PURCHASE_ACCOUNT_ID='') ORDER BY CREATEDATE");
        Long CURRENT_TIME = new Date().getTime();

        formStatusCache cache = formStatusCache.getInstance();

        for (RowMap rowMap : list) {
            Date CREATEDATE = rowMap.getDate("CREATEDATE");
            //跳过创建5分钟之内的
            if (CURRENT_TIME - CREATEDATE.getTime() < 5 * 60 * 1000) {
                continue;
            }

            //1天前的直接删除
            if (CURRENT_TIME - CREATEDATE.getTime() > 24 * 60 * 60 * 1000) {
                SDK.getProcessAPI().deleteById(rowMap.getString("BINDID"), "admin");
            }

            Date UPDATE_TIME = cache.get(rowMap.getString("BINDID"));
            if (UPDATE_TIME == null) {
                SDK.getProcessAPI().deleteById(rowMap.getString("BINDID"), "admin");
            } else {
                if (CURRENT_TIME - UPDATE_TIME.getTime() > 5 * 60 * 1000) {
                    SDK.getProcessAPI().deleteById(rowMap.getString("BINDID"), "admin");
                }
            }
        }
    }
}
