<?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.jarvis.mapper.JDOrderMapper">

    <resultMap id="JDOrderResult" type="JDOrder">
        <result property="id" column="id"/>
        <result property="remark" column="remark"/>
        <result property="distributionMark" column="distribution_mark"/>
        <result property="modelNumber" column="model_number"/>
        <result property="link" column="link"/>
        <result property="paymentAmount" column="payment_amount"/>
        <result property="rebateAmount" column="rebate_amount"/>
        <result property="address" column="address"/>
        <result property="logisticsLink" column="logistics_link"/>
        <result property="tencentDocPushed" column="tencent_doc_pushed"/>
        <result property="tencentDocPushTime" column="tencent_doc_push_time"/>
        <result property="orderId" column="order_id"/>
        <result property="buyer" column="buyer"/>
        <result property="orderTime" column="order_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="isCountEnabled" column="is_count_enabled"/>
        <result property="thirdPartyOrderNo" column="third_party_order_no"/>
        <result property="jingfenActualPrice" column="jingfen_actual_price"/>
        <result property="isRefunded" column="is_refunded"/>
        <result property="refundDate" column="refund_date"/>
        <result property="isRefundReceived" column="is_refund_received"/>
        <result property="refundReceivedDate" column="refund_received_date"/>
        <result property="isRebateReceived" column="is_rebate_received"/>
        <result property="rebateReceivedDate" column="rebate_received_date"/>
    </resultMap>

    <sql id="selectJDOrderBase">
        select id, remark, distribution_mark, model_number, link, payment_amount, rebate_amount,
               address, logistics_link, order_id, buyer, order_time, create_time, update_time, status, is_count_enabled, third_party_order_no, jingfen_actual_price,
               is_refunded, refund_date, is_refund_received, refund_received_date, is_rebate_received, rebate_received_date
        from jd_order
    </sql>

    <select id="selectJDOrderList" parameterType="JDOrder" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        <where>
            <if test="remark != null and remark != ''"> and remark like concat('%', #{remark}, '%')</if>
            <if test="params.orderSearch != null and params.orderSearch != ''">
                and (
                    order_id like concat('%', #{params.orderSearch}, '%')
                    or third_party_order_no like concat('%', #{params.orderSearch}, '%')
                    or distribution_mark like concat('%', #{params.orderSearch}, '%')
                )
            </if>
            <if test="distributionMark != null and distributionMark != ''"> and distribution_mark = #{distributionMark}</if>
            <if test="modelNumber != null and modelNumber != ''"> and model_number like concat('%', #{modelNumber}, '%')</if>
            <if test="link != null and link != ''"> and link like concat('%', #{link}, '%')</if>
            <if test="paymentAmount != null"> and payment_amount = #{paymentAmount}</if>
            <if test="rebateAmount != null"> and rebate_amount = #{rebateAmount}</if>
            <if test="address != null and address != ''"> and address like concat('%', #{address}, '%')</if>
            <if test="logisticsLink != null and logisticsLink != ''"> and logistics_link like concat('%', #{logisticsLink}, '%')</if>
            <if test="buyer != null and buyer != ''"> and buyer like concat('%', #{buyer}, '%')</if>
            <if test="orderTime != null"> and order_time = #{orderTime}</if>
            <if test="status != null and status != ''"> and status like concat('%', #{status}, '%')</if>
            <if test="isRefunded != null"> and is_refunded = #{isRefunded}</if>
            <if test="isRefundReceived != null"> and is_refund_received = #{isRefundReceived}</if>
            <if test="isRebateReceived != null"> and is_rebate_received = #{isRebateReceived}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date(order_time) &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date(order_time) &lt;= #{params.endTime}
            </if>
        </where>
        order by remark asc, create_time desc, id desc
    </select>

    <select id="selectJDOrderListWithSort" parameterType="JDOrder" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        <where>
            <if test="remark != null and remark != ''"> and remark like concat('%', #{remark}, '%')</if>
            <if test="params.orderSearch != null and params.orderSearch != ''">
                and (
                    order_id like concat('%', #{params.orderSearch}, '%')
                    or third_party_order_no like concat('%', #{params.orderSearch}, '%')
                    or distribution_mark like concat('%', #{params.orderSearch}, '%')
                )
            </if>
            <if test="distributionMark != null and distributionMark != ''"> and distribution_mark = #{distributionMark}</if>
            <if test="modelNumber != null and modelNumber != ''"> and model_number like concat('%', #{modelNumber}, '%')</if>
            <if test="link != null and link != ''"> and link like concat('%', #{link}, '%')</if>
            <if test="paymentAmount != null"> and payment_amount = #{paymentAmount}</if>
            <if test="rebateAmount != null"> and rebate_amount = #{rebateAmount}</if>
            <if test="address != null and address != ''"> and address like concat('%', #{address}, '%')</if>
            <if test="logisticsLink != null and logisticsLink != ''"> and logistics_link like concat('%', #{logisticsLink}, '%')</if>
            <if test="buyer != null and buyer != ''"> and buyer like concat('%', #{buyer}, '%')</if>
            <if test="orderTime != null"> and order_time = #{orderTime}</if>
            <if test="status != null and status != ''"> and status like concat('%', #{status}, '%')</if>
            <if test="isRefunded != null"> and is_refunded = #{isRefunded}</if>
            <if test="isRefundReceived != null"> and is_refund_received = #{isRefundReceived}</if>
            <if test="isRebateReceived != null"> and is_rebate_received = #{isRebateReceived}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date(order_time) &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date(order_time) &lt;= #{params.endTime}
            </if>
        </where>
        <choose>
            <when test="params.orderBy != null and params.orderBy != ''">
                order by ${params.orderBy}
                <if test="params.isAsc != null and params.isAsc == 'asc'">asc</if>
                <if test="params.isAsc == null or params.isAsc == 'desc'">desc</if>
            </when>
            <otherwise>
                order by remark asc, create_time desc, id desc
            </otherwise>
        </choose>
    </select>

    <select id="selectJDOrderById" parameterType="long" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where id = #{id}
    </select>

    <insert id="insertJDOrder" parameterType="JDOrder" useGeneratedKeys="true" keyProperty="id">
        insert into jd_order (
            remark, distribution_mark, model_number, link,
            payment_amount, rebate_amount, address, logistics_link,
            tencent_doc_pushed, tencent_doc_push_time,
            order_id, buyer, order_time, create_time, update_time, status, is_count_enabled, third_party_order_no, jingfen_actual_price,
            is_refunded, refund_date, is_refund_received, refund_received_date, is_rebate_received, rebate_received_date
        ) values (
            #{remark}, #{distributionMark}, #{modelNumber}, #{link},
            #{paymentAmount}, #{rebateAmount}, #{address}, #{logisticsLink},
            0, null,
            #{orderId}, #{buyer}, #{orderTime}, now(), now(), #{status}, #{isCountEnabled}, #{thirdPartyOrderNo}, #{jingfenActualPrice},
            #{isRefunded}, #{refundDate}, #{isRefundReceived}, #{refundReceivedDate}, #{isRebateReceived}, #{rebateReceivedDate}
        )
    </insert>

    <update id="updateJDOrder" parameterType="JDOrder">
        update jd_order
        <set>
            <if test="remark != null"> remark = #{remark},</if>
            <if test="distributionMark != null"> distribution_mark = #{distributionMark},</if>
            <if test="modelNumber != null"> model_number = #{modelNumber},</if>
            <if test="link != null"> link = #{link},</if>
            <if test="paymentAmount != null"> payment_amount = #{paymentAmount},</if>
            <if test="rebateAmount != null"> rebate_amount = #{rebateAmount},</if>
            <if test="address != null"> address = #{address},</if>
            <if test="logisticsLink != null"> logistics_link = #{logisticsLink},</if>
            <if test="tencentDocPushed != null"> tencent_doc_pushed = #{tencentDocPushed},</if>
            <if test="tencentDocPushTime != null"> tencent_doc_push_time = #{tencentDocPushTime},</if>
            <if test="orderId != null"> order_id = #{orderId},</if>
            <if test="buyer != null"> buyer = #{buyer},</if>
            <if test="orderTime != null"> order_time = #{orderTime},</if>
            <if test="status != null"> status = #{status},</if>
            <if test="isCountEnabled != null"> is_count_enabled = #{isCountEnabled},</if>
            <if test="thirdPartyOrderNo != null"> third_party_order_no = #{thirdPartyOrderNo},</if>
            <if test="jingfenActualPrice != null"> jingfen_actual_price = #{jingfenActualPrice},</if>
            <if test="isRefunded != null"> is_refunded = #{isRefunded},</if>
            <if test="refundDate != null"> refund_date = #{refundDate},</if>
            <if test="isRefundReceived != null"> is_refund_received = #{isRefundReceived},</if>
            <if test="refundReceivedDate != null"> refund_received_date = #{refundReceivedDate},</if>
            <if test="isRebateReceived != null"> is_rebate_received = #{isRebateReceived},</if>
            <if test="rebateReceivedDate != null"> rebate_received_date = #{rebateReceivedDate},</if>
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <select id="selectJDOrderByRemark" parameterType="string" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where remark = #{remark}
        limit 1
    </select>

    <select id="selectJDOrderListByAddress" parameterType="string" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where address = #{address}
        order by order_time desc
    </select>

    <select id="selectJDOrderByOrderId" parameterType="string" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where order_id = #{orderId}
        order by order_time desc
        limit 1
    </select>

    <select id="selectJDOrderByLogisticsLink" parameterType="string" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where logistics_link = #{logisticsLink}
        order by order_time desc
        limit 1
    </select>

    <select id="selectJDOrderByThirdPartyOrderNo" parameterType="string" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        where third_party_order_no = #{thirdPartyOrderNo}
        limit 1
    </select>

    <delete id="deleteJDOrderByIds" parameterType="long">
        delete from jd_order where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectJDOrderListByDistributionMarkFOrPDD" resultMap="JDOrderResult">
        <include refid="selectJDOrderBase"/>
        <where>
            (distribution_mark = 'F' OR distribution_mark = 'PDD' OR distribution_mark = 'H' OR distribution_mark = 'W' OR distribution_mark = 'PDD-W')
            AND logistics_link IS NOT NULL
            AND logistics_link != ''
            AND create_time >= DATE_SUB(NOW(), INTERVAL 30 DAY)
        </where>
        ORDER BY create_time DESC
    </select>

</mapper>


