package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TblUserVacateBo;
import org.dromara.system.domain.vo.TblUserVacateVo;
import org.dromara.system.domain.TblUserVacate;
import org.dromara.system.mapper.TblUserVacateMapper;
import org.dromara.system.service.ITblUserVacateService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员请假Service业务层处理
 *
 * @author Lion Li
 * @date 2024-02-22
 */
@RequiredArgsConstructor
@Service
public class TblUserVacateServiceImpl implements ITblUserVacateService {

    private final TblUserVacateMapper baseMapper;

    /**
     * 查询人员请假
     */
    @Override
    public TblUserVacateVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询人员请假列表
     */
    @Override
    public TableDataInfo<TblUserVacateVo> queryPageList(TblUserVacateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblUserVacate> lqw = buildQueryWrapper(bo);
        Page<TblUserVacateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询人员请假列表
     */
    @Override
    public List<TblUserVacateVo> queryList(TblUserVacateBo bo) {
        LambdaQueryWrapper<TblUserVacate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblUserVacate> buildQueryWrapper(TblUserVacateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblUserVacate> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserInfo()), TblUserVacate::getUserInfo, bo.getUserInfo());
        lqw.eq(bo.getStartDate() != null, TblUserVacate::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, TblUserVacate::getEndDate, bo.getEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getDes()), TblUserVacate::getDes, bo.getDes());
        lqw.eq(bo.getStatus() != null, TblUserVacate::getStatus, bo.getStatus());
        lqw.eq(bo.getCTime() != null, TblUserVacate::getCTime, bo.getCTime());
        lqw.eq(bo.getUTime() != null, TblUserVacate::getUTime, bo.getUTime());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblUserVacate::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblUserVacate::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增人员请假
     */
    @Override
    public Boolean insertByBo(TblUserVacateBo bo) {
        TblUserVacate add = MapstructUtils.convert(bo, TblUserVacate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改人员请假
     */
    @Override
    public Boolean updateByBo(TblUserVacateBo bo) {
        TblUserVacate update = MapstructUtils.convert(bo, TblUserVacate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TblUserVacate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除人员请假
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
