package com.hfkj.dao;

import com.hfkj.entity.CmsCategory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

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

/**
 * mapper扩展类
 */
public interface CmsCategoryMapperExt {


    @Select(value = {
            "<script>",
            "SELECT",
            "DISTINCT cc.*",
            "FROM cms_category AS cc",
            "LEFT OUTER JOIN cms_category_role AS ccr",
            "ON ccr.category_id = cc.id",
            "<where>",
            "<if test=\"parentId != null\">",
            "    cc.parent_id = #{parentId}",
            "</if>",
            "<if test=\"name != null\">",
            "    AND cc.name REGEXP #{name}",
            "</if>",
            "<if test=\"code != null\">",
            "    AND cc.code = #{code}",
            "</if>",
            "<choose>",
            "    <when test=\"status == null\">",
            "        AND cc.status != 0",
            "    </when>",
            "    <when test=\"status != null\">",
            "        AND cc.status = #{status}",
            "    </when>",
            "</choose>",
            "<if test=\"roleType != null\">",
            "    AND ccr.role_type = #{roleType}",
            "</if>",
            "</where>",
            "ORDER BY",
            "sort ASC",
            "</script>"
    })
    @ResultMap(value = "CategoryResultMap")
    List<CmsCategory> getCategoryList(Map<String, Object> paramsMap) throws Exception;

    @Select(value = {
            "<script>",
            "SELECT",
            "DISTINCT cc.*",
            "FROM cms_category AS cc",
            ", cms_category_role AS ccr",
            "WHERE ccr.category_id = cc.id",
            "<if test=\"parentId != null\">",
            "    AND cc.parent_id = #{parentId}",
            "</if>",
            "<if test=\"name != null\">",
            "    AND cc.name REGEXP #{name}",
            "</if>",
            "<if test=\"code != null\">",
            "    AND cc.code = #{code}",
            "</if>",
            "<choose>",
            "    <when test=\"status == null\">",
            "        AND cc.status != 0",
            "    </when>",
            "    <when test=\"status != null\">",
            "        AND cc.status = #{status}",
            "    </when>",
            "</choose>",
            "<if test=\"roleType != null\">",
            "    AND ccr.role_type = #{roleType}",
            "</if>",
            "ORDER BY",
            "id ASC",
            "</script>"
    })
    @ResultMap(value = "CategoryResultMap")
    List<CmsCategory> getOwnCategoryList(Map<String, Object> paramsMap) throws Exception;

    @Select(value = {
            "<script>",
            "SELECT",
            "DISTINCT cc.*",
            "FROM cms_category AS cc",
            "WHERE",
            "(cc.code = #{category.code})",
            "AND cc.status != 0",
            "LIMIT 2",
            "</script>"
    })
    @Results(id = "CategoryResultMap", value = {
            @Result(column="id", property="id", jdbcType= JdbcType.BIGINT, id=true),
            @Result(column="parent_id", property="parentId", jdbcType= JdbcType.BIGINT),
            @Result(column="name", property="name", jdbcType= JdbcType.VARCHAR),
            @Result(column="code", property="code", jdbcType= JdbcType.VARCHAR),
            @Result(column="sort", property="sort", jdbcType= JdbcType.INTEGER),
            @Result(column="cover_img", property="coverImg", jdbcType= JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType= JdbcType.INTEGER),
            @Result(column="remark", property="remark", jdbcType= JdbcType.VARCHAR),
            @Result(column="ext_1", property="ext1", jdbcType= JdbcType.VARCHAR),
            @Result(column="ext_2", property="ext2", jdbcType= JdbcType.VARCHAR),
            @Result(column="ext_3", property="ext3", jdbcType= JdbcType.VARCHAR)
    })
    List<CmsCategory> getCategoryListByCode(@Param(value = "category") CmsCategory category) throws Exception;

    @Select(value = {
            "<script>",
            "SELECT",
            "DISTINCT ccr.role_type",
            "FROM cms_category_role AS ccr",
            "WHERE ccr.category_id IN (" +
                    "SELECT " +
                    "DISTINCT cc.id " +
                    "FROM cms_category AS cc " +
                    "WHERE cc.parent_id = #{id}" +
                    ")",
            "</script>"
    })
    List<Integer> getRolesOfChildren(@Param(value = "id") Long id) throws Exception;

    @Select(value = {
            "<script>",
            "SELECT",
            "DISTINCT ccr.role_type",
            "FROM cms_category_role AS ccr",
            "WHERE ccr.category_id = #{id}",
            "</script>"
    })
    List<Integer> getRolesOfCategory(@Param(value = "id") Long id) throws Exception;
}
