package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
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.employCategory.entity.EmployCategory;
import org.jeecg.modules.employCategory.service.IEmployCategoryService;
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.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private IEmployResumeService employResumeService;

    @Resource
    private IEmployCategoryService employCategoryService;

    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;

    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    @Lazy
    @Resource
    private CommonAPI commonApi;
    /******************************************************************************************************************/

    //简历信息-查询简历信息列表
    @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.getCategoryOne()){
                //一级分类
                query.eq(EmployResume::getCategoryOne, employResume.getCategoryOne());
            }
            if(null != employResume.getCategoryTwo()){
                //二级分类
                query.eq(EmployResume::getCategoryTwo, employResume.getCategoryTwo());
            }
            //按时间倒序
            query.orderByDesc(EmployResume::getCreateTime);


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

            for (EmployResume record : pageList.getRecords()) {
                //2、获取用户实名信息
                EmployAuthenticationPerson person = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, record.getUserId())
                        .one();
                if(null != person){
                    record.setEmployAuthenticationPerson(person);
                }

                //3、获取用户信息信息
                HanHaiMember hanHaiMember = hanHaiMemberService.getById(record.getUserId());
                record.setHanHaiMember(hanHaiMember);

            }

            //判断执行结果
            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) {
//        //权限验证
//        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        EmployResume resume = null;
        try{
            //1、获取简历详情
            resume = employResumeService.getById(resumeId);

            //分类信息
            EmployCategory categoryOne = employCategoryService.getById(resume.getCategoryOne());
            resume.setCategoryOne(categoryOne.getName());
            EmployCategory categoryTwo = employCategoryService.getById(resume.getCategoryTwo());
            resume.setCategoryTwo(categoryTwo.getName());

            //性别
            switch (resume.getSex()){
                case "0":
                    resume.setSex("男");
                    break;
                case "1":
                    resume.setSex("女");
                    break;
            }

            //结算方式
            switch (resume.getPayType()){
                case "0":
                    resume.setPayType("微信支付");
                break;
                case "1":
                    resume.setPayType("支付宝支付");
                    break;
                case "2":
                    resume.setPayType("线下结算");
                    break;
            }

            //2、获取用户实名信息
            EmployAuthenticationPerson person = employAuthenticationPersonService.getById(resume.getUserId());
            if(null != person){
                resume.setEmployAuthenticationPerson(person);
            }

            //3、用户信息
            HanHaiMember hanHaiMember = hanHaiMemberService.getById(resume.getUserId());
            resume.setHanHaiMember(hanHaiMember);

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

    //简历信息-我的简历
    @Override
    public Result<?> queryResumeByUserId(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        EmployResume resume  = null;

        try{
            resume  = employResumeService
                    .lambdaQuery()
                    .eq(EmployResume::getUserId, hanHaiMember.getId())
                    .one();

            return Result.OK("我的简历", resume);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我的简历信息获取失败", resume);
        }
    }

    //简历信息-添加简历信息
    @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) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String message = "简历信息修改失败";
        //执行结果
        boolean result = false;

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

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

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

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