<?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.ruoyi.bst.part.mapper.PartMapper">

    <resultMap type="PartVO" id="PartResult" autoMapping="true">
        <result property="partId"    column="part_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="partName"    column="part_name"    />
        <result property="picture"    column="picture"    />
        <result property="createTime"    column="create_time"    />
        <result property="orderNum"    column="order_num"    />
    </resultMap>

    <sql id="selectPartVo">
        select
            bp.part_id,
            bp.parent_id,
            bp.store_id,
            bp.part_name,
            bp.picture,
            bp.create_time,
            bp.order_num,
            bs.store_name,
            bs.user_id,
            bs.store_name as storeName,
            bp_parent.part_name as parent_name
        <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        from bst_part bp
        left join bst_store bs on bp.store_id = bs.store_id
        left join bst_part bp_parent on bp.parent_id = bp_parent.part_id
    </sql>

    <sql id="searchCondition">
        <if test="query.partName != null  and query.partName != ''"> and bp.part_name like concat('%', #{query.partName}, '%')</if>
        <if test="query.partId != null  and query.partId != ''"> and bp.part_id = #{query.partId}</if>
        <if test="query.storeId != null  and query.storeId != ''"> and bp.store_id = #{query.storeId}</if>
        <if test="query.parentId != null  and query.parentId != ''"> and parent_id = #{query.parentId}</if>
        <if test="query.picture != null  and query.picture != ''"> and picture = #{query.picture}</if>
        <if test="query.storeName != null  and query.storeName != ''"> and bs.store_name like concat('%', #{query.storeName}, '%') </if>
        <if test="query.parentName != null  and query.parentName != ''"> and bp_parent.part_name like concat('%', #{query.parentName}, '%') </if>
        <if test="query.parentIds != null and query.parentIds.size() > 0">
            and bp.parent_id in
            <foreach collection="query.parentIds" item="ids" open="(" separator="," close=")">
                #{ids}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .userSetAlias("bs.user_id")
        .storeAlias("bp.store_id",query.storePermissions)
        .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectPartList" parameterType="PartQuery" resultMap="PartResult">
        <include refid="selectPartVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        order by bp.order_num asc
    </select>

    <select id="selectPartByPartId" parameterType="Long" resultMap="PartResult">
        <include refid="selectPartVo"/>
        where bp.part_id = #{partId}
    </select>

    <!-- selectIdByQuery -->
    <select id="selectIdByQuery" resultType="Long">
        select bp.part_id <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectChildren" resultType="PartVO">
        <include refid="selectPartVo"/>
        where bp.parent_id = #{partId}
    </select>

    <insert id="insertPart" parameterType="Part" useGeneratedKeys="true" keyProperty="partId">
        insert into bst_part
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="partName != null">part_name,</if>
            <if test="picture != null">picture,</if>
            <if test="createTime != null">create_time,</if>
            <if test="orderNum != null">order_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="storeId != null">#{storeId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="partName != null">#{partName},</if>
            <if test="picture != null">#{picture},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="orderNum != null">#{orderNum},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Part" useGeneratedKeys="true" keyProperty="partId">
        insert into bst_part
        <trim prefix="(" suffix=")" suffixOverrides=",">
            floor_id
            store_id,
            part_name,
            picture,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.partName != null ">#{i.partName},</if>
                <if test="i.partName == null ">default,</if>
                <if test="i.picture != null ">#{i.picture},</if>
                <if test="i.picture == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_part
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="store_id = CASE part_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.floorId != null ">
                        WHEN #{item.part_id} THEN #{item.floorId}
                    </when>
                    <otherwise>
                        WHEN #{item.part_id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="part_name = CASE part_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.partName != null ">
                        WHEN #{item.part_id} THEN #{item.partName}
                    </when>
                    <otherwise>
                        WHEN #{item.part_id} THEN `part_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE part_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null ">
                        WHEN #{item.part_id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.part_id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE part_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.part_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.part_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where part_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.partId}
        </foreach>
    </update>

    <update id="updatePart" parameterType="Part">
        update bst_part
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where part_id = #{data.partId}
    </update>

    <sql id="updateColumns">
        <if test="data.parentId != null">parent_id = #{data.parentId},</if>
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.partName != null">part_name = #{data.partName},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.orderNum != null">order_num = #{data.orderNum},</if>
    </sql>

    <delete id="deletePartByPartId" parameterType="Long">
        delete from bst_part where part_id = #{partId}
    </delete>

    <delete id="deletePartByPartIds" parameterType="String">
        delete from bst_part where part_id in
        <foreach item="partId" collection="ids" open="(" separator="," close=")">
            #{partId}
        </foreach>
    </delete>
</mapper>
