<?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.bonus.canteen.core.order.common.mapper.OrderDeliveryMapper">


  <update id="updateDeliveryCounter">
    update order_delivery
    <set>
      <if test="deliveryAddr != null and deliveryAddr != ''">
        consignee_address = concat(consignee_address, #{deliveryAddr}),
      </if>
      <if test="deliveryTime != null">
        delivery_time = #{deliveryTime},
      </if>
      <if test="deliveryFinishTime != null">
        delivery_finish_time = #{deliveryFinishTime},
      </if>
    </set>
    WHERE order_id = #{orderId}
    <if test="startDate != null">
      AND order_date <![CDATA[ >= ]]> #{startDate}
    </if>
    <if test="endDate != null">
      AND order_date <![CDATA[ <= ]]> #{endDate}
    </if>
  </update>

  <select id="countOrderUnTakeNum" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM order_info oi
    INNER JOIN order_delivery od ON oi.order_id = od.order_id AND oi.order_date = od.order_date
    WHERE od.consignee_address = #{deliveryAddr}
      AND oi.delivery_type = #{deliveryType}
      AND oi.order_state = 1
    <if test="startDate != null">
      AND oi.order_date <![CDATA[ >= ]]> #{startDate}
      AND od.order_date <![CDATA[ >= ]]> #{startDate}
    </if>
    <if test="endDate != null">
      AND oi.order_date <![CDATA[ <= ]]> #{endDate}
      AND od.order_date <![CDATA[ <= ]]> #{endDate}
    </if>
  </select>

</mapper>
