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.FilesClassifyMarkDto;
import org.springblade.system.service.FilesClassifyMarkService;
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/9 - 17:01
 * 文件分类标记
 */
@RestController
@RequestMapping("/filesClassifyMark")
@Slf4j
@NonDS
public class FilesClassifyMarkController extends BaseController {

    @Autowired
    private FilesClassifyMarkService service;

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

    @PostMapping("addArchivalCatalogue")
    @SysLog(title = "文件分类标记新增", module = "档案分类管理->文件分类标记管理", businessType = OperaType.INSERT, details = "文件分类标记新增", logType = 1)
    @RequiresPermissions("files:classify:add")
    public R addArchivalCatalogueTree(@RequestBody @Validated FilesClassifyMarkDto dto) {
        try {
            return service.add(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }

    @PostMapping("editArchivalCatalogue")
    @SysLog(title = "文件分类标记修改", module = "档案分类管理->文件分类标记管理", businessType = OperaType.UPDATE, details = "文件分类标记修改", logType = 1)
    @RequiresPermissions("files:classify:edit")
    public R editArchivalCatalogueTree(@RequestBody @Validated FilesClassifyMarkDto dto) {
        try {
            // 如果在被使用，则不能修改
            Integer isUse = service.isUseByFilesContents(dto.getId());
            if (isUse > 0) {
                return R.fail("该分类标记正在被使用，请勿修改");
            }
            return service.edit(dto);
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }

    @PostMapping("delArchivalCatalogue")
    @SysLog(title = "文件分类标记删除", module = "档案分类管理->文件分类标记管理", businessType = OperaType.DELETE, details = "文件分类标记删除", logType = 1)
    @RequiresPermissions("files:classify:del")
    public R delArchivalCatalogueTree(@RequestBody FilesClassifyMarkDto dto) {
        try {
            // 如果在被使用，则不能删除
            Integer isUse = service.isUseByFilesContents(dto.getId());
            if (isUse > 0) {
                return R.fail("该分类标记正在被使用，请勿删除");
            }
            Integer i = service.del(dto);
            if (i > 0) {
                return R.ok();
            } else {
                return R.fail("删除失败");
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("请求出错了");
        }
    }
}
