package com.sqx.modules.app.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserBrowseDao;
import com.sqx.modules.app.dao.UserVisitorDao;
import com.sqx.modules.app.entity.UserBrowse;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserBrowseService;
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.PostPush;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.entity.ResumesDetails;
import com.sqx.modules.resumes.service.PostPushService;
import com.sqx.modules.resumes.service.ResumesDetailsService;
import com.sqx.modules.resumes.service.ResumesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserBrowseServiceImpl extends ServiceImpl<UserBrowseDao, UserBrowse> implements UserBrowseService {

    @Autowired
    private PostPushService postPushService;
    @Autowired
    private UserVisitorDao userVisitorDao;
    @Autowired
    private UserBrowseDao userBrowseDao;
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private ResumesDetailsService resumesDetailsService;
    @Autowired
    private UserService userService;

    @Override
    public IPage<UserBrowse> selectMyBrowse(Long userId, Integer page, Integer limit, UserBrowse userBrowse) {
        UserEntity entity = userService.getById(userId);
        Page<UserBrowse> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<UserBrowse> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(userBrowse).eq("user_id", entity.getUserId()).eq("browse_type", entity.getUserType()));
        for (UserBrowse record : selectPage.getRecords()) {
            if (record.getBrowseType() == 1) {
                record.setPostPush(postPushService.selectPostPushDetails(userId, record.getPostPushId(), null, null));
            } else {
                Resumes resumes = resumesService.getById(record.getResumesId());
                resumes.setResumesCompanyList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", resumes.getResumesId()).eq("resumes_classify", 1)));
                resumes.setResumesWorkList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", resumes.getResumesId()).eq("resumes_classify", 2)));
                record.setResumes(resumes);
            }
        }
        return selectPage;

    }



    @Override
    public Result deleteMyBrowse(Long id) {

        UserBrowse userBrowse = userBrowseDao.selectById(id);
        if (userBrowse == null) {
            return Result.error("当前记录被删除！");
        } else {
            int i = userBrowseDao.deleteById(id);
            if (i > 0) {
                return Result.success();
            } else {
                return Result.error();
            }
        }
    }

    @Override
    public IPage<UserPostPushBrowseVo> selectMyBrowseV2(Long userId, Integer page, Integer limit, UserBrowse userBrowse, 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.selectBrowseList(pages, userBrowse, industry, education, experience, companyPeople, salaryRange);


    }

    @Override
    public IPage<UserEntity> getUserByBrowse(Integer page, Integer limit, Long postPushId) {
        Page<UserEntity> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getUserByBrowse(pages, postPushId);


    }

    @Override
    public IPage<ResumesBrowseVo> selectCompanyBrowse(Long userId, Integer page, Integer limit, UserBrowse userBrowse, Resumes resumes, String startTime, String endTime) {
        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> resumesBrowseVoIPage = baseMapper.selectCompanyBrowse(pages, userBrowse,resumes,startTime,endTime, resumesCompensation, resumesEducation, resumesWorkExperience, industryName);
        for (ResumesBrowseVo record : resumesBrowseVoIPage.getRecords()) {
            record.setResumesCompanyList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", record.getResumesId()).eq("resumes_classify", 1)));
            record.setResumesWorkList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", record.getResumesId()).eq("resumes_classify", 2)));

        }
        return resumesBrowseVoIPage;

    }


}
