<?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.suit.mapper.SuitMapper">

    <resultMap type="SuitVO" id="SuitResult" autoMapping="true">
        <result property="suitId"    column="suit_id"    />
        <result property="userId"    column="user_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="rechargeAmount"    column="recharge_amount"    />
        <result property="lightingNums"    column="lighting_nums"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="deleted"    column="deleted"    />
        <result property="orderNum"    column="order_num"    />
    </resultMap>

    <sql id="selectSuitVo">
        select
            bsu.suit_id,
            bsu.user_id,
            bsu.store_id,
            bsu.recharge_amount,
            bsu.lighting_nums,
            bsu.status,
            bsu.create_time,
            bsu.deleted,
            bsu.order_num,
            bs.user_id,
            bs.store_name
        from bst_suit bsu
        left join bst_store bs on bsu.store_id = bs.store_id
    </sql>

    <sql id="searchCondition">
        <if test="query.userId != null "> and bsu.user_id = #{query.userId}</if>
        <if test="query.suitId != null "> and bsu.suit_id = #{query.suitId}</if>
        <if test="query.storeId != null "> and bsu.store_id = #{query.storeId}</if>
        <if test="query.rechargeAmount != null "> and bsu.recharge_amount = #{query.rechargeAmount}</if>
        <if test="query.lightingNums != null "> and bsu.lighting_nums = #{query.lightingNums}</if>
        <if test="query.status != null  and query.status != ''"> and bsu.status = #{query.status}</if>
        <if test="query.deleted != null "> and bsu.deleted = #{query.deleted}</if>
        <if test="query.orderNum != null "> and bsu.order_num = #{query.orderNum}</if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .userSetAlias("bs.user_id")
        .storeAlias("bsu.store_id",query.storePermissions)
         .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectSuitList" parameterType="SuitQuery" resultMap="SuitResult">
        <include refid="selectSuitVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSuitBySuitId" parameterType="Long" resultMap="SuitResult">
        <include refid="selectSuitVo"/>
        where suit_id = #{suitId}
    </select>

    <insert id="insertSuit" parameterType="Suit" useGeneratedKeys="true" keyProperty="suitId">
        insert into bst_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="rechargeAmount != null">recharge_amount,</if>
            <if test="lightingNums != null">lighting_nums,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="deleted != null">deleted,</if>
            <if test="orderNum != null">order_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="rechargeAmount != null">#{rechargeAmount},</if>
            <if test="lightingNums != null">#{lightingNums},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="orderNum != null">#{orderNum},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Suit" useGeneratedKeys="true" keyProperty="suitId">
        insert into bst_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            store_id,
            recharge_amount,
            lighting_nums,
            status,
            create_time,
            deleted,
            order_num,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.storeId != null ">#{i.storeId},</if>
                <if test="i.storeId == null ">default,</if>
                <if test="i.rechargeAmount != null ">#{i.rechargeAmount},</if>
                <if test="i.rechargeAmount == null ">default,</if>
                <if test="i.lightingNums != null ">#{i.lightingNums},</if>
                <if test="i.lightingNums == null ">default,</if>
                <if test="i.status != null ">#{i.status},</if>
                <if test="i.status == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.deleted != null ">#{i.deleted},</if>
                <if test="i.deleted == null ">default,</if>
                <if test="i.orderNum != null ">#{i.orderNum},</if>
                <if test="i.orderNum == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_suit
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="user_id = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.suit_id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="store_id = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeId != null ">
                        WHEN #{item.suit_id} THEN #{item.storeId}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="recharge_amount = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.rechargeAmount != null ">
                        WHEN #{item.suit_id} THEN #{item.rechargeAmount}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `recharge_amount`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="lighting_nums = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.lightingNums != null ">
                        WHEN #{item.suit_id} THEN #{item.lightingNums}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `lighting_nums`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="status = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.status != null ">
                        WHEN #{item.suit_id} THEN #{item.status}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `status`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.suit_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="deleted = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.deleted != null ">
                        WHEN #{item.suit_id} THEN #{item.deleted}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `deleted`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="order_num = CASE suit_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.orderNum != null ">
                        WHEN #{item.suit_id} THEN #{item.orderNum}
                    </when>
                    <otherwise>
                        WHEN #{item.suit_id} THEN `order_num`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where suit_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.suitId}
        </foreach>
    </update>

    <update id="updateSuit" parameterType="Suit">
        update bst_suit
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where suit_id = #{data.suitId}
    </update>

    <!--logicDel-->
    <update id="logicDel">
        update bst_suit
        set deleted = true
        where suit_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and deleted = false
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.rechargeAmount != null">recharge_amount = #{data.rechargeAmount},</if>
        <if test="data.lightingNums != null">lighting_nums = #{data.lightingNums},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
        <if test="data.orderNum != null">order_num = #{data.orderNum},</if>
    </sql>

    <delete id="deleteSuitBySuitId" parameterType="Long">
        delete from bst_suit where suit_id = #{suitId}
    </delete>

    <delete id="deleteSuitBySuitIds" parameterType="String">
        delete from bst_suit where suit_id in
        <foreach item="suitId" collection="array" open="(" separator="," close=")">
            #{suitId}
        </foreach>
    </delete>
</mapper>
