package org.springblade.system.service.impl;

import jakarta.annotation.Resource;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.domain.*;
import org.springblade.system.mapper.FileManageMapper;
import org.springblade.system.mapper.ProjectMapper;
import org.springblade.system.mapper.TransferApplyMapper;
import org.springblade.system.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author：liang.chao
 * @Date：2025/9/11 - 10:05
 */
@Service
public class ProjectServiceImpl implements ProjectService {

	@Resource
	private ProjectMapper projectMapper;

	@Resource
	private FileManageMapper fileManageMapper;

	@Resource
	private TransferApplyMapper transferApplyMapper;

	@Override
	public List<ProjectDto> list(ProjectDto dto) {
		List<ProjectDto> list = projectMapper.list(dto);
		for (ProjectDto projectDto : list) {
			Integer num = fileManageMapper.getFilesNum(projectDto.getId());
			projectDto.setFileUplaudNum(num);
		}
		return list;
	}

	@Override
	public List<SelectDto> getFileCatalogSelect() {
		return projectMapper.getFileCatalogSelect();
	}

	@Override
	@Transactional
	public AjaxResult updateContentsName(ProjectDto projectDto) {
		try {
			Integer num = projectMapper.updateContentsName(projectDto);
			if (num > 0) {
				// 档案管理中新增该工程下所有目录
				List<ArchivalCatalogueDto> list = projectMapper.getfilesContentsById(projectDto);
				for (ArchivalCatalogueDto archivalCatalogueDto : list) {
					DaKyProFilesContentsDto dakyProFilesContentsDto = new DaKyProFilesContentsDto();
					BeanUtils.copyProperties(archivalCatalogueDto, dakyProFilesContentsDto);
					dakyProFilesContentsDto.setId(archivalCatalogueDto.getId().toString());
					dakyProFilesContentsDto.setParentId(archivalCatalogueDto.getParentId().toString());
					dakyProFilesContentsDto.setCreateUserId(AuthUtil.getUser().getUserId());
					dakyProFilesContentsDto.setCreateUserName(AuthUtil.getUser().getUserName());
					dakyProFilesContentsDto.setProId(projectDto.getProId());
					dakyProFilesContentsDto.setDataSource("1");
					dakyProFilesContentsDto.setIsUnique("0");
					projectMapper.insertDakyProFilesContents(dakyProFilesContentsDto);
				}
				return AjaxResult.success("配置成功");
			} else {
				return AjaxResult.error("配置失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("接口异常");
		}

	}

	@Override
	public ProjectDto getProjectNameById(ProjectDto dto) {
		return projectMapper.getProjectNameById(dto);
	}

	@Override
	public List<ProjectDto> getArchivingManage(ProjectDto dto) {
		return projectMapper.getArchivingManageList(dto);
	}

	@Override
	public String getAuditStatus(ProjectDto dto) {
		return projectMapper.getAuditStatus(dto);
	}

	@Override
	public List<DaKyProFilesContentsDto> getArchivingManageFiles(TransferApplyDto dto) {
		List<String> contents = transferApplyMapper.getTransferApplyFilesContents(dto);
		List<DaKyProFilesContentsDto> list = new ArrayList<>();
		for (String content : contents) {
			dto.setParentId(content);
			dto.setDeptId(AuthUtil.getDeptId());
			List<DaKyProFilesContentsDto> dtos = transferApplyMapper.ArchivingManageFile(dto);
			list.addAll(dtos);
		}
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addRectification(RectificationDto dto) {
		try {
			dto.setCreateUserId(AuthUtil.getUser().getUserId());
			dto.setIsRectification("1");
			dto.setRectificationType("1");
			Integer i = 0;
			for (String s : dto.getFileId().split(",")) {
				dto.setFileId(s);
				fileManageMapper.updateisRectification(dto);
				dto.setIssueUser(AuthUtil.getUser().getUserId().toString());
				dto.setRectifyStatus("0");
				LocalDateTime currentDateTime = LocalDateTime.now();
				// 定义完整日期时间格式
				DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
				// 格式化当前日期时间
				dto.setIssueTime(currentDateTime.format(formatter));
				i += fileManageMapper.addRectification(dto);
			}
			dto.setAuditDept(AuthUtil.getUser().getDeptId().toString());
			dto.setAuditStatus("2");
			fileManageMapper.updateTransferAudit(dto);
			if (i > 0) {
				return AjaxResult.success("添加整改成功");
			} else {
				return AjaxResult.error("添加整改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("接口异常");
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult agreeRectification(RectificationDto dto) {
		try {
			dto.setAuditDept(AuthUtil.getUser().getDeptId());
			dto.setAuditStatus("1");
			// 更新审核状态
			fileManageMapper.updateTransferAudit(dto);
			// 检查是否所有部门都审核通过（0表示全部通过）
			Integer auditStatus = fileManageMapper.selectAuditStatus(dto);
			if (auditStatus != null && auditStatus == 0) {
				Integer i = fileManageMapper.agreeRectification(dto);
				if (i != null && i > 0) {
					return AjaxResult.success("归档成功");
				} else {
					return AjaxResult.error("归档失败");
				}
			}
			return AjaxResult.success();
		} catch (Exception e) {
			return AjaxResult.error("接口异常: " + e.getMessage());
		}

	}

	@Override
	public AjaxResult transferAuditDetail(RectificationDto dto) {
		List<RectificationDto> list = fileManageMapper.transferAuditDetail(dto);
		return AjaxResult.success(list);
	}
}
