package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.CheckService;
import org.jeecg.modules.employCheckJob.entity.EmployCheckJob;
import org.jeecg.modules.employCheckJob.service.IEmployCheckJobService;
import org.jeecg.modules.employCheckResume.entity.EmployCheckResume;
import org.jeecg.modules.employCheckResume.service.IEmployCheckResumeService;
import org.jeecg.modules.employCollectionJob.service.IEmployCollectionJobService;
import org.jeecg.modules.employCollectionResume.service.IEmployCollectionResumeService;
import org.jeecg.modules.employJob.entity.EmployJob;
import org.jeecg.modules.employJob.service.IEmployJobService;
import org.jeecg.modules.employResume.entity.EmployResume;
import org.jeecg.modules.employResume.service.IEmployResumeService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CheckServiceImpl implements CheckService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //用户表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //简历信息表
    @Resource
    private IEmployResumeService employResumeService;

    //简历查看表
    @Resource
    private IEmployCheckResumeService employCheckResumeService;

    //工作信息表
    @Resource
    private IEmployJobService employJobService;

    //工作查看表
    @Resource
    private IEmployCheckJobService employCheckJobService;
    /******************************************************************************************************************/

    //会员中心-联系记录-我看过谁
    @Override
    public Result<?> queryWatchWhoBoss(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查看简历查看记录
            Page<EmployCheckResume> page = new Page<EmployCheckResume>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCheckResume> pageList = employCheckResumeService
                    .lambdaQuery()
                    .eq(EmployCheckResume::getUserId, hanHaiMember.getId())
                    .page(page);

            //根据查看记录关联简历id获取简历信息
            for (EmployCheckResume record : pageList.getRecords()) {
                //获取简历信息
                EmployResume employResume = employResumeService.getById(record.getResumeId());
                //将简历信息添加到查看记录中
                record.setEmployResume(employResume);
            }

            return Result.OK("我看过谁", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我看过谁列表查询失败");
        }
    }

    //会员中心-联系记录-谁看过我(谁看过我的招工)
    @Override
    public Result<?> queryWatchMeBoss(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询我发布的工作列表
            List<EmployJob> jobList = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getUserId, hanHaiMember.getId())
                    .list();

            //获取查看我发布的工作id列表
            int jobSize = (int)jobList.size();
            Object[] jobIds = new Object[jobSize];
            int jobIndex = 0;
            for (EmployJob job : jobList) {
                jobIds[jobIndex] = job.getId();
                jobIndex++;
            }
            if(jobSize == 0){
                return Result.OK("您还没有发布过招工", jobList);
            }

            //获取工作被查询的记录
            List<EmployCheckJob> checkJobList = employCheckJobService
                    .lambdaQuery()
                    .in(EmployCheckJob::getJobId, jobIds)
                    .list();

            //获取查看我的工作的求职者id
            int size = (int)checkJobList.size();
            Object[] employeeIds = new Object[size];
            int index = 0;
            for (EmployCheckJob employCheckJob : checkJobList) {
                employeeIds[index] = employCheckJob.getUserId();
                index++;
            }

            if(size == 0){
                return Result.OK("没有人看过我的招工", checkJobList);
            }

            //根据求职者id查询求职者基本信息
            Page<HanHaiMember> page = new Page<HanHaiMember>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<HanHaiMember> hanHaiMemberList = hanHaiMemberService
                    .lambdaQuery()
                    .in(HanHaiMember::getId, employeeIds)
                    .page(page);

            //根据求职者id查询求职者简历
            for (HanHaiMember record : hanHaiMemberList.getRecords()) {
                EmployResume resume = employResumeService
                        .lambdaQuery()
                        .eq(EmployResume::getUserId, record.getId())
                        .one();
                record.setEmployResume(resume);
            }

            return Result.OK("谁看过我(谁看过我的招工)列表", hanHaiMemberList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("谁看过我(谁看过我的招工)列表查询失败");
        }
    }

    //会员中心-联系记录-我看过谁
    @Override
    public Result<?> queryWatchWhoEmployee(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try {
            //获取我看过谁的记录信息
            Page<EmployCheckJob> page = new Page<EmployCheckJob>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCheckJob> pageList = employCheckJobService
                    .lambdaQuery()
                    .eq(EmployCheckJob::getUserId, hanHaiMember.getId())
                    .page(page);

            //添加工作信息
            for (EmployCheckJob record : pageList.getRecords()) {
                //获取工作信息
                EmployJob employJob = employJobService.getById(record.getJobId());
                //添加工作信息
                record.setEmployJob(employJob);
            }

            return Result.OK("我看过谁", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.OK("我看过谁列表查询失败");
        }
    }

    //会员中心-联系记录-谁看过我(谁看过我的简历)
    @Override
    public Result<?> queryWatchMeEmployee(String token, PageBean pageBean) {

        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询我发布的简历列表
            List<EmployResume> resumeList = employResumeService
                    .lambdaQuery()
                    .eq(EmployResume::getUserId, hanHaiMember.getId())
                    .list();

            //获取查看我发布的简历id列表
            int resumeSize = (int)resumeList.size();
            Object[] resumeIds = new Object[resumeSize];
            int jobIndex = 0;
            for (EmployResume resume : resumeList) {
                resumeIds[jobIndex] = resume.getId();
                jobIndex++;
            }
            if(resumeSize == 0){
                return Result.OK("您还没有发布过简历", resumeList);
            }

            //获取简历被查询的记录
            List<EmployCheckResume> checkResumeList = employCheckResumeService
                    .lambdaQuery()
                    .in(EmployCheckResume::getResumeId, resumeIds)
                    .list();

            //获取查看我的简历的招聘者id
            int size = (int)checkResumeList.size();
            Object[] bossIds = new Object[size];
            int index = 0;
            for (EmployCheckResume employCheckResume : checkResumeList) {
                bossIds[index] = employCheckResume.getUserId();
                index++;
            }

            if(size == 0){
                return Result.OK("没有人看过我的简历", checkResumeList);
            }

            //根据招聘者id查询招聘者基本信息
            Page<HanHaiMember> page = new Page<HanHaiMember>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<HanHaiMember> hanHaiMemberList = hanHaiMemberService
                    .lambdaQuery()
                    .in(HanHaiMember::getId, bossIds)
                    .page(page);

            //根据招聘者id查询招聘者发布的工作
            for (HanHaiMember record : hanHaiMemberList.getRecords()) {
                List<EmployJob> employJobList = employJobService
                        .lambdaQuery()
                        .eq(EmployJob::getUserId, record.getId())
                        .list();

                record.setEmployJobList(employJobList);
            }

            return Result.OK("谁看过我(谁看过我的简历)", hanHaiMemberList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.OK("谁看过我(谁看过我的简历)列表查询失败");
        }
    }

}
