package com.zt.plat.module.qms.resource.record.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.qms.resource.record.controller.admin.recordcategory.vo.RecordCategoryPageReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordcategory.RecordCategoryDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 文件记录分类 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface RecordCategoryMapper extends BaseMapperX<RecordCategoryDO> {

    default PageResult<RecordCategoryDO> selectPage(RecordCategoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RecordCategoryDO>()
                .eqIfPresent(RecordCategoryDO::getCancelFlag, "1")
                .eqIfPresent(RecordCategoryDO::getParentId, reqVO.getParentId())
                .eqIfPresent(RecordCategoryDO::getIdPath, reqVO.getIdPath())
                .likeIfPresent(RecordCategoryDO::getName, reqVO.getName())
                .eqIfPresent(RecordCategoryDO::getBusinessType, reqVO.getBusinessType())
                .likeIfPresent(RecordCategoryDO::getModuleName, reqVO.getModuleName())
                .eqIfPresent(RecordCategoryDO::getModuleCode, reqVO.getModuleCode())
                .eqIfPresent(RecordCategoryDO::getSortNo, reqVO.getSortNo())
                .eqIfPresent(RecordCategoryDO::getDataSource, reqVO.getDataSource())
                .eqIfPresent(RecordCategoryDO::getDataDimension, reqVO.getDataDimension())
                .eqIfPresent(RecordCategoryDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(RecordCategoryDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(RecordCategoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RecordCategoryDO::getId));
    }

    /**
     * 通过查询 JSON 字段（CST_CFG）中 appraisalFlag = 1 的值，获取指定 ID 的分类记录
     * 注：使用达梦 JSON_VALUE 函数解析 JSON 字符串字段
     *
     * @param categoryRootId
     * @return 匹配的分类 DO，不存在返回 null
     */
    @Select("SELECT * FROM t_rcd_ctgr WHERE deleted = 0 AND id = #{categoryRootId} AND JSON_VALUE(CST_CFG, '$.appraisalFlag') = '1'")
    RecordCategoryDO selectCategoryAppraisalDO(Long categoryRootId);

    /**
     * 通过查询 JSON 字段（CST_CFG）中指定字段的值，获取指定 ID 的分类记录
     *  例：1，"$.appraisalFlag", "1"
     * @param categoryRootId
     * @param jsonKey
     * @param jsonValue
     * @return
     */
//    @Select("SELECT * FROM t_rcd_ctgr WHERE deleted = 0 AND id = #{categoryRootId} AND JSON_VALUE(CST_CFG, #{jsonKey}) = #{jsonValue}")
    List<RecordCategoryDO> selectCategoryByJsonField(@Param("categoryRootId") Long categoryRootId, @Param("jsonKey") String jsonKey, @Param("jsonValue") String jsonValue);


}