<?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.zcloud.mapper.datasource.electronicFence.ElectronicFenceMapper">

    <!--表名 -->
    <sql id="tableName">
        electronic_fence
    </sql>

    <!--数据字典表名 -->
    <sql id="dicTableName">
        SYS_DICTIONARIES
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        f.ELECTRONIC_FENCE_AREA_ID,
        f.ELECTRONIC_FENCE_AREA_NAME,
        f.ELECTRONIC_FENCE_ID,
		f.ELECTRONIC_FENCE_NAME,
		f.HEADMAN,
		f.TEL,
		f.BZ,
		f.PARENT_ID,
		f.CORPINFO_ID,
		f.ISDELETE,
		f.OPERATOR,
		f.OPERATTIME,
		f.CREATTIME,
		f.CREATOR
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        ELECTRONIC_FENCE_AREA_ID,
        ELECTRONIC_FENCE_ID,
		ELECTRONIC_FENCE_AREA_NAME,
		ELECTRONIC_FENCE_NAME,
        HEADMAN,
		TEL,
		BZ,
		PARENT_ID,
		CORPINFO_ID,
		ISDELETE,
		OPERATOR,
		OPERATTIME,
		CREATTIME,
		CREATOR
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{ELECTRONIC_FENCE_AREA_ID},
        #{ELECTRONIC_FENCE_ID},
		#{ELECTRONIC_FENCE_AREA_NAME},
		#{ELECTRONIC_FENCE_NAME},
        #{HEADMAN},
        #{TEL},
        #{BZ},
		#{PARENT_ID},
		#{CORPINFO_ID},
		#{ISDELETE},
		#{OPERATOR},
		#{OPERATTIME},
		#{CREATTIME},
		#{CREATOR}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 删除-->
    <delete id="delete" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1',
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        ELECTRONIC_FENCE_AREA_ID = #{ELECTRONIC_FENCE_AREA_ID}
    </delete>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ELECTRONIC_FENCE_AREA_NAME = #{ELECTRONIC_FENCE_AREA_NAME},
        HEADMAN = #{HEADMAN},
        TEL = #{TEL},
        BZ = #{BZ},
        ELECTRONIC_FENCE_NAME = #{ELECTRONIC_FENCE_NAME},
        ELECTRONIC_FENCE_ID = #{ELECTRONIC_FENCE_ID}
        where
        ELECTRONIC_FENCE_AREA_ID = #{ELECTRONIC_FENCE_AREA_ID}
    </update>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where
        f.ELECTRONIC_FENCE_AREA_ID = #{ELECTRONIC_FENCE_AREA_ID}
        and ISDELETE = 0
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            ELECTRONIC_FENCE_AREA_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            ELECTRONIC_FENCE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            <!--	根据需求自己加检索条件
                字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            -->
            )
        </if>
        <if test="pd.PARENT_ID != null and pd.PARENT_ID != ''"><!-- 关键词检索 -->
           and PARENT_ID = #{pd.PARENT_ID}
        </if>

        ORDER BY f.OPERATTIME DESC
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
    </select>
    <select id="listTree" resultType="java.util.Map">
        select
        ELECTRONIC_FENCE_AREA_ID id,
        ELECTRONIC_FENCE_ID,
        ELECTRONIC_FENCE_NAME,
        ELECTRONIC_FENCE_AREA_NAME name,
        PARENT_ID pId
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
    </select>
    <select id="findByPId" resultType="com.zcloud.entity.PageData">
        select
        ELECTRONIC_FENCE_AREA_ID id,
        ELECTRONIC_FENCE_ID,
        ELECTRONIC_FENCE_NAME,
        ELECTRONIC_FENCE_AREA_NAME name,
        PARENT_ID pId
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID} and f.PARENT_ID = #{ELECTRONIC_FENCE_AREA_ID}
    </select>
    <select id="listTreeApp" resultType="java.util.Map">
        SELECT
            ELECTRONIC_FENCE_AREA_ID,
            IFNULL(ELECTRONIC_FENCE_ID,ELECTRONIC_FENCE_AREA_ID) id,
            ELECTRONIC_FENCE_ID,
            ELECTRONIC_FENCE_AREA_NAME name,
            PARENT_ID pId
        FROM
            electronic_fence f
        WHERE
            f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
    </select>


    <!-- 批量删除 -->
    <delete id="deleteAll" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1',
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        ELECTRONIC_FENCE_AREA_ID  = #{ELECTRONIC_FENCE_AREA_ID}
    </delete>

</mapper>
