package com.zhongzheng.modules.recruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.recruit.bo.RecruitResumeAddBo;
import com.zhongzheng.modules.recruit.bo.RecruitResumeEditBo;
import com.zhongzheng.modules.recruit.bo.RecruitResumeQueryBo;
import com.zhongzheng.modules.recruit.domain.RecruitResume;
import com.zhongzheng.modules.recruit.mapper.RecruitResumeMapper;
import com.zhongzheng.modules.recruit.service.IRecruitResumeService;
import com.zhongzheng.modules.recruit.vo.RecruitResumeVo;
import com.zhongzheng.modules.user.bo.UserEditBo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 招聘用户简历Service业务层处理
 *
 * @author hjl
 * @date 2021-08-18
 */
@Service
public class RecruitResumeServiceImpl extends ServiceImpl<RecruitResumeMapper, RecruitResume> implements IRecruitResumeService {

    @Override
    public RecruitResumeVo queryById(Long resumeId){
        RecruitResume db = this.baseMapper.selectById(resumeId);
        return BeanUtil.toBean(db, RecruitResumeVo.class);
    }

    @Override
    public List<RecruitResumeVo> queryList(RecruitResumeQueryBo bo) {
        LambdaQueryWrapper<RecruitResume> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, RecruitResume::getUserId, bo.getUserId());
        lqw.like(StrUtil.isNotBlank(bo.getRealname()), RecruitResume::getRealname, bo.getRealname());
        lqw.eq(bo.getUserBirth() != null, RecruitResume::getUserBirth, bo.getUserBirth());
        lqw.eq(StrUtil.isNotBlank(bo.getEduLevel()), RecruitResume::getEduLevel, bo.getEduLevel());
        lqw.eq(StrUtil.isNotBlank(bo.getTelphone()), RecruitResume::getTelphone, bo.getTelphone());
        lqw.eq(StrUtil.isNotBlank(bo.getJob()), RecruitResume::getJob, bo.getJob());
        lqw.eq(StrUtil.isNotBlank(bo.getIntroduceVideoUrl()), RecruitResume::getIntroduceVideoUrl, bo.getIntroduceVideoUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getEduExperienceJson()), RecruitResume::getEduExperienceJson, bo.getEduExperienceJson());
        lqw.eq(StrUtil.isNotBlank(bo.getWorkExperienceJson()), RecruitResume::getWorkExperienceJson, bo.getWorkExperienceJson());
        lqw.eq(StrUtil.isNotBlank(bo.getCertificateJson()), RecruitResume::getCertificateJson, bo.getCertificateJson());
        lqw.eq(StrUtil.isNotBlank(bo.getSelfEvaluation()), RecruitResume::getSelfEvaluation, bo.getSelfEvaluation());
        lqw.eq(bo.getStatus() != null, RecruitResume::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<RecruitResumeVo> entity2Vo(Collection<RecruitResume> collection) {
        List<RecruitResumeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, RecruitResumeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<RecruitResume> page = (Page<RecruitResume>)collection;
            Page<RecruitResumeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(RecruitResumeAddBo bo) throws IllegalAccessException {
        RecruitResume add = BeanUtil.toBean(bo, RecruitResume.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        Field[] field = bo.getClass().getDeclaredFields();
        Integer integer = countUser(add);
        String num = String.valueOf(new BigDecimal(integer.toString())
                .divide(new BigDecimal(Convert.toStr(field.length)),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
        add.setCompleteness(Convert.toInt(num));
        return this.save(add);
    }



    @Override
    public Boolean updateByEditBo(RecruitResumeEditBo bo) throws IllegalAccessException {
        RecruitResume update = BeanUtil.toBean(bo, RecruitResume.class);
        update.setTenantId(null);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        Field[] field = update.getClass().getDeclaredFields();
        boolean b = this.updateById(update);
        RecruitResumeVo recruitResumeVo = this.queryById(update.getResumeId());
        Integer integer = countUserEditBo(recruitResumeVo);
        integer = integer+2;
        String num = String.valueOf(new BigDecimal(integer.toString())
                .divide(new BigDecimal(Convert.toStr(field.length)),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
        update.setCompleteness(Convert.toInt(num));
        boolean b1 = this.updateById(update);
        return b1;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(RecruitResume entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }


    public Integer  countUser(RecruitResume userVo) throws IllegalAccessException {
        // Java的反射
        Field[] field = userVo.getClass().getDeclaredFields();
        int count = 0;
        for(int i=0; i<field.length; i++){
            Field f = field[i];
            f.setAccessible(true);
            if (f.get(userVo) != null){
                count++;
            }
        }
        return count;
    }

    public Integer  countUserEditBo(RecruitResumeVo userVo) throws IllegalAccessException {
        // Java的反射
        Field[] field = userVo.getClass().getDeclaredFields();
        int count = 0;
        for(int i=0; i<field.length; i++){
            Field f = field[i];
            f.setAccessible(true);
            if (f.get(userVo) != null){
                count++;
            }
        }
        return count;
    }
}
