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.FileIndexDto;
import org.springblade.system.service.FileIndexService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author：liang.chao
 * @Date：2025/10/30 - 16:20
 */

@RestController
@RequestMapping("/FileIndex")
@Slf4j
@NonDS
public class FileIndexController extends BaseController {

    @Resource
    private FileIndexService fileIndexService;
    @PostMapping("query")
    @SysLog(title = "档案索引列表", module = "数据/档案移交->档案索引", businessType = OperaType.QUERY, details = "档案索引", logType = 1)
    @RequiresPermissions("file:index:query")
    public TableDataInfo getFileIndex(@RequestBody FileIndexDto 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<FileIndexDto> list = fileIndexService.getList(dto);
			return getDataTable(list);
		} catch (Exception e) {
			log.error(e.toString(), e);
			return getDataTable(new ArrayList<>());
		}
    }

    @PostMapping("add")
    public AjaxResult addFileIndex(@RequestBody @Validated FileIndexDto dto) {
        try {
            return fileIndexService.add(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }

    @PostMapping("update")
    @SysLog(title = "修改档案索引", module = "数据/档案移交->修改档案索引", businessType = OperaType.UPDATE, details = "修改档案索引", logType = 1)
    @RequiresPermissions("file:index:edit")
    public AjaxResult updateFileIndex(@RequestBody @Validated FileIndexDto dto) {
        try {
            return fileIndexService.update(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }
    @PostMapping("del")
    @SysLog(title = "删除档案索引", module = "数据/档案移交->删除档案索引", businessType = OperaType.DELETE, details = "删除档案索引", logType = 1)
    @RequiresPermissions("file:index:del")
    public AjaxResult delFileIndex(@RequestBody FileIndexDto dto) {
        try {
            return fileIndexService.del(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return AjaxResult.error("请求出错了");
        }
    }
}
