package cn.ey.biz.module.system.dal.mapper.category;

import cn.ey.biz.framework.mybatis.core.mapper.BaseMapperX;
import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.api.category.dto.CategoryCodeExtraRespDTO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryExportReqVO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryPageReqVO;
import cn.ey.biz.module.system.controller.admin.category.vo.CategoryRespVO;
import cn.ey.biz.module.system.dal.dataobject.category.CategoryDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 字典分类 Mapper
 *
 */
@Mapper
public interface CategoryMapper extends BaseMapperX<CategoryDO> {

    default List<CategoryDO> selectList(CategoryExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CategoryDO>()
                .eqIfPresent(CategoryDO::getCode, reqVO.getCode())
                .likeIfPresent(CategoryDO::getName, reqVO.getName())
                .eqIfPresent(CategoryDO::getSysFlag, reqVO.getSysFlag())
                .eqIfPresent(CategoryDO::getStatus, reqVO.getStatus())
                .eqIfPresent(CategoryDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CategoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CategoryDO::getId));
    }

    IPage<CategoryRespVO> selectPage(IPage page, @Param("c") CategoryPageReqVO pageReqVO);

    default CategoryDO selectByName(String name) {
        return selectOne(CategoryDO::getName, name);
    }


    @SuppressWarnings("MybatisXMapperMethodInspection")
    IPage<Map<String, Object>> selectPageCategoryDataDynamic(IPage page, @Param("tableName") String tableName,
                                                             @Param("isVersionControl") Integer isVersionControl,
                                                             @Param("queryParams") Map<String, Object> queryParams);


    List<CategoryCodeExtraRespDTO> listCategoryCodeExtraInfo();

    /**
     * 逻辑删除全表数据
     * @param tableName 表名
     */
    void deleteFullDataDynamic(@Param("tableName") String tableName) ;

    /**
     * 动态插入表数据
     * @param tableName 数据表
     * @param head 表头
     * @param list 值
     */
    void insertDataDynamic(@Param("tableName") String tableName,
                               @Param("head") Map<String, Object> head,
                               @Param("list") List<Map<String, Object>> list);
    /**
     * 动态更新表数据
     * @param tableName 数据表
     * @param bizKey 业务主键
     * @param row 值
     */
    void updateDataDynamic(@Param("tableName") String tableName,
                               @Param("bizKey") Map<String, Object> bizKey,
                               @Param("row") Map<String, Object> row);

    /**
     * 查询字典表数据量
     * @param categoryCode 字典编码
     * @return 数据量
     */
    @Select("SELECT count(1) from (SELECT T1.store_table_name  FROM system_category T1 WHERE T1.category_code = #{categoryCode} and T1.deleted = 0 LIMIT 1) T")
    Long countTableNumber(@Param("categoryCode") String categoryCode);

    /**
     * 查询字典编码是否存在（所有状态）
     * @param categoryCode 字典编码
     * @return 数据量
     */
    @Select("SELECT count(1) FROM system_category T1 WHERE T1.category_code = #{categoryCode}")
    Long countCategoryCodeNumber(@Param("categoryCode") String categoryCode);

}
