package com.sqx.modules.myCollection.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.myCollection.entity.MyCollection;
import com.sqx.modules.myCollection.dao.MyCollectionDao;
import com.sqx.modules.myCollection.service.MyCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.resumes.dao.ResumesDao;
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.entity.ResumesListDto;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.PostPushService;
import com.sqx.modules.resumes.service.ResumesDetailsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2023-01-17
 */
@Service
public class MyCollectionServiceImpl extends ServiceImpl<MyCollectionDao, MyCollection> implements MyCollectionService {

    @Autowired
    private UserService userService;
    @Autowired
    private PostPushService postPushService;
    @Autowired
    private ResumesDetailsService resumesDetailsService;
    @Autowired
    private MyCollectionService myCollectionService;
    @Autowired
    private ResumesDao resumesDao;

    @Override
    public Result saveCollection(MyCollection collection) {
        QueryWrapper<MyCollection> wrapper = new QueryWrapper<MyCollection>().eq("user_id", collection.getUserId());
        if (collection.getType() == 1) {
            wrapper.eq("post_push_id", collection.getPostPushId());
        } else {
            wrapper.eq("resumes_id", collection.getResumesId());
        }
        MyCollection myCollection = baseMapper.selectOne(wrapper);

        if (myCollection != null) {
            baseMapper.deleteById(myCollection.getCollectionId());
            return Result.success("取消收藏成功");
        }
        collection.setCreateTime(LocalDateTime.now());
        baseMapper.insert(collection);
        return Result.success("收藏成功");
    }

    @Override
    public IPage<MyCollection> getMyCollectionList(Integer page, Integer limit, MyCollection collection) {
        Page<MyCollection> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<MyCollection> selectPage = baseMapper.selectPage(pages, new QueryWrapper<>(collection).orderByDesc("create_time"));
        for (MyCollection record : selectPage.getRecords()) {
            record.setUserEntity(userService.getById(collection.getUserId()));
            record.setPostPush(postPushService.getById(collection.getPostPushId()));
        }
        return selectPage;
    }

    @Override
    public IPage<MyCollection> getCompanyCollectionList(Integer page, Integer limit, MyCollection collection, Resumes resumes, Long userId) {
        Page<MyCollection> 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> companyCollectionList = baseMapper.getCompanyCollectionList(pages, collection, resumes, resumesCompensation, resumesEducation, resumesWorkExperience, industryName);
//        for (ResumesBrowseVo record : companyCollectionList.getRecords()) {
//            record.setResumesCompanyList(resumesDetailsService.list(new QueryWrapper<ResumesDetails>().eq("resumes_id", record.getResumesId()).eq("resumes_classify", 1)));
//
//        }
        QueryWrapper<MyCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("type",2);
        IPage<MyCollection> companyCollectionList =myCollectionService.getBaseMapper().selectPage(pages,queryWrapper);
        for (MyCollection c : companyCollectionList.getRecords()){
            ResumesListDto resumesListDto = new ResumesListDto();
            resumesListDto.setResumesId(Long.valueOf(c.getResumesId()));
            c.setResumesListDto(resumesDao.selectResumesListHr(resumesListDto));
        }
        return companyCollectionList;
    }

    @Override
    public IPage<UserPostPushBrowseVo> getMyCollectionListV2(Integer page, Integer limit, MyCollection collection, 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.getMyCollectionList(pages, collection, postPush, industry, education, experience, companyPeople, salaryRange);

    }

}
