<?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.tao.mapper.AppElementMapper">

    <resultMap type="AppElement" id="AppElementResult">
        <result property="elementID" column="element_id"/>
        <result property="elementName" column="element_name"/>
        <result property="elementAndroidLocate" column="element_android_locate"/>
        <result property="elementIOSLocate" column="element_iOS_locate"/>
        <result property="elementRemark" column="element_remark"/>
        <result property="elementType" column="element_type"/>
        <result property="pageID" column="page_id"/>
        <association property="appPage" column="page_id" javaType="AppPage" resultMap="AppPageResult"/>
    </resultMap>

    <resultMap type="AppPage" id="AppPageResult">
        <result property="pageID" column="page_id"/>
        <result property="pageName" column="page_name"/>
        <result property="pageClassName" column="page_class_name"/>
    </resultMap>

    <sql id="selectAppElementVo">
        select e.element_id,
               e.element_name,
               e.element_android_locate,
               e.element_iOS_locate,
               e.element_remark,
               e.element_type,
               e.page_id,
               p.page_id,
               p.page_name,
               p.page_class_name
        from app_element e
                 left join app_page p on p.page_id = e.page_id
    </sql>

    <select id="selectAppElementById" parameterType="Long" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        where e.element_id = #{elementID}
    </select>

    <select id="selectAppElementByName" parameterType="String" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        where e.element_name = #{elementName}
    </select>

    <select id="selectAppElementListByPageID" parameterType="Long" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        where e.page_id = #{pageID}
    </select>

    <select id="selectAppElementListByType" parameterType="Long" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        where e.element_type = #{elementType}
    </select>

    <select id="selectAppElementList" parameterType="AppElement" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        <where>
            <if test="elementID != null  and elementID != ''">and e.element_id = #{elementID}</if>
            <if test="elementName != null  and elementName != ''">and e.element_name = #{elementName}</if>
            <if test="elementAndroidLocate != null  and elementAndroidLocate != ''">and e.element_android_locate = #{elementAndroidLocate}</if>
            <if test="elementIOSLocate != null  and elementIOSLocate != ''">and e.element_iOS_locate = #{elementIOSLocate}</if>
            <if test="elementRemark != null  and elementRemark != ''">and e.element_remark = #{elementRemark}</if>
            <if test="elementType != null">and e.element_type = #{elementType}</if>
            <if test="pageID != null  and pageID != ''">and e.page_id = #{pageID}</if>
        </where>
    </select>

    <select id="selectAppElementListFuzzy" parameterType="AppElement" resultMap="AppElementResult">
        <include refid="selectAppElementVo"/>
        <where>
            <if test="elementID != null  and elementID != ''">and e.element_id = #{elementID}</if>
            <if test="elementName != null  and elementName != ''">and e.element_name like '%'||#{elementName}||'%'</if>
            <if test="elementAndroidLocate != null  and elementAndroidLocate != ''">and e.element_android_locate like '%'||#{elementAndroidLocate}||'%'</if>
            <if test="elementIOSLocate != null  and elementIOSLocate != ''">and e.element_iOS_locate like '%'||#{elementIOSLocate}||'%'</if>
            <if test="elementRemark != null  and elementRemark != ''">and e.element_remark like '%'||#{elementRemark}||'%'</if>
            <if test="elementType != null">and e.element_type = #{elementType}</if>
            <if test="pageID != null  and pageID != ''">and e.page_id = #{pageID}</if>
        </where>
    </select>

    <insert id="insertAppElement" parameterType="AppElement" useGeneratedKeys="true" keyProperty="elementID">
        insert into app_element
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="elementName != null">element_name,</if>
            <if test="elementAndroidLocate != null">element_android_locate,</if>
            <if test="elementIOSLocate != null">element_iOS_locate,</if>
            <if test="elementRemark != null">element_remark,</if>
            <if test="elementType != null">element_type,</if>
            <if test="pageID != null">page_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="elementName != null">#{elementName},</if>
            <if test="elementAndroidLocate != null">#{elementAndroidLocate},</if>
            <if test="elementIOSLocate != null">#{elementIOSLocate},</if>
            <if test="elementRemark != null">#{elementRemark},</if>
            <if test="elementType != null">#{elementType},</if>
            <if test="pageID != null">#{pageID},</if>
        </trim>
    </insert>

    <update id="updateAppElement" parameterType="AppElement" keyProperty="elementID">
        update app_element
        <trim prefix="SET" suffixOverrides=",">
            <if test="elementName != null">element_name = #{elementName},</if>
            <if test="elementAndroidLocate != null">element_android_locate = #{elementAndroidLocate},</if>
            <if test="elementIOSLocate != null">element_iOS_locate = #{elementIOSLocate},</if>
            <if test="elementRemark != null">element_remark = #{elementRemark},</if>
            <if test="elementType != null">element_type = #{elementType},</if>
            <if test="pageID != null">page_id = #{pageID},</if>
        </trim>
        where element_id = #{elementID}
    </update>

    <delete id="deleteAppElementById" parameterType="Long">
        delete
        from app_element
        where element_id = #{elementID}
    </delete>

    <delete id="deleteAppElementByType" parameterType="Long">
        delete
        from app_element
        where element_type = #{elementType}
    </delete>

</mapper>