package org.springblade.system.controller;

import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.common.annotation.RequiresPermissions;
import org.springblade.common.annotation.SysLog;
import org.springblade.common.core.controller.BaseController;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.common.core.page.TableDataInfo;
import org.springblade.common.enums.OperaType;
import org.springblade.common.utils.sql.SqlUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.system.domain.*;
import org.springblade.system.mapper.FileManageMapper;
import org.springblade.system.service.FileManageService;
import org.springblade.system.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2025/12/5 - 9:29
 * 归档管理
 */
@RestController
@RequestMapping("/archivingManage")
@Slf4j
@NonDS
public class ArchivingManageController extends BaseController {

	@Autowired
	private ProjectService service;
	@Resource
	private FileManageMapper fileManageMapper;
	@Autowired
	private FileManageService fileManageService;

	@PostMapping("list")
	public TableDataInfo getProjectList(@RequestBody ProjectDto dto) {
		try {
			// 直接使用 dto 中的分页参数
			PageHelper.startPage(dto.getPageNum() != null ? dto.getPageNum() : 1,
					dto.getPageSize() != null ? dto.getPageSize() : 10,
					SqlUtil.escapeOrderBySql(dto.getOrderBy()))
				.setReasonable(dto.getReasonable() != null ? dto.getReasonable() : true);
			List<ProjectDto> list = service.getArchivingManage(dto);
			for (ProjectDto projectDto : list) {
				projectDto.setDeptId(getDeptId());
				projectDto.setAuditStatus(service.getAuditStatus(projectDto));
			}
			if (StringUtils.isNotBlank(dto.getAuditStatus())) {
				list = list.stream().filter(projectDto -> projectDto.getAuditStatus().equals(dto.getAuditStatus())).toList();
			}
			return getDataTable(list);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return getDataTable(new ArrayList<>());
		}
	}

	@PostMapping("getFileManageByDept")
	@SysLog(title = "第四级档案信息", module = "档案管理->档案右侧列表", businessType = OperaType.QUERY, details = "第四级档案信息", logType = 1)
	@RequiresPermissions("file:manage:query")
	public TableDataInfo getFileManageByDept(@RequestBody DaKyProFilesContentsDto dto) {
		if (dto.getParentId().equals("0")) {
			return getDataTable(new ArrayList<>());
		} else {
			Integer level = fileManageMapper.getLevelById(dto);
			if (level == 4) {
				// 直接使用 dto 中的分页参数
				PageHelper.startPage(dto.getPageNum() != null ? dto.getPageNum() : 1,
						dto.getPageSize() != null ? dto.getPageSize() : 10,
						SqlUtil.escapeOrderBySql(dto.getOrderBy()))
					.setReasonable(dto.getReasonable() != null ? dto.getReasonable() : true);
				dto.setUnitName(getDeptId());
				List<DaKyProFilesContentsDto> list = fileManageService.FileManage(dto);
				return getDataTable(list);
			} else {
				return getDataTable(new ArrayList<>());
			}
		}
	}

	@PostMapping("getArchivingManageFiles")
	@Operation(summary = "归档审核获取整改文件")
	public AjaxResult getArchivingManageFiles(@RequestBody TransferApplyDto dto) {
		try {
			List<DaKyProFilesContentsDto> list = service.getArchivingManageFiles(dto);
			return AjaxResult.success(list);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return AjaxResult.error("请求出错了");
		}
	}

	@PostMapping("addRectification")
	@Operation(summary = "下发整改")
	public AjaxResult addRectification(@RequestBody RectificationDto dto) {
		try {
			return service.addRectification(dto);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return AjaxResult.error("请求出错了");
		}
	}

	@PostMapping("agreeRectification")
	@Operation(summary = "同意归档")
	public AjaxResult agreeRectification(@RequestBody RectificationDto dto) {
		try {
			return service.agreeRectification(dto);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return AjaxResult.error("请求出错了");
		}
	}

	@PostMapping("transferAuditDetail")
	@Operation(summary = "责任单位审核情况")
	public AjaxResult transferAuditDetail(@RequestBody RectificationDto dto) {
		try {
			return service.transferAuditDetail(dto);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return AjaxResult.error("请求出错了");
		}
	}
}
