package com.hwrj.cloud.admin.listener;

import com.hwrj.cloud.admin.excelEntity.UmsMemberOriginExcel;
import com.hwrj.cloud.admin.service.UmsMemberOriginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @className:
 * @description: 大批量导入异步事件
 * @author: lym
 * @date: 2020/9/6 15:24
 */
@Component
public class ImportExcelEventListener {

    @Autowired
    private UmsMemberOriginService umsMemberOriginService;

    /**
     * 批量添加生产地信息
     * lym
     * @param list
     */
    @Async
    public void batchSaveOriginExcel(List<UmsMemberOriginExcel> list){
        if (list == null && list.size() < 1) return;
        int total = list.size(),i = 0,saveNum = 500; //名次添加500
        List<UmsMemberOriginExcel> saveList = new ArrayList<>();

        while (total > saveNum) {
            saveList = list.subList(i, i + saveNum);
            i = i + saveNum;
            total = total - saveNum;
            umsMemberOriginService.batchSave(saveList);
        }
        if(total > 0) {
            saveList = list.subList(i, i + total);
            umsMemberOriginService.batchSave(saveList);
        }
    }

}
