package org.springblade.system.tool;

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.KyFilesMultiClassify;
import org.springblade.common.core.page.TableDataInfo;
import org.springblade.common.enums.OperaType;
import org.springblade.common.utils.DateUtils;
import org.springblade.common.utils.sql.SqlUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.system.service.FilesMultiClassifyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 档案多维度分类管理
 *
 * @author 马三炮
 * @date 2025/9/11
 */
@RestController
@RequestMapping("/files/multi")
@Slf4j
@NonDS
public class FilesMultiClassifyController extends BaseController {

	@Resource
	private FilesMultiClassifyService filesMultiClassifyService;

	/**
	 * 获取档案多维度分类管理列表
	 *
	 * @param
	 * @return
	 */
	@SysLog(title = "档案多维度分类管理", businessType = OperaType.QUERY, module = "档案多维度分类管理->获取档案多维度分类管理列表", details = "获取档案多维度分类管理列表")
	@RequiresPermissions("files:multi:list")
	@PostMapping("/getKyFilesMultiClassifyList")
	public TableDataInfo getKyFilesMultiClassifyList(@RequestBody KyFilesMultiClassify dto) {
		// 直接使用 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<KyFilesMultiClassify> list = filesMultiClassifyService.selectKyFilesMultiClassifyList(dto);
		return getDataTable(list);
	}

	@SysLog(title = "档案多维度分类管理", businessType = OperaType.QUERY, module = "档案多维度分类管理->获取档案多维度分类管理详情", details = "获取档案多维度分类管理详情")
	@RequiresPermissions("files:multi:list")
	@PostMapping("/detail")
	public AjaxResult getDetail(@RequestBody KyFilesMultiClassify dto) {
		KyFilesMultiClassify  kyFilesMultiClassify = filesMultiClassifyService.getDetail(dto);
		return AjaxResult.success(kyFilesMultiClassify);
	}

	/**
	 * 新增档案多维度分类管理
	 *
	 * @param kyFilesMultiClassify
	 * @return
	 */
	@SysLog(title = "档案多维度分类管理", businessType = OperaType.INSERT, module = "档案多维度分类管理->新增档案多维度分类管理", details = "新增档案多维度分类管理")
	@RequiresPermissions("files:multi:add")
	@PostMapping("/addKyFilesMultiClassify")
	public AjaxResult addKyFilesMultiClassify(@Validated @RequestBody KyFilesMultiClassify kyFilesMultiClassify) {

		kyFilesMultiClassify.setCreateUserId(getUserId());
		kyFilesMultiClassify.setCreateUserName(getUsername());
		kyFilesMultiClassify.setCreateTime(DateUtils.getNowDate());
		kyFilesMultiClassify.setUpdateUserId(getUserId());
		kyFilesMultiClassify.setUpdateUserName(getUsername());
		kyFilesMultiClassify.setUpdateTime(DateUtils.getNowDate());
		int res = filesMultiClassifyService.addKyFilesMultiClassify(kyFilesMultiClassify);
		if (res == 1) {
			return toAjax(res);
		} else {
			return error("分类名称已存在");
		}
	}

	/**
	 * 修改档案多维度分类管理
	 *
	 * @param kyFilesMultiClassify
	 * @return
	 */
	@SysLog(title = "档案多维度分类管理", businessType = OperaType.UPDATE, module = "档案多维度分类管理->新增档案多维度分类管理", details = "新增档案多维度分类管理")
	@RequiresPermissions("files:multi:update")
	@PostMapping("/updateKyFilesMultiClassify")
	public AjaxResult updateKyFilesMultiClassify(@Validated @RequestBody KyFilesMultiClassify kyFilesMultiClassify) {
		kyFilesMultiClassify.setUpdateUserId(getUserId());
		kyFilesMultiClassify.setUpdateUserName(getUsername());
		kyFilesMultiClassify.setUpdateTime(DateUtils.getNowDate());
		int res = filesMultiClassifyService.updateKyFilesMultiClassify(kyFilesMultiClassify);
		if (res == 1) {
			return toAjax(res);
		} else {
			return error("分类名称已存在");
		}
	}

	/**
	 * 删除档案多维度分类管理
	 *
	 * @param kyFilesMultiClassify
	 * @return
	 */
	@SysLog(title = "档案多维度分类管理", businessType = OperaType.DELETE, module = "档案多维度分类管理->删除档案多维度分类管理", details = "删除档案多维度分类管理")
	@RequiresPermissions("files:multi:del")
	@PostMapping("/delKyFilesMultiClassify")
	public AjaxResult delKyFilesMultiClassify(@Validated @RequestBody KyFilesMultiClassify kyFilesMultiClassify) {
		return toAjax(filesMultiClassifyService.delKyFilesMultiClassify(kyFilesMultiClassify));
	}

}
