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.dromara.system.mapper.FileCategoryMapper;
import org.dromara.system.service.IFileCategoryService;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.FileCategoryBo;
import org.dromara.system.domain.vo.FileCategoryVo;
import org.dromara.system.domain.FileCategory;


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

/**
 * 文件分类Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-01
 */
@RequiredArgsConstructor
@Service
public class FileCategoryServiceImpl implements IFileCategoryService {

    private final FileCategoryMapper baseMapper;

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

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

    /**
     * 查询文件分类列表
     */
    @Override
    public List<FileCategoryVo> queryList(FileCategoryBo bo) {
        LambdaQueryWrapper<FileCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FileCategory> buildQueryWrapper(FileCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FileCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), FileCategory::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), FileCategory::getCode, bo.getCode());
        lqw.eq(bo.getParentId() != null, FileCategory::getParentId, bo.getParentId());
        lqw.eq(bo.getType() != null, FileCategory::getType, bo.getType());
        lqw.eq(bo.getStatus() != null, FileCategory::getStatus, bo.getStatus());
        lqw.orderByAsc(FileCategory::getSort);
        return lqw;
    }

    /**
     * 新增文件分类
     */
    @Override
    public Boolean insertByBo(FileCategoryBo bo) {
        FileCategory add = MapstructUtils.convert(bo, FileCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件分类
     */
    @Override
    public Boolean updateByBo(FileCategoryBo bo) {
        FileCategory update = MapstructUtils.convert(bo, FileCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 查询分类树结构
     */
    @Override
    public List<FileCategoryVo> queryCategoryTree() {
        LambdaQueryWrapper<FileCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(FileCategory::getStatus, 0);
        lqw.orderByAsc(FileCategory::getSort);
        List<FileCategoryVo> list = baseMapper.selectVoList(lqw);
        return buildTree(list, 0L);
    }

    /**
     * 根据类型查询分类列表
     */
    @Override
    public List<FileCategoryVo> queryByType(Integer type) {
        LambdaQueryWrapper<FileCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(FileCategory::getType, type);
        lqw.eq(FileCategory::getStatus, 0);
        lqw.orderByAsc(FileCategory::getSort);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建树形结构
     */
    private List<FileCategoryVo> buildTree(List<FileCategoryVo> list, Long parentId) {
        return list.stream()
            .filter(item -> parentId.equals(item.getParentId()))
            .peek(item -> item.setChildren(buildTree(list, item.getId())))
            .toList();
    }
}
