package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.CollectionBean;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.EmployeeApiService;
import org.jeecg.modules.employCert.entity.EmployCert;
import org.jeecg.modules.employCert.service.IEmployCertService;
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.entity.EmployCollectionJob;
import org.jeecg.modules.employCollectionJob.service.IEmployCollectionJobService;
import org.jeecg.modules.employContract.entity.EmployContract;
import org.jeecg.modules.employContract.service.IEmployContractService;
import org.jeecg.modules.employJob.entity.EmployJob;
import org.jeecg.modules.employJob.service.IEmployJobService;
import org.jeecg.modules.employRedister.entity.EmployRedister;
import org.jeecg.modules.employRedister.service.IEmployRedisterService;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class EmployeeApiServiceImpl implements EmployeeApiService {
    /***************首页***************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //用户表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

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

    //个人简历表
    @Resource
    private IEmployResumeService employResumeService;

    /***************考证咨询***************/
    //考证咨询表
    @Resource
    private IEmployCertService employCertService;

    //报名材料表
    @Resource
    private IEmployRedisterService employRedisterService;

    /***************会员中心***************/
    //工作收藏表
    @Resource
    private IEmployCollectionJobService employCollectionJobService;

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

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

    //电子合同表
    @Resource
    private IEmployContractService employContractService;


    /**************************************首页***********************************************/
    /****************************************************************************************/
    //首页-查询工作信息列表
    @Override
    public Result<?> queryJobListByAll(EmployJob employJob, PageBean pageBean, HttpServletRequest req) {

        //返回信息
        String message = "获取工作信息列表失败!";
        //商品列表
        Page<EmployJob> pageList = null;
        //分页信息
        Page<EmployJob> page = null;

        try{
            //分页
            page = new Page<EmployJob>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployJob> query = employJobService.lambdaQuery();

            //组装查询条件
            if(null != employJob.getTypeId()){
                //工作类型
                query.eq(EmployJob::getTypeId, employJob.getTypeId());
            }
            if(null != employJob.getNatureId()){
                //工作性质
                query.eq(EmployJob::getNatureId, employJob.getNatureId());
            }


            //获取商品列表
            pageList = query.page(page);

            //判断执行结果
            if(null != pageList){
                message = "工作列表信息";
            }else {
                message = "工作列表信息为空";
            }

            //返回执行结果
            return Result.OK(message, pageList);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message, pageList);
        }
    }

    //首页-根据Id查看工作详情
    @Override
    public Result<?> queryJobById(String token, String jobId) {

        CollectionBean collectionBean = new CollectionBean();
        Boolean collectionFlag = false;

        try{
            //获取工作详情
            EmployJob one = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getId, jobId)
                    .one();

            collectionBean.setEmployJob(one);

            //添加工作被查看记录
            if(null != token){
                //权限验证
                HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

                EmployCheckJob employCheckJob = new EmployCheckJob();
                employCheckJob.setJobId(one.getId());
                employCheckJob.setUserId(hanHaiMember.getId());
                employCheckJobService.saveOrUpdate(employCheckJob);

                //添加是否被收藏标识
                EmployCollectionJob collection = employCollectionJobService
                        .lambdaQuery()
                        .eq(EmployCollectionJob::getJobId, one.getId())
                        .eq(EmployCollectionJob::getUserId, hanHaiMember.getId())
                        .one();

                if(null != collection){
                    collectionFlag = true;
                }
            }

            collectionBean.setCollectionFlag(collectionFlag);

            return Result.OK("工作详情", collectionBean);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("工作详情获取失败", collectionBean);
        }
    }

    /**************************************我的简历***********************************************/
    /********************************************************************************************/
    //我的简历
    @Override
    public Result<?> queryResume(String token) {
        //返回信息
        String message = "简历信息查询失败";

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

            //获取简历详情
            EmployJob one = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getUserId, hanHaiMember.getId())
                    .one();

            if(null == one){
                message = "没有查询到简历信息";
            }else {
                message = "简历详情";
            }

            return Result.OK("简历详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历详情获取失败");
        }
    }

    //添加简历
    @Override
    public Result<?> addResume(String token, EmployResume employResume) {

        //返回信息
        String message = "简历信息添加失败";
        //执行结果
        boolean result = false;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        employResume.setUserId(hanHaiMember.getId());

        try{
            //执行简历信息添加
            result = employResumeService.save(employResume);

            //判断执行结果
            if(result){
                message = "简历信息添加成功！";
            }else {
                message = "简历信息添加失败！";
            }

            //返回执行结果
            return Result.OK(message);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //修改简历
    @Override
    public Result<?> updateResume(String token, EmployResume employResume) {

        //返回信息
        String message = "简历信息修改失败";
        //执行结果
        boolean result = false;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //执行简历信息修改
            result = employResumeService.updateById(employResume);

            //判断执行结果
            if(result){
                message = "简历信息修改成功！";
            }else {
                message = "简历信息修改失败！";
            }

            //返回执行结果
            return Result.OK(message);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }


    /**************************************考证咨询***********************************************/
    /********************************************************************************************/

    //考证咨询-证书查询
    @Override
    public Result<?> queryCertList(PageBean pageBean) {
        try{
            Page<EmployCert> page = new Page<EmployCert>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCert> pageList = employCertService
                    .lambdaQuery()
                    .page(page);

            return Result.OK("证书咨询列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("证书咨询列表查询失败");
        }
    }

    //考证咨询-提交报名材料
    @Override
    public Result<?> addMaterial(String token, EmployRedister employRedister) {
        //返回信息
        String message = "报名材料提交失败！";

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

            //提交报名材料
            employRedister.setUserId(hanHaiMember.getId());
            boolean result = employRedisterService.save(employRedister);

            //判断招工信息是否添加成功
            if(result){
                message = "报名材料提交成功！";
            }else {
                message = "报名材料提交失败！";
            }
            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

    /***************会员中心***************/
    //会员中心-我的收藏
    @Override
    public Result<?> queryJobCollectionList(String token, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //获取工作收藏列表
            Page<EmployCollectionJob> page = new Page<EmployCollectionJob>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCollectionJob> pageList = employCollectionJobService
                    .lambdaQuery()
                    .eq(EmployCollectionJob::getUserId, hanHaiMember.getId())
                    .page(page);

            //根据收藏列表工作id查询工作信息
            for (EmployCollectionJob record : pageList.getRecords()) {
                //获取工作信息
                EmployJob job = employJobService.getById(record.getJobId());

                //将工作信息添加至收藏列表
                record.setEmployJob(job);
            }

            return Result.OK("工作收藏列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("工作收藏列表查询失败");
        }
    }

    //会员中心-我的收藏(数量统计)
    @Override
    public Result<?> queryJobCollectionCount(String token) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //获取工作收藏列表
            List<EmployCollectionJob> list = employCollectionJobService
                    .lambdaQuery()
                    .eq(EmployCollectionJob::getUserId, hanHaiMember.getId())
                    .list();

            //工作收藏数量
            int count = list.size();

            return Result.OK("我的收藏数量", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我的收藏数量查询失败");
        }
    }

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

            //获取我看过谁的记录信息
            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.error("我看过谁列表查询失败");
        }
    }

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

        try{
            //获取我看过谁的记录信息
            List<EmployCheckJob> list = employCheckJobService
                    .lambdaQuery()
                    .eq(EmployCheckJob::getUserId, hanHaiMember.getId())
                    .list();

            //获取数量统计
            int count = list.size();

            return Result.OK("我看过谁(数量统计)", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我看过谁(数量统计)失败");
        }
    }

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

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

            //查询我发布的简历列表
            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.error("谁看过我(谁看过我的简历)查询失败");
        }
    }

    //会员中心-联系记录-谁看过我(谁看过我的简历-数量统计)
    @Override
    public Result<?> queryWatchMeCount(String token) {

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

            //查询我发布的简历列表
            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();

            int count = checkResumeList.size();

            return Result.OK("谁看过我(谁看过我的简历-数量统计)", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("谁看过我(谁看过我的简历-数量统计)查询失败", 0);
        }
    }


    //会员中心-我的找活
    @Override
    public Result<?> queryResumeByUserId(String token, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            Page<EmployResume> page = new Page<EmployResume>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployResume> pageList = employResumeService
                    .lambdaQuery()
                    .eq(EmployResume::getUserId, hanHaiMember.getId())
                    .page(page);

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

    //电子合同-获取电子合同列表
    @Override
    public Result<?> queryContractList(String token, EmployContract employContract, PageBean pageBean) {

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

            //分页
            Page<EmployContract> page = new Page<EmployContract>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployContract> query = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getPersonId, hanHaiMember.getId());

            //组装查询条件
            if(null != employContract.getSignCompany()){
                query.like(EmployContract::getSignCompany, employContract.getSignCompany());
            }
            if(null != employContract.getSignPerson()){
                query.like(EmployContract::getSignPerson, employContract.getSignPerson());
            }

            //获取电子合同信息
            Page<EmployContract> pageList = query.page(page);

            return Result.OK("电子合同列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同列表查询失败");
        }

    }

    //电子合同-获取电子合同详情
    @Override
    public Result<?> queryContracById(String token, String contractId) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            EmployContract one = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getId, contractId)
                    .eq(EmployContract::getPersonId, hanHaiMember.getId())
                    .one();

            return Result.OK("电子合同详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同详情查询失败");
        }
    }
}
