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.core.domain.entity.SysDictData;
import com.zhongzheng.common.core.domain.model.LoginUser;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.recruit.bo.RecruitIncomeQueryBo;
import com.zhongzheng.modules.recruit.bo.RecruitNeedsAddBo;
import com.zhongzheng.modules.recruit.bo.RecruitNeedsEditBo;
import com.zhongzheng.modules.recruit.bo.RecruitNeedsQueryBo;
import com.zhongzheng.modules.recruit.domain.RecruitNeeds;
import com.zhongzheng.modules.recruit.mapper.RecruitNeedsMapper;
import com.zhongzheng.modules.recruit.mapper.RecruitTemplateMapper;
import com.zhongzheng.modules.recruit.service.IRecruitNeedsService;
import com.zhongzheng.modules.recruit.vo.RecruitNeedsVo;
import com.zhongzheng.modules.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 招聘需求Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-12
 */
@Service
public class RecruitNeedsServiceImpl extends ServiceImpl<RecruitNeedsMapper, RecruitNeeds> implements IRecruitNeedsService {


    @Autowired
    private RecruitNeedsMapper recruitNeedsMapper;

    @Autowired
    private ISysDictDataService dictDataService;



    @Override
    public RecruitNeedsVo queryById(Long id){
        RecruitNeeds db = this.baseMapper.selectById(id);
        RecruitNeedsVo recruitNeedsVo = BeanUtil.toBean(db, RecruitNeedsVo.class);
        SysDictData sysDictData1 = dictDataService.selectDictDataById(Convert.toLong(recruitNeedsVo.getMinimumEduDict()));
        SysDictData sysDictData = dictDataService.selectDictDataById(Convert.toLong(recruitNeedsVo.getWorkingYearsDict()));
        recruitNeedsVo.setMinimumEduDictLabel(sysDictData1.getDictLabel());
        recruitNeedsVo.setWorkingYearsDictLabel(sysDictData.getDictLabel());
        return recruitNeedsVo;
    }

    @Override
    public List<RecruitNeedsVo> queryList(RecruitNeedsQueryBo bo) {
        LambdaQueryWrapper<RecruitNeeds> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, RecruitNeeds::getCompanyId, bo.getCompanyId());
        lqw.eq(StrUtil.isNotBlank(bo.getJob()), RecruitNeeds::getJob, bo.getJob());
        lqw.eq(bo.getIncomeStart() != null, RecruitNeeds::getIncomeStart, bo.getIncomeStart());
        lqw.eq(bo.getIncomeEnd() != null, RecruitNeeds::getIncomeEnd, bo.getIncomeEnd());
        lqw.eq(StrUtil.isNotBlank(bo.getDuty()), RecruitNeeds::getDuty, bo.getDuty());
        lqw.eq(StrUtil.isNotBlank(bo.getRequirement()), RecruitNeeds::getRequirement, bo.getRequirement());
        lqw.eq(StrUtil.isNotBlank(bo.getWelfare()), RecruitNeeds::getWelfare, bo.getWelfare());
        lqw.eq(StrUtil.isNotBlank(bo.getJobKey()), RecruitNeeds::getJobKey, bo.getJobKey());
        lqw.eq(StrUtil.isNotBlank(bo.getWorkingYearsDict()), RecruitNeeds::getWorkingYearsDict, bo.getWorkingYearsDict());
        lqw.eq(bo.getWorkingYearsStart() != null, RecruitNeeds::getWorkingYearsStart, bo.getWorkingYearsStart());
        lqw.eq(bo.getWorkingYearsEnd() != null, RecruitNeeds::getWorkingYearsEnd, bo.getWorkingYearsEnd());
        lqw.eq(StrUtil.isNotBlank(bo.getMinimumEduDict()), RecruitNeeds::getMinimumEduDict, bo.getMinimumEduDict());
        lqw.eq(StrUtil.isNotBlank(bo.getMajor()), RecruitNeeds::getMajor, bo.getMajor());
        lqw.eq(bo.getStatus() != null, RecruitNeeds::getStatus, bo.getStatus());
        lqw.eq(bo.getIncomeType() != null, RecruitNeeds::getIncomeType, bo.getIncomeType());
        lqw.eq(StrUtil.isNotBlank(bo.getProvince()), RecruitNeeds::getProvince, bo.getProvince());
        lqw.eq(StrUtil.isNotBlank(bo.getCity()), RecruitNeeds::getCity, bo.getCity());
        lqw.eq(StrUtil.isNotBlank(bo.getDistrict()), RecruitNeeds::getDistrict, bo.getDistrict());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), RecruitNeeds::getAddress, bo.getAddress());
        lqw.eq(bo.getPeopleNum() != null, RecruitNeeds::getPeopleNum, bo.getPeopleNum());
        lqw.eq(StrUtil.isNotBlank(bo.getJobNature()), RecruitNeeds::getJobNature, bo.getJobNature());
        lqw.eq(StrUtil.isNotBlank(bo.getBonusCertificate()), RecruitNeeds::getBonusCertificate, bo.getBonusCertificate());
        lqw.eq(bo.getIsUrgent() != null, RecruitNeeds::getIsUrgent, bo.getIsUrgent());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<RecruitNeeds> queryRecruitNeedsList(RecruitNeedsQueryBo bo) {
        if (bo.getIncomeType() != null && bo.getIncomeType() == 2){
            for (RecruitIncomeQueryBo recruitIncomeQueryBo : bo.getIncomeList()) {
                recruitIncomeQueryBo.setIncomeStart(recruitIncomeQueryBo.getIncomeStart().divide(new BigDecimal("12"),1,BigDecimal.ROUND_HALF_UP));
                recruitIncomeQueryBo.setIncomeEnd(recruitIncomeQueryBo.getIncomeEnd().divide(new BigDecimal("12"),1,BigDecimal.ROUND_HALF_UP));
            }
        }
        if (bo.getJobNature() != null){
            String[] split = bo.getJobNature().split(",");
            bo.setJobNatures(Arrays.asList(split));
        }
        return recruitNeedsMapper.queryRecruitNeedsList(bo);
    }

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

    @Override
    public Boolean insertByAddBo(RecruitNeedsAddBo bo) {
        RecruitNeeds add = BeanUtil.toBean(bo, RecruitNeeds.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(RecruitNeedsEditBo bo,String operator_nickName) {
        RecruitNeeds update = BeanUtil.toBean(bo, RecruitNeeds.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if(bo.getStatus()!=null&&bo.getStatus()==2){
            RecruitNeedsVo vo = queryById(bo.getId());
            if(vo.getStatus()<1){
                throw new CustomException("禁止发布");
            }
            if(vo.getStatus()==1){
                if(update.getPushTime()!=null){
                    throw new CustomException("不允许二次发布");
                }
                //发布操作
                update.setPushTime(DateUtils.getNowTime());
                update.setPushMan(operator_nickName);
            }
        }
        return this.updateById(update);
    }

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

    }

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

    @Override
    public List<RecruitNeeds> queryRecruitNeedsListString(RecruitNeedsQueryBo recruitNeedsQueryBo) {
        return recruitNeedsMapper.queryRecruitNeedsListString(recruitNeedsQueryBo);
    }
}
