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.domain.entity.SysDept;
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.DaKyProFilesContentsDto;
import org.springblade.system.domain.ProSelectDto;
import org.springblade.system.domain.TransferApplyDto;
import org.springblade.system.service.TransferApplyService;
import org.springframework.web.bind.annotation.*;

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


/**
 * @Author：liang.chao
 * @Date：2025/9/15 - 11:24
 */
@RestController
@RequestMapping("/transferApply")
@Slf4j
@NonDS
public class TransferApplyController extends BaseController {

    @Resource
    private TransferApplyService transferApplyService;

    @PostMapping("getTransferApplyList")
    @SysLog(title = "档案移交申请列表", module = "数据/档案移交->档案移交申请", businessType = OperaType.QUERY, details = "档案移交申请列表", logType = 1)
    @RequiresPermissions("transfer:apply:list")
    public TableDataInfo getTransferApplyList(@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.setUserId(getLoginUser().getUserId().toString());
            List<TransferApplyDto> list = transferApplyService.list(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("getProSelect")
    @SysLog(title = "获取工程下拉", module = "数据/档案移交->档案移交申请", businessType = OperaType.QUERY, details = "获取工程下拉", logType = 1)
    public AjaxResult getProSelect(@RequestBody TransferApplyDto dto) {
        try {
            List<ProSelectDto> list = transferApplyService.getProSelect(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }
    @PostMapping("getDeptSelect")
    @SysLog(title = "获取部门下拉", module = "数据/档案移交->档案移交申请", businessType = OperaType.QUERY, details = "获取部门下拉", logType = 1)
    public AjaxResult getDeptSelect(@RequestBody TransferApplyDto dto) {
        try {
            List<SysDept> list = transferApplyService.getDeptSelect(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("saveTransferApply")
    @SysLog(title = "新增档案移交申请", module = "数据/档案移交->档案移交申请", businessType = OperaType.INSERT, details = "新增档案移交申请", logType = 1)
    @RequiresPermissions("transfer:apply:add")
    public AjaxResult saveTransferApply(@RequestBody TransferApplyDto dto) {
        try {
            return transferApplyService.save(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("getTransferApply")
    @SysLog(title = "档案移交申请详情", module = "数据/档案移交->档案移交申请", businessType = OperaType.QUERY, details = "档案移交申请详情", logType = 1)
    @RequiresPermissions("transfer:apply: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("getTransferAudit")
    @SysLog(title = "档案移交审核详情", module = "数据/档案移交->档案移交审核", businessType = OperaType.QUERY, details = "档案移交审核详情", logType = 1)
    @RequiresPermissions("transfer:apply:query")
    public AjaxResult getTransferAudit(@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("editTransferApply")
    @SysLog(title = "编辑档案移交申请", module = "数据/档案移交->档案移交申请", businessType = OperaType.UPDATE, details = "编辑档案移交申请", logType = 1)
    @RequiresPermissions("transfer:apply:edit")
    public AjaxResult editTransferApply(@RequestBody TransferApplyDto dto) {
        try {
            return transferApplyService.edit(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("delTransferApply")
    @SysLog(title = "删除档案移交申请", module = "数据/档案移交->档案移交申请", businessType = OperaType.DELETE, details = "删除档案移交申请", logType = 1)
    @RequiresPermissions("transfer:apply:del")
    public AjaxResult delTransferApply(@RequestBody TransferApplyDto dto) {
        try {
            return transferApplyService.del(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("getTransferAuditList")
    @SysLog(title = "档案移交审核列表", module = "数据/档案移交->档案移交申请", businessType = OperaType.QUERY, details = "档案移交审核列表", logType = 1)
    @RequiresPermissions("transfer:audit:list")
    public TableDataInfo getTransferAuditList(@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);
            List<TransferApplyDto> list = transferApplyService.list(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("auditTransferApply")
    @SysLog(title = "档案移交审核", module = "数据/档案移交->档案移交申请", businessType = OperaType.UPDATE, details = "档案移交审核", logType = 1)
    @RequiresPermissions("transfer:apply:audit")
    public AjaxResult auditTransferApply(@RequestBody TransferApplyDto dto) {
        try {
            return transferApplyService.audit(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("getTransferApplyFiles")
    public AjaxResult getTransferApplyFiles(@RequestBody TransferApplyDto dto) {
        try {
            List<DaKyProFilesContentsDto> list = transferApplyService.getTransferApplyFiles(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error();
        }
    }
    // 档案移交申请文件详情
    @PostMapping("getTransferApplyFilesByApplyId")
    @RequiresPermissions("transfer:apply:query")
    public AjaxResult getTransferApplyFilesByApplyId(@RequestBody TransferApplyDto dto) {
        try {
            List<DaKyProFilesContentsDto> list = transferApplyService.getTransferApplyFilesByApplyId(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error();
        }
    }

    // 档案移交审核文件详情
    @PostMapping("getTransferAudieFilesByApplyId")
    @RequiresPermissions("transfer:apply:query")
    public AjaxResult getTransferAudieFilesByApplyId(@RequestBody TransferApplyDto dto) {
        try {
            List<DaKyProFilesContentsDto> list = transferApplyService.getTransferApplyFilesByApplyId(dto);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error();
        }
    }
}
