package com.community.huiju.config;

import com.community.huiju.dao.TaUserLicenseOrderMapper;
import com.community.huiju.dao.TpBillInvoiceMapper;
import com.community.huiju.dao.TpBillOrderMapper;
import com.community.huiju.model.TaUserLicenseOrder;
import com.community.huiju.model.TpBillInvoice;
import com.community.huiju.model.TpBillOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时器
 * @author weiximei
 */
@Component
@Slf4j
public class BillQuartz {

    @Autowired
    private TpBillOrderMapper tpBillOrderMapper;

    @Autowired
    private TpBillInvoiceMapper tpBillInvoiceMapper;

    @Autowired
    private TaUserLicenseOrderMapper taUserLicenseOrderMapper;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void billInvoice() {
        log.info("定时任务，扫描订单，超过 10 分未付款则，关闭订单！");
        // 物业缴费
        List<TpBillOrder> billOrderList = tpBillOrderMapper.selectCloseOrder();
        if (!billOrderList.isEmpty()) {
            // 把缴费单变为未交费
            billOrderList.forEach(e->{
                TpBillInvoice tpBillInvoice = tpBillInvoiceMapper.selectByPrimaryKey(e.getTpBillInvoiceId());
                tpBillInvoice.setBillStatus("0");
                tpBillInvoiceMapper.updateByPrimaryKeySelective(tpBillInvoice);
            });

            // 关闭订单
            billOrderList.forEach(e -> {
                e.setOrderStatus("3");
                tpBillOrderMapper.updateByPrimaryKeySelective(e);
            });

        }

        // 车缴费订单
        List<TaUserLicenseOrder> userLicenseOrders = taUserLicenseOrderMapper.selectCloseOrder();
        if (!userLicenseOrders.isEmpty()) {
            userLicenseOrders.forEach(e-> {
                e.setOrderStatus("3");
                taUserLicenseOrderMapper.updateByPrimaryKeySelective(e);
            });
        }


    }

}
