<?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.crm.manager.dao.mapper.SysAreaMapper">

    <insert id="save" parameterType="com.crm.rely.backend.core.pojo.table.SysAreaTable">
        insert into sys_area(area_name,point_id,add_time,add_user,add_ip,note)
        values(#{areaName},#{pointId},#{addTime},#{addUser},#{addIp},#{note})
    </insert>

    <update id="update" parameterType="com.crm.rely.backend.core.pojo.table.SysAreaTable">
        update sys_area
        set area_name = #{areaName},
            point_id = #{pointId},
            modify_time = #{modifyTime},
            modify_user = #{modifyUser},
            modify_ip = #{modifyIp},
            note = #{note}
        where id = #{id}
    </update>

    <select id="findInfoById" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.SysAreaTable">
        select *
        from sys_area
        where id = #{id}
    </select>

    <select id="countByAreaName" parameterType="String" resultType="Integer">
        select count(id)
        from sys_area
        <where>
            <if test="areaName != null">
                and area_name = #{areaName}
            </if>
        </where>
    </select>

    <select id="findViewById" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.view.SysAreaView">
        select sa.*,sp.`name` point_name
        from sys_area sa
        inner join sys_point sp on sa.point_id = sp.id
        where sa.id = #{id}
    </select>

    <select id="countList"
            parameterType="com.crm.rely.backend.core.entity.system.area.SysAreaSearchEntity"
            resultType="Integer">
        select count(id)
        from sys_area sa
        <where>
            <if test="areaName != null">
                and sa.area_name like concat('%',#{areaName},'%')
            </if>
            <if test="startDate != null">
                and sa.add_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and sa.add_time <![CDATA[<=]]>#{endDate}
            </if>
        </where>
    </select>

    <select id="pageList"
            parameterType="com.crm.rely.backend.core.entity.system.area.SysAreaSearchEntity"
            resultType="com.crm.rely.backend.core.pojo.view.SysAreaView">
        select sa.*,sp.`name` point_name
        from sys_area sa
        inner join sys_point sp on sa.point_id = sp.id
        <where>
            <if test="areaName != null">
                and sa.area_name like concat('%',#{areaName},'%')
            </if>
            <if test="startDate != null">
                and sa.add_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and sa.add_time <![CDATA[<=]]>#{endDate}
            </if>
        </where>
        order by sa.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <delete id="removeById" parameterType="Long">
        delete from sys_area where id=#{id}
    </delete>

    <select id="findAll" resultType="com.crm.rely.backend.core.pojo.table.SysAreaTable">
        select *
        from sys_area
    </select>
</mapper>