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.CollectionService;
import org.jeecg.modules.employCollectionJob.entity.EmployCollectionJob;
import org.jeecg.modules.employCollectionJob.service.IEmployCollectionJobService;
import org.jeecg.modules.employCollectionResume.entity.EmployCollectionResume;
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.ArrayList;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

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

    //用户信息表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

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

    //工作收藏表
    @Resource
    private IEmployCollectionJobService employCollectionJobService;

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

    //简历收藏表
    @Resource
    private IEmployCollectionResumeService employCollectionResumeService;
    /******************************************************************************************************************/

    //我的收藏-收藏工作信息
    @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.OK("工作收藏列表查询失败！");
        }
    }

    //我的收藏-添加工作收藏
    @Override
    public Result<?> addJobCollection(String token, String jobId){
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //判断该工作是否已经被收藏，如果已被收藏，则取消收藏
            EmployCollectionJob collection = employCollectionJobService
                    .lambdaQuery()
                    .eq(EmployCollectionJob::getUserId, hanHaiMember.getId())
                    .eq(EmployCollectionJob::getJobId, jobId)
                    .one();
            if(null != collection){
                //执行取消收藏
                boolean result = employCollectionJobService.removeById(collection.getId());
                if(result){
                    return Result.OK("取消收藏成功");
                }else {
                    return Result.error("取消收藏失败");
                }
            }

            //添加收藏
            EmployCollectionJob collectionJob = new EmployCollectionJob();
            collectionJob.setJobId(jobId);
            collectionJob.setUserId(hanHaiMember.getId());

            //执行工作收藏操作
            boolean result = employCollectionJobService.save(collectionJob);

            if(result){
                return Result.OK("收藏成功");
            }else {
                return Result.error("收藏失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("收藏失败");
        }

    }

    //我的收藏-收藏简历信息
    @Override
    public Result<?> queryResumeCollectionList(String token, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            Page<EmployCollectionResume> page = new Page<EmployCollectionResume>(pageBean.getPageNo(), pageBean.getPageSize());
            //1、获取收藏列表
            Page<EmployCollectionResume> pageList = employCollectionResumeService
                    .lambdaQuery()
                    .eq(EmployCollectionResume::getUserId, hanHaiMember.getId())
                    .page(page);

            //2、获取收藏简历
            for (EmployCollectionResume record : pageList.getRecords()) {
                //获取简历信息
                EmployResume resume = employResumeService.getById(record.getResumeId());
                //获取用户信息
                HanHaiMember member = hanHaiMemberService.getById(resume.getUserId());
                resume.setHanHaiMember(member);
                //将简历信息添加到我的收藏中
                record.setEmployResume(resume);
            }

            return Result.OK("简历收藏列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历收藏列表获取失败");
        }
    }

    //我的收藏-添加简历收藏
    @Override
    public Result<?> addResumeCollection(String token, String resumeId){
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //1、判断该工作是否已经被收藏，如果已被收藏，则取消收藏
            EmployCollectionResume collection = employCollectionResumeService
                    .lambdaQuery()
                    .eq(EmployCollectionResume::getUserId, hanHaiMember.getId())
                    .eq(EmployCollectionResume::getResumeId, resumeId)
                    .one();
            if(null != collection){
                //执行取消收藏
                boolean result = employCollectionResumeService.removeById(collection.getId());
                if(result){
                    return Result.OK("取消收藏成功");
                }else {
                    return Result.error("取消收藏失败");
                }
            }

            //2、添加收藏
            EmployCollectionResume collectionResume = new EmployCollectionResume();
            collectionResume.setResumeId(resumeId);
            collectionResume.setUserId(hanHaiMember.getId());

            //执行工作收藏操作
            boolean result = employCollectionResumeService.save(collectionResume);

            if(result){
                return Result.OK("收藏成功");
            }else {
                return Result.error("收藏失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("收藏失败");
        }

    }

}
