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.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.JobService;
import org.jeecg.modules.employAhthenticationCompany.entity.EmployAhthenticationCompany;
import org.jeecg.modules.employAhthenticationCompany.service.IEmployAhthenticationCompanyService;
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.employCollectionJob.entity.EmployCollectionJob;
import org.jeecg.modules.employCollectionJob.service.IEmployCollectionJobService;
import org.jeecg.modules.employCompany.entity.EmployCompany;
import org.jeecg.modules.employCompany.service.IEmployCompanyService;
import org.jeecg.modules.employJob.entity.EmployJob;
import org.jeecg.modules.employJob.service.IEmployJobService;
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.HashMap;
import java.util.Map;

@Service
public class JobServiceImpl implements JobService {

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

    //工作信息表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

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

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

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

    //企业实名认证表
    @Resource
    private IEmployAhthenticationCompanyService employAhthenticationCompanyService;

    //个人实名认证表
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;

    //企业信息表
    @Resource
    private IEmployCompanyService employCompanyService;

    /******************************************************************************************************************/


    //查询工作信息列表
    @Override
    public Result<?> queryJobList(EmployJob employJob, PageBean pageBean) {
        //返回信息
        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.getAreaId()){
                //工作地域
                query.eq(EmployJob::getAreaId, employJob.getAreaId());
            }
            if(null != employJob.getTypeId()){
                //工作类型
                query.eq(EmployJob::getTypeId, employJob.getTypeId());
            }
            if(null != employJob.getNatureId()){
                //工作性质
                query.eq(EmployJob::getNatureId, employJob.getNatureId());
            }
            if(null != employJob.getCompany()){
                //所属公司
                query.eq(EmployJob::getCompany, employJob.getCompany());
            }
            //按时间倒序
            query.orderByDesc(EmployJob::getCreateTime);


            //获取工作信息列表
            pageList = query.page(page);

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

            //把用户信息放入工作详情中
            for (EmployJob record : pageList.getRecords()) {
                HanHaiMember hanHaiMember = hanHaiMemberService.getById(record.getUserId());
                record.setHanHaiMember(hanHaiMember);
            }

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

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

    @Override
    public Result<?> queryJobById(String token, String jobId) {

        //返回信息
        Map<String, Object> map = new HashMap<>();

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

            //1、查看工作详情信息
            EmployJob job = employJobService.getById(jobId);
            map.put("jobInfo", job);


            //2、查看招聘者信息
            HanHaiMember member = hanHaiMemberService.getById(job.getUserId());
            map.put("hanHaiMember", member);

            //3、根据工作关联用户id查询出用户实名认证信息
            EmployAuthenticationPerson person = employAuthenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, job.getUserId())
                    .one();
            map.put("personInfo", person);

            //4、根据关联公司id查询出公司信息
            EmployCompany company = employCompanyService
                    .lambdaQuery()
                    .eq(EmployCompany::getId, job.getCompany())
                    .one();
            map.put("companyInfo", company);

            //5、添加工作是否被收藏标识
            EmployCollectionJob collection = employCollectionJobService
                    .lambdaQuery()
                    .eq(EmployCollectionJob::getJobId, job.getId())
                    .eq(EmployCollectionJob::getUserId, hanHaiMember.getId())
                    .one();
            if(null != collection){
                map.put("collectionFlag", true);
            }else {
                map.put("collectionFlag", false);
            }

            //6、添加工作被查看记录
            //如果之前查看过相同的工作信息，将之前的记录删除掉，只保留最新的记录
            EmployCheckJob checkRecord = employCheckJobService
                    .lambdaQuery()
                    .eq(EmployCheckJob::getJobId, job.getId())
                    .eq(EmployCheckJob::getUserId, hanHaiMember.getId())
                    .one();
            if(null != checkRecord){
                employCheckJobService.removeById(checkRecord.getId());
            }
            //添加新的记录
            EmployCheckJob employCheckJob = new EmployCheckJob();
            employCheckJob.setJobId(job.getId());
            employCheckJob.setUserId(hanHaiMember.getId());
            employCheckJobService.save(employCheckJob);

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

    //我的招工
    @Override
    public Result<?> queryJobListByUserId(String token, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //查询我的招工列表信息
            Page<EmployJob> page = new Page<EmployJob>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployJob> pageList = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getUserId, hanHaiMember.getId())
                    .page(page);

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

    //添加工作信息
    @Override
    public Result<?> addJob(String token, EmployJob employJob) {
        //返回信息
        String message = "工作信息添加失败";
        //执行结果
        boolean result = false;

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

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

            //2、判断用户是否通过企业实名认证信息，没有通过实名认证信息则不能发布工作岗位信息
            EmployAhthenticationCompany ahthenticationCompany = employAhthenticationCompanyService
                    .lambdaQuery()
                    .eq(EmployAhthenticationCompany::getUserId, hanHaiMember.getId())
                    .eq(EmployAhthenticationCompany::getStatus, "1")
                    .one();
            if(null == ahthenticationCompany){
                return Result.error("未进行企业实名认证或企业实名认证审核未通过，请先去进行企业实名认证");
            }else {
                //查询企业信息
                EmployCompany company = employCompanyService
                        .lambdaQuery()
                        .eq(EmployCompany::getSocialCode, ahthenticationCompany.getSocialCode())
                        .one();
                if(null != company){
                    employJob.setCompany(company.getId());//工作信息关联公司
                    if(null == employJob.getWorkAddress()){
                        employJob.setWorkAddress(company.getAddress());//如果没有填写工作地址，则默认为公司地址
                    }
                }else {
                    return Result.error("未查询到企业信息，工作发布失败");
                }

            }

            //3、执行工作信息添加
            result = employJobService.save(employJob);

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

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

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

    //修改工作信息
    @Override
    public Result<?> updateJob(String token, EmployJob employJob) {
        //返回信息
        String message = "工作信息修改失败";
        //执行结果
        boolean result = false;

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

            //执行工作信息修改
            result = employJobService.updateById(employJob);

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

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

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

}
