package org.dromara.payment.timer;

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

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.payment.bill.domain.MerBillDetail;
import org.dromara.payment.bill.mapper.MerBillDetailMapper;
import org.dromara.payment.channel.mapper.BusChannelAccountMapper;
import org.dromara.payment.common.domain.TranLog;
import org.dromara.payment.common.domain.vo.TranLogVo;
import org.dromara.payment.common.mapper.TranLogMapper;
import org.dromara.payment.common.service.ITranLogService;
import org.dromara.payment.merchant.mapper.BusMerchantAccountMapper;
import org.dromara.payment.sys.domain.SysSydAccount;
import org.dromara.payment.sys.mapper.SysSydAccountMapper;
import org.dromara.settlement.api.RemoteAccountService;
import org.dromara.settlement.api.domain.Result;
import org.dromara.settlement.api.domain.TransactionLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;
import tech.powerjob.worker.log.OmsLogger;

/**
 * 处理银行交易任务
 * @author 张涛
 * @date 2021年9月3日
 */
@Component
public class BankTransactionJob {
    @Autowired
    private TranLogMapper synDao;

    @Autowired
    private ITranLogService tranLogService;

    @XxlJob("BankTransactionJob")
    public void process() throws Exception {
		long minId = 0;
		int limit = 10;
        XxlJobHelper.log("开始处理银行交易");
		String synBatch = null;
		Integer row = 0;
		try {
			synBatch = DateUtils.parseDateToString("yyyyMMddHHmmssSSS", new Date());
			do {
				//分配交易的处理批次
				row = synDao.allotSyns(synBatch,-1,null,minId,0,limit);
				if(row > 0) {
					List<TranLogVo> schools = synDao.selectTranLog(synBatch,-1,null, null);
					for (TranLogVo accid : schools) {
                        try {
                            tranLogService.transaction(accid,false);
                        } catch (Exception e) {
							e.printStackTrace();
                            XxlJobHelper.log("处理银行交易出错（" + minId + "," + limit + "," +JSONObject.toJSONString(accid) + "）："+e.getMessage());
                        }
                        synDao.cancelSyn(accid.getId());
						minId = accid.getId();
					}
				}
			} while (row == limit);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
            XxlJobHelper.log("处理银行交易记录出错（" + minId + "," + limit + "）：",e);
		}finally {
			if(row > 0) {
				try {
					synDao.cancelSynByBatch(synBatch, -1);
				} catch (Exception e) {
                    XxlJobHelper.log("处理银行交易时解锁出错（" + minId + "," + limit + "," + synBatch + "）：", e);
				}
			}
		}
        XxlJobHelper.log("结束处理银行交易");
	}
}
