<?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.pj.project4sp.apilog.SpApilogMapper">

	
	<!-- 保存入库 -->
	<insert id="saveObj">
		insert into 
		sp_apilog (
			id, req_ip, req_api, req_parame, req_type, req_token, req_header, user_id, admin_id, start_time,
			res_code, res_msg, res_string, end_time, cost_time) 
		values (
			#{id}, #{reqIp}, #{reqApi}, #{reqParame}, #{reqType}, #{reqToken}, #{reqHeader}, #{userId}, #{adminId}, #{startTime},
			#{resCode}, #{resMsg}, #{resString}, #{endTime}, #{costTime}) 
	</insert>
	
	
	<!-- 增 -->
	<insert id="add">
		insert into 
		sp_apilog (id, req_ip, req_api, req_parame, req_token, user_id, admin_id, start_time) 
		values (#{id}, #{reqIp}, #{reqApi}, #{reqParame}, #{reqToken}, #{userId}, #{adminId}, now(3)) 
	</insert>

	<!-- 删 -->
	<delete id="delete">
		delete from sp_apilog 
		where id = #{id}
	</delete>

	<!-- 删 -->
	<delete id="deleteByStartEnd">
		delete from sp_apilog 
		where start_time BETWEEN #{startTime} AND #{endTime} 
	</delete>
	
	<!-- 改 -->
	<update id="update">
		update sp_apilog set
		res_code = #{resCode}, 
		res_msg = #{resMsg}, 
		res_string = #{resString}, 
		end_time = now(3), 
		cost_time = #{costTime} 
		where id = #{id}
	</update>
	
	<!-- ================================== 查询相关 ================================== -->
	
	<!-- 通用映射 -->
	<resultMap id="model" type="com.pj.project4sp.apilog.SpApilog">
		<result property="id" column="id" />
		<result property="reqIp" column="req_ip" />
		<result property="reqApi" column="req_api" />
		<result property="reqParame" column="req_parame" />
		<result property="reqType" column="req_type" />
		<result property="reqToken" column="req_token" />
		<result property="reqHeader" column="req_header" />
		<result property="resCode" column="res_code" />
		<result property="resMsg" column="res_msg" />
		<result property="resString" column="res_string" />
		<result property="userId" column="user_id" />
		<result property="adminId" column="admin_id" />
		<result property="startTime" column="start_time" />
		<result property="endTime" column="end_time" />
		<result property="costTime" column="cost_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * from sp_apilog 
	</sql>
	
	<!-- 查询，根据条件(参数为null或0时默认忽略此条件) -->
	<select id="getList" resultMap="model" >
		<include refid="select_sql"></include>
		where 1 = 1 
		<if test=' this.has("id")  '>and id = #{id} </if>
		<if test=' this.has("reqToken") '>and req_token = #{reqToken} </if>
		<if test=' this.has("reqIp") '> and req_ip = #{reqIp} </if>
		<if test=' this.has("reqApi") '>and req_api = #{reqApi} </if>
		<if test=' this.has("resCode") '>and res_code = #{resCode} </if>
		<if test=' this.has("userId") '>and user_id = #{userId} </if>
		<if test=' this.has("adminId") '>and admin_id = #{adminId} </if>
		<if test=' this.has("sTime") '>and start_time &gt;= #{sTime} </if>
		<if test=' this.has("eTime") '>and start_time &lt;= #{eTime} </if>
		order by 
		<choose>
			<when test='sortType == 0'>id desc</when> 
			<when test='sortType == 1'>start_time desc, id desc</when> 
			<when test='sortType == 2'>cost_time desc, id desc</when> 
		 	<otherwise>id desc</otherwise>
		 </choose>
	</select>
	
	
	
	<!-- 统计 -->
	<select id="staBy" resultType="somap">
		select sum(cost_time) as cost_time_count from sp_apilog 
		where 1 = 1 
		<if test=' this.has("id")  '>and id = #{id} </if>
		<if test=' this.has("reqToken") '>and req_token = #{reqToken} </if>
		<if test=' this.has("reqIp") '> and req_ip = #{reqIp} </if>
		<if test=' this.has("reqApi") '>and req_api = #{reqApi} </if>
		<if test=' this.has("resCode") '>and res_code = #{resCode} </if>
		<if test=' this.has("userId") '>and user_id = #{userId} </if>
		<if test=' this.has("adminId") '>and admin_id = #{adminId} </if>
		<if test=' this.has("sTime") '>and start_time &gt;= #{sTime} </if>
		<if test=' this.has("eTime") '>and start_time &lt;= #{eTime} </if>
	</select>


</mapper>
