package com.pj.tb_goods;

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 15 52
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodGoodsService {

    @Autowired
    private TbGoodsService tbGoodsService;


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

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

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            //封装数据到行对象
            row = sheet.getRow(i);
            //定义返回值实体
            TbGoods tbGoods = new TbGoods();
            //解析数据
            if(row.getCell(1) != null)
                tbGoods.setGName(row.getCell(1).getStringCellValue());
            if(row.getCell(2) != null)
                tbGoods.setAvatar(row.getCell(2).getStringCellValue());
            if(row.getCell(3) != null)
                tbGoods.setTypeIds(row.getCell(3).getStringCellValue());
            if(row.getCell(4) != null)
                tbGoods.setTypeNames(row.getCell(4).getStringCellValue());
            if(row.getCell(5) != null)
                tbGoods.setHsCode(row.getCell(5).getStringCellValue());
            if(row.getCell(6) != null)
                tbGoods.setRemark(row.getCell(6).getStringCellValue());
            if(row.getCell(7) != null)
                tbGoods.setSource(row.getCell(7).getStringCellValue());

            HSSFCell cell8 = row.getCell(8);
            if (cell8 != null && (cell8.getCellType() == CellType.NUMERIC || cell8.getCellType() == CellType.FORMULA))
                tbGoods.setStatus((int)row.getCell(9).getNumericCellValue());

            if(row.getCell(9) != null)
                tbGoods.setGUnit(row.getCell(9).getStringCellValue());

            HSSFCell cell11 = row.createCell(10);
            if (cell11 != null && (cell11.getCellType() == CellType.NUMERIC || cell11.getCellType() == CellType.FORMULA))
                tbGoods.setSinglePrice(row.getCell(11).getNumericCellValue());

            if(row.getCell(11) != null)
                tbGoods.setTaxNo(row.getCell(11).getStringCellValue());

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

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

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

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

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

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

        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<TbGoods> enterpriseList = list.subList(start, end);

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

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

        return count;
    }

}
