package com.pj.tb_group;


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 17 38
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodGroupService {
    
    @Autowired
    private TbGroupService tbGroupService;



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

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

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            //封装数据到行对象
            row = sheet.getRow(i);
            //定义返回值实体
            TbGroup tbGroup = new TbGroup();
            //解析数据
            if(row.getCell(1) != null && !row.getCell(1).toString().trim().equals(""))
                tbGroup.setAddressIds(row.getCell(1).getStringCellValue());

            if(row.getCell(2) != null && !row.getCell(2).toString().trim().equals(""))
                tbGroup.setAddress(row.getCell(2).getStringCellValue());

            if(row.getCell(3) != null && !row.getCell(3).toString().trim().equals(""))
                tbGroup.setDetailAddress(row.getCell(3).getStringCellValue());

            if(row.getCell(4) != null && !row.getCell(4).toString().trim().equals(""))
                tbGroup.setOrgName(row.getCell(4).getStringCellValue());


            HSSFCell cell6 = row.getCell(5);
            if (cell6 != null && (cell6.getCellType() == CellType.NUMERIC || cell6.getCellType() == CellType.FORMULA))
                tbGroup.setLeaderId((long) cell6.getNumericCellValue());

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

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

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

            HSSFCell cell9 = row.getCell(9);
            if (cell9 != null && (cell9.getCellType() == CellType.NUMERIC || cell9.getCellType() == CellType.FORMULA))
                tbGroup.setLeftPrice(cell9.getNumericCellValue());


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

            HSSFCell cell = row.getCell(11);
            if (cell != null && (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA))
                tbGroup.setCreateBy((long) row.getCell(11).getNumericCellValue());

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

            HSSFCell cel3 = row.getCell(13);
            if (cel3 != null && (cel3.getCellType() == CellType.NUMERIC || cel3.getCellType() == CellType.FORMULA))
                tbGroup.setUpdateBy(Long.valueOf(row.getCell(13).getStringCellValue()));

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

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

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

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

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

        return count;
    }
}
