<?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.fzm.mall.server.admin.order.mapper.DeliveryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fzm.mall.server.admin.order.model.Delivery">
        <id column="id" property="id" />
        <result column="order_delivery_id" property="orderDeliveryId" />
        <result column="uid" property="uid" />
        <result column="merchant_id" property="merchantId" />
        <result column="goods_id" property="goodsId" />
        <result column="name" property="name" />
        <result column="coin" property="coin" />
        <result column="number" property="number" />
        <result column="uname" property="uname" />
        <result column="phone" property="phone" />
        <result column="address" property="address" />
        <result column="note" property="note" />
        <result column="delivery_state" property="deliveryState" />
        <result column="express_type" property="expressType" />
        <result column="express_no" property="expressNo" />
        <result column="express_detail" property="expressDetail" />
        <result column="delivery_time" property="deliveryTime" />
        <result column="finish_time" property="finishTime" />
        <result column="is_delete" property="isDelete" />
        <result column="buyer_state" property="buyerState" />
        <result column="seller_state" property="sellerState" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <select id="list" resultType="com.fzm.mall.server.admin.order.entity.dto.DeliveryDTO">
        select d.*, e.exp_name as expressName
        from order_delivery d
        left join distribution_express e on e.simple_name = d.express_type
        <where>
            <if test="gid != null and gid != ''">
                and d.goods_id like CONCAT('%', #{gid},'%')
            </if>
            <if test="orderDeliveryId != null and orderDeliveryId != ''">
                and d.order_delivery_id like CONCAT('%', #{orderDeliveryId},'%')
            </if>
            <if test="uid != null and uid != ''">
                and d.uid like CONCAT('%', #{uid},'%')
            </if>
            <if test="deliveryState != null">
                and d.delivery_state = #{deliveryState}
            </if>
            <if test="merchantId != null">
                and d.merchant_id = #{merchantId}
            </if>
        </where>
        order by d.create_time desc
    </select>

    <select id="read" resultMap="BaseResultMap">
        select d.*, e.exp_name as expressName, r.return_reason as returnReason, r.return_img as returnImg
        from order_delivery d
        left join distribution_express e on e.simple_name = d.express_type
        left join order_delivery_refund r on d.order_delivery_id = r.order_delivery_id
        where d.order_delivery_id = #{orderDeliveryId}
    </select>
</mapper>
