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.AskLeaveFjBo;
import org.dromara.system.domain.vo.AskLeaveFjVo;
import org.dromara.system.domain.AskLeaveFj;
import org.dromara.system.mapper.AskLeaveFjMapper;
import org.dromara.system.service.IAskLeaveFjService;

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

/**
 * 请假附件Service业务层处理
 *
 * @author boman
 * @date 2024-11-04
 */
@RequiredArgsConstructor
@Service
public class AskLeaveFjServiceImpl implements IAskLeaveFjService {

    private final AskLeaveFjMapper baseMapper;

    /**
     * 查询请假附件
     */
    @Override
    public AskLeaveFjVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询请假附件列表
     */
    @Override
    public TableDataInfo<AskLeaveFjVo> queryPageList(AskLeaveFjBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AskLeaveFj> lqw = buildQueryWrapper(bo);
        Page<AskLeaveFjVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询请假附件列表
     */
    @Override
    public List<AskLeaveFjVo> queryList(AskLeaveFjBo bo) {
        LambdaQueryWrapper<AskLeaveFj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AskLeaveFj> buildQueryWrapper(AskLeaveFjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AskLeaveFj> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLeaveId() != null, AskLeaveFj::getLeaveId, bo.getLeaveId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), AskLeaveFj::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), AskLeaveFj::getUrl, bo.getUrl());
        return lqw;
    }

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

    /**
     * 修改请假附件
     */
    @Override
    public Boolean updateByBo(AskLeaveFjBo bo) {
        AskLeaveFj update = MapstructUtils.convert(bo, AskLeaveFj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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