package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.CollectionBean;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.ResumeService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
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.employCollectionResume.entity.EmployCollectionResume;
import org.jeecg.modules.employCollectionResume.service.IEmployCollectionResumeService;
import org.jeecg.modules.employJob.entity.EmployJob;
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 org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class ResumeServiceImpl implements ResumeService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

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

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

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

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

    //简历查看表
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;
    /******************************************************************************************************************/

    //查询简历信息列表
    @Override
    public Result<?> queryResumeList(EmployResume employResume, PageBean pageBean) {
        //返回信息
        String message = "获取简历信息列表失败!";
        //简历信息列表
        Page<EmployResume> pageList = null;
        //分页信息
        Page<EmployResume> page = null;

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

            //组装查询条件
            if(null != employResume.getTypeId()){
                //工作类型
                query.eq(EmployResume::getTypeId, employResume.getTypeId());
            }
            if(null != employResume.getNatureId()){
                //工作性质
                query.eq(EmployResume::getNatureId, employResume.getNatureId());
            }
            //按时间倒序
            query.orderByDesc(EmployResume::getCreateTime);


            //获取简历信息列表
            pageList = query.page(page);

            //把用户信息放入简历详情中
            for (EmployResume record : pageList.getRecords()) {
                //获取用户信息
                HanHaiMember hanHaiMember = hanHaiMemberService.getById(record.getUserId());
                record.setHanHaiMember(hanHaiMember);

                //获取个人实名认证信息
                EmployAuthenticationPerson person = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, record.getUserId())
                        .one();
                record.setEmployAuthenticationPerson(person);
            }

            //判断执行结果
            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<?> queryResumeById(String token, String resumeId) {
        //返回信息
        Map<String, Object> map = new HashMap<>();

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

            //1、获取简历详情
            EmployResume resume = employResumeService.getById(resumeId);
            map.put("resume", resume);

            //2、获取个人实名认证信息
            EmployAuthenticationPerson person = employAuthenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, resume.getUserId())
                    .one();
            map.put("authenticationPerson", person);

            //3、获取简历拥有者信息
            HanHaiMember member = hanHaiMemberService.getById(resume.getUserId());
            map.put("hanHaiMember", member);

            //4、添加是否被收藏标识
            EmployCollectionResume collection = employCollectionResumeService
                    .lambdaQuery()
                    .eq(EmployCollectionResume::getResumeId, resume.getId())
                    .eq(EmployCollectionResume::getUserId, hanHaiMember.getId())
                    .one();
            if(null != collection){
                map.put("collectionFlag", true);
            }else {
                map.put("collectionFlag", false);
            }

            //5、添加简历被查看记录
            //如果之前查看过相同的工作信息，将之前的记录删除掉，只保留最新的记录
            EmployCheckResume resumeRecord = employCheckResumeService
                    .lambdaQuery()
                    .eq(EmployCheckResume::getResumeId, resume.getId())
                    .eq(EmployCheckResume::getUserId, hanHaiMember.getId())
                    .one();
            if(null != resumeRecord){
                employCheckResumeService.removeById(resumeRecord.getId());
            }
            //添加新的记录
            EmployCheckResume employCheckResume = new EmployCheckResume();
            employCheckResume.setResumeId(resume.getId());
            employCheckResume.setUserId(hanHaiMember.getId());
            employCheckResumeService.saveOrUpdate(employCheckResume);

            return Result.OK("简历详情", map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历详情查询失败", map);
        }
    }

    //会员中心-我的简历
    @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<?> addResume(String token, EmployResume employResume) {
        //返回信息
        String message = "简历信息添加失败";
        //执行结果
        boolean result = false;
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        employResume.setUserId(hanHaiMember.getId());

        try{
            //判断用户是否通过个人实名认证企业信息，没有通过实名认证信息则不能发布简历信息
            EmployAuthenticationPerson authenticationPerson = employAuthenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                    .eq(EmployAuthenticationPerson::getStatus, "1")
                    .one();
            if(null == authenticationPerson){
                return Result.error("未进行个人实名认证或个人实名认证审核未通过，请先去进行个人实名认证");
            }

            //查询是用户是否存在简历，存在则进行修改，不存在则进行新增
            EmployResume resume = employResumeService
                    .lambdaQuery()
                    .eq(EmployResume::getUserId, hanHaiMember.getId())
                    .one();

            if(null == resume){
                //执行新增
                result = employResumeService.save(employResume);
            }else {
                //执行修改
                employResume.setUserId(hanHaiMember.getId());
                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<?> 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);
        }
    }
}
