package org.springblade.system.controller;

import com.github.pagehelper.PageHelper;
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.R;
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.FilesClassifyNameStandardDto;
import org.springblade.system.service.FilesClassifyNameStandardService;
import org.springframework.beans.factory.annotation.Autowired;
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/9/12 - 10:38
 * 文件分类命名规范识别管理
 */
@RestController
@RequestMapping("/classifyNameStandard")
@Slf4j
@NonDS
public class FilesClassifyNameStandardController extends BaseController {

    @Autowired
    private FilesClassifyNameStandardService service;

    @PostMapping("list")
    @SysLog(title = "文件分类命名规范识别列表", module = "档案分类管理->文件分类命名规范识别管理", businessType = OperaType.QUERY, details = "文件分类命名规范识别列表", logType = 1)
    @RequiresPermissions("classify:standard:list")
    public TableDataInfo getClassifyNameStandardList(@RequestBody FilesClassifyNameStandardDto 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<FilesClassifyNameStandardDto> list = service.getClassifyNameStandardList(dto);
            return getDataTable(list);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return getDataTable(new ArrayList<>());
        }
    }

    @PostMapping("add")
    @SysLog(title = "新增文件分类命名规范识别", module = "档案分类管理->文件分类命名规范识别管理", businessType = OperaType.INSERT, details = "新增文件分类命名规范识别", logType = 1)
    @RequiresPermissions("classify:standard:add")
    public R saveClassifyNameStandard(@RequestBody @Validated FilesClassifyNameStandardDto dto) {
        try {
            return service.add(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }

    @PostMapping("edit")
    @SysLog(title = "新增文件分类命名规范识别", module = "档案分类管理->文件分类命名规范识别管理", businessType = OperaType.UPDATE, details = "新增文件分类命名规范识别", logType = 1)
    @RequiresPermissions("classify:standard:edit")
    public R updateClassifyNameStandard(@RequestBody @Validated FilesClassifyNameStandardDto dto) {
        try {
            return service.edit(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }
    @PostMapping("del")
    @SysLog(title = "删除文件分类命名规范识别", module = "档案分类管理->文件分类命名规范识别管理", businessType = OperaType.DELETE, details = "删除文件分类命名规范识别", logType = 1)
    @RequiresPermissions("classify:standard:del")
    public R delClassifyNameStandard(@RequestBody FilesClassifyNameStandardDto dto) {
        try {
            return service.del(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }
}
