<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hzs.sale.product.mapper.BdAreaClassifyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.product.BdAreaClassify">
        <id column="PK_ID" property="pkId"/>
        <result column="CLASSIFY_NAME" property="classifyName"/>
        <result column="PK_TRANSACTION_KEY" property="pkTransactionKey"/>
        <result column="SORT" property="sort"/>
        <result column="ENABLE_STATE" property="enableState"/>
        <result column="PK_PARENT" property="pkParent"/>
        <result column="CLASSIFY_IMG" property="classifyImg"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="HIERARCHY" property="hierarchy"/>
    </resultMap>

    <resultMap id="SelectResultMap" type="com.hzs.sale.product.vo.AreaClassifyVo">
        <id column="PK_ID" property="pkId"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="CLASSIFY_NAME" property="classifyName"/>
        <result column="ENABLE_STATE" property="enableState"/>
        <result column="SORT" property="sort"/>
        <result column="PK_PARENT" property="pkParent"/>
        <result column="CLASSIFY_IMG" property="classifyImg"/>
        <result column="HIERARCHY" property="hierarchy"/>
        <result column="parentClassifyName" property="parentClassifyName"/>
        <result column="PK_TRANSACTION_KEY" property="pkTransactionKey"/>
    </resultMap>

    <select id="selectByAreaClassifyList" resultMap="SelectResultMap">
        select ac.pk_id,
        <choose>
            <when test="languageColumn != null and languageColumn != ''">
                case
                when bt.${languageColumn} is not null then
                bt.${languageColumn}
                else
                ac.classify_name
                end classify_name,
            </when>
            <otherwise>
                ac.classify_name classify_name,
            </otherwise>
        </choose>
        ac.pk_transaction_key,
        ac.sort,
        ac.enable_state,
        ac.pk_parent,
        ac.classify_img,
        ac.creation_time,
        ac.modified_time,
        ac.del_flag,
        ac.pk_country,
        ac.pk_creator,
        ac.pk_modified,
        ac.hierarchy,
        ac.is_recommend,
        pac.CLASSIFY_NAME parentClassifyName

        from BD_AREA_CLASSIFY ac
        left join BD_AREA_CLASSIFY pac on pac.PK_ID = ac.PK_PARENT
        left join bd_transaction bt
        on bt.pk_id = ac.pk_transaction_key
        and bt.del_flag = 0
        where ac.DEL_FLAG = 0
        <if test="pkParent != null">
            AND ac.PK_PARENT = #{pkParent}
        </if>
        <if test="pkCountry != null">
            and ac.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="enableState != null">
            AND ac.ENABLE_STATE = #{enableState}
        </if>
        <if test="specialArea != null">
            AND (
            (select count(csa.PK_ID) from BD_AREA_CLASSIFY_SPECIAL_AREA csa where csa.SPECIAL_AREA = #{specialArea} and csa.PK_AREA_CLASSIFY = ac.PK_ID ) > 0
            or
            (
            select count(t.PK_ID)
            from BD_AREA_CLASSIFY t
            where (select count(csa.PK_ID) from BD_AREA_CLASSIFY_SPECIAL_AREA csa where csa.SPECIAL_AREA = #{specialArea} and csa.PK_AREA_CLASSIFY = t.PK_ID) > 0
            start with t.pk_id = ac.PK_ID
            connect by t.pk_id = prior t.pk_parent
            ) > 0
            )
        </if>
        <if test="hierarchy != null">
            AND ac.HIERARCHY = #{hierarchy}
        </if>
        <if test="isRecommend != null">
            AND ac.IS_RECOMMEND = #{isRecommend}
        </if>
        <if test="classifyName != null and classifyName !='' ">
            AND ac.CLASSIFY_NAME like #{classifyName}||'%'
        </if>
    </select>

</mapper>
