package com.sqx.modules.resumes.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.service.UserService;
import com.sqx.modules.resumes.dao.ViewResumesDao;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.entity.ViewResumes;
import com.sqx.modules.resumes.service.*;
import com.sqx.modules.resumes.vo.ViewResumesVo;
import com.sqx.modules.sendRecord.entity.SendRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class ViewResumesServiceImpl extends ServiceImpl<ViewResumesDao, ViewResumes> implements ViewResumesService {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private UserService userService;
    @Autowired
    private ResumesDetailsService resumesDetailsService;


    @Override
    public Integer selectResumesByCompanyIdAndTime(Long companyId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return baseMapper.selectCount(new QueryWrapper<ViewResumes>().eq("company_id",companyId).like("create_time",sdf.format(new Date())));
    }

    @Override
    public Result selectResumesByUserId(Integer page,Integer limit,Long userId){
        Company company = companyService.selectCompanyByUserId(userId);
        Long companyId=-1L;
        if(company!=null){
            companyId = company.getCompanyId();
        }
        Page<ViewResumes> companyPage = new Page<>(page, limit);
        IPage<ViewResumes> viewResumesPage = baseMapper.selectResumesByCompanyId(companyPage, companyId);
        List<ViewResumes> records = viewResumesPage.getRecords();
        for(ViewResumes viewResumes:records){
            Resumes resumes = resumesService.getById(viewResumes.getResumesId());
            resumes.setResumesCompanyList(resumesDetailsService.selectResumesDetailsList(viewResumes.getResumesId(),1));
            resumes.setResumesWorkList(resumesDetailsService.selectResumesDetailsList(viewResumes.getResumesId(),2));
            viewResumes.setResumes(resumes);
            viewResumes.setUser(userService.selectUserById(viewResumes.getResumes().getUserId()));
        }
        return Result.success().put("data",viewResumesPage);
    }

    @Override
    public int getUseCount(Long companyId) {


        return baseMapper.getUseCount(companyId);
    }

    @Override
    public IPage<ViewResumesVo> getViewResumesList(Integer page, Integer limit, Long userId) {
        Page<ViewResumesVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        Company company = companyService.getOne(new QueryWrapper<Company>().eq("user_id", userId));
        return baseMapper.getViewResumesList(pages,company.getCompanyId());



    }

}
