package com.pj.tb_shop;

import com.pj.enummj.DeleteStatus;
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 20 37
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodShopService {
    
    @Autowired
    private TbShopService tbShopService;


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

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

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            //封装数据到行对象
            row = sheet.getRow(i);
            //定义返回值实体
            TbShop tbShop = new TbShop();
            //解析数据
            HSSFCell cell8 = row.getCell(1);
            if (cell8 != null && (cell8.getCellType() == CellType.NUMERIC || cell8.getCellType() == CellType.FORMULA))
                tbShop.setEnterpriseId((long) cell8.getNumericCellValue());

            HSSFCell cell2 = row.getCell(2);
            if (cell2 != null && (cell2.getCellType() == CellType.NUMERIC || cell2.getCellType() == CellType.FORMULA))
                tbShop.setTradeAreaId((long) cell2.getNumericCellValue());

            if(row.getCell(3) != null)
                tbShop.setTradeAreaName(row.getCell(3).getStringCellValue());

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

            if(row.getCell(5) != null)
                tbShop.setAddressIds(row.getCell(5).getStringCellValue());

            if(row.getCell(6) != null)
                tbShop.setShopName(row.getCell(6).getStringCellValue());

            if(row.getCell(7) != null)
                tbShop.setShopNo(row.getCell(7).getStringCellValue());

            if(row.getCell(8) != null)
                tbShop.setArea(row.getCell(8).getStringCellValue());

            HSSFCell cell19 = row.getCell(9);
            if (cell19 != null && (cell19.getCellType() == CellType.NUMERIC || cell19.getCellType() == CellType.FORMULA))
                tbShop.setStatus(Integer.valueOf(cell19.getStringCellValue()));

            if(row.getCell(10) != null)
                tbShop.setRemark(row.getCell(10).getStringCellValue());

            if(row.getCell(11) != null)
                tbShop.setEnterpriseName(row.getCell(11).getStringCellValue());


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

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

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

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

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

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

        }

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

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

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

        return count;
    }
    
}
