<?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.changeRecord.mapper.ChangeRecordMapper">

    <resultMap type="ChangeRecordVO" id="ChangeRecordResult" autoMapping="true">
        <result property="recordId"    column="record_id"    />
        <result property="userId"    column="user_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="number"    column="number"    />
        <result property="beforeNum"    column="before_num"    />
        <result property="afterNum"    column="after_num"    />
        <result property="bstType"    column="bst_type"    />
        <result property="bstId"    column="bst_id"    />
        <result property="reason"    column="reason"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectChangeRecordVo">
        select
            bcr.record_id,
            bcr.user_id,
            bcr.store_id,
            bcr.number,
            bcr.before_num,
            bcr.after_num,
            bcr.bst_type,
            bcr.bst_id,
            bcr.reason,
            bcr.create_time,
            bs.user_id,
            bs.store_name,
            bs.user_name,
            bo.pay_amount as amount
        from <include refid="searchTables"/>
    </sql>


    <sql id="searchTables">
        bst_change_record bcr
            left join bst_store bs on bcr.store_id = bs.store_id
            left join bst_order bo on bcr.bst_id = bo.id
    </sql>

    <sql id="searchCondition">
        <if test="query.userId != null "> and bcr.user_id = #{query.userId}</if>
        <if test="query.storeId != null "> and bcr.store_id = #{query.storeId}</if>
        <if test="query.number != null "> and bcr.number = #{query.number}</if>
        <if test="query.beforeNum != null "> and bcr.before_num = #{query.beforeNum}</if>
        <if test="query.afterNum != null "> and bcr.after_num = #{query.afterNum}</if>
        <if test="query.bstType != null  and query.bstType != ''"> and bcr.bst_type = #{query.bstType}</if>
        <if test="query.bstId != null "> and bcr.bst_id = #{query.bstId}</if>
        <if test="query.reason != null  and query.reason != ''"> and bcr.reason = #{query.reason}</if>
        <if test="query.userName != null "> and bs.user_name like concat('%',#{query.userName},'%') </if>
        <if test="query.storeName != null "> and bs.store_name like concat('%',#{query.storeName},'%')</if>
        <if test="query.beginTime != null"> and bcr.create_time &gt;= #{query.beginTime}</if>
        <if test="query.endTime != null"> and bcr.create_time &lt;= #{query.endTime}</if>
        <if test="query.bstTypeList != null and query.bstTypeList.size() > 0">
            and bcr.bst_type in
            <foreach collection="query.bstTypeList" item="type" open="(" separator="," close=")">
                #{type}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
            .userSetAlias("bs.user_id")
            .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectChangeRecordList" parameterType="ChangeRecordQuery" resultMap="ChangeRecordResult">
        <include refid="selectChangeRecordVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectChangeRecordByRecordId" parameterType="Long" resultMap="ChangeRecordResult">
        <include refid="selectChangeRecordVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertChangeRecord" parameterType="ChangeRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into bst_change_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="number != null">number,</if>
            <if test="beforeNum != null">before_num,</if>
            <if test="afterNum != null">after_num,</if>
            <if test="bstType != null">bst_type,</if>
            <if test="bstId != null">bst_id,</if>
            <if test="reason != null">reason,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="number != null">#{number},</if>
            <if test="beforeNum != null">#{beforeNum},</if>
            <if test="afterNum != null">#{afterNum},</if>
            <if test="bstType != null">#{bstType},</if>
            <if test="bstId != null">#{bstId},</if>
            <if test="reason != null">#{reason},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="ChangeRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into bst_change_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            store_id,
            number,
            before_num,
            after_num,
            bst_type,
            bst_id,
            reason,
            create_time,
        </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.number != null ">#{i.number},</if>
                <if test="i.number == null ">default,</if>
                <if test="i.beforeNum != null ">#{i.beforeNum},</if>
                <if test="i.beforeNum == null ">default,</if>
                <if test="i.afterNum != null ">#{i.afterNum},</if>
                <if test="i.afterNum == null ">default,</if>
                <if test="i.bstType != null ">#{i.bstType},</if>
                <if test="i.bstType == null ">default,</if>
                <if test="i.bstId != null ">#{i.bstId},</if>
                <if test="i.bstId == null ">default,</if>
                <if test="i.reason != null ">#{i.reason},</if>
                <if test="i.reason == 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_change_record
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="user_id = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.record_id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="store_id = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeId != null ">
                        WHEN #{item.record_id} THEN #{item.storeId}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="number = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.number != null ">
                        WHEN #{item.record_id} THEN #{item.number}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `number`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="before_num = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.beforeNum != null ">
                        WHEN #{item.record_id} THEN #{item.beforeNum}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `before_num`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="after_num = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.afterNum != null ">
                        WHEN #{item.record_id} THEN #{item.afterNum}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `after_num`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="bst_type = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.bstType != null ">
                        WHEN #{item.record_id} THEN #{item.bstType}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `bst_type`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="bst_id = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.bstId != null ">
                        WHEN #{item.record_id} THEN #{item.bstId}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `bst_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="reason = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.reason != null ">
                        WHEN #{item.record_id} THEN #{item.reason}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `reason`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE record_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.record_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.record_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where record_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.recordId}
        </foreach>
    </update>

    <update id="updateChangeRecord" parameterType="ChangeRecord">
        update bst_change_record
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where record_id = #{data.recordId}
    </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.number != null">number = #{data.number},</if>
        <if test="data.beforeNum != null">before_num = #{data.beforeNum},</if>
        <if test="data.afterNum != null">after_num = #{data.afterNum},</if>
        <if test="data.bstType != null">bst_type = #{data.bstType},</if>
        <if test="data.bstId != null">bst_id = #{data.bstId},</if>
        <if test="data.reason != null">reason = #{data.reason},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteChangeRecordByRecordId" parameterType="Long">
        delete from bst_change_record where record_id = #{recordId}
    </delete>

    <delete id="deleteChangeRecordByRecordIds" parameterType="String">
        delete from bst_change_record where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>
