<?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.microservices.zone.detail.mapper.ZoneApplicationMapper">

    <resultMap type="com.microservices.zone.detail.domain.ZoneApplication" id="ZoneApplicationResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="introduction"    column="introduction"    />
        <result property="purpose"    column="purpose"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="contactWechat"    column="contact_wechat"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="key"    column="key"    />
        <result property="servicesProvided"    column="services_provided"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="zoneId" column="zone_id"/>
        <result property="zoneKey" column="zoneKey"/>
    </resultMap>

    <sql id="selectZoneApplicationVo">
        select id,
               name,
               introduction,
               purpose,
               contact_person,
               contact_wechat,
               contact_phone,
               `key`,
               services_provided,
               create_by,
               create_time,
               update_by,
               update_time,
               zone_id
        from zone_application
    </sql>

    <sql id="selectZoneApplicationAliasVo">
        SELECT za.id,
               za.NAME,
               za.introduction,
               za.purpose,
               za.contact_person,
               za.contact_wechat,
               za.contact_phone,
               za.`key`,
               za.services_provided,
               za.create_by,
               za.create_time,
               za.update_by,
               za.update_time,
               za.zone_id,
               zd.`key` AS zoneKey
        FROM zone_application AS za
                 LEFT JOIN zone_detail AS zd ON za.zone_id = zd.id
    </sql>

    <select id="selectZoneApplicationList" parameterType="ZoneApplication" resultMap="ZoneApplicationResult">
        <include refid="selectZoneApplicationAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and za.name like concat('%', #{name}, '%')</if>
            <if test="introduction != null  and introduction != ''">and za.introduction = #{introduction}</if>
            <if test="purpose != null  and purpose != ''">and za.purpose = #{purpose}</if>
            <if test="contactPerson != null  and contactPerson != ''">and za.contact_person = #{contactPerson}</if>
            <if test="contactWechat != null  and contactWechat != ''">and za.contact_wechat = #{contactWechat}</if>
            <if test="contactPhone != null  and contactPhone != ''">and za.contact_phone = #{contactPhone}</if>
            <if test="key != null  and key != ''">and za.`key` = #{key}</if>
            <if test="servicesProvided != null  and servicesProvided != ''">and za.services_provided =
                #{servicesProvided}
            </if>
            <if test="zoneId != null">and za.zone_id = #{zoneId}</if>
        </where>
    </select>
    
    <select id="selectZoneApplicationById" parameterType="Long" resultMap="ZoneApplicationResult">
        <include refid="selectZoneApplicationVo"/>
        where id = #{id}
    </select>
    <select id="selectZoneApplicationListBySearchInput"
            parameterType="com.microservices.zone.detail.domain.vo.ZoneApplicationSearchVo"
            resultMap="ZoneApplicationResult">
        <include refid="selectZoneApplicationAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and za.name like concat('%', #{name}, '%')</if>
            <if test="contactPerson != null  and contactPerson != ''">and za.contact_person like concat('%',
                #{contactPerson}, '%')
            </if>
            <if test="contactPhone != null  and contactPhone != ''">and za.contact_phone like concat('%',
                #{contactPhone}, '%')
            </if>
        </where>
    </select>

    <insert id="insertZoneApplication" parameterType="ZoneApplication" useGeneratedKeys="true" keyProperty="id">
        insert into zone_application
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="introduction != null and introduction != ''">introduction,</if>
            <if test="purpose != null">purpose,</if>
            <if test="contactPerson != null and contactPerson != ''">contact_person,</if>
            <if test="contactWechat != null">contact_wechat,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="key != null">`key`,</if>
            <if test="servicesProvided != null">services_provided,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="zoneId != null">zone_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="introduction != null and introduction != ''">#{introduction},</if>
            <if test="purpose != null">#{purpose},</if>
            <if test="contactPerson != null and contactPerson != ''">#{contactPerson},</if>
            <if test="contactWechat != null">#{contactWechat},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="key != null">#{key},</if>
            <if test="servicesProvided != null">#{servicesProvided},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="zoneId != null">#{zoneId},</if>
         </trim>
    </insert>

    <update id="updateZoneApplication" parameterType="ZoneApplication">
        update zone_application
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="introduction != null and introduction != ''">introduction = #{introduction},</if>
            <if test="purpose != null">purpose = #{purpose},</if>
            <if test="contactPerson != null and contactPerson != ''">contact_person = #{contactPerson},</if>
            <if test="contactWechat != null">contact_wechat = #{contactWechat},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="key != null">`key` = #{key},</if>
            <if test="servicesProvided != null">services_provided = #{servicesProvided},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="zoneId != null">zone_id = #{zoneId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteZoneApplicationById" parameterType="Long">
        delete from zone_application where id = #{id}
    </delete>

    <delete id="deleteZoneApplicationByIds" parameterType="String">
        delete from zone_application where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>