package com.pj.tb_enterprise;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pj.api.client.admin.AdminInterface;
import com.pj.api.client.transport.TransportInterface;
import com.pj.api.dto.AppUserDto;
import com.pj.api.dto.TbLogisticsDto;
import com.pj.common.core.exception.ServiceException;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.enummj.DeleteStatus;
import com.pj.tb_enterprise.vo.OrdersVo;
import com.pj.tb_goods.TbGoods;
import com.pj.tb_goods.TbGoodsMapper;
import com.pj.tb_goods_transit.TbGoodsTransit;
import com.pj.tb_goods_transit.TbGoodsTransitMapper;
import com.pj.tb_order.TbOrder;
import com.pj.tb_people.TbPeople;
import com.pj.tb_people.TbPeopleMapper;
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.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Mechrevo
 * @Date 2023 07 21 11 26
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class MethodEnterpriseService {

    @Autowired
    private AdminInterface adminInterface;
    @Autowired
    private TbEnterpriseService tbEnterpriseService;
    @Autowired
    private TbPeopleMapper tbPeopleMapper;
    @Autowired
    private TransportInterface transportInterface;
    @Autowired
    private TbGoodsTransitMapper tbGoodsTransitMapper;
    @Autowired
    private TbEnterpriseMapper tbEnterpriseMapper;

    /**
     * 验证重复提交
     * @param loginUserFK 登录用户的ID
     */
    void againApply(Long loginUserFK){
        //根据AppUser表的fk查询对应的enterprise表数据
        List<TbEnterprise> enterpriseList = tbEnterpriseMapper.selectList(new LambdaQueryWrapper<TbEnterprise>().eq(TbEnterprise::getId, loginUserFK).eq(TbEnterprise::getDeleteStatus, DeleteStatus.DELETE_STATUS_ON.getCode()));
        if(enterpriseList.size() > 0){
            switch (enterpriseList.get(0).getJudgeStatus()){
                case 0:
                    throw new RuntimeException("当前账号待审核!");
                case 1:
                    throw new RuntimeException("当前账号已审核通过!");
                case 2:
                    throw new RuntimeException("当前账号审核不通过!原因是: " + enterpriseList.get(0).getJudgeContent() + " 审核时间: " + enterpriseList.get(0).getJudgeTime());
                default:
                    throw new ServiceException("服务器繁忙~");
            }
        }

    }

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

        List<TbEnterprise> list = new ArrayList<>();

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

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

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

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

            if(row.getCell(5) != null && !row.getCell(5).toString().trim().equals(""))
                tbEnterprise.setIdCard(row.getCell(5).getStringCellValue());

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

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

            HSSFCell cell5 = row.getCell(8);
            if (cell5 != null && (cell5.getCellType() == CellType.NUMERIC || cell5.getCellType() == CellType.FORMULA))
                tbEnterprise.setShopId((long)cell5.getNumericCellValue());

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

            if(row.getCell(10) != null && !row.getCell(10).toString().trim().equals(""))
                tbEnterprise.setBankNo(row.getCell(10).getStringCellValue());

            if(row.getCell(11) != null && !row.getCell(11).toString().trim().equals(""))
                tbEnterprise.setBank(row.getCell(11).getStringCellValue());

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

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

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

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

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

            HSSFCell cell6 = row.getCell(17);
            if (cell6 != null && (cell6.getCellType() == CellType.NUMERIC || cell6.getCellType() == CellType.FORMULA))
                 tbEnterprise.setJudgeStatus(Integer.parseInt(row.getCell(17).getStringCellValue()));

            if(row.getCell(18) != null && !row.getCell(18).toString().trim().equals(""))
                tbEnterprise.setJudgeContent(row.getCell(18).getStringCellValue());

            HSSFCell cell = row.getCell(19);
            if (cell != null && cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell))
                tbEnterprise.setJudgeTime(cell.getDateCellValue());

            HSSFCell cell1 = row.getCell(20);
            if (cell1 != null && cell1.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell1)) {
                Date dateValue = cell1.getDateCellValue();
                tbEnterprise.setRegisterTime(dateValue);
            }

//            if(row.getCell(21) != null && !row.getCell(21).toString().trim().equals(""))
//                tbEnterprise.setPersonId(row.getCell(21).getStringCellValue());

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

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

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

            if(row.getCell(24) != null && !row.getCell(24).toString().trim().equals(""))
                tbEnterprise.setUpdateBy(row.getCell(24).getStringCellValue());
            if(row.getCell(25) != null && !row.getCell(25).toString().trim().equals(""))
                tbEnterprise.setUpdateName(row.getCell(25).getStringCellValue());

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

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

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

        //每次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<TbEnterprise> enterpriseList = list.subList(start, end);

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

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

        return count;
    }
}
