<?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.ErpGoofishOrderMapper">

    <resultMap id="ErpGoofishOrderResult" type="com.ruoyi.jarvis.domain.ErpGoofishOrder">
        <id     property="id" column="id"/>
        <result property="appKey" column="app_key"/>
        <result property="sellerId" column="seller_id"/>
        <result property="userName" column="user_name"/>
        <result property="orderNo" column="order_no"/>
        <result property="orderType" column="order_type"/>
        <result property="orderStatus" column="order_status"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="modifyTime" column="modify_time"/>
        <result property="productId" column="product_id"/>
        <result property="itemId" column="item_id"/>
        <result property="goodsTitle" column="goods_title"/>
        <result property="goodsImageUrl" column="goods_image_url"/>
        <result property="buyerNick" column="buyer_nick"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="detailWaybillNo" column="detail_waybill_no"/>
        <result property="detailExpressCode" column="detail_express_code"/>
        <result property="detailExpressName" column="detail_express_name"/>
        <result property="receiverName" column="receiver_name"/>
        <result property="receiverMobile" column="receiver_mobile"/>
        <result property="receiverAddress" column="receiver_address"/>
        <result property="receiverRegion" column="receiver_region"/>
        <result property="recvProvName" column="recv_prov_name"/>
        <result property="recvCityName" column="recv_city_name"/>
        <result property="recvAreaName" column="recv_area_name"/>
        <result property="recvTownName" column="recv_town_name"/>
        <result property="detailJson" column="detail_json"/>
        <result property="lastNotifyJson" column="last_notify_json"/>
        <result property="jdOrderId" column="jd_order_id"/>
        <result property="localWaybillNo" column="local_waybill_no"/>
        <result property="shipStatus" column="ship_status"/>
        <result property="shipError" column="ship_error"/>
        <result property="shipTime" column="ship_time"/>
        <result property="shipExpressCode" column="ship_express_code"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="jdThirdPartyOrderNo" column="jd_third_party_order_no"/>
        <result property="jdRemark" column="jd_remark"/>
        <result property="jdAddress" column="jd_address"/>
    </resultMap>

    <sql id="selectJoinVo">
        select e.id, e.app_key, e.seller_id, e.user_name, e.order_no, e.order_type, e.order_status, e.refund_status,
               e.modify_time, e.product_id, e.item_id, e.goods_title, e.goods_image_url, e.buyer_nick, e.pay_amount, e.detail_waybill_no,
               e.detail_express_code, e.detail_express_name, e.receiver_name, e.receiver_mobile, e.receiver_address,
               e.receiver_region, e.recv_prov_name, e.recv_city_name, e.recv_area_name, e.recv_town_name,
               e.detail_json, e.last_notify_json, e.jd_order_id, e.local_waybill_no,
               e.ship_status, e.ship_error, e.ship_time, e.ship_express_code, e.create_time, e.update_time,
               o.third_party_order_no as jd_third_party_order_no, o.remark as jd_remark, o.address as jd_address
        from erp_goofish_order e
        left join jd_order o on e.jd_order_id = o.id
    </sql>

    <select id="selectById" resultMap="ErpGoofishOrderResult">
        <include refid="selectJoinVo"/> where e.id = #{id}
    </select>

    <select id="selectByAppKeyAndOrderNo" resultMap="ErpGoofishOrderResult">
        <include refid="selectJoinVo"/> where e.app_key = #{appKey} and e.order_no = #{orderNo} limit 1
    </select>

    <select id="selectList" parameterType="com.ruoyi.jarvis.domain.ErpGoofishOrder" resultMap="ErpGoofishOrderResult">
        <include refid="selectJoinVo"/>
        <where>
            <if test="appKey != null and appKey != ''">and e.app_key = #{appKey}</if>
            <if test="userName != null and userName != ''">and e.user_name like concat('%', #{userName}, '%')</if>
            <if test="orderNo != null and orderNo != ''">and e.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="orderStatus != null">and e.order_status = #{orderStatus}</if>
            <if test="shipStatus != null">and e.ship_status = #{shipStatus}</if>
            <if test="jdOrderId != null">and e.jd_order_id = #{jdOrderId}</if>
        </where>
        order by e.modify_time desc, e.id desc
    </select>

    <select id="selectPendingShip" resultMap="ErpGoofishOrderResult">
        <include refid="selectJoinVo"/>
        where e.jd_order_id is not null
          and (e.ship_status is null or e.ship_status != 1)
          <if test="statuses != null and statuses.size() &gt; 0">
              and e.order_status in
              <foreach collection="statuses" item="st" open="(" separator="," close=")">
                  #{st}
              </foreach>
          </if>
        order by e.update_time asc
        limit #{limit}
    </select>

    <insert id="insert" parameterType="com.ruoyi.jarvis.domain.ErpGoofishOrder" useGeneratedKeys="true" keyProperty="id">
        insert into erp_goofish_order
        (app_key, seller_id, user_name, order_no, order_type, order_status, refund_status, modify_time, product_id, item_id,
         goods_title, goods_image_url, buyer_nick, pay_amount, detail_waybill_no, detail_express_code, detail_express_name,
         receiver_name, receiver_mobile, receiver_address, receiver_region,
         recv_prov_name, recv_city_name, recv_area_name, recv_town_name,
         detail_json, last_notify_json, jd_order_id, local_waybill_no, ship_status, ship_error, ship_time, ship_express_code,
         create_time, update_time)
        values
        (#{appKey}, #{sellerId}, #{userName}, #{orderNo}, #{orderType}, #{orderStatus}, #{refundStatus}, #{modifyTime}, #{productId}, #{itemId},
         #{goodsTitle}, #{goodsImageUrl}, #{buyerNick}, #{payAmount}, #{detailWaybillNo}, #{detailExpressCode}, #{detailExpressName},
         #{receiverName}, #{receiverMobile}, #{receiverAddress}, #{receiverRegion},
         #{recvProvName}, #{recvCityName}, #{recvAreaName}, #{recvTownName},
         #{detailJson}, #{lastNotifyJson}, #{jdOrderId}, #{localWaybillNo}, #{shipStatus}, #{shipError}, #{shipTime}, #{shipExpressCode},
         #{createTime}, #{updateTime})
    </insert>

    <update id="update" parameterType="com.ruoyi.jarvis.domain.ErpGoofishOrder">
        update erp_goofish_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="sellerId != null">seller_id = #{sellerId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="refundStatus != null">refund_status = #{refundStatus},</if>
            <if test="modifyTime != null">modify_time = #{modifyTime},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="goodsTitle != null">goods_title = #{goodsTitle},</if>
            <if test="goodsImageUrl != null">goods_image_url = #{goodsImageUrl},</if>
            <if test="buyerNick != null">buyer_nick = #{buyerNick},</if>
            <if test="payAmount != null">pay_amount = #{payAmount},</if>
            <if test="detailWaybillNo != null">detail_waybill_no = #{detailWaybillNo},</if>
            <if test="detailExpressCode != null">detail_express_code = #{detailExpressCode},</if>
            <if test="detailExpressName != null">detail_express_name = #{detailExpressName},</if>
            <if test="receiverName != null">receiver_name = #{receiverName},</if>
            <if test="receiverMobile != null">receiver_mobile = #{receiverMobile},</if>
            <if test="receiverAddress != null">receiver_address = #{receiverAddress},</if>
            <if test="receiverRegion != null">receiver_region = #{receiverRegion},</if>
            <if test="recvProvName != null">recv_prov_name = #{recvProvName},</if>
            <if test="recvCityName != null">recv_city_name = #{recvCityName},</if>
            <if test="recvAreaName != null">recv_area_name = #{recvAreaName},</if>
            <if test="recvTownName != null">recv_town_name = #{recvTownName},</if>
            <if test="detailJson != null">detail_json = #{detailJson},</if>
            <if test="lastNotifyJson != null">last_notify_json = #{lastNotifyJson},</if>
            <if test="jdOrderId != null">jd_order_id = #{jdOrderId},</if>
            <if test="localWaybillNo != null">local_waybill_no = #{localWaybillNo},</if>
            <if test="shipStatus != null">ship_status = #{shipStatus},</if>
            <if test="shipError != null">ship_error = #{shipError},</if>
            <if test="shipTime != null">ship_time = #{shipTime},</if>
            <if test="shipExpressCode != null">ship_express_code = #{shipExpressCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="resetShipForRetry">
        update erp_goofish_order
        set ship_status = 0,
            ship_error = null,
            update_time = now()
        where id = #{id}
    </update>
</mapper>
