<?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.dao.OrderAddrDao">

    <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"/>
        <result column="accept_name_cipher" jdbcType="VARCHAR" property="acceptNameCipher"/>
        <result column="street_cipher" jdbcType="VARCHAR" property="streetCipher"/>
        <result column="door_number_cipher" jdbcType="VARCHAR" property="doorNumberCipher"/>
        <result column="mobile_cipher" jdbcType="VARCHAR" property="mobileCipher"/>

    </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, seller_id, hide
    </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, #{sellerId}, #{hide}
  </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="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="payDetail" index="index" separator=",">
            (
            #{payDetail.id}, #{payDetail.orderNo}, #{payDetail.userId}, #{payDetail.acceptName}, #{payDetail.sex},
            #{payDetail.zip}, #{payDetail.province},
            #{payDetail.city}, #{payDetail.area}, #{payDetail.street}, #{payDetail.lng},
            #{payDetail.lat}, #{payDetail.mobile}, #{payDetail.doorNumber},
            #{payDetail.provinceName}, #{payDetail.cityName}, #{payDetail.areaName}, #{payDetail.label},
            now(),now(), 0, #{payDetail.sellerId}, #{payDetail.hide}
            )
        </foreach>
    </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>

    <update id="updateUpdatedAt" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateAddr" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now(),
            accept_name = #{acceptName},
            street = #{street},
            door_number = #{doorNumber}
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateAddrInfo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now(),
            <if test="acceptName != null and acceptName != ''">
                accept_name = #{acceptName},
            </if>
            <if test="acceptNameCipher != null and acceptNameCipher != ''">
                accept_name_cipher = #{acceptNameCipher},
            </if>
            <if test="street != null and street != ''">
                street = #{street},
            </if>
            <if test="streetCipher != null and streetCipher != ''">
                street_cipher = #{streetCipher},
            </if>
            <if test="doorNumber != null and doorNumber != ''">
                door_number = #{doorNumber},
            </if>
            <if test="doorNumberCipher != null and doorNumberCipher != ''">
                door_number_cipher = #{doorNumberCipher},
            </if>
            <if test="mobile != null and mobile != ''">
                mobile = #{mobile},
            </if>
            <if test="mobileCipher != null and mobileCipher != ''">
                mobile_cipher = #{mobileCipher},
            </if>
        </set>
        where order_no = #{orderNo}
        and user_id = #{userId}
    </update>

    <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="selectOne" parameterType="map" resultMap="OrderAddrMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where id = #{id} 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>

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

    <update id="deleteTemp" parameterType="map">
        delete from
        <include refid="tb"/>
        where user_id = #{userId}
        and order_no = #{orderNo}
    </update>

    <update id="updateSellerUser" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = #{sellerId},
            user_id = #{newUserId},
            updated_at= now()
        </set>
        where
        user_id = #{oldUserId}
        and order_no = #{orderNo}
    </update>
</mapper>