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.TransferProblemDto;
import org.springblade.system.service.TransferProblemService;
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("/transferProblem")
@Slf4j
@NonDS
public class FileTransferProblemController extends BaseController {

    @Resource
    private TransferProblemService transferProblemService;

    @PostMapping("getTransferProblemList")
    @SysLog(title = "档案移交问题列表", module = "数据/档案移交->档案移交问题管理", businessType = OperaType.QUERY, details = "档案移交问题列表", logType = 1)
    @RequiresPermissions("transfer:problem:list")
    public TableDataInfo getTransferProblemList(@RequestBody TransferProblemDto 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<TransferProblemDto> list = transferProblemService.list(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("addTransferProblem")
    @SysLog(title = "新增档案移交问题", module = "数据/档案移交->档案移交问题管理", businessType = OperaType.INSERT, details = "新增档案移交问题", logType = 1)
    @RequiresPermissions("transfer:problem:add")
    public AjaxResult addTransferProblem(@RequestBody TransferProblemDto dto) {
        try {
            return transferProblemService.add(dto);
        } catch (Exception e) {
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("editTransferProblem")
    @SysLog(title = "修改档案移交问题", module = "数据/档案移交->档案移交问题管理", businessType = OperaType.UPDATE, details = "修改档案移交问题", logType = 1)
    @RequiresPermissions("transfer:problem:edit")
    public AjaxResult editTransferProblem(@RequestBody TransferProblemDto dto) {
        try {
            return transferProblemService.edit(dto);
        } catch (Exception e) {
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("delTransferProblem")
    @SysLog(title = "删除档案移交问题", module = "数据/档案移交->档案移交问题管理", businessType = OperaType.DELETE, details = "删除档案移交问题", logType = 1)
    @RequiresPermissions("transfer:problem:del")
    public AjaxResult delTransferProblem(@RequestBody TransferProblemDto dto) {
        try {
            return transferProblemService.del(dto);
        } catch (Exception e) {
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("getTransferProblemById")
    @SysLog(title = "档案移交问题详情", module = "数据/档案移交->档案移交问题管理", businessType = OperaType.QUERY, details = "档案移交问题详情", logType = 1)
    @RequiresPermissions("transfer:problem:query")
    public AjaxResult getTransferProblemById(@RequestBody TransferProblemDto dto) {
        try {
            return transferProblemService.getTransferProblemById(dto);
        } catch (Exception e) {
            return AjaxResult.error("请求出错了");
        }
    }

}
