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

    <select id="findByAddress" resultType="com.sckw.transport.model.KwtWaybillOrderAddress" >
        select
        id, w_order_id wOrderId, 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_waybill_order_address
        where del_flag = 0
        <if test="wOrderId != null and wOrderId != ''">
            and w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
        <if test="addressType != null and addressType != ''">
            and address_type = #{addressType, jdbcType=INTEGER}
        </if>
        order by sort
    </select>

    <select id="findByAddresses" resultType="com.sckw.transport.model.KwtWaybillOrderAddress" >
        select
        id, w_order_id wOrderId, w_subtask_id wSubtaskId, l_address_id lAddressId, address_type addressType,
        `name`, `type`, contacts, phone, city_code cityCode, city_name cityName, detail_address detailAddress,
        lat, lng, fence, entry_type entryType, plan_time planTime, entrust_amount entrustAmount, sort, remark,
        `status`, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_waybill_order_address
        where del_flag = 0
        <if test="wOrderId != null and wOrderId != ''">
            and w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
        <if test="addressType != null and addressType != ''">
            and address_type = #{addressType, jdbcType=INTEGER}
        </if>
        order by sort
    </select>

    <select id="findBySubtaskId" resultType="com.sckw.transport.model.KwtWaybillOrderAddress" >
        select
        id, w_order_id wOrderId, w_subtask_id wSubtaskId, l_address_id lAddressId, address_type addressType,
        `name`, `type`, contacts, phone, city_code cityCode, city_name cityName, detail_address detailAddress,
        lat, lng, fence, entry_type entryType, plan_time planTime, entrust_amount entrustAmount, sort, remark,
        `status`, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_waybill_order_address
        where del_flag = 0
        <if test="wSubtaskId != null and wSubtaskId != ''">
            and w_subtask_id = #{wSubtaskId, jdbcType=BIGINT}
        </if>
        <if test="addressType != null and addressType != ''">
            and address_type = #{addressType, jdbcType=BIGINT}
        </if>
        order by sort
    </select>

    <select id="findByLAddressIds" resultType="com.sckw.transport.model.KwtWaybillOrderAddress" >
        select
        id, w_order_id wOrderId, w_subtask_id wSubtaskId, l_address_id lAddressId, address_type addressType,
        `name`, `type`, contacts, phone, city_code cityCode, city_name cityName, detail_address detailAddress,
        lat, lng, fence, entry_type entryType, plan_time planTime, entrust_amount entrustAmount, sort, remark,
        `status`, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_waybill_order_address
        where del_flag = 0
        <if test="wOrderId != null and wOrderId != ''">
            and w_order_id = #{wOrderId, jdbcType=BIGINT}
        </if>
        <if test="lAddressIds != null and lAddressIds.size() > 0">
            AND l_address_id IN
            <foreach collection="lAddressIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by sort
    </select>

    <select id="findAddressCodeList" resultType="java.lang.Integer" >
        SELECT
        DISTINCT kwoa.city_code
        from kwt_waybill_order kwo
        left join kwt_waybill_order_address kwoa on kwoa.w_order_id = kwo.id
        where kwo.del_flag = 0 and kwoa.del_flag = 0
        and kwo.ent_id = #{entId, jdbcType=BIGINT}
        and kwoa.address_type = #{addressType, jdbcType=INTEGER}
    </select>

</mapper>