package org.springblade.system.controller;


import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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.TransferApplyDto;
import org.springblade.system.domain.TransferFileDto;
import org.springblade.system.service.TransferApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author：liang.chao
 * @Date：2025/9/18 - 16:06
 */

@RestController
@RequestMapping("/transferRecord")
@Slf4j
@NonDS
public class FileTransferRecordController extends BaseController {

    @Resource
    private TransferApplyService transferApplyService;
    @PostMapping("getTransferRecordList")
    @SysLog(title = "档案移交记录列表", module = "数据/档案移交->档案移交记录", businessType = OperaType.QUERY, details = "档案移交记录列表", logType = 1)
    @RequiresPermissions("transfer:record:list")
    public TableDataInfo getTransferRecordList(@RequestBody TransferApplyDto 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);
            dto.setAuditStatus("1");
            List<TransferApplyDto> list = transferApplyService.list(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("getTransferRecordDetail")
    @SysLog(title = "档案移交记录详情", module = "数据/档案移交->档案移交记录", businessType = OperaType.QUERY, details = "档案移交记录详情", logType = 1)
    @RequiresPermissions("transfer:record:query")
    public AjaxResult getTransferApply(@RequestBody TransferApplyDto dto) {
        try {
            TransferApplyDto transferApply = transferApplyService.getTransferApply(dto);
            return AjaxResult.success(transferApply);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }
    @PostMapping("getTransferRecordFiles")
    @SysLog(title = "档案移交清单文件详情", module = "数据/档案移交->档案移交记录", businessType = OperaType.QUERY, details = "档案移交清单文件详情", logType = 1)
    public AjaxResult getTransferRecordFiles(@RequestBody TransferFileDto dto) {
        try {
            List<TransferFileDto> recordFiles = transferApplyService.getTransferRecordFiles(dto);
            return AjaxResult.success(recordFiles);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }
    @PostMapping("updateTransferRecordFile")
    @SysLog(title = "修改档案移交清单文件名", module = "数据/档案移交->档案移交记录", businessType = OperaType.UPDATE, details = "修改档案移交清单文件名", logType = 1)
    @RequiresPermissions("record:file:update")
    public AjaxResult updateTransferRecordFile(@RequestBody TransferFileDto dto) {
        try {
            return transferApplyService.updateTransferRecordFile(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("updateTransferRecordFilesStatus")
    @SysLog(title = "档案移交", module = "数据/档案移交->档案移交记录", businessType = OperaType.UPDATE, details = "档案移交", logType = 1)
    @RequiresPermissions("record:file:transfer")
    public AjaxResult updateTransferRecordFilesStatus(@RequestBody TransferFileDto dto) {
        try {
            return transferApplyService.updateTransferRecordFilesStatus(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

}
