package org.dromara.payment.timer;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.payment.invoice.domain.bo.MerInvoiceBo;
import org.dromara.payment.invoice.domain.vo.MerInvoiceVo;
import org.dromara.payment.invoice.service.IMerInvoiceService;
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;

/**
 * 发票快递自动签收任务
 */
@Component
public class InvoiceIogisticsReceivingJob {

    @Autowired
    private IMerInvoiceService invoiceService;

    @XxlJob("InvoiceIogisticsReceivingJob")
    public void process() throws Exception {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setOrderByColumn("iogistics_time");
        pageQuery.setIsAsc("asc");
        pageQuery.setPageSize(50);
        pageQuery.build();
        XxlJobHelper.log("开始自动签收发票快递");
		try {
            MerInvoiceBo where = new MerInvoiceBo();
            where.setIogisticsStatus(2);
            TableDataInfo<MerInvoiceVo> pageData = null;
			do {
                pageData = invoiceService.queryPageList(where,pageQuery);
				if(pageData.getRows().size() > 0) {
					for (MerInvoiceVo inv : pageData.getRows()) {
                        try {
                            invoiceService.receivingIogistics(inv.getId(),"系统");
                        } catch (Exception e) {
                            XxlJobHelper.log("自动签收发票快递失败（" + inv.getId() + "）：",e);
                        }
                    }
                    pageQuery.setPageNum(pageQuery.getPageNum()+1);
				}
			} while (pageData.getRows().size() == pageQuery.getPageNum());
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
            XxlJobHelper.log("自动签收发票快递出错（" + pageQuery.getPageNum() + "," + pageQuery.getPageSize() + "）：",e);
		}
        XxlJobHelper.log("结束自动签收发票快递");
	}
}
