package com.pj.tb_people;

import com.pj.common.core.exception.ServiceException;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
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 17 52
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodPeopleService {

    @Autowired
    private TbPeopleService tbPeopleService;
    @Autowired
    private TbPeopleMapper tbPeopleMapper;


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

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

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

            if(row.getCell(2) != null)
                tbPeople.setCode(row.getCell(2).getStringCellValue());

            HSSFCell cell8 = row.getCell(3);
            if (cell8 != null && (cell8.getCellType() == CellType.NUMERIC || cell8.getCellType() == CellType.FORMULA))
                tbPeople.setSex((int) cell8.getNumericCellValue());

            HSSFCell cell4 = row.getCell(4);
            if (cell4 != null && (cell4.getCellType() == CellType.NUMERIC || cell4.getCellType() == CellType.FORMULA))
                tbPeople.setAge((int) cell4.getNumericCellValue());

            if(row.getCell(5) != null)
                tbPeople.setIdCard(row.getCell(3).getStringCellValue());

            if(row.getCell(6) != null)
                tbPeople.setIdCardImg(row.getCell(3).getStringCellValue());

            if(row.getCell(7) != null)
                tbPeople.setPhone(row.getCell(3).getStringCellValue());

            if(row.getCell(8) != null)
                tbPeople.setBankNo(row.getCell(3).getStringCellValue());

            if(row.getCell(9) != null)
                tbPeople.setBankCode(row.getCell(3).getStringCellValue());

            if(row.getCell(10) != null)
                tbPeople.setBankName(row.getCell(3).getStringCellValue());

            HSSFCell cell11 = row.getCell(11);
            if (cell11 != null && (cell11.getCellType() == CellType.NUMERIC || cell11.getCellType() == CellType.FORMULA))
                tbPeople.setGroupId((long) cell11.getNumericCellValue());

            if(row.getCell(12) != null)
                tbPeople.setGroupName(row.getCell(3).getStringCellValue());

            HSSFCell cell13 = row.getCell(13);
            if (cell13 != null && (cell13.getCellType() == CellType.NUMERIC || cell13.getCellType() == CellType.FORMULA))
                tbPeople.setStatus((int) cell13.getNumericCellValue());

            HSSFCell cell14 = row.getCell(14);
            if (cell14 != null && (cell14.getCellType() == CellType.NUMERIC || cell14.getCellType() == CellType.FORMULA))
                tbPeople.setRole((int) cell14.getNumericCellValue());

            HSSFCell cell15 = row.getCell(15);
            if (cell15 != null && (cell15.getCellType() == CellType.NUMERIC || cell15.getCellType() == CellType.FORMULA))
                tbPeople.setLng((double) cell15.getNumericCellValue());

            HSSFCell cell16 = row.getCell(16);
            if (cell16 != null && (cell16.getCellType() == CellType.NUMERIC || cell16.getCellType() == CellType.FORMULA))
                tbPeople.setLat((double) cell16.getNumericCellValue());

            if(row.getCell(17) != null)
                tbPeople.setLastLocation(row.getCell(17).getStringCellValue());

            if(row.getCell(18) != null)
                tbPeople.setAddress(row.getCell(18).getStringCellValue());


            if(row.getCell(19) != null)
                tbPeople.setAddressIds(row.getCell(19).getStringCellValue());


            if(row.getCell(20) != null)
                tbPeople.setDetailAddress(row.getCell(20).getStringCellValue());


            HSSFCell cell21 = row.getCell(21);
            if (cell21 != null && (cell21.getCellType() == CellType.NUMERIC || cell21.getCellType() == CellType.FORMULA))
                tbPeople.setIsLock((int) cell21.getNumericCellValue());

            HSSFCell cell22 = row.getCell(22);
            if (cell22 != null && (cell22.getCellType() == CellType.NUMERIC || cell22.getCellType() == CellType.FORMULA))
                tbPeople.setLeftPrice((double) cell22.getNumericCellValue());

            HSSFCell cell23 = row.getCell(23);
            if (cell23 != null && (cell23.getCellType() == CellType.NUMERIC || cell23.getCellType() == CellType.FORMULA))
                tbPeople.setJudgeStatus((int) cell23.getNumericCellValue());

            if(row.getCell(24) != null)
                tbPeople.setJudgeContent(row.getCell(24).getStringCellValue());


            HSSFCell cel25 = row.getCell(25);
            if (cel25 != null && cel25.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cel25)) {
                tbPeople.setRegisterTime(cel25.getDateCellValue());
            }

            HSSFCell cell26 = row.getCell(26);
            if (cell26 != null && cell26.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell26)) {
                tbPeople.setJudgeTime(cell26.getDateCellValue());
            }

            if(row.getCell(27) != null)
                tbPeople.setPersonId(row.getCell(27).getStringCellValue());


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

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

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

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

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

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

        }

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

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

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

        return count;
    }

    /** 边民-根据当前登录账号获取边民表信息 */
    public TbPeople getByAppUserFk() {
        //获取登录人
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        if(appLoginInfo == null || appLoginInfo.getLoginId() == null)
            throw new RuntimeException("当前登录账号信息已失效!");
        //查询当前边民
        TbPeople tbPeople = tbPeopleMapper.selectById(appLoginInfo.getFk());
        if(tbPeople == null)
            throw new ServiceException("当前边民暂未认证!");
        return tbPeople;
    }
}
