package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.R;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.domain.ArchivalCatalogueDto;
import org.springblade.system.domain.SelectDto;
import org.springblade.system.domain.vo.ArchivalCatalogueVo;
import org.springblade.system.mapper.ArchiveMapper;
import org.springblade.system.service.ArchiveService;
import org.springblade.system.tool.TreeBuilder;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2025/9/9 - 14:47
 */
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Resource
    private ArchiveMapper archiveMapper;

    @Override
    public List<ArchivalCatalogueDto> getArchivalCatalogueTree(ArchivalCatalogueDto dto) {
        List<ArchivalCatalogueDto> archivalCatalogueDto = archiveMapper.getArchivalCatalogueTree(dto);
        if (dto.getId() != null) {
            Integer level = archiveMapper.getLevelById(dto.getId());
            for (Iterator<ArchivalCatalogueDto> it = archivalCatalogueDto.iterator(); it.hasNext(); ) {
                ArchivalCatalogueDto item = it.next();
                if (level <= item.getLevel()) {
                    it.remove();
                }
            }
        }
        // 构建树
        List<ArchivalCatalogueDto> tree = TreeBuilder.buildTree(archivalCatalogueDto);
        return tree;
    }

    @Override
    public R saveArchivalCatalogue(ArchivalCatalogueDto dto) {
        Integer archivalCatalogueByName = archiveMapper.getArchivalCatalogueByName(dto);
        if (archivalCatalogueByName > 0) {
            return R.fail("该名称已存在");
        }
        dto.setCreateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setCreateUserName(AuthUtil.getUser().getUserName());
        return R.ok(archiveMapper.saveArchivalCatalogue(dto));
    }

    @Override
    public R saveArchivalCatalogue(ArchivalCatalogueVo vo) {
        Integer archivalCatalogueByName = archiveMapper.getArchivalCatalogueByName(vo);
        if (archivalCatalogueByName > 0) {
            return R.fail("该名称已存在");
        }
        vo.setCreateUserId(AuthUtil.getUser().getUserId().toString());
        vo.setCreateUserName(AuthUtil.getUser().getUserName());
        vo.setUpdateUserId(AuthUtil.getUser().getUserId().toString());
        vo.setUpdateUserName(AuthUtil.getUser().getUserName());
        return R.ok(archiveMapper.saveArchivalCatalogue(vo));
    }

    @Override
    public List<ArchivalCatalogueDto> getArchivalCatalogue(ArchivalCatalogueDto dto) {
        return archiveMapper.getArchivalCatalogue(dto);
    }

    @Override
    public R editArchivalCatalogue(ArchivalCatalogueDto dto) {
        Integer archivalCatalogueByName = archiveMapper.getArchivalCatalogueByName(dto);
        if (archivalCatalogueByName > 0) {
            return R.fail("该名称已存在");
        }
        dto.setUpdateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        return R.ok(archiveMapper.editArchivalCatalogue(dto));
    }

    @Override
    public R editArchivalCatalogue(ArchivalCatalogueVo dto) {
        Integer archivalCatalogueByName = archiveMapper.getArchivalCatalogueByName(dto);
        if (archivalCatalogueByName > 0) {
            return R.fail("该名称已存在");
        }
        dto.setUpdateUserId(AuthUtil.getUser().getUserId().toString());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        return R.ok(archiveMapper.editArchivalCatalogue(dto));
    }

    @Override
    public Integer delArchivalCatalogue(ArchivalCatalogueDto dto) {
        // 先判断下面是否还有节点，有的话无法删除
        Integer num = archiveMapper.getchild(dto);
        if (num > 0) {
            return -1;
        }
        return archiveMapper.delArchivalCatalogue(dto);
    }

    @Override
    public ArchivalCatalogueDto getArchivalCatalogueById(ArchivalCatalogueDto dto) {
        ArchivalCatalogueDto archivalCatalogueDto = archiveMapper.getArchivalCatalogueById(dto);
        if (archivalCatalogueDto != null && archivalCatalogueDto.getLevel() == 4){
            archivalCatalogueDto.setClassifyMark(archiveMapper.getClassifyMark(dto));
        }
        return archivalCatalogueDto;
    }

    @Override
    public List<SelectDto> getFilesClassifyMarkSelect(ArchivalCatalogueDto dto) {
        return archiveMapper.getFilesClassifyMarkSelect(dto);
    }

    @Override
    public Integer geMaxSort(String parentId) {
        return archiveMapper.geMaxSort(parentId);
    }

    @Override
    public Integer getSortById(Integer id) {
        return archiveMapper.getSortById(id);
    }
}
