<?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.zm.order.tool.server.tempdb.TmpOrderAddrDao">

    <resultMap id="OrderAddrMap" type="com.zm.order.tool.server.model.OrderAddr">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="accept_name" jdbcType="VARCHAR" property="acceptName"/>
        <result column="sex" jdbcType="INTEGER" property="sex"/>
        <result column="zip" jdbcType="VARCHAR" property="zip"/>
        <result column="province" jdbcType="INTEGER" property="province"/>
        <result column="city" jdbcType="INTEGER" property="city"/>
        <result column="area" jdbcType="INTEGER" property="area"/>
        <result column="street" jdbcType="VARCHAR" property="street"/>
        <result column="lng" jdbcType="VARCHAR" property="lng"/>
        <result column="lat" jdbcType="VARCHAR" property="lat"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="door_number" jdbcType="VARCHAR" property="doorNumber"/>
        <result column="province_name" jdbcType="VARCHAR" property="provinceName"/>
        <result column="city_name" jdbcType="VARCHAR" property="cityName"/>
        <result column="area_name" jdbcType="VARCHAR" property="areaName"/>
        <result column="label" jdbcType="VARCHAR" property="label"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>

    <sql id="tb">
        order_addresses
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
     order_no, user_id, accept_name, sex, zip, province, city, area, street, lng, lat, mobile, door_number, province_name, city_name, area_name, label,
     created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
    #{id}, #{orderNo}, #{userId}, #{acceptName}, #{sex}, #{zip}, #{province},
    #{city}, #{area}, #{street}, #{lng}, #{lat}, #{mobile}, #{doorNumber}, #{provinceName}, #{cityName}, #{areaName}, #{label},
    now(),now(), 0
  </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderAddr">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (id, order_no, user_id, accept_name, sex, zip, province, city, area, street, lng, lat, mobile, door_number, province_name, city_name, area_name, label,
        created_at, updated_at, is_deleted, seller_id)
        values
        <foreach collection="orderAddrList" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.orderNo}, #{item.userId}, #{item.acceptName}, #{item.sex}, #{item.zip},
            #{item.province}, #{item.city}, #{item.area},
            #{item.street}, #{item.lng}, #{item.lat},
            #{item.mobile}, #{item.doorNumber}, #{item.provinceName},
            #{item.cityName}, #{item.areaName}, 0, now(), now(), 0, #{item.sellerId}
            )
        </foreach>
    </insert>

    <select id="selectByOrderNo" parameterType="map" resultMap="OrderAddrMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        and user_id = #{userId}
    </select>

    <select id="selectList" parameterType="map" resultMap="OrderAddrMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <choose>
                <when test="userIdList != null and userIdList.size > 0">
                    AND user_id in
                    <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                        #{userId}
                    </foreach>
                </when>
                <otherwise>
                    AND user_id = #{userId}
                </otherwise>
            </choose>
            <if test="orderNoList != null and orderNoList.size > 0">
                and order_no in
                <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </if>
        </where>
    </select>

    <update id="delete" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = -1,
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>
</mapper>