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.SeekService;
import org.jeecg.modules.employAuthenticationCompany.service.IEmployAuthenticationCompanyService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
import org.jeecg.modules.employCategory.service.IEmployCategoryService;
import org.jeecg.modules.employResume.entity.EmployResume;
import org.jeecg.modules.employResume.service.IEmployResumeService;
import org.jeecg.modules.employSeek.entity.EmploySeek;
import org.jeecg.modules.employSeek.service.IEmploySeekService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SeekServiceImpl implements SeekService {

    @Autowired
    private IEmploySeekService employSeekService;
    @Autowired
    private IHanHaiMemberService hanHaiMemberService;
    @Autowired
    private IEmployResumeService employResumeService;
    @Resource
    private IEmployCategoryService employCategoryService;
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;
    @Resource
    private IEmployAuthenticationCompanyService employAuthenticationCompanyService;
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;



    //求职信息-查询求职信息列表
    @Override
    public Result<?> querySeekList(EmploySeek employSeek, PageBean pageBean){
        Page page =  new Page(pageBean.getPageNo(), pageBean.getPageSize());
        Page pageList = employSeekService.lambdaQuery().page(page);
        //循环替换数据
        List records = pageList.getRecords();
        //创建一个新的集合
        List<EmploySeek> list = new ArrayList<>();
        for (Object record : records) {
            EmploySeek seek = (EmploySeek) record;
            seek.setHanHaiMember(hanHaiMemberService.getById(seek.getUserId()));
            seek.setEmployResume(employResumeService.getById(seek.getResumeId()));
            list.add(seek);
        }
        pageList.setRecords(list);
        return Result.OK("求职信息查询成功", pageList);
    }

    //求职信息-根据Id查看求职信息详情
    @Override
    public Result<?> querySeekById(String token, String id){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        EmploySeek one = employSeekService.getById(id);
        one.setEmployResume(employResumeService.getById(one.getResumeId()));
        one.setHanHaiMember(hanHaiMemberService.getById(hanHaiMember.getId()));
        one.setCategoryOne(employCategoryService.getById(one.getCategoryOne()).getName());
        one.setCategoryTwo(employCategoryService.getById(one.getCategoryTwo()).getName());

        return Result.OK("求职信息查询成功", one);
    }
    //求职信息-添加求职信息
    @Override
    public Result<?> addSeek(String token, EmploySeek employSeek){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //根据用户信息查询是否做过实名认证
        EmployAuthenticationPerson authentication = employAuthenticationPersonService.lambdaQuery()
                .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                .one();
        if(null == authentication){
            return Result.error("请先进行实名认证");
        }
        //判断用户是否已实名认证通过
        if(!"1".equals(authentication.getStatus())){
           return Result.error("认证正在审核中，请通过之后再来");
        }
        //判断用户是否已经填写了个人简历
        EmployResume resume = employResumeService.lambdaQuery()
                .eq(EmployResume::getUserId, hanHaiMember.getId())
                .one();
        if(null == resume){
            return Result.error("请先进行个人简历填写");
        }

        //根据用户标识查询是否存在求职信息
        EmploySeek one = employSeekService.lambdaQuery()
                .eq(EmploySeek::getUserId, hanHaiMember.getId())
                .one();
        if(null != one){
            return Result.error("您有求职订单未结束");
        }else{
            employSeek.setUserId(hanHaiMember.getId());
            employSeek.setResumeId(resume.getId());
            employSeek.setCreateTime(new Date());
            employSeekService.save(employSeek);
        }
        return Result.OK("求职信息添加成功", employSeek);
    }
    //求职信息-修改求职信息
    public Result<?> updateSeek(String token, EmploySeek employSeek){
        return null;
    }

    //查询我的求职信息列表-求职订单
    public Result<?> queryMySeekList(String token, EmploySeek employSeek, PageBean pageBean){
        return null;
    }

}
