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.DaKyProFilesContentsDto;
import org.springblade.system.domain.vo.DaKyProFilesContentsVo;
import org.springblade.system.mapper.FileManageMapper;
import org.springblade.system.mapper.ProjectMapper;
import org.springblade.system.service.FileManageService;
import org.springblade.system.tool.TreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * @Author：liang.chao
 * @Date：2025/9/11 - 13:33
 */
@Service
public class FileManageServiceImpl implements FileManageService {
    @Resource
    private FileManageMapper fileManageMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public List<DaKyProFilesContentsDto> list(DaKyProFilesContentsDto daKyProFilesContentsDto) {
        List<DaKyProFilesContentsDto> list = fileManageMapper.list(daKyProFilesContentsDto);
        List<DaKyProFilesContentsDto> tree = TreeBuilder.buildTreeDaKyProFilesContents(list);
        return tree;
    }

    @Override
    public List<DaKyProFilesContentsDto> FileManage(DaKyProFilesContentsDto dto) {
        return fileManageMapper.FileManage(dto);
    }

    @Override
    public R saveFileManage(DaKyProFilesContentsDto dto) {
        dto.setCreateUserId(AuthUtil.getUser().getUserId());
        dto.setCreateUserName(AuthUtil.getUser().getUserName());
        dto.setUpdateUserId(AuthUtil.getUser().getUserId());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        // 查询档案名称是否重复
        Integer i = fileManageMapper.selectFileManage(dto);
        if (i > 0) {
            return R.fail("档案名称重复");
        }
        String uuid32 = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        dto.setId(uuid32);
        dto.setDataSource("1");
        dto.setIsUnique("1");
        return R.ok(fileManageMapper.saveFileManage(dto));
    }

    @Override
    public R saveFileManage(DaKyProFilesContentsVo dto) {
        String uuid32 = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        dto.setId(uuid32);
        return R.ok(fileManageMapper.saveFileManage(dto));
    }

    @Override
    public R updateFileManage(DaKyProFilesContentsDto dto) {
        dto.setUpdateUserId(AuthUtil.getUser().getUserId());
        dto.setUpdateUserName(AuthUtil.getUser().getUserName());
        Integer i = fileManageMapper.selectFileManage(dto);
        if (i > 0) {
            return R.fail("档案名称重复");
        }
        return R.ok(fileManageMapper.updateFileManage(dto));
    }

    @Override
    public R updateFileManage(DaKyProFilesContentsVo dto) {
        return R.ok(fileManageMapper.updateFileManage(dto));
    }

    @Override
    public Integer delFileManage(DaKyProFilesContentsDto dto) {
        fileManageMapper.delFileSource(dto);
        return fileManageMapper.delFileManage(dto);
    }

    @Override
    public Integer updateIntegrityStatus(DaKyProFilesContentsDto dto) {
        fileManageMapper.updateIntegrityStatus(dto);
        return projectMapper.updateFileStatus(dto);
    }

    @Override
    public Integer getMaxSort(DaKyProFilesContentsDto dto) {
        return fileManageMapper.getMaxSort(dto);
    }

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

    @Override
    public List<DaKyProFilesContentsDto> getFileManageTree(DaKyProFilesContentsDto daKyProFilesContentsDto) {
        List<DaKyProFilesContentsDto> list = fileManageMapper.list(daKyProFilesContentsDto);
        list.removeIf(dto -> "0".equals(dto.getIsUnique()) && dto.getLevel() != 1);
        if (daKyProFilesContentsDto.getId() != null) {
            daKyProFilesContentsDto.setParentId(daKyProFilesContentsDto.getId());
            Integer level = fileManageMapper.getLevelById(daKyProFilesContentsDto);
            for (Iterator<DaKyProFilesContentsDto> it = list.iterator(); it.hasNext(); ) {
                DaKyProFilesContentsDto item = it.next();
                if (level <= item.getLevel()) {
                    it.remove();
                }
            }
        }
        // 构建树
        List<DaKyProFilesContentsDto> tree = TreeBuilder.buildTreeDaKyProFilesContents(list);
        return tree;
    }

    @Override
    public DaKyProFilesContentsDto getFileManageById(DaKyProFilesContentsDto dto) {
        return fileManageMapper.getFileManageById(dto);
    }

    @Override
    public DaKyProFilesContentsVo getFileById(Long id) {
        return fileManageMapper.getFileById(id);
    }
}
