<?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.dgtly.system.mapper.SysMagnetLogMapper">
    
    <resultMap type="SysMagnetLog" id="SysMagnetLogResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="orgCode"    column="org_code"    />
        <result property="orgName"    column="org_name"    />
        <result property="level"    column="level"    />
        <result property="magnetName"    column="magnet_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysMagnetLogVo">
        select id, user_id, user_name, org_code, org_name, level, magnet_name, create_by, create_time, update_by, update_time from sys_magnet_log
    </sql>

    <select id="selectSysMagnetLogList" parameterType="SysMagnetLog" resultMap="SysMagnetLogResult">
        select a.id, a.user_id, a.user_name, b.org_code, b.org_name, b.sales_level level, a.magnet_name, a.create_by, a.create_time, a.update_by, a.update_time
        from sys_magnet_log a left join sys_user_ext b on a.user_id=b.user_id
        <where>
            <if test="userId != null "> and a.user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and a.user_name like concat('%', #{userName}, '%')</if>
            <if test="orgCode != null  and orgCode != ''"> and b.org_code = #{orgCode}</if>
            <if test="orgName != null  and orgName != ''"> and b.org_name like concat('%', #{orgName}, '%')</if>
            <if test="level != null  and level != ''"> and b.sales_level = #{level}</if>
            <if test="magnetName != null  and magnetName != ''"> and a.magnet_name like concat('%', #{magnetName}, '%')</if>
            <if test="params.beginTime != null and params.beginTime != ''">
                and date_format(a.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                and date_format(a.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
        order by a.create_time desc
    </select>
    
    <select id="selectSysMagnetLogById" parameterType="Long" resultMap="SysMagnetLogResult">
        <include refid="selectSysMagnetLogVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSysMagnetLog" parameterType="SysMagnetLog" useGeneratedKeys="true" keyProperty="id">
        insert into sys_magnet_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null ">user_id,</if>
            <if test="userName != null  and userName != ''">user_name,</if>
            <if test="orgCode != null  and orgCode != ''">org_code,</if>
            <if test="orgName != null  and orgName != ''">org_name,</if>
            <if test="level != null  and level != ''">level,</if>
            <if test="magnetName != null  and magnetName != ''">magnet_name,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null ">#{userId},</if>
            <if test="userName != null  and userName != ''">#{userName},</if>
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="level != null  and level != ''">#{level},</if>
            <if test="magnetName != null  and magnetName != ''">#{magnetName},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSysMagnetLog" parameterType="SysMagnetLog">
        update sys_magnet_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null ">user_id = #{userId},</if>
            <if test="userName != null  and userName != ''">user_name = #{userName},</if>
            <if test="orgCode != null  and orgCode != ''">org_code = #{orgCode},</if>
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="level != null  and level != ''">level = #{level},</if>
            <if test="magnetName != null  and magnetName != ''">magnet_name = #{magnetName},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysMagnetLogById" parameterType="Long">
        delete from sys_magnet_log where id = #{id}
    </delete>

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