<?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.bonus.boot.manager.basic.dao.SysLogDao">

	<resultMap type="com.bonus.boot.manager.basic.entity.SysLogs" id="SysLogsMap">
		<result property="id" column="id" jdbcType="INTEGER"/>
		<result property="userid" column="userId" jdbcType="INTEGER"/>
		<result property="username" column="username" jdbcType="VARCHAR"/>
		<result property="module" column="module" jdbcType="VARCHAR"/>
		<result property="flag" column="flag" jdbcType="INTEGER"/>
		<result property="remark" column="remark" jdbcType="VARCHAR"/>
		<result property="phone" column="phone" jdbcType="VARCHAR"/>
		<result property="createtime" column="createTime" jdbcType="TIMESTAMP"/>

		<result property="isApp" column="is_app" jdbcType="VARCHAR"/>
		<result property="operationFunction" column="operation_function" jdbcType="VARCHAR"/>
		<result property="operationRecord" column="operation_record" jdbcType="VARCHAR"/>
		<result property="operationStatus" column="operation_status" jdbcType="VARCHAR"/>
	</resultMap>

	<!--查询单个-->
	<select id="queryById" resultMap="SysLogsMap">
        select
          id, userId, module, flag, remark, createTime
        from sys_logs
        where id = #{id}
    </select>

	<!--查询指定行数据-->
	<select id="queryAllByLimit" resultMap="SysLogsMap">
		SELECT
		sl.id,
		sl.userId,
		sl.`module`,
		if(sl.is_app = '1','app','后台') as is_app,
		sl.operation_function,
		sl.operation_record,
		sl.operation_status,
		sl.createTime
		FROM
		sys_logs sl

		<where>
			<if test="user.username != null and user.username != ''">
				and (
				sl.`module` like concat ('%',#{user.username},'%')
				)
			</if>
		</where>
		ORDER BY createTime DESC
		limit #{page}, #{pageSize}
	</select>

	<!--统计总行数-->
	<select id="count" resultType="java.lang.Integer">
		select count(1) from sys_logs sl
		<where>
			<if test="username != null and username != ''">
				and (
				sl.`module` like concat ('%',#{username},'%')
				)
			</if>
		</where>
	</select>

	<!--新增所有列-->
	<insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into sys_logs(userId, module, flag, remark, createTime)
        values (#{userid}, #{module}, #{flag}, #{remark}, #{createtime})
    </insert>

	<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
		insert into sys_logs(userId, module, flag, remark, createTime)
		values
		<foreach collection="entities" item="entity" separator=",">
			(#{entity.userid}, #{entity.module}, #{entity.flag}, #{entity.remark}, #{entity.createtime})
		</foreach>
	</insert>

	<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
		insert into sys_logs(userId, module, flag, remark, createTime)
		values
		<foreach collection="entities" item="entity" separator=",">
			(#{entity.userid}, #{entity.module}, #{entity.flag}, #{entity.remark}, #{entity.createtime})
		</foreach>
		on duplicate key update
		userId = values(userId),
		module = values(module),
		flag = values(flag),
		remark = values(remark),
		createTime = values(createTime)
	</insert>

	<!--通过主键修改数据-->
	<update id="update">
		update sys_logs
		<set>
			<if test="userid != null">
				userId = #{userid},
			</if>
			<if test="module != null and module != ''">
				module = #{module},
			</if>
			<if test="flag != null">
				flag = #{flag},
			</if>
			<if test="remark != null and remark != ''">
				remark = #{remark},
			</if>
			<if test="createtime != null">
				createTime = #{createtime},
			</if>
		</set>
		where id = #{id}
	</update>

	<!--通过主键删除-->
	<delete id="deleteById">
        delete from sys_logs where id = #{id}
    </delete>

</mapper>

