package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.AjaxResult;
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.RectificationDto;
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) {
		// 先查询需要哪些部门审核
		List<String> deptIds = fileManageMapper.getdeptIds(dto);
		for (String deptId : deptIds) {
			// 生成部门审核记录
			fileManageMapper.insertRecordTransferAudit(deptId);
		}
		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);
	}

	@Override
	public AjaxResult addRectification(RectificationDto dto) {
		dto.setCreateUserId(AuthUtil.getUser().getUserId());
		dto.setIsRectification("1");
		fileManageMapper.updateisRectification(dto);
		Integer i = fileManageMapper.addRectification(dto);
		if (i > 0) {
			return AjaxResult.success("添加整改成功");
		} else {
			return AjaxResult.error("添加整改失败");
		}
	}

	@Override
	public AjaxResult updateRectification(RectificationDto dto) {
		dto.setUpdateUserId(AuthUtil.getUser().getUserId());
		Integer i = fileManageMapper.updateRectification(dto);
		if (i > 0) {
			return AjaxResult.success("修改成功");
		} else {
			return AjaxResult.error("修改失败");
		}
	}

	@Override
	public List<RectificationDto> selectRectificationList(RectificationDto dto) {
		return fileManageMapper.selectRectificationList(dto);
	}

	@Override
	public AjaxResult delRectification(RectificationDto dto) {
		dto.setIsRectification("0");
		fileManageMapper.updateisRectification(dto);
		Integer i = fileManageMapper.delRectification(dto);
		if (i > 0) {
			return AjaxResult.success("移除成功");
		} else {
			return AjaxResult.error("移除失败");
		}
	}

	@Override
	public AjaxResult issue(RectificationDto dto) {
		Integer i = 0;
		if (dto != null) {
			try {
				for (String id : dto.getIds()) {
					dto.setIsIssue(1);
					dto.setId(id);
					dto.setIssueUser(AuthUtil.getUser().getUserId().toString());
					Integer result = fileManageMapper.issue(dto);
					if (result != null) {
						i += result;
					}
				}
			} catch (Exception e) {
				return AjaxResult.error("发布过程中发生异常：" + e.getMessage());
			}
		} else {
			return AjaxResult.error("发布失败：没有数据需要处理");
		}
		if (i > 0) {
			return AjaxResult.success("发布成功");
		} else {
			return AjaxResult.error("发布失败");
		}
	}
}
