package com.sqx.modules.sendRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.app.vo.ResumesBrowseVo;
import com.sqx.modules.app.vo.UserPostPushBrowseVo;
import com.sqx.modules.resumes.entity.*;
import com.sqx.modules.resumes.service.*;
import com.sqx.modules.sendRecord.entity.SendRecord;
import com.sqx.modules.sendRecord.dao.SendRecordDao;
import com.sqx.modules.sendRecord.service.SendRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2023-02-02
 */
@Service
public class SendRecordServiceImpl extends ServiceImpl<SendRecordDao, SendRecord> implements SendRecordService {
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private PostPushService postPushService;
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private ResumesDetailsService resumesDetailsService;
    @Autowired
    private ShieldCompanyService shieldCompanyService;

    @Override
    public IPage<UserPostPushBrowseVo> getMyRecordListV2(Integer page, Integer limit, SendRecord sendRecord, PostPush postPush) {
        Page<UserPostPushBrowseVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        String[] industry = StringUtils.isNotBlank(postPush.getIndustry()) ? postPush.getIndustry().split(",") : null;
        String[] education = StringUtils.isNotBlank(postPush.getEducation()) ? postPush.getEducation().split(",") : null;
        String[] experience = StringUtils.isNotBlank(postPush.getExperience()) ? postPush.getExperience().split(",") : null;
        String[] companyPeople = StringUtils.isNotBlank(postPush.getCompanyPeople()) ? postPush.getCompanyPeople().split(",") : null;
        String[] salaryRange = StringUtils.isNotBlank(postPush.getSalaryRange()) ? postPush.getSalaryRange().split(",") : null;

        return baseMapper.getRecordList(pages, sendRecord, industry, education, experience, companyPeople, salaryRange);


    }

    @Override
    public IPage<SendRecord> getRecordList(Integer page, Integer limit, SendRecord sendRecord) {
        Page<SendRecord> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<SendRecord> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(sendRecord));
        List<SendRecord> records = selectPage.getRecords();
        for (SendRecord record : records) {
            Resumes resumes = resumesService.getOne(new QueryWrapper<Resumes>().eq("user_id", record.getUserId()));
            record.setCompanyEntity(userService.getById(record.getCompanyUserId()));
            Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", record.getCompanyUserId()));
            record.setCompany(company);
            record.setPostPush(postPushService.getById(record.getPostPushId()));
            record.setResumes(resumesService.selectResumesByResumesId(resumes.getResumesId(), sendRecord.getCompanyUserId(), sendRecord.getCompanyId()));
            if (company.getCompanyId() != null) {
                int count = shieldCompanyService.count(new QueryWrapper<ShieldCompany>().eq("user_id", resumes.getUserId())
                        .eq("company_id", company.getCompanyId()).eq("shield_type", 2));
                if (count > 0) {
                    resumes.setIsShield(1);
                } else {
                    resumes.setIsShield(0);
                }
            }
            record.setResumes(resumes);
        }
        return selectPage;

    }

    @Override
    public Result topResumes(Long userId) {
        SendRecord sendRecord = new SendRecord();
        sendRecord.setUpdateTime(LocalDateTime.now());
        sendRecord.setUserId(userId);
        baseMapper.update(sendRecord, new QueryWrapper<SendRecord>().eq("user_id", userId));
        return Result.success();
    }

    @Override
    public Result seeRecord(Long recordId) {
        SendRecord sendRecord = baseMapper.selectById(recordId);
        if (sendRecord != null) {
            sendRecord.setIsSee(1);
            baseMapper.updateById(sendRecord);
        }
        return Result.success();

    }

    @Override
    public IPage<ResumesBrowseVo> getCompanyRecordListV2(Integer page, Integer limit, SendRecord sendRecord, Resumes resumes) {
        Page<ResumesBrowseVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        String[] resumesCompensation = StringUtils.isNotBlank(resumes.getResumesCompensation()) ? resumes.getResumesCompensation().split(",") : null;
        String[] resumesEducation = StringUtils.isNotBlank(resumes.getResumesEducation()) ? resumes.getResumesEducation().split(",") : null;
        String[] resumesWorkExperience = StringUtils.isNotBlank(resumes.getResumesWorkExperience()) ? resumes.getResumesWorkExperience().split(",") : null;
        String[] industryName = StringUtils.isNotBlank(resumes.getIndustryName()) ? resumes.getIndustryName().split(",") : null;

        IPage<ResumesBrowseVo> companyRecordList = baseMapper.getCompanyRecordList(pages, sendRecord, resumesCompensation, resumesEducation, resumesWorkExperience, industryName);
        for (ResumesBrowseVo record : companyRecordList.getRecords()) {
            record.setResumesCompanyList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", record.getResumesId()).eq("resumes_classify", 1)));

        }
        return companyRecordList;
    }

    @Override
    public Result addSendRecord(Long userId, Long postPushId) {
        QueryWrapper<SendRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("post_push_id",postPushId);
        SendRecord oldSendRecord = baseMapper.selectOne(queryWrapper);
        if(oldSendRecord !=null){
            return Result.error("已投递，请勿重复投递");
        }else {
            SendRecord sendRecord =new SendRecord();
            PostPush postPush = postPushService.getById(postPushId);
            sendRecord.setCompanyUserId(Long.valueOf(postPush.getUserId()));
            sendRecord.setUserId(userId);
            sendRecord.setPostPushId(postPushId);
            sendRecord.setCompanyId(postPush.getCompanyId());
            sendRecord.setIsSee(0);
            sendRecord.setCreateTime(LocalDateTime.now());
            sendRecord.setUpdateTime(LocalDateTime.now());
            baseMapper.insert(sendRecord);
            return Result.success();
        }
    }
}
