<?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.dk.mdm.mapper.mst.GoodsCategoryMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        cat_id
        , cat_code, cat_name, parent_id, display_no, top_id, level_upper, level_lower, level_leaf, level_no, level_code, level_name, remarks, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
    </sql>

    <sql id="Base_Column_List_Join">
        tmgc.cat_id
        , tmgc.cat_code, tmgc.cat_name, tmgc.parent_id, tmgc.display_no, tmgc.top_id, tmgc.level_upper,
        tmgc.level_lower, tmgc.level_leaf, tmgc.level_no, tmgc.level_code, tmgc.level_name, tmgc.remarks, tmgc.flg_valid, tmgc.cp_id
    </sql>


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.GoodsCategory">
        <id column="cat_id" property="catId"/>
        <result column="cat_code" property="catCode"/>
        <result column="cat_name" property="catName"/>
        <result column="parent_id" property="parentId" typeHandler="UuidTypeHandler"/>
        <result column="display_no" property="displayNo"/>
        <result column="top_id" property="topId" typeHandler="UuidTypeHandler"/>
        <result column="level_upper" property="levelUpper" typeHandler="UuidTypeHandler"/>
        <result column="level_lower" property="levelLower" typeHandler="UuidTypeHandler"/>
        <result column="level_leaf" property="levelLeaf"/>
        <result column="level_no" property="levelNo"/>
        <result column="level_code" property="levelCode"/>
        <result column="level_name" property="levelName"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
    </resultMap>


    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.mst.GoodsCategoryResponse">
        <id column="cat_id" property="catId"/>
        <result column="cat_code" property="catCode"/>
        <result column="cat_name" property="catName"/>
        <result column="parent_id" property="parentId" typeHandler="UuidTypeHandler"/>
        <result column="display_no" property="displayNo"/>
        <result column="top_id" property="topId" typeHandler="UuidTypeHandler"/>
        <result column="level_upper" property="levelUpper" typeHandler="UuidTypeHandler"/>
        <result column="level_lower" property="levelLower" typeHandler="UuidTypeHandler"/>
        <result column="level_leaf" property="levelLeaf"/>
        <result column="level_no" property="levelNo"/>
        <result column="level_code" property="levelCode"/>
        <result column="level_name" property="levelName"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="parentName" property="parentName"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="catCode != null and catCode != ''">
                AND tmgc.cat_code LIKE concat('%',my_ex.likequery(#{catCode}),'%')
            </if>
            <if test="catName != null and catName != ''">
                AND tmgc.cat_name LIKE concat('%',my_ex.likequery(#{catName}),'%')
            </if>
            <if test="parentId != null and parentId != ''">
                AND tmgc.parent_id = #{parentId}
            </if>
            <if test="displayNo != null">
                AND tmgc.display_no = #{displayNo}
            </if>
            <if test="topId != null and topId != ''">
                AND tmgc.top_id = #{topId}
            </if>
            <if test="levelUpper != null and levelUpper != ''">
                AND tmgc.level_upper = #{levelUpper}
            </if>
            <if test="levelLower != null and levelLower != ''">
                AND tmgc.level_lower = #{levelLower}
            </if>
            <if test="levelLeaf != null">
                AND tmgc.level_leaf = #{levelLeaf}
            </if>
            <if test="levelNo != null">
                AND tmgc.level_no = #{levelNo}
            </if>
            <if test="levelCode != null and levelCode != ''">
                AND tmgc.level_code = #{levelCode}
            </if>
            <if test="levelName != null and levelName != ''">
                AND tmgc.level_name = #{levelName}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tmgc.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND tmgc.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tmgc.cp_id = #{cpId}
            </if>


            <if test="flgValidList != null and flgValidList.size()>0">
                AND tmgc.flg_valid =
                any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if  test="searchText != null">
                AND (tmgc.cat_code  like concat('%', my_ex.likequery(#{searchText}) , '%')
                OR  tmgc.cat_name  like concat('%', my_ex.likequery(#{searchText}) , '%')
                OR  tmgc.remarks like concat('%', my_ex.likequery(#{searchText}) , '%')
                )
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键catId批量操作 -->
        WHERE cat_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_mst_goods_category，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Join"/>,

        (select cat_name FROM dkic_b.t_mst_goods_category tmstg where tmstg.cat_id = tmgc.parent_id) AS "parentName"
        FROM dkic_b.t_mst_goods_category tmgc

        <include refid="Condition"/>
        ORDER BY tmgc.display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_mst_goods_category，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_mst_goods_category tmgc
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_mst_goods_category的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_goods_category
        WHERE cat_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_mst_goods_category的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_goods_category
        WHERE cat_id = #{catId} ::uuid
        for update
    </select>

    <!-- 根据主键锁定表t_mst_goods_category的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_goods_category
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_mst_goods_category
        (
        <trim suffixOverrides=",">
            cat_code,
            cat_name,
            parent_id,
            display_no,
            top_id,
                level_upper,
                level_lower,
            level_leaf,
            level_no,
            level_code,
            level_name,
            remarks,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.catCode},
                #{item.catName},
                #{item.parentId}::uuid,
                #{item.displayNo},
                #{item.topId}::uuid,
                #{item.levelUpper}::uuid[],
                #{item.levelLower}::uuid[],
                #{item.levelLeaf},
                #{item.levelNo},
                #{item.levelCode},
                #{item.levelName},
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>





    <select id="resetLevelGoodsCategory" resultType="java.lang.Boolean">
        select dkic_b.f_reset_level_goods_category(#{cpId}, #{topId}::uuid)
    </select>


    <!--    批量启用 根据父级id-->
    <update id="enableBachByParentId">
        update dkic_b.t_mst_goods_category set  flg_valid = true  where parent_id = #{id}::uuid
    </update>

    <!--    批量停用 根据父级id-->
    <update id="disableBachByParentId">
        update dkic_b.t_mst_goods_category set  flg_valid = false  where parent_id = #{id}::uuid
    </update>
</mapper>
