<?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.frequency.illegaldeductionrecord.mapper.IllegalDeductionRecordMapper">

    <resultMap type="com.ruoyi.frequency.illegaldeductionrecord.entity.IllegalDeductionRecord" id="IllegalDeductionRecordResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="withdrawalRecordId"    column="withdrawal_record_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="type"    column="type"    />
        <result property="remark"    column="remark"    />
        <result property="price"    column="price"    />
        <result property="currentPrice"    column="current_price"    />
        <result property="deductionType"    column="deduction_type"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderNo"    column="order_no"    />
    </resultMap>



    <sql id="selectIllegalDeductionRecordVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.withdrawal_record_id ,
            t.store_id ,
            t.type ,
            t.remark ,
            t.price ,
            t.current_price,
            t.deduction_type,
            t.order_id,
            t2.order_no
        from t_illegal_deduction_record AS t
        LEFT JOIN t_order t2 on t.order_id = t2.id

    </sql>

    <select id="selectIllegalDeductionRecordList" parameterType="com.ruoyi.frequency.illegaldeductionrecord.entity.IllegalDeductionRecord" resultMap="IllegalDeductionRecordResult">
        <include refid="selectIllegalDeductionRecordVo"/>
        <where> AND t.del_flag != 2
            <if test="withdrawalRecordId != null "> and t.withdrawal_record_id = #{withdrawalRecordId}</if>
            <if test="storeId != null "> and t.store_id = #{storeId}</if>
            <if test="type != null "> and t.type = #{type}</if>
            <if test="price != null "> and t.price = #{price}</if>
            <if test="currentPrice != null "> and t.current_price = #{currentPrice}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="deductionType != null and deductionType != ''"> and t.deduction_type  LIKE CONCAT('%',#{deductionType},'%')</if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectIllegalDeductionRecordById" parameterType="Long" resultMap="IllegalDeductionRecordResult">
        <include refid="selectIllegalDeductionRecordVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertIllegalDeductionRecord" parameterType="com.ruoyi.frequency.illegaldeductionrecord.entity.IllegalDeductionRecord">
        insert into t_illegal_deduction_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="withdrawalRecordId != null">withdrawal_record_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="type != null">type,</if>
            <if test="remark != null">remark,</if>
            <if test="price != null">price,</if>
            <if test="currentPrice != null">current_price,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="withdrawalRecordId != null">#{withdrawalRecordId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="type != null">#{type},</if>
            <if test="remark != null">#{remark},</if>
            <if test="price != null">#{price},</if>
            <if test="currentPrice != null">#{currentPrice},</if>
        </trim>
    </insert>

    <update id="updateIllegalDeductionRecord" parameterType="com.ruoyi.frequency.illegaldeductionrecord.entity.IllegalDeductionRecord">
        update t_illegal_deduction_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="withdrawalRecordId != null">withdrawal_record_id = #{withdrawalRecordId},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="price != null">price = #{price},</if>
            <if test="currentPrice != null">current_price = #{currentPrice},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="illegalDeductionPage" resultType="com.ruoyi.controller.resp.IllegalDeductionPageResp">
        SELECT
        t.id,
        t.withdrawal_record_id,
        t.create_time,
        t.price,
        t.type,
        t.current_price,
        CASE
        t.type
        WHEN 1 THEN '平台添加'
        WHEN 2 THEN '平台扣除'
        WHEN 3 THEN '提现抵消'
        WHEN 4 THEN '提现失败返还'
        ELSE ''
        END record
        FROM
        t_illegal_deduction_record t
        WHERE
        t.del_flag = 1
        AND t.store_id = #{userVo.userId}
        <if test="req.type != null and req.type != '' ">
            <if test="req.type == 1 ">
                and t.type in (1,4)
            </if>
            <if test="req.type == 2 ">
                and t.type in (2,3)
            </if>
        </if>
        ORDER BY
        t.create_time DESC
    </select>
    <select id="getOrderInfo"
            resultType="com.ruoyi.frequency.illegaldeductionrecord.model.result.OrderInfoResp">
        select order_no from t_order t where t.id = #{orderId}
    </select>
</mapper>