package cn.pluss.platform.task;

import cn.pluss.platform.bill.BillSendRecordService;
import cn.pluss.platform.entity.BillSendRecord;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author nb2020
 */
@Slf4j
@Component
public class BillStatisticsHandler {

    @Setter(onMethod_ = {@Lazy, @Autowired})
    public BillSendRecordService billSendRecordService;

    @Async
    public void execute(BillSendRecord entity) {
        if (entity == null) {
            return;
        }

        if ("1".equals(entity.getStatus())) {
            log.info("账单已处理，无须再操作");
            return;
        }
        XSSFWorkbook workbook = billSendRecordService.createBill(entity);

        try {
            billSendRecordService.sendBill(entity, workbook);
            entity.setStatus("1");
            billSendRecordService.updateById(entity);
        } catch (Exception e) {
            log.error("账单生成失败");
            e.printStackTrace();
            entity.setStatus("2");
            billSendRecordService.updateById(entity);
        }

    }
}
