package com.yifu.cloud.plus.v1.batch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.batch.entity.TElectronicArchiveTask;
import com.yifu.cloud.plus.v1.batch.service.ElectronicArchiveImportService;
import com.yifu.cloud.plus.v1.batch.service.TElectronicArchiveTaskService;
import com.yifu.cloud.plus.v1.batch.vo.SingleArchiveUploadVO;
import com.yifu.cloud.plus.v1.batch.vo.TElectronicArchiveTaskSearchVo;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 电子档案任务表
 *
 * @author huych
 * @date 2025-12-17 11:03:07
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/telectronicarchivetask")
@Tag(name = "电子档案任务表管理")
public class TElectronicArchiveTaskController {

	private final TElectronicArchiveTaskService tElectronicArchiveTaskService;
	private final ElectronicArchiveImportService importService;

	/**
	 * 简单分页查询
	 *
	 * @param page                   分页对象
	 * @param tElectronicArchiveTask 电子档案任务表
	 * @return
	 */
	@Operation(description = "简单分页查询")
	@GetMapping("/page")
	public R<IPage<TElectronicArchiveTask>> getTElectronicArchiveTaskPage(Page<TElectronicArchiveTask> page, TElectronicArchiveTaskSearchVo tElectronicArchiveTask) {
		return new R<>(tElectronicArchiveTaskService.getTElectronicArchiveTaskPage(page, tElectronicArchiveTask));
	}

	/**
	 * 通过id查询电子档案任务表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	public R<TElectronicArchiveTask> getById(@PathVariable("id") String id) {
		return R.ok(tElectronicArchiveTaskService.getById(id));
	}

	/**
	 * 通过id删除电子档案任务表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除", description = "通过id删除")
	@PostMapping("/deleteById")
	public R<?> deleteById(@RequestParam String id) {
		return tElectronicArchiveTaskService.deleteTaskById(id);
	}

	/**
	 * 第一步：导入Excel模板文件
	 *
	 * @param file Excel文件
	 * @return R
	 */
	@Operation(summary = "导入Excel模板文件", description = "第一步：导入Excel模板文件，包含身份证、姓名、项目编码、项目名称")
	@PostMapping("/importExcel")
	public R<?> importExcel(@RequestBody MultipartFile file) {
		if (file == null || file.isEmpty()) {
			return R.failed("文件不能为空");
		}

		String fileName = file.getOriginalFilename();
		if (fileName == null || !fileName.toLowerCase().endsWith(".xlsx") && !fileName.toLowerCase().endsWith(".xls")) {
			return R.failed("文件格式不正确，请上传Excel文件");
		}

		try {
			return importService.importExcelTemplate(file.getInputStream(), fileName);
		} catch (IOException e) {
			return R.failed("文件读取失败：" + e.getMessage());
		}
	}

	/**
	 * 第二步：导入ZIP文件
	 *
	 * @param file   ZIP文件
	 * @param taskId 任务ID
	 * @return R
	 */
	@Operation(summary = "导入ZIP文件", description = "第二步：导入ZIP文件，包含员工档案资料")
	@PostMapping("/importZip")
	public R<?> importZip(
			@Parameter(description = "ZIP文件") @RequestBody MultipartFile file,
			@Parameter(description = "任务ID") @RequestParam("taskId") String taskId) {

		if (file == null || file.isEmpty()) {
			return R.failed("文件不能为空");
		}

		String fileName = file.getOriginalFilename();
		if (fileName == null || !fileName.toLowerCase().endsWith(".zip")) {
			return R.failed("文件格式不正确，请上传ZIP文件");
		}

		return importService.importZipFile(file, taskId);
	}

	/**
	 * 查询任务状态
	 *
	 * @param taskId 任务ID
	 * @return R
	 */
	@Operation(summary = "查询任务状态", description = "查询电子档案导入任务的处理状态")
	@GetMapping("/status/{taskId}")
	public R<TElectronicArchiveTask> getTaskStatus(@PathVariable("taskId") String taskId) {
		TElectronicArchiveTask task = tElectronicArchiveTaskService.getById(taskId);
		if (task == null) {
			return R.failed("任务不存在");
		}
		return R.ok(task);
	}

	/**
	 * 单个新增人员附件
	 * 前端先调用 /fileUpload/uploadArchiveFile 接口上传文件获取附件ID列表
	 * 然后调用此接口传入人员信息和附件ID列表，更新附件的domainId
	 *
	 * @param vo 人员信息，包含身份证号、姓名、项目编码、项目名称、资料类型、附件ID列表
	 * @return R
	 */
	@Operation(summary = "单个新增人员附件", description = "单个新增人员档案附件，前端先上传文件获取附件ID，再调用此接口更新domainId")
	@PostMapping("/uploadSingleArchive")
	public R<?> uploadSingleArchive(@Validated @RequestBody SingleArchiveUploadVO vo) {
		return importService.uploadSinglePersonArchiveByAttaIds(
				vo.getEmpIdcard(),
				vo.getEmpName(),
				vo.getDeptNo(),
				vo.getDeptName(),
				vo.getMaterialType(),
				vo.getAttaIdList()
		);
	}
}
