<?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.ruoyi.logistics.mapper.BizAddressBookMapper">
    
    <resultMap type="com.ruoyi.logistics.domain.BizAddressBook" id="BizAddressBookResult">
        <result property="addressId"    column="address_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="contactName"    column="contact_name"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="provinceName"    column="province_name"    />
        <result property="cityName"    column="city_name"    />
        <result property="countyName"    column="county_name"    />
        <result property="detailedAddress"    column="detailed_address"    />
        <result property="defaultFlag"    column="default_flag"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectBizAddressBookVo">
        select address_id, company_name, contact_name, contact_phone, province_name, city_name, county_name, detailed_address, default_flag, dept_id, user_id, remark, del_flag, create_time, update_time, create_by, update_by from biz_address_book
    </sql>

    <select id="selectBizAddressBookList" parameterType="com.ruoyi.logistics.domain.BizAddressBook" resultMap="BizAddressBookResult">
        <include refid="selectBizAddressBookVo"/>
        <where>  
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="contactName != null  and contactName != ''"> and contact_name like concat('%', #{contactName}, '%')</if>
            <if test="contactPhone != null  and contactPhone != ''"> and contact_phone = #{contactPhone}</if>
            <if test="provinceName != null  and provinceName != ''"> and province_name like concat('%', #{provinceName}, '%')</if>
            <if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>
            <if test="countyName != null  and countyName != ''"> and county_name like concat('%', #{countyName}, '%')</if>
            <if test="detailedAddress != null  and detailedAddress != ''"> and detailed_address = #{detailedAddress}</if>
            <if test="defaultFlag != null  and defaultFlag != ''"> and default_flag = #{defaultFlag}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="searchKeyword != null and searchKeyword != ''"> and (contact_name like concat('%', #{searchKeyword}, '%') or
                                                                            contact_phone like concat('%', #{searchKeyword}, '%') or
                                                                            detailed_address like concat('%', #{searchKeyword}, '%'))</if>
        </where>
        order by default_flag desc, create_time desc
    </select>
    
    <select id="selectBizAddressBookByAddressId" parameterType="Long" resultMap="BizAddressBookResult">
        <include refid="selectBizAddressBookVo"/>
        where address_id = #{addressId}
    </select>

    <insert id="insertBizAddressBook" parameterType="com.ruoyi.logistics.domain.BizAddressBook" useGeneratedKeys="true" keyProperty="addressId">
        insert into biz_address_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyName != null">company_name,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="provinceName != null">province_name,</if>
            <if test="cityName != null">city_name,</if>
            <if test="countyName != null">county_name,</if>
            <if test="detailedAddress != null">detailed_address,</if>
            <if test="defaultFlag != null">default_flag,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyName != null">#{companyName},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="provinceName != null">#{provinceName},</if>
            <if test="cityName != null">#{cityName},</if>
            <if test="countyName != null">#{countyName},</if>
            <if test="detailedAddress != null">#{detailedAddress},</if>
            <if test="defaultFlag != null">#{defaultFlag},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateBizAddressBook" parameterType="com.ruoyi.logistics.domain.BizAddressBook">
        update biz_address_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="provinceName != null">province_name = #{provinceName},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
            <if test="countyName != null">county_name = #{countyName},</if>
            <if test="detailedAddress != null">detailed_address = #{detailedAddress},</if>
            <if test="defaultFlag != null">default_flag = #{defaultFlag},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where address_id = #{addressId}
    </update>

    <delete id="deleteBizAddressBookByAddressId" parameterType="Long">
        delete from biz_address_book where address_id = #{addressId}
    </delete>

    <delete id="deleteBizAddressBookByAddressIds" parameterType="String">
        delete from biz_address_book where address_id in 
        <foreach item="addressId" collection="array" open="(" separator="," close=")">
            #{addressId}
        </foreach>
    </delete>

    <update id="updateBizAddressBookDefault" parameterType="Long">
        update biz_address_book set default_flag = '0' where user_id = #{userId} and default_flag = 1
    </update>

    <select id="selectDefaultAddressBook" parameterType="com.ruoyi.logistics.domain.BizAddressBook" resultMap="BizAddressBookResult">
        <include refid="selectBizAddressBookVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="defaultFlag != null "> and default_flag = #{defaultFlag}</if>
        </where>
    </select>
</mapper>