package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.system.query.QueryGenerator;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.CollectionBean;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.BossApiService;
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.employContract.entity.EmployContract;
import org.jeecg.modules.employContract.service.IEmployContractService;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BossApiServiceImpl implements BossApiService {
    /***************首页***************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    @Resource
    private IHanHaiMemberService hanHaiMemberService;

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

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

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

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

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

    //电子合同表
    @Resource
    private IEmployContractService employContractService;

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

    //获取所有简历带分页
    @Override
    public Result<?> queryResumeListByAll(EmployResume employResume, PageBean pageBean, HttpServletRequest req) {
        try{
            QueryWrapper<EmployResume> queryWrapper = QueryGenerator.initQueryWrapper(employResume, req.getParameterMap());
            Page<EmployResume> page = new Page<EmployResume>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployResume> pageList = employResumeService.page(page, queryWrapper);
            return Result.OK("简历列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历列表查询失败");
        }
    }

    //根据id查看简历详情
    @Override
    public Result<?> queryResumeById(String token, String resumeId) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        Map<String, Object> map = new HashMap<>();

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

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

            //3、添加是否被收藏标识
            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);
            }

            //4、添加简历被查看记录
            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.OK("简历详情查询失败", map);
        }
    }

    //首页-发布招工
    @Override
    public Result<?> addJob(EmployJob employJob) {
        //返回信息
        String message = "发布招工失败";

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

            //添加招工信息
            //employJob.setUserId(hanHaiMember.getId());
            boolean result = employJobService.save(employJob);

            //判断招工信息是否添加成功
            if(result){
                message = "发布招工成功！";
            }else {
                message = "发布招工失败！";
            }
            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

    /***************会员中心***************/
    //会员中心-我的收藏
    @Override
    public Result<?> queryJobCollectionList(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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

            //根据收藏列表关联简历id获取简历信息
            for (EmployCollectionResume record : pageList.getRecords()) {
                //获取简历信息
                EmployResume resume = employResumeService.getById(record.getResumeId());
                //将简历信息添加到我的收藏中
                record.setEmployResume(resume);
            }
            return Result.OK("简历收藏列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历收藏列表查询失败");
        }
    }

    //会员中心-我的收藏(数量统计)
    @Override
    public Result<?> queryJobCollectionCount(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //获取收藏列表
            List<EmployCollectionResume> list = employCollectionResumeService
                    .lambdaQuery()
                    .eq(EmployCollectionResume::getUserId, hanHaiMember.getId())
                    .list();

            int count = list.size();

            return Result.OK("简历收藏数量统计", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("简历收藏数量统计失败", 0);
        }
    }

    //会员中心-联系记录-我看过谁
    @Override
    public Result<?> queryWatchWho(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查看简历查看记录
            Page<EmployCheckResume> page = new Page<EmployCheckResume>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCheckResume> pageList = employCheckResumeService
                    .lambdaQuery()
                    .eq(EmployCheckResume::getUserId, hanHaiMember.getId())
                    .page(page);

            //根据查看记录关联简历id获取简历信息
            for (EmployCheckResume record : pageList.getRecords()) {
                //获取简历信息
                EmployResume employResume = employResumeService.getById(record.getResumeId());
                //将简历信息添加到查看记录中
                record.setEmployResume(employResume);
            }

            return Result.OK("我看过谁", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我看过谁列表查询失败");
        }
    }

    //会员中心-联系记录-我看过谁(数量统计)
    @Override
    public Result<?> queryWatchWhoCount(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查看简历查看记录
            List<EmployCheckResume> list = employCheckResumeService
                    .lambdaQuery()
                    .eq(EmployCheckResume::getUserId, hanHaiMember.getId())
                    .list();

            int count = list.size();

            return Result.OK("我看过谁(数量统计)", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("我看过谁(数量统计)失败", 0);
        }
    }

    //会员中心-联系记录-谁看过我(谁看过我的招工)
    @Override
    public Result<?> queryWatchMe(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询我发布的工作列表
            List<EmployJob> jobList = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getUserId, hanHaiMember.getId())
                    .list();

            //获取查看我发布的工作id列表
            int jobSize = (int)jobList.size();
            Object[] jobIds = new Object[jobSize];
            int jobIndex = 0;
            for (EmployJob job : jobList) {
                jobIds[jobIndex] = job.getId();
                jobIndex++;
            }
            if(jobSize == 0){
                return Result.OK("您还没有发布过招工", jobList);
            }

            //获取工作被查询的记录
            List<EmployCheckJob> checkJobList = employCheckJobService
                    .lambdaQuery()
                    .in(EmployCheckJob::getJobId, jobIds)
                    .list();

            //获取查看我的工作的求职者id
            int size = (int)checkJobList.size();
            Object[] employeeIds = new Object[size];
            int index = 0;
            for (EmployCheckJob employCheckJob : checkJobList) {
                employeeIds[index] = employCheckJob.getUserId();
                index++;
            }

            if(size == 0){
                return Result.OK("没有人看过我的招工", checkJobList);
            }

            //根据求职者id查询求职者基本信息
            Page<HanHaiMember> page = new Page<HanHaiMember>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<HanHaiMember> hanHaiMemberList = hanHaiMemberService
                    .lambdaQuery()
                    .in(HanHaiMember::getId, employeeIds)
                    .page(page);

            //根据求职者id查询求职者简历
            for (HanHaiMember record : hanHaiMemberList.getRecords()) {
                List<EmployResume> employResumeList = employResumeService
                        .lambdaQuery()
                        .eq(EmployResume::getUserId, record.getId())
                        .list();

                EmployResume resume = employResumeService.getById(record.getId());

                record.setEmployResume(resume);
            }

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

    //会员中心-联系记录-谁看过我(谁看过我的招工)
    @Override
    public Result<?> queryWatchMeCount(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询我发布的工作列表
            List<EmployJob> jobList = employJobService
                    .lambdaQuery()
                    .eq(EmployJob::getUserId, hanHaiMember.getId())
                    .list();

            //获取查看我发布的工作id列表
            int jobSize = (int)jobList.size();
            Object[] jobIds = new Object[jobSize];
            int jobIndex = 0;
            for (EmployJob job : jobList) {
                jobIds[jobIndex] = job.getId();
                jobIndex++;
            }
            if(jobSize == 0){
                return Result.OK("您还没有发布过招工", jobList);
            }

            //获取工作被查询的记录
            List<EmployCheckJob> checkJobList = employCheckJobService
                    .lambdaQuery()
                    .in(EmployCheckJob::getJobId, jobIds)
                    .list();

            int count = checkJobList.size();

            return Result.OK("谁看过我(数量统计)", count);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("谁看过我(数量统计)失败", 0);
        }
    }

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

        try{
            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.error("我的招工列表查询失败");
        }
    }

    //电子合同-获取电子合同列表
    @Override
    public Result<?> queryContractList(String token, EmployContract employContract, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //分页
            Page<EmployContract> page = new Page<EmployContract>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployContract> query = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getBossId, hanHaiMember.getId());

            //组装查询条件
            if(null != employContract.getSignCompany()){
                query.like(EmployContract::getSignCompany, employContract.getSignCompany());
            }
            if(null != employContract.getSignPerson()){
                query.like(EmployContract::getSignPerson, employContract.getSignPerson());
            }

            //获取电子合同信息
            Page<EmployContract> pageList = query.page(page);

            return Result.OK("电子合同列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同列表查询失败");
        }

    }

    //电子合同-获取电子合同详情
    @Override
    public Result<?> queryContracById(String token, String contractId) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            EmployContract one = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getBossId, hanHaiMember.getId())
                    .eq(EmployContract::getId, contractId)
                    .one();

            return Result.OK("电子合同详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同详情查询失败");
        }
    }
}
