package org.jeecg.modules.apiService.impl;


import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBoot401Exception;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiService.UserInfoService;
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.employCheckResume.entity.EmployCheckResume;
import org.jeecg.modules.employCheckResume.service.IEmployCheckResumeService;
import org.jeecg.modules.employCollectionJob.entity.EmployCollectionJob;
import org.jeecg.modules.employCollectionJob.service.IEmployCollectionJobService;
import org.jeecg.modules.employCollectionResume.entity.EmployCollectionResume;
import org.jeecg.modules.employCollectionResume.service.IEmployCollectionResumeService;
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.employResume.entity.EmployResume;
import org.jeecg.modules.employResume.service.IEmployResumeService;
import org.jeecg.modules.employScoreRecord.entity.EmployScoreRecord;
import org.jeecg.modules.employScoreRecord.service.IEmployScoreRecordService;
import org.jeecg.modules.employVip.entity.EmployVip;
import org.jeecg.modules.employVipType.entity.EmployVipType;
import org.jeecg.modules.employVipType.service.IEmployVipTypeService;
import org.jeecg.modules.employVipUser.entity.EmployVipUser;
import org.jeecg.modules.employVipUser.service.IEmployVipUserService;
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.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {

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

    //个人信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

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

    //个人实名信息
    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;

    //企业实名信息
    @Resource
    private IEmployAhthenticationCompanyService employAhthenticationCompanyService;

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

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

    //我收藏的工作记录表
    @Resource
    private IEmployCollectionJobService employCollectionJobService;

    //我收藏的简历记录表
    @Resource
    private IEmployCollectionResumeService employCollectionResumeService;

    //我查看的工作记录表
    @Resource
    private IEmployCheckJobService employCheckJobService;

    //我查看的简历记录表
    @Resource
    private IEmployCheckResumeService employCheckResumeService;

    @Resource
    private IEmployScoreRecordService employScoreRecordService;

    //会员信息关联表
    @Resource
    private IEmployVipUserService employVipUserService;

    //会员类型表
    @Resource
    private IEmployVipTypeService employVipTypeService;


    @Resource
    private ScoreServiceImpl scoreService;
    /******************************************************************************************************************/



    @Override
    public Result<?> queryUserInfo(String token){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            HanHaiMember byId = hanHaiMemberService.getById(hanHaiMember.getId());
            return Result.OK(byId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("用户信息获取失败", null);
        }
    }

    @Override
    public Result<?> updateUserInfo(String token,String nickName,String phone,String headImage){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //用户昵称不能为空
            if(StringUtils.isBlank(nickName)){
                return Result.error("用户昵称不能为空");
            }
            if(StringUtils.isBlank(headImage)){
                return Result.error("头像不能为空");
            }

            hanHaiMember.setNickName(nickName);
            hanHaiMember.setPhone(phone);
            hanHaiMember.setHeadImage(headImage);
            hanHaiMemberService.updateById(hanHaiMember);

            return Result.OK("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }



    //lzx：获取个人中心基础数据信息
    @Override
    public Result<?> getUserCenterData(String token){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //返回信息
        Map map = new HashMap();

        try{
            //获取个人实名信息
            EmployAuthenticationPerson ahthenticationPerson = employAuthenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, hanHaiMember.getId())
                    .one();

            //获取企业实名信息
            EmployAhthenticationCompany ahthenticationCompany = employAhthenticationCompanyService
                    .lambdaQuery()
                    .eq(EmployAhthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();

            //获取是否签到
            boolean signFlag = scoreService.judgeSign(token);

            //获取用户vip类型
            String vipType = getVip(token);

            //boss
            //简历收藏数量
            int resumeCollectionNum = collectionResumeNum(hanHaiMember.getId());
            //简历查看数量(我看过谁)
            int resumeCheckNumBoss = checkResumeNumBoss(hanHaiMember.getId());
            //工作被查看数量(谁看过我)
            int jobCheckNumBoss = checkResumeNumBoss(hanHaiMember.getId());

            //employee
            //工作收藏数量
            int jobCollectionNum = collectionJobNum(hanHaiMember.getId());
            //工作查看数量(我看过谁)
            int jobcheckNumEmployee = checkJobNumEmployee(hanHaiMember.getId());
            //简历被查看数量(谁看过我)
            int resumeCheckNumEmployee = checkResumeNumEmployee(hanHaiMember.getId());

            //组装个人基础数据
            map.put("integerNum",hanHaiMember.getIntegerPrice());
            map.put("signFlag",signFlag);
            map.put("vipType", vipType);//VIP类型
            map.put("resumeCollectionNum", resumeCollectionNum);
            map.put("resumeCheckNumBoss", resumeCheckNumBoss);
            map.put("jobCheckNumBoss", jobCheckNumBoss);

            map.put("jobCollectionNum", jobCollectionNum);
            map.put("jobcheckNumEmployee", jobcheckNumEmployee);
            map.put("resumeCheckNumEmployee", resumeCheckNumEmployee);
            if(null != ahthenticationPerson){
                map.put("personAuthenticationStatus",ahthenticationPerson.getStatus());//个人实名状态
            }else {
                map.put("personAuthenticationStatus","-1");//个人实名状态
            }
            if(null != ahthenticationCompany){
                map.put("companyAuthenticationStatus",ahthenticationCompany.getStatus());//企业实名状态
            }else {
                map.put("companyAuthenticationStatus","-1");//企业实名状态
            }
            return Result.OK("获取个人中心基础数据信息",map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取个人中心基础数据信息失败",map);
        }
    }

    //获取用户VIP信息
    public String getVip(String token){
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        try{
            //查询用户vip关联
            EmployVipUser vu = employVipUserService
                    .lambdaQuery()
                    .eq(EmployVipUser::getUserId, hanHaiMember.getId())
                    .one();

            if(null == vu){
                return "暂未开通VIP";
            }

            //查询vip信息
            EmployVipType vt = employVipTypeService.getById(vu.getTypeId());

            return vt.getName();
        }catch (Exception e){
            e.printStackTrace();
            return "Vip信息查询失败";
        }
    }

    //我的工作收藏数量统计
    public int collectionJobNum(String userId){

        List<EmployCollectionJob> collectionJobList = employCollectionJobService
                .lambdaQuery()
                .eq(EmployCollectionJob::getUserId, userId)
                .list();

        return collectionJobList.size();
    }

    //我的简历收藏数量统计
    public int collectionResumeNum(String userId){

        List<EmployCollectionResume> collectionResumeList = employCollectionResumeService
                .lambdaQuery()
                .eq(EmployCollectionResume::getUserId, userId)
                .list();

        return collectionResumeList.size();
    }

    //boss
    //工作联系记录(谁看过我)
    public int checkJobNumBoss(String userId){
        //查看我发布的简历
        List<EmployJob> jobList  = employJobService
                .lambdaQuery()
                .eq(EmployJob::getUserId, userId)
                .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 0;
        }

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

        return checkJobList.size();
    }

    //简历联系记录(我看过谁)
    public int checkResumeNumBoss(String userId){
        List<EmployCheckResume> checkResumeList = employCheckResumeService
                .lambdaQuery()
                .eq(EmployCheckResume::getUserId, userId)
                .list();
        return checkResumeList.size();
    }

    //employee
    //工作联系记录(我看过谁)
    public int checkJobNumEmployee(String userId){
        List<EmployCheckJob> checkJobList = employCheckJobService
                .lambdaQuery()
                .eq(EmployCheckJob::getUserId, userId)
                .list();
        return checkJobList.size();
    }

    //简历联系记录(谁看过我)
    public int checkResumeNumEmployee(String userId){
        //1、查看我的简历
        EmployResume resume = employResumeService
                .lambdaQuery()
                .eq(EmployResume::getUserId, userId)
                .one();

        //2、根据我简历id查询简历被查看记录
        List<EmployCheckResume> checkResumeList = employCheckResumeService
                .lambdaQuery()
                .eq(EmployCheckResume::getResumeId, resume.getId())
                .list();
        return checkResumeList.size();
    }



}
