<?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.sckw.transport.dao.KwtLogisticsOrderAddressMapper">
    <resultMap id="BaseResultMap" type="com.sckw.transport.model.KwtLogisticsOrderAddress">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="l_order_id" jdbcType="BIGINT" property="lOrderId" />
        <result column="address_type" jdbcType="INTEGER" property="addressType" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="type" jdbcType="BIGINT" property="type" />
        <result column="contacts" jdbcType="VARCHAR" property="contacts" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="city_code" jdbcType="INTEGER" property="cityCode" />
        <result column="city_name" jdbcType="VARCHAR" property="cityName" />
        <result column="detail_address" jdbcType="VARCHAR" property="detailAddress" />
        <result column="lat" jdbcType="VARCHAR" property="lat" />
        <result column="lng" jdbcType="VARCHAR" property="lng" />
        <result column="fence" jdbcType="VARCHAR" property="fence" />
        <result column="entry_type" jdbcType="INTEGER" property="entryType" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="BIGINT" property="updateBy" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    </resultMap>
    <sql id="Base_Column_List">
        id, l_order_id, address_type, `name`, `type`, contacts, phone, city_code, city_name,
    detail_address, lat, lng, fence, entry_type, remark, `status`, create_by, create_time,
    update_by, update_time, del_flag
    </sql>

    <select id="findByAddress" resultType="com.sckw.transport.model.KwtLogisticsOrderAddress" >
        select
        id, l_order_id lOrderId, address_type addressType, `name`, `type`, contacts, phone, city_code cityCode, city_name cityName,
        detail_address detailAddress, lat, lng, fence, entry_type entryType, remark, `status`, create_by createBy, create_time createTime,
        update_by updateBy, update_time updateTime
        from kwt_logistics_order_address
        where del_flag = 0
        and l_order_id = #{lOrderId, jdbcType=BIGINT}
        and address_type = #{addressType, jdbcType=INTEGER}
    </select>

    <select id="findByAddressAndLoadAmount" resultType="com.sckw.transport.model.vo.LoadAddressVo">
        SELECT a.id             AS addressId,
               a.`name`         AS loadName,
               a.city_name      AS loadAddress,
               a.contacts       AS loadContacts,
               a.phone          AS loadPhone,
               a.detail_address AS loadAddressDetail,
               b.load_amount    AS loadAmount
        FROM kwt_logistics_order_address a
        LEFT JOIN kwt_logistics_order_amount b ON a.id = b.l_address_id AND b.del_flag = 0
        WHERE a.del_flag = 0
          AND a.address_type = #{addressType,jdbcType=INTEGER}
          AND a.l_order_id = #{lOrderId}
    </select>

    <select id="findByAddressAndUnLoadAmount" resultType="com.sckw.transport.model.vo.UnLoadAddressVo">
        SELECT a.id             AS addressId,
        a.`name`         AS unloadName,
        a.city_name      AS unloadAddress,
        a.contacts       AS unloadContacts,
        a.phone          AS unloadPhone,
        a.detail_address AS unloadAddressDetail,
        b.load_amount    AS unloadAmount
        FROM kwt_logistics_order_address a
        LEFT JOIN kwt_logistics_order_amount b ON a.id = b.l_address_id AND b.del_flag = 0
        WHERE a.del_flag = 0
        AND a.address_type = #{addressType,jdbcType=INTEGER}
        AND a.l_order_id = #{lOrderId}
    </select>

    <select id="findByAddresses" resultType="com.sckw.transport.model.KwtLogisticsOrderAddress" >
        select
        id, l_order_id lOrderId, address_type addressType, `name`, `type`, contacts, phone, city_code cityCode, city_name cityName,
        detail_address detailAddress, lat, lng, fence, entry_type entryType, remark, `status`, create_by createBy, create_time createTime,
        update_by updateBy, update_time updateTime
        from kwt_logistics_order_address
        where del_flag = 0
        <if test="lOrderId != null and lOrderId != ''">
            and l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="addressType != null and addressType != ''">
            and address_type = #{addressType, jdbcType=INTEGER}
        </if>
    </select>
</mapper>