package com.pj.tb_trade_area;

import com.pj.tb_goods.TbGoods;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Mechrevo
 * @Date 2023 07 21 21 01
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodTradeAreaService {
    
    @Autowired
    private TbTradeAreaService tbTradeAreaService;


    /**
     * 方法抽取  导入
     * @param row
     * @param sheet
     * @param count
     * @return
     */
    public int importMethod(HSSFRow row, HSSFSheet sheet, int count){

        //创建接收集合
        List<TbTradeArea> list = new ArrayList<>();

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            //封装数据到行对象
            row = sheet.getRow(i);
            //定义返回值实体
            TbTradeArea tbTradeArea = new TbTradeArea();
            //解析数据
            if(row.getCell(1) != null)
                tbTradeArea.setName(row.getCell(1).getStringCellValue());

            if(row.getCell(2) != null)
                tbTradeArea.setAddressIds(row.getCell(2).getStringCellValue());

            if(row.getCell(3) != null)
                tbTradeArea.setDetailAddress(row.getCell(3).getStringCellValue());

            if(row.getCell(4) != null)
                tbTradeArea.setAddress(row.getCell(4).getStringCellValue());

            //公共字段
            HSSFCell cell2 = row.getCell(5);
            if (cell2 != null && cell2.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell2)) {
                tbTradeArea.setCreateTime(cell2.getDateCellValue());
            }

            if(row.getCell(6) != null && !row.getCell(6).toString().trim().equals(""))
                tbTradeArea.setCreateBy(row.getCell(6).getStringCellValue());

            if(row.getCell(7) != null && !row.getCell(7).toString().trim().equals(""))
                tbTradeArea.setCreateName(row.getCell(7).getStringCellValue());

            if(row.getCell(8) != null && !row.getCell(8).toString().trim().equals(""))
                tbTradeArea.setUpdateBy(row.getCell(8).getStringCellValue());

            if(row.getCell(9) != null && !row.getCell(9).toString().trim().equals(""))
                tbTradeArea.setUpdateName(row.getCell(9).getStringCellValue());

            HSSFCell cell3 = row.getCell(10);
            if (cell3 != null && cell3.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell3)) {
                tbTradeArea.setUpdateTime(cell3.getDateCellValue());
            }
            //删除状态,默认可用
            tbTradeArea.setDeleteStatus(1);
			//添加到集合
			list.add(tbTradeArea);

        }

        System.out.println("待处理数据量为: " + list.size() + " 条。");

        //创建多线程
        ExecutorService executorService = Executors.newFixedThreadPool(6);

        //每次100条
        int bai = 100;
        //计算需要创建的线程数
        int threads = (int) Math.ceil((double) list.size() / bai);
        for (int i = 0; i < threads; i++) {
            int start = i * bai;
            int end = Math.min(start + bai,list.size());

            //截取集合数量
            List<TbTradeArea> enterpriseList = list.subList(start, end);

            //多线程执行任务
            executorService.submit(() -> {
                tbTradeAreaService.saveBatch(enterpriseList);
            });
            //计数
            count += enterpriseList.size();

        }
        //关闭线程池
        executorService.shutdown();

        return count;
    }
    
}
