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.KyDataClassify;
import org.springblade.common.core.domain.entity.KyDataClassifyVo;
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.KyDataClassifyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据分类管理
 *
 * @author 马三炮
 * @date 2025/9/1
 */
@RestController
@RequestMapping("/data/classify")
@Slf4j
@NonDS
public class KyDataClassifyController extends BaseController {


	@Resource
	private KyDataClassifyService kyDataClassifyService;


	/**
	 * 获取数据分类管理列表
	 *
	 * @param
	 * @return
	 */
	@SysLog(title = "数据集成", businessType = OperaType.QUERY, module = "数据集成->数据分类管理", details = "查询数据分类列表")
	@RequiresPermissions("data:classify:list")
	@PostMapping("/list")
	public TableDataInfo list(@RequestBody KyDataClassify 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<KyDataClassify> list = kyDataClassifyService.selectKyDataClassifyList(dto);
		return getDataTable(list);
	}

	/**
	 * 获取数据分类详情
	 *
	 * @param
	 * @return
	 */
	@SysLog(title = "数据集成", businessType = OperaType.QUERY, module = "数据集成->数据分类管理", details = "查询数据分类详情")
	@PostMapping("/getById")
	public AjaxResult getById(@RequestBody KyDataClassify dto) {
		KyDataClassify kyDataClassify = kyDataClassifyService.getById(dto);
		return AjaxResult.success(kyDataClassify);
	}

	/**
	 * 新增数据分类
	 *
	 * @param kyDataClassify
	 * @return
	 */
	@RequiresPermissions("data:classify:add")
	@SysLog(title = "数据集成", businessType = OperaType.INSERT, module = "数据集成->数据分类管理", details = "新增数据分类")
	@PostMapping("/addKyDataClassify")
	public AjaxResult addKyDataClassify(@Validated @RequestBody KyDataClassify kyDataClassify) {

		kyDataClassify.setCreateUserId(getUserId());
		kyDataClassify.setCreateUserName(getUsername());
		kyDataClassify.setCreateTime(DateUtils.getNowDate());
		kyDataClassify.setUpdateUserId(getUserId());
		kyDataClassify.setUpdateUserName(getUsername());
		kyDataClassify.setUpdateTime(DateUtils.getNowDate());
		int res = kyDataClassifyService.addKyDataClassify(kyDataClassify);
		if (res == 1) {
			return toAjax(res);
		} else {
			return error("数据类型名称已存在");
		}

	}

	/**
	 * 修改数据分类
	 *
	 * @param kyDataClassify
	 * @return
	 */
	@RequiresPermissions("data:classify:update")
	@SysLog(title = "数据集成", businessType = OperaType.UPDATE, module = "数据集成->数据分类管理", details = "修改数据分类")
	@PostMapping("/updateKyDataClassify")
	public AjaxResult updateKyDataClassify(@Validated @RequestBody KyDataClassify kyDataClassify) {

		kyDataClassify.setUpdateUserId(getUserId());
		kyDataClassify.setUpdateUserName(getUsername());
		kyDataClassify.setUpdateTime(DateUtils.getNowDate());
		return toAjax(kyDataClassifyService.updateKyDataClassify(kyDataClassify));
	}

	/**
	 * 删除数据分类
	 *
	 * @param kyDataClassify
	 * @return
	 */
	@RequiresPermissions("data:classify:del")
	@SysLog(title = "数据集成", businessType = OperaType.DELETE, module = "数据集成->数据分类管理", details = "删除数据分类")
	@PostMapping("/delKyDataClassify")
	public AjaxResult delKyDataClassify(@RequestBody KyDataClassify kyDataClassify) {

		return toAjax(kyDataClassifyService.delKyDataClassify(kyDataClassify));
	}

	/**
	 * 获取数据分类管理下拉框
	 *
	 * @param kyDataClassify
	 * @return
	 */
	/*@PreAuthorize("@ss.hasPermi('data:classify:list')")*/
	@RequiresPermissions("data:Collect:list")
	@PostMapping("/listAll")
	public TableDataInfo listAll(@RequestBody KyDataClassify kyDataClassify) {
		List<KyDataClassifyVo> list = kyDataClassifyService.listAll(kyDataClassify);
		return getDataTable(list);
	}
}
