package com.ruoyi.cms.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.domain.query.ESJobSearch;
import com.ruoyi.cms.domain.vo.CandidateVO;
import com.ruoyi.cms.mapper.*;
import com.ruoyi.cms.service.*;
import com.ruoyi.cms.util.*;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.JobTitle;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.io.File;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
 * 岗位Service业务层处理
 *
 * @author lishundong
 * @date 2024-09-03
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper,Job> implements IJobService
{
    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private JobApplyMapper jobApplyMapper;
    @Autowired
    private JobCollectionMapper jobCollectionMapper;
    @Autowired
    private AppReviewJobMapper appReviewJobMapper;
    @Autowired
    private IBussinessDictTypeService iBussinessDictTypeService;
    @Autowired
    private IBussinessDictDataService iBussinessDictDataService;
    @Autowired
    private IIndustryService industryService;
    @Autowired
    private IESJobSearchService iesJobSearchService;
    @Autowired
    private JobTitleMapper jobTitleMapper;
    Logger logger = LoggerFactory.getLogger(JobServiceImpl.class);
    // 高德API配置
    private static final String API_KEY = "32edd506a411de754d34a0f4ecb72345";
    private static final String AMAP_URL = "https://restapi.amap.com/v3/place/text";
    @Autowired
    private RedisCache redisCache;

    /**
     * 更新工作地址的经纬度信息
     */
    public void updateJobLon() {
        System.out.println("开始执行工作地址经纬度更新...");

        // 1. 查询需要更新经纬度的工作地址
        List<Job> jobs = jobMapper.selectList(
                Wrappers.<Job>lambdaQuery()
                        .isNull(Job::getLongitude)  // 查询经度为null的记录
                        .or()
                        .eq(Job::getLongitude, BigDecimal.ZERO)  // 或者经度为0的记录
                                 // 限制每次处理10条
        );

        if (jobs.isEmpty()) {
            System.out.println("没有需要更新经纬度的工作地址");
            return;
        }

        // 2. 遍历处理每个工作地址
        for (Job job : jobs) {
            try {
                System.out.println("\n正在处理工作ID: " + job.getJobId() +
                        ", 地址: " + job.getJobLocation());

                // 调用高德API获取经纬度（添加适当延迟）
                TimeUnit.MILLISECONDS.sleep(500);
                Map<String, BigDecimal> coordinates = getCoordinates(job.getJobLocation(), "青岛市");

                if (coordinates != null) {
                    // 3. 更新工作记录的经纬度
                    job.setLongitude(coordinates.get("lng"));
                    job.setLatitude(coordinates.get("lat"));

                    // 使用MyBatis Plus的update方法
                    jobMapper.updateById(job);
                    System.out.println("成功更新工作ID: " + job.getJobId() +
                            " 的经纬度 -> 经度: " + job.getLongitude() +
                            ", 纬度: " + job.getLatitude());
                } else {
                    System.out.println("无法获取工作地址的经纬度: " + job.getJobLocation());
                    jobMapper.updateById(job);
                }
            } catch (Exception e) {
                System.err.println("处理工作ID " + job.getJobId() + " 时出错: " + e.getMessage());
            }
        }

        System.out.println("工作地址经纬度更新完成，共处理 " + jobs.size() + " 条记录");
    }

    /**
     * 调用高德地图API获取地址的经纬度并返回BigDecimal
     */
    private Map<String, BigDecimal> getCoordinates(String placeName, String city) {
        try {
            // 构造请求URL
            String encodedPlaceName = java.net.URLEncoder.encode(placeName, "UTF-8");
            String encodedCity = java.net.URLEncoder.encode(city, "UTF-8");

            String params = "key=" + API_KEY + "&keywords=" + encodedPlaceName +
                    "&types=190000&city=" + encodedCity + "&output=JSON";
            String requestUrl = AMAP_URL + "?" + params;

            // 发送HTTP请求
            URL url = new URL(requestUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), "UTF-8"));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 解析JSON响应
                com.alibaba.fastjson.JSONObject jsonResponse =
                        com.alibaba.fastjson.JSON.parseObject(response.toString());

                if ("1".equals(jsonResponse.getString("status"))) {
                    com.alibaba.fastjson.JSONArray pois = jsonResponse.getJSONArray("pois");
                    if (pois != null && !pois.isEmpty()) {
                        com.alibaba.fastjson.JSONObject firstPoi = pois.getJSONObject(0);
                        String location = firstPoi.getString("location");
                        String[] lngLat = location.split(",");

                        // 将字符串坐标转换为BigDecimal
                        Map<String, BigDecimal> coordinates = new HashMap<>();
                        coordinates.put("lng", new BigDecimal(lngLat[0]));
                        coordinates.put("lat", new BigDecimal(lngLat[1]));
                        return coordinates;
                    }
                }
            }
            return null;
        } catch (Exception e) {
            System.err.println("调用高德地图API出错: " + e.getMessage());
            return null;
        }
    }

    /**
     * 查询岗位
     *
     * @param jobId 岗位主键
     * @return 岗位
     */
    @Override
    public Job selectJobByJobId(Long jobId)
    {
        Job job = jobMapper.selectById(jobId);
        //查询公司信息
        if(Objects.nonNull(job.getCompanyId())){
            Company company = companyMapper.selectById(job.getCompanyId());
            job.setCompany(company);
        }
        return job;
    }

    /**
     * 查询岗位列表
     *
     * @param job 岗位
     * @return 岗位
     */
    @Override
    public List<Job> selectJobList(Job job)
    {
        List<Job> jobs = jobMapper.selectJobList(job);
        return jobs;
    }

    @Override
    public int view(Long jobId) {
        Job job = jobMapper.selectById(jobId);
        if(Objects.isNull(job)){
            throw new ServiceException("岗位不存在");
        }
        job.setView(job.getView()+1);
        jobMapper.updateById(job);
        return 1;
    }

    /**
     * 新增岗位
     *
     * @param job 岗位
     * @return 结果
     */
    @Override
    public int insertJob(Job job)
    {
        int insert = jobMapper.insert(job);
        //todo 线程池管理
        Thread thread = new Thread(() -> {
            VectorJob jobVector = jobMapper.selectVectorJob(job.getJobId());
            jobVector.setJobUrl("http://39.98.44.136/app#/packageA/pages/post/post?jobId="+job.getJobId());
            String jsonBody = JSONUtil.toJsonStr(jobVector);
            HttpUtil.post("http://39.98.44.136:6004/insert_vector", jsonBody);
        });
        thread.start();
        return insert;
    }


    /**
     * 修改岗位
     *
     * @param job 岗位
     * @return 结果
     */
    @Override
    public int updateJob(Job job)
    {
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //修改岗位状态
        //修改岗位状态
        if(job.getIsPublish()!=null) {
            if(job.getIsPublish()==1){
                job.setPostingDate(formattedDate);
                iesJobSearchService.updateJob(job.getJobId());
            }else {
                job.setPostingDate(null);
                iesJobSearchService.deleteJob(job.getJobId());
            }
        }
        return jobMapper.updateById(job);
    }

    /**
     * 批量删除岗位
     *
     * @param jobIds 需要删除的岗位主键
     * @return 结果
     */
    @Override
    public int deleteJobByJobIds(Long[] jobIds)
    {
        return jobMapper.deleteBatchIds(Arrays.asList(jobIds));
    }

    @Override
    public EsPageInfo<ESJobDocument> appList(ESJobSearch jobQuery) {
        EsPageInfo<ESJobDocument> jobs = iesJobSearchService.appList(jobQuery);
        List<ESJobDocument> esJobDocuments = userCollection(jobs.getList());
        jobs.setList(esJobDocuments);
        return jobs;
    }

    @Override
    public List<ESJobDocument> recommend(ESJobSearch esJobSearch) {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        JSONArray cacheObject = redisCache.getCacheObject(esJobSearch.getSessionId());
        List<Long> jobList = new ArrayList<>();
        if(Objects.isNull(cacheObject)){
            ArrayList<Long> longs = new ArrayList<>();
            jobList =longs;
        }else {
            jobList = cacheObject.toList(Long.class);
        }
        //从es中查询
        List<ESJobDocument> jobListResult = iesJobSearchService.selectTextListExceptJobId(esJobSearch,jobList);
        //存入当前session中查看的岗位 避免重复 todo 定时删除 key上保存用户信息
        jobList.addAll(jobListResult.stream().map(ESJobDocument::getJobId).collect(Collectors.toList()));
        redisCache.setCacheObject(esJobSearch.getSessionId(),jobList);
        List<ESJobDocument> esJobDocuments = userCollection(jobListResult);
        return esJobDocuments;
    }

    @Override
    public EsPageInfo<ESJobDocument> countyJobList(ESJobSearch job) {
        EsPageInfo<ESJobDocument> esJobDocumentEsPageInfo = iesJobSearchService.countyJobList(job);
        List<ESJobDocument> esJobDocuments = userCollection(esJobDocumentEsPageInfo.getList());
        esJobDocumentEsPageInfo.setList(esJobDocuments);
        return esJobDocumentEsPageInfo;
    }

    @Override
    public EsPageInfo<ESJobDocument> subway(ESJobSearch jobQuery) {
        EsPageInfo<ESJobDocument> esJobDocumentEsPageInfo = iesJobSearchService.subway(jobQuery);
        List<ESJobDocument> esJobDocuments = userCollection(esJobDocumentEsPageInfo.getList());
        esJobDocumentEsPageInfo.setList(esJobDocuments);
        return esJobDocumentEsPageInfo;
    }

    @Override
    public EsPageInfo<ESJobDocument> commercialArea(ESJobSearch jobQuery) {
        EsPageInfo<ESJobDocument> esJobDocumentEsPageInfo = iesJobSearchService.commercialArea(jobQuery);
        List<ESJobDocument> esJobDocuments = userCollection(esJobDocumentEsPageInfo.getList());
        esJobDocumentEsPageInfo.setList(esJobDocuments);
        return esJobDocumentEsPageInfo;
    }

    @Override
    public Job selectJobByJobIdApp(Long jobId) {
            Job job = jobMapper.selectById(jobId);
            //查询公司信息
            if(Objects.nonNull(job.getCompanyId())){
                Company company = companyMapper.selectById(job.getCompanyId());
                job.setCompany(company);
            }
            if(SecurityUtils.isLogin()){
                //查询申请信息
                Long applyCount = jobApplyMapper.selectCount(Wrappers.<JobApply>lambdaQuery().eq(JobApply::getJobId, jobId).eq(JobApply::getUserId, SecurityUtils.getUserId()));
                job.setIsApply(applyCount>0?1:0);
                //查询收藏信息
                Long collectionCount = jobCollectionMapper.selectCount(Wrappers.<JobCollection>lambdaQuery().eq(JobCollection::getJobId, jobId).eq(JobCollection::getUserId, SecurityUtils.getUserId()));
                job.setIsCollection(collectionCount>0?1:0);
                //todo asyn
                //保存浏览记录
                List<AppReviewJob> appReviewJobs = appReviewJobMapper.selectList(Wrappers.<AppReviewJob>lambdaQuery().eq(AppReviewJob::getUserId, SecurityUtils.getUserId()).eq(AppReviewJob::getJobId, jobId));
                //之前相同岗位的记录删除 保存最新的浏览记录
                if(!appReviewJobs.isEmpty()){
                    appReviewJobMapper.deleteBatchIds(appReviewJobs.stream().map(AppReviewJob::getId).collect(Collectors.toList()));
                }
                AppReviewJob appReviewJob = new AppReviewJob();
                appReviewJob.setUserId(SecurityUtils.getUserId());
                LocalDateTime now = LocalDateTime.now();
                String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                appReviewJob.setReviewDate(formattedDate);
                appReviewJob.setJobId(jobId);
                appReviewJobMapper.insert(appReviewJob);
            }
            this.view(jobId);
            return job;

    }

    @Override
    public List<CandidateVO> candidates(Long jobId) {
        List<CandidateVO> jobList = jobApplyMapper.candidates(jobId);
        List<JobTitle> jobTitles = jobTitleMapper.selectList(new QueryWrapper<>());
        jobList.forEach(x->x.setJobTitle(wrapperJobTitleList(jobTitles,x.getJobTitleId())));
        return jobList;
    }

    private List<String> wrapperJobTitleList(List<JobTitle> jobTitles, String jobTitleId) {
        List<String> result = new ArrayList<>();
        if(StringUtil.isEmptyOrNull(jobTitleId)){return result;}
        // 将 jobTitleId 按逗号分隔，得到 jobId 列表
        List<String> jobIdList = Arrays.asList(jobTitleId.split(","));
        // 遍历 jobTitles，比对 jobId
        for (JobTitle jobTitle : jobTitles) {
            if (jobIdList.contains(jobTitle.getJobId().toString())) {
                result.add(jobTitle.getJobName());
            }
        }
        return result;
    }

    @Override
    public void updateLon() {
        updateJobLon();
    }

    @Override
    public List<ESJobDocument> littleVideo(ESJobSearch esJobSearch) {

        return iesJobSearchService.littleVideo(esJobSearch);

    }

    @Override
    public List<ESJobDocument> littleVideoRandom(String uuid, Integer count,String jobTitle) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Long> cacheList = new ArrayList<>();

        try {
            // 从Redis获取字符串格式的缓存
            String cacheJson = redisCache.getCacheObject("random_video:" + uuid);
            if (cacheJson != null && !cacheJson.isEmpty()) {
                // 将JSON字符串转换为List<Long>
                cacheList = objectMapper.readValue(cacheJson, new TypeReference<List<Long>>() {});
            }
        } catch (Exception e) {
            // 处理转换异常，重置缓存
            log.error("Failed to parse cache JSON for key: random_video:" + uuid, e);
            redisCache.deleteObject("random_video:" + uuid);
        }

        // 从服务获取随机视频
        List<ESJobDocument> esJobDocuments = iesJobSearchService.littleVideoRandom(cacheList, count,jobTitle);
        if(esJobDocuments.isEmpty()){
            esJobDocuments = iesJobSearchService.littleVideoRandom(new ArrayList<>(), count,jobTitle);
            redisCache.deleteObject("random_video:" + uuid);
        }


        // 提取新视频ID并添加到缓存列表
        List<Long> newJobIds = esJobDocuments.stream()
                .map(ESJobDocument::getJobId)
                .collect(Collectors.toList());
        cacheList.addAll(newJobIds);

        try {
            // 将更新后的列表转换为JSON字符串
            String updatedJson = objectMapper.writeValueAsString(cacheList);
            // 保存到Redis
            redisCache.setCacheObject("random_video:" + uuid, updatedJson);
        } catch (Exception e) {
            log.error("Failed to serialize cache list for key: random_video:" + uuid, e);
        }

        return esJobDocuments;
    }

    @Override
    public void importRow(String path) {
        try {
            logger.info("开始导入Excel数据...");
            // 读取Excel文件中的数据
            List<RowWork> rowWorks = ExcelToObject.readExcelToObjects(path, RowWork.class);
            logger.info("从Excel中读取到 {} 条数据", rowWorks.size());

            // 按ORG分类存储数据
            Map<String, List<RowWork>> orgDataMap = new HashMap<>();
            orgDataMap.put("智联招聘", new ArrayList<>());
            orgDataMap.put("58同城", new ArrayList<>());
            orgDataMap.put("前程无忧", new ArrayList<>());
            orgDataMap.put("猎聘网", new ArrayList<>());

            // 遍历数据，按ORG分类，每类最多取20000条
            for (RowWork rowWork : rowWorks) {
                String org = rowWork.getORG(); // 假设RowWork有getORG()方法
                if (orgDataMap.containsKey(org)) {
                    List<RowWork> orgList = orgDataMap.get(org);
                    if (orgList.size() < 20000) {
                        orgList.add(rowWork);
                    }
                }
            }

            // 统计各类数据量
            int zlCount = orgDataMap.get("智联招聘").size();
            int tcCount = orgDataMap.get("58同城").size();
            int qcCount = orgDataMap.get("前程无忧").size();
            int lpCount = orgDataMap.get("前程无忧").size();
            logger.info("智联招聘数据量: {}", zlCount);
            logger.info("58同城数据量: {}", tcCount);
            logger.info("前程无忧数据量: {}", qcCount);
            logger.info("猎聘网数据量: {}", lpCount);

            // 合并所有数据（智联招聘 + 58同城 + 前程无忧）
            List<RowWork> allData = new ArrayList<>();
            allData.addAll(orgDataMap.get("智联招聘"));
            allData.addAll(orgDataMap.get("58同城"));
            allData.addAll(orgDataMap.get("前程无忧"));
            allData.addAll(orgDataMap.get("猎聘网"));

            // 批量插入数据
            int batchSize = 50;
            int totalInserted = 0;
            List<RowWork> batchList = new ArrayList<>();

            for (RowWork rowWork : allData) {
//                Thread.sleep(500); // 控制处理速度（可选）
                batchList.add(rowWork);
                if (batchList.size() >= batchSize) {
                    try {
                        jobMapper.insertBatchRowWork(batchList);
                        totalInserted += batchList.size();
                        logger.info("成功插入 {} 条数据，当前总共插入 {} 条数据", batchList.size(), totalInserted);
                        batchList.clear();
                    } catch (Exception e) {
                        logger.error("批量插入失败: " + e.getMessage(), e);
                    }
                }
            }

            // 插入剩余数据
            if (!batchList.isEmpty()) {
                try {
                    jobMapper.insertBatchRowWork(batchList);
                    totalInserted += batchList.size();
                    logger.info("成功插入剩余的 {} 条数据，当前总共插入 {} 条数据", batchList.size(), totalInserted);
                } catch (Exception e) {
                    logger.error("批量插入剩余数据失败: " + e.getMessage(), e);
                }
            }

            logger.info("数据导入完成，总共插入 {} 条数据", totalInserted);
        } catch (Exception e) {
            logger.error("导入数据过程中发生异常: " + e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
    @Override
    public void importData() {
        try {
            logger.info("开始导入数据...");

            // 获取字典数据
            List<BussinessDictData> dictData = iBussinessDictTypeService.selectDictDataByType("education");
            List<BussinessDictData> expData = iBussinessDictTypeService.selectDictDataByType("experience");
            List<BussinessDictData> areaData = iBussinessDictTypeService.selectDictDataByType("area");
            logger.info("成功获取字典数据: education={}, experience={}, area={}", dictData.size(), expData.size(), areaData.size());

            // 获取需要插入的行数据
            List<RowWork> rowWorks = this.baseMapper.selectAllInsertRowWork();
            logger.info("从数据库中获取到 {} 条待插入的行数据", rowWorks.size());

            // 获取公司数据并转换
            List<Company> companyRowList = this.baseMapper.selectRowCompany();
            List<Company> companyList = company2New(companyRowList);
            logger.info("成功转换 {} 条公司数据", companyList.size());

            // 批量插入公司数据
            int batchSizeCompany = 50; // 每批次插入50条数据
            List<Company> companyBatch = new ArrayList<>();
            int totalCompanyInserted = 0;

            for (Company company : companyList) {
                companyBatch.add(company);
                if (companyBatch.size() == batchSizeCompany) {
                    companyMapper.batchInsert(companyBatch); // 批量插入
                    totalCompanyInserted += companyBatch.size();
                    logger.info("成功插入 {} 条公司数据，当前总共插入 {} 条", companyBatch.size(), totalCompanyInserted);
                    companyBatch.clear(); // 清空批次
                }
            }

            // 插入剩余的记录
            if (!companyBatch.isEmpty()) {
                companyMapper.batchInsert(companyBatch);
                totalCompanyInserted += companyBatch.size();
                logger.info("成功插入剩余的 {} 条公司数据，当前总共插入 {} 条", companyBatch.size(), totalCompanyInserted);
            }

            // 获取所有公司数据
            List<Company> allCompany = companyMapper.selectCompanyList(new Company());
            logger.info("从数据库中获取到 {} 条公司数据", allCompany.size());

            // 批量插入职位数据
            List<Job> jobBatch = new ArrayList<>();
            int batchSize = 50;
            int totalJobInserted = 0;
            String string = UUID.randomUUID().toString();
            logger.info("生成唯一标识符: {}", string);
            LocalDateTime now = LocalDateTime.now();
            String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            for (RowWork rowWork : rowWorks) {
                try {
                    Job job = new Job();
                    if(StringUtil.isEmptyOrNull(rowWork.getAca112())){
                        continue;
                    }
                    job.setJobTitle(rowWork.getAca112());
                    job.setMinSalary(Long.valueOf(rowWork.getSalaryLow()));
                    job.setMaxSalary(Long.valueOf(rowWork.getSalaryHight()));
                    if(Objects.isNull(rowWork.getAac011())){
                        job.setEducation("-1");
                    }else {
                        job.setEducation(iBussinessDictDataService.findCode(dictData, rowWork.getAac011()));
                    }
                    if(Objects.isNull(rowWork.getExperience())){
                        job.setExperience("0");
                    }else {
                        job.setExperience(iBussinessDictDataService.findCode(expData, rowWork.getExperience()));
                    }
                    job.setCompanyName(rowWork.getAAB004());
                    job.setJobLocation(rowWork.getAAE006());
                    job.setPostingDate(formattedDate);
                    job.setJobCategory(rowWork.getJobCategory());
                    if (rowWork.getAcb240() == null) {
                        job.setVacancies(-1L);
                    } else {
                        job.setVacancies(Long.valueOf(rowWork.getAcb240().replace("人", "")));
                    }
                    if (rowWork.getLatitude() != null && !rowWork.getLatitude().isEmpty()) {
                        job.setLatitude(new BigDecimal(rowWork.getLatitude()));
                        job.setLongitude(new BigDecimal(rowWork.getLongitude()));
                    }
                    job.setView(0L);
                    job.setCompanyId(getCompanyId(allCompany, rowWork.getAAB004()));
                    job.setIsHot(0);
                    job.setApplyNum(0);
                    if(Objects.isNull(rowWork.getCounty())){
                        job.setJobLocationAreaCode(null);
                    }else {
                        job.setJobLocationAreaCode(Integer.valueOf(Objects.requireNonNull(iBussinessDictDataService.findCode(areaData, rowWork.getCounty()))));
                    }
                    job.setDescription(rowWork.getAcb22a());
                    job.setIsPublish(1);
                    job.setDataSource(rowWork.getORG());
                    job.setJobUrl(rowWork.getACE760());
                    job.setRemark(string);
                    job.setDelFlag("0");
                    job.setCreateBy(string);
                    job.setCreateTime(formattedDate);
                    job.setRowId(Long.valueOf(rowWork.getId()));

                    jobBatch.add(job);

                    // 当批次达到50条时，执行批量插入
                    if (jobBatch.size() == batchSize) {
                        this.baseMapper.batchInsert(jobBatch);
                        totalJobInserted += jobBatch.size();
                        logger.info("成功插入 {} 条职位数据，当前总共插入 {} 条", jobBatch.size(), totalJobInserted);
                        jobBatch.clear(); // 清空批次
                    }
                } catch (Exception e) {
                    logger.error("处理职位数据时发生异常: {}", e.getMessage(), e);
                }
            }

            // 插入剩余的记录
            if (!jobBatch.isEmpty()) {
                this.baseMapper.batchInsert(jobBatch);
                totalJobInserted += jobBatch.size();
                logger.info("成功插入剩余的 {} 条职位数据，当前总共插入 {} 条", jobBatch.size(), totalJobInserted);
            }

            logger.info("数据导入完成，总共插入 {} 条公司数据和 {} 条职位数据", totalCompanyInserted, totalJobInserted);
        } catch (Exception e) {
            logger.error("导入数据过程中发生异常: {}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    private Long getCompanyId(List<Company> allCompany, String aab004) {
        for (Company company:allCompany){
            if(company.getName().equals(aab004)){
                return company.getCompanyId();
            }
        }
        return null;
    }

    private List<Company> company2New(List<Company> companyRowList) {
        List<BussinessDictData> scaleData = iBussinessDictTypeService.selectDictDataByType("scale");
        List<BussinessDictData> natureData = iBussinessDictTypeService.selectDictDataByType("company_nature");
        List<TreeSelect> treeSelects = industryService.appIndustry();
        List<Company> companyList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        for(Company company:companyRowList){
            Company companyNew = new Company();
            companyNew.setName(company.getName());
            companyNew.setLocation(company.getLocation());
            if(Objects.isNull(company.getScale())){
                companyNew.setScale("0");
            }else {
                companyNew.setScale(iBussinessDictDataService.findCode(scaleData,company.getScale()));
            }
            companyNew.setDelFlag("0");
            companyNew.setCreateBy("system");
            companyNew.setCreateTime(formattedDate);
            companyNew.setRemark("add");
            companyNew.setDescription(company.getDescription());
            companyNew.setIndustry(findIndustry(treeSelects,company));
            if(Objects.isNull(company.getNature())){
                companyNew.setNature("8");
            }else {
                companyNew.setNature(iBussinessDictDataService.findCode(natureData,company.getNature()));
            }
            companyList.add(companyNew);
        }
        return companyList;
    }


    private String findIndustry(List<TreeSelect> treeSelects, Company company) {
        if(company.getRemark()==null){return "";}
        if(Objects.equals(company.getRemark(), "")){return "";}
        for (TreeSelect treeSelect:treeSelects){
            if(treeSelect.getLabel().equals(company.getRemark())){
                if(company.getIndustry()==null){return String.valueOf(treeSelect.getId());}
                if(Objects.equals(company.getIndustry(), "")){return String.valueOf(treeSelect.getId());}
                for(TreeSelect treeSelectSub:treeSelect.getChildren()){
                    if(treeSelectSub.getLabel().equals(company.getIndustry())){
                        return String.valueOf(treeSelectSub.getId());
                    }
                }
            }
        }
        return "";
    }

    private List<ESJobDocument> userCollection(List<ESJobDocument> jobs){
        if(jobs.isEmpty()){return new ArrayList<>();}
        if(SecurityUtils.isLogin()){
            //收藏
            Set<Long> collectionIds = jobCollectionMapper.selectList(Wrappers.<JobCollection>lambdaQuery()
                            .eq(JobCollection::getUserId, SecurityUtils.getUserId())
                            .in(JobCollection::getJobId, jobs.stream().map(ESJobDocument::getJobId).collect(Collectors.toList())))
                    .stream().map(JobCollection::getJobId).collect(Collectors.toSet());
            for (ESJobDocument j : jobs) {
                if (collectionIds.contains(j.getJobId())) {
                    j.setIsCollection(1);
                }
            }
        }
        return jobs;
    }

}
