<?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.yiqi.order.mapper.OrderActivityMapper">

    <resultMap type="OrderActivity" id="OrderActivityResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="orgId" column="org_id"/>
        <result property="sourceType" column="source_type"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="payTime" column="pay_time"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="payType" column="pay_type"/>
        <result property="orderSource" column="order_source"/>
        <result property="outTradeOrderNo" column="out_trade_order_no"/>
        <result property="orderStatus" column="order_status"/>
        <result property="refundApplyTime" column="refund_apply_time"/>
        <result property="refundTime" column="refund_time"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="refundContent" column="refund_content"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectOrderActivityVo">
        select id, order_no, org_id, source_type, app_user_id, pay_time, pay_amount, pay_type, order_source, out_trade_order_no, order_status, refund_apply_time, refund_time, refund_amount, refund_content, del_flag, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark
        from order_activity
    </sql>

    <select id="selectOrderActivityList" resultType="com.yiqi.order.domain.vo.OrderActivityVO">
        select
        a.id as id, a.order_no as orderNo,a.order_status as orderStatus,
        a.activity_id as activityId,a.create_time as createTime,
        a.pay_amount as payAmount ,a.pay_type as payType, a.org_id as orgId,
        a.source_type as sourceType,a.pay_time as payTime,
        u.id as appUserId, u.nick_name as nickName ,u.real_name as realName,
        u.phone_number as phoneNumber ,
        s.name as storeName,
        i.title as activityTitle

        from order_activity a
        left join app_user u on a.app_user_id = u.id
        left join sys_store s on a.org_id = s.id
        left join activity_info i on a.activity_id = i.id
        <where>
            and a.del_flag='0'
            <if test="orderNo != null  and orderNo != ''">
                and a.order_no = #{orderNo}
            </if>
            <if test="orderStatus != null  and orderStatus != ''">
                and a.order_status = #{orderStatus}
            </if>
            <if test="realName != null  and realName != ''">
                and u.real_name = #{realName}
            </if>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and u.phone_number = #{phoneNumber}
            </if>
            <if test="activityTitle != null  and activityTitle != ''">
                and i.title = #{activityTitle}
            </if>
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="createTimeFrom != null and createTimeFrom !=''">
                <![CDATA[and a.create_time >= concat(#{createTimeFrom}, ' 00:00:00')]]>
            </if>
            <if test="createTimeTo != null and createTimeTo !=''">
                <![CDATA[and a.create_time <= concat(#{createTimeTo}, ' 23:59:59')]]>
            </if>
            <if test="beginPayTime != null and beginPayTime !=''">
                <![CDATA[and a.pay_time >= concat(#{beginPayTime}, ' 00:00:00')]]>
            </if>
            <if test="endPayTime != null and endPayTime !=''">
                <![CDATA[and a.pay_time <= concat(#{endPayTime}, ' 23:59:59')]]>
            </if>
            order by a.create_time desc
            <if test="pageSizeExport != 0">
                limit #{pageSizeExport}
            </if>
        </where>

    </select>

    <select id="selectOrderActivityById" parameterType="Long"
            resultMap="OrderActivityResult">
        <include refid="selectOrderActivityVo"/>
        where id = #{id}
        and del_flag='0'
    </select>

    <insert id="insertOrderActivity" parameterType="OrderActivity" useGeneratedKeys="true"
            keyProperty="id">
        insert into order_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,
            </if>
            <if test="orgId != null">org_id,
            </if>
            <if test="sourceType != null">source_type,
            </if>
            <if test="appUserId != null">app_user_id,
            </if>
            <if test="payTime != null">pay_time,
            </if>
            <if test="payAmount != null">pay_amount,
            </if>
            <if test="payType != null">pay_type,
            </if>
            <if test="orderSource != null">order_source,
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no,
            </if>
            <if test="orderStatus != null">order_status,
            </if>
            <if test="refundApplyTime != null">refund_apply_time,
            </if>
            <if test="refundTime != null">refund_time,
            </if>
            <if test="refundAmount != null">refund_amount,
            </if>
            <if test="refundContent != null">refund_content,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="payTime != null">#{payTime},
            </if>
            <if test="payAmount != null">#{payAmount},
            </if>
            <if test="payType != null">#{payType},
            </if>
            <if test="orderSource != null">#{orderSource},
            </if>
            <if test="outTradeOrderNo != null">#{outTradeOrderNo},
            </if>
            <if test="orderStatus != null">#{orderStatus},
            </if>
            <if test="refundApplyTime != null">#{refundApplyTime},
            </if>
            <if test="refundTime != null">#{refundTime},
            </if>
            <if test="refundAmount != null">#{refundAmount},
            </if>
            <if test="refundContent != null">#{refundContent},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateOrderActivity" parameterType="OrderActivity">
        update order_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="sourceType != null">source_type =
                #{sourceType},
            </if>
            <if test="appUserId != null">app_user_id =
                #{appUserId},
            </if>
            <if test="payTime != null">pay_time =
                #{payTime},
            </if>
            <if test="payAmount != null">pay_amount =
                #{payAmount},
            </if>
            <if test="payType != null">pay_type =
                #{payType},
            </if>
            <if test="orderSource != null">order_source =
                #{orderSource},
            </if>
            <if test="outTradeOrderNo != null">out_trade_order_no =
                #{outTradeOrderNo},
            </if>
            <if test="orderStatus != null">order_status =
                #{orderStatus},
            </if>
            <if test="refundApplyTime != null">refund_apply_time =
                #{refundApplyTime},
            </if>
            <if test="refundTime != null">refund_time =
                #{refundTime},
            </if>
            <if test="refundAmount != null">refund_amount =
                #{refundAmount},
            </if>
            <if test="refundContent != null">refund_content =
                #{refundContent},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderActivityById" parameterType="Long">
        update order_activity set del_flag='2' where id = #{id}
    </delete>

    <delete id="deleteOrderActivityByIds" parameterType="String">
        update order_activity set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>