<?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.yami.shop.delivery.comment.dao.Transport2Mapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.Transport2">
        <id column="transport_id" jdbcType="BIGINT" property="transportId"/>
        <result column="trans_name" jdbcType="VARCHAR" property="transName"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="piece" jdbcType="DOUBLE" property="piece"/>
        <result column="distance" jdbcType="DOUBLE" property="distance"/>
        <result column="amount" jdbcType="DOUBLE" property="amount"/>
        <result column="channel_id" jdbcType="BIGINT" property="channelId"/>
    </resultMap>

    <resultMap id="transportConutMap" type="com.yami.shop.delivery.comment.param.ShopTransport">

        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        <result column="transport_conut" jdbcType="BIGINT" property="transportConut"/>
        <result column="province" jdbcType="VARCHAR" property="province"/>
        <result column="city" jdbcType="VARCHAR" property="city"/>
        <result column="area" jdbcType="VARCHAR" property="area"/>
        <result column="shop_address" jdbcType="VARCHAR" property="shopAddress"/>

    </resultMap>


    <select id="getTransportShopPlatform" resultMap="BaseResultMap">
        SELECT *
        FROM tz_transport2 p
        WHERE p.shop_id = #{shopId}
          AND p.channel_id = #{platform}
        ORDER BY p.distance LIMIT 1
    </select>

    <select id="getTransport" resultMap="BaseResultMap">
        SELECT *
        FROM tz_transport2 p
        WHERE p.shop_id = #{shopId}
          AND p.distance &gt;= #{distance}
        ORDER BY p.distance LIMIT 1
    </select>

    <select id="getTransportWeight" resultMap="BaseResultMap">
        SELECT *
        FROM tz_transport2 p
        WHERE p.shop_id = #{shopId}
          AND p.distance &gt;= #{distance}
          AND p.weight &gt;= #{weight}
        ORDER BY p.distance LIMIT 1
    </select>


    <select id="findByTransportId" resultMap="BaseResultMap">
        select *
        from tz_transport2 p
        where p.transport_id = #{transportId}
    </select>


    <delete id="deleteTransports">
        delete from tz_transport2 where transport_id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <select id="findShopTransports" resultMap="transportConutMap">
        SELECT
        shop.shop_id,shop.shop_name,shop.province,shop.city,shop.area,shop.shop_address,
        COUNT( tr.transport_id ) AS transport_conut
        FROM
        tz_shop_detail shop
        LEFT JOIN tz_transport2 tr ON shop.shop_id = tr.shop_id
        <!--        <where>-->
        <!--            <if test="shopName != null and shopName != ''">-->
        <!--                AND shop.shop_name LIKE CONCAT('%', #{shopName}, '%')-->
        <!--            </if>-->
        <!--        </where>-->
        WHERE shop.shop_id = #{shopId}
        GROUP BY shop.shop_id
    </select>

    <select id="findByPlatform" resultMap="BaseResultMap">
        SELECT *
        FROM tz_transport2 p
        WHERE  p.channel_id = #{channelId}
        ORDER BY p.distance LIMIT 1
    </select>

    <select id="transport2List" resultMap="BaseResultMap">
        SELECT *
        FROM tz_transport2
        WHERE !ISNULL(channel_id)
    </select>

</mapper>
