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.KyFilesCustomizeClassify;
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.FilesCustomizeClassifyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 档案自定义分类
 * @author 马三炮
 * @date 2025/9/11
 */
@RestController
@RequestMapping("/files/customize")
@Slf4j
@NonDS
public class FilesCustomizeClassifyController extends BaseController {

    @Resource
    private FilesCustomizeClassifyService filesCustomizeClassifyService;

    /**
     * 获取档案自定义分类管理列表
     * @param
     * @return
     */
    @SysLog(title = "档案自定义分类管理", businessType = OperaType.QUERY, module = "档案自定义分类管理->获取档案自定义分类管理列表", details = "获取档案自定义分类管理列表")
    @RequiresPermissions("files:customize:list")
    @PostMapping("/getKyFilesCustomizeClassifyList")
    public TableDataInfo getKyFilesCustomizeClassifyList(@RequestBody KyFilesCustomizeClassify 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<KyFilesCustomizeClassify> list = filesCustomizeClassifyService.getKyFilesCustomizeClassifyList(dto);
        return getDataTable(list);
    }

    /**
     * 新增档案自定义分类管理
     * @param kyFilesCustomizeClassify
     * @return
     */
    @SysLog(title = "档案自定义分类管理", businessType = OperaType.INSERT, module = "档案自定义分类管理->新增档案自定义分类管理", details = "新增档案自定义分类管理")
    @RequiresPermissions("files:customize:add")
    @PostMapping("/addKyFilesCustomizeClassify")
    public AjaxResult addKyFilesCustomizeClassify(@Validated @RequestBody KyFilesCustomizeClassify kyFilesCustomizeClassify)
    {

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

    /**
     * 修改档案自定义分类管理
     * @param kyFilesCustomizeClassify
     * @return
     */
    @SysLog(title = "档案自定义分类管理", businessType = OperaType.UPDATE, module = "档案自定义分类管理->修改档案自定义分类管理", details = "修改档案自定义分类管理")
    @RequiresPermissions("files:customize:update")
    @PostMapping("/updateKyFilesCustomizeClassify")
    public AjaxResult updateKyFilesCustomizeClassify(@Validated @RequestBody KyFilesCustomizeClassify kyFilesCustomizeClassify)
    {

        kyFilesCustomizeClassify.setUpdateUserId(getUserId());
        kyFilesCustomizeClassify.setUpdateUserName(getUsername());
        kyFilesCustomizeClassify.setUpdateTime(DateUtils.getNowDate());
        int res = filesCustomizeClassifyService.updateKyFilesCustomizeClassify(kyFilesCustomizeClassify);
        if (res==1){
            return toAjax(res);
        }else {
            return error("分类名称已存在");
        }
    }

    /**
     * 修改档案自定义分类管理
     * @param kyFilesCustomizeClassify
     * @return
     */
    @SysLog(title = "档案自定义分类管理", businessType = OperaType.UPDATE, module = "档案自定义分类管理->删除档案自定义分类管理", details = "删除档案自定义分类管理")
    @RequiresPermissions("files:customize:del")
    @PostMapping("/delKyFilesCustomizeClassify")
    public AjaxResult delKyFilesCustomizeClassify(@Validated @RequestBody KyFilesCustomizeClassify kyFilesCustomizeClassify)
    {
        return toAjax(filesCustomizeClassifyService.delKyFilesCustomizeClassify(kyFilesCustomizeClassify));
    }
}
