package com.yongqi.xinrenli.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 com.yongqi.xinrenli.domain.bo.DbFileBo;
import com.yongqi.xinrenli.domain.vo.DbFileVo;
import com.yongqi.xinrenli.domain.DbFile;
import com.yongqi.xinrenli.mapper.DbFileMapper;
import com.yongqi.xinrenli.service.IDbFileService;

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

/**
 * 文件Service业务层处理
 *
 * @author zyf
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class DbFileServiceImpl implements IDbFileService {

    private final DbFileMapper baseMapper;

    /**
     * 查询文件
     */
    @Override
    public DbFileVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件列表
     */
    @Override
    public TableDataInfo<DbFileVo> queryPageList(DbFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DbFile> lqw = buildQueryWrapper(bo);
        Page<DbFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件列表
     */
    @Override
    public List<DbFileVo> queryList(DbFileBo bo) {
        LambdaQueryWrapper<DbFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DbFile> buildQueryWrapper(DbFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DbFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getServiceType()), DbFile::getServiceType, bo.getServiceType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), DbFile::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getFileType()), DbFile::getFileType, bo.getFileType());
        lqw.eq(bo.getSort() != null, DbFile::getSort, bo.getSort());
        lqw.eq(bo.getDeleteStatus() != null, DbFile::getDeleteStatus, bo.getDeleteStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCreateName()), DbFile::getCreateName, bo.getCreateName());
        lqw.eq(bo.getModifyTime() != null, DbFile::getModifyTime, bo.getModifyTime());
        lqw.like(StringUtils.isNotBlank(bo.getModifyName()), DbFile::getModifyName, bo.getModifyName());
        return lqw;
    }

    /**
     * 新增文件
     */
    @Override
    public Boolean insertByBo(DbFileBo bo) {
        DbFile add = BeanUtil.toBean(bo, DbFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件
     */
    @Override
    public Boolean updateByBo(DbFileBo bo) {
        DbFile update = BeanUtil.toBean(bo, DbFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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