<?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.GoodsBrandMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        brand_id, brand_code, brand_name, short_name, display_no, 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">
        tmgb.brand_id, tmgb.brand_code, tmgb.brand_name, tmgb.short_name, tmgb.display_no, tmgb.remarks, tmgb.flg_valid, tmgb.cp_id
    </sql>


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.GoodsBrand">
        <id column="brand_id" property="brandId"/>
                <result column="brand_code" property="brandCode"/>
                <result column="brand_name" property="brandName"/>
                <result column="short_name" property="shortName"/>
                <result column="display_no" property="displayNo"/>
                <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.GoodsBrandResponse">
        <id column="brand_id" property="brandId"/>
        <result column="brand_code" property="brandCode"/>
        <result column="brand_name" property="brandName"/>
        <result column="short_name" property="shortName"/>
        <result column="display_no" property="displayNo"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="goodsBrand" property="goodsBrand"/>
        <result column="supplierNames" property="supplierNames"/>
        <result column="supplierIds" property="supplierIds"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="brandCode != null and brandCode != ''">
                AND tmgb.brand_code LIKE concat('%',my_ex.likequery(#{brandCode}),'%')
            </if>
            <if test="brandName != null and brandName != ''">
                AND tmgb.brand_name LIKE concat('%',my_ex.likequery(#{brandName}),'%')
            </if>
            <if test="shortName != null and shortName != ''">
                AND tmgb.short_name = #{shortName}
            </if>
            <if test="displayNo != null">
                AND tmgb.display_no = #{displayNo}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tmgb.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND tmgb.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tmgb.cp_id = #{cpId}
            </if>
            <if test="opCreateTime != null">
                AND tmgb.op_create_time = #{opCreateTime}
            </if>

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

        </where>
    </sql>

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

    <!-- 查询表t_mst_goods_brand，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Join"/>,
        '商品品牌' AS "goodsBrand",
        (SELECT  COALESCE(array_to_string(array_agg(tsm.sup_name), ','),'')
        FROM dkic_b.t_mst_supplier_brand tsmb
        left join dkic_b.t_mst_supplier tsm on tsm.sup_id = tsmb.sup_id
        WHERE tsmb.brand_id  =  tmgb.brand_id) AS "supplierNames",
        (SELECT  COALESCE(array_to_string(array_agg(tsm.sup_id), ','),'')
        FROM dkic_b.t_mst_supplier_brand tsmb
        left join dkic_b.t_mst_supplier tsm on tsm.sup_id = tsmb.sup_id
        WHERE tsmb.brand_id  =  tmgb.brand_id) AS "supplierIds"
        FROM dkic_b.t_mst_goods_brand tmgb
        <include refid="Condition"/>
        order by display_no asc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>



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

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

    <!-- 根据主键锁定表t_mst_goods_brand的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List_Join"/>
        FROM dkic_b.t_mst_goods_brand tmgb
        WHERE tmgb.brand_id = #{brandId}
        for update
    </select>

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

    <!-- 校验名称不重复 -->
    <select id="checkNameDuplicated" resultType="java.lang.Long">
        SELECT COUNT(brand_id)
        FROM dkic_b.t_mst_goods_brand
        WHERE
        cp_id = #{cpId}
        AND flg_valid = TRUE
        <!-- 通过判断id是否存在判断为新增操作还是修改操作 -->
        <if test="brandId != null and brandId != ''">
            AND brand_id != #{brandId}::uuid
        </if>
        <if test="brandName != null and brandName != ''">
            AND brand_name = #{brandName}
        </if>
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_mst_goods_brand
        (
        <trim suffixOverrides=",">
            brand_code,
            brand_name,
            short_name,
            display_no,
            remarks,
            cp_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.brandCode},
                #{item.brandName},
                #{item.shortName},
                #{item.displayNo},
                #{item.remarks},
                #{item.cpId},
            </trim>
            )
        </foreach>
    </insert>





    <insert id="saveSupplierBrand">
        insert into dkic_b.t_mst_supplier_brand
            (sup_id, brand_id)
        values (#{supId}::uuid,
                #{brandId}::uuid)
    </insert>


    <!-- 根据名称查ID -->
    <select id="selectByUuId" resultMap="BaseResultMapResponse">
        SELECT brand_id
        FROM dkic_b.t_mst_goods_brand
        WHERE
        cp_id = #{cpId}
        AND flg_valid = TRUE
        <if test="brandName != null and brandName != ''">
            AND brand_name = #{brandName}
        </if>
    </select>
</mapper>
