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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.order.mapper.OrderMapper;
import com.zhongzheng.modules.recruit.bo.RecruitTemplateAddBo;
import com.zhongzheng.modules.recruit.bo.RecruitTemplateEditBo;
import com.zhongzheng.modules.recruit.bo.RecruitTemplateQueryBo;
import com.zhongzheng.modules.recruit.domain.RecruitTemplate;
import com.zhongzheng.modules.recruit.mapper.RecruitTemplateMapper;
import com.zhongzheng.modules.recruit.service.IRecruitTemplateService;
import com.zhongzheng.modules.recruit.vo.RecruitTemplateVo;
import org.springframework.beans.factory.annotation.Autowired;
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.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 招聘模板Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-12
 */
@Service
public class RecruitTemplateServiceImpl extends ServiceImpl<RecruitTemplateMapper, RecruitTemplate> implements IRecruitTemplateService {

    @Autowired
    private RecruitTemplateMapper recruitTemplateMapper;

    @Override
    public RecruitTemplateVo queryById(Long id){
        RecruitTemplate db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, RecruitTemplateVo.class);
    }

    @Override
    public List<RecruitTemplateVo> queryList(RecruitTemplateQueryBo bo) {
        LambdaQueryWrapper<RecruitTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, RecruitTemplate::getCompanyId, bo.getCompanyId());
        lqw.eq(StrUtil.isNotBlank(bo.getJob()), RecruitTemplate::getJob, bo.getJob());
        lqw.eq(bo.getIncomeStart() != null, RecruitTemplate::getIncomeStart, bo.getIncomeStart());
        lqw.eq(bo.getIncomeEnd() != null, RecruitTemplate::getIncomeEnd, bo.getIncomeEnd());
        lqw.eq(StrUtil.isNotBlank(bo.getDuty()), RecruitTemplate::getDuty, bo.getDuty());
        lqw.eq(StrUtil.isNotBlank(bo.getRequirement()), RecruitTemplate::getRequirement, bo.getRequirement());
        lqw.eq(StrUtil.isNotBlank(bo.getWelfare()), RecruitTemplate::getWelfare, bo.getWelfare());
        lqw.eq(StrUtil.isNotBlank(bo.getJobKey()), RecruitTemplate::getJobKey, bo.getJobKey());
        lqw.eq(StrUtil.isNotBlank(bo.getWorkingYearsDict()), RecruitTemplate::getWorkingYearsDict, bo.getWorkingYearsDict());
        lqw.eq(bo.getWorkingYearsStart() != null, RecruitTemplate::getWorkingYearsStart, bo.getWorkingYearsStart());
        lqw.eq(bo.getWorkingYearsEnd() != null, RecruitTemplate::getWorkingYearsEnd, bo.getWorkingYearsEnd());
        lqw.eq(StrUtil.isNotBlank(bo.getMinimumEduDict()), RecruitTemplate::getMinimumEduDict, bo.getMinimumEduDict());
        lqw.eq(StrUtil.isNotBlank(bo.getMajor()), RecruitTemplate::getMajor, bo.getMajor());
        lqw.eq(bo.getStatus() != null, RecruitTemplate::getStatus, bo.getStatus());
        lqw.eq(bo.getIncomeType() != null, RecruitTemplate::getIncomeType, bo.getIncomeType());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<RecruitTemplate> queryRecruitTemplateList(RecruitTemplateQueryBo bo) {
        return recruitTemplateMapper.queryRecruitTemplateList(bo);
    }

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

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

    @Override
    public Boolean updateByEditBo(RecruitTemplateEditBo bo) {
        RecruitTemplate update = BeanUtil.toBean(bo, RecruitTemplate.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

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

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