<?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.fuzamei.mapperInterface.OperationHistoryMapper">
	
	<!-- 插入订单操作记录表中 -->
	<insert id="addToHistory">
		insert into
			operation_history(order_id,
						  	  operation_type_id,
						  	  operator_id,
						      operation_time,
						      hash)
			values(#{orderId},
				   #{operationTypeId},
				   #{operatorId},
				   #{operationTime},
				   #{hash}) 
	</insert>
	
	<select id="queryOperationHistory" parameterType="Params" resultType="OperationHistory">
		select
			oh.order_id,
			oh.operation_type_id,
			oh.operator_id,
			ot.operation_type_name,
			u.person_name as operator_name,
			oh.operation_time,
			oh.hash
		from 
			operation_history oh
		left join 
			operation_type ot
		on 
			oh.operation_type_id=ot.operation_type_id
		left join 
			users u
		on 
			oh.operator_id=u.user_id
		<where>
			oh.operator_id=#{userId}
			<if test="operationTypeId!=null">
				and oh.operation_type_id=#{operationTypeId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and oh.operation_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by oh.operation_time desc
		limit #{startPage},#{rowNum}
	</select>
	
	<select id="findQueryCount" parameterType="Params" resultType="int">
		select
			count(*)
		from 
			operation_history
		<where>
			operator_id=#{userId}
			<if test="operationTypeId!=null">
				operation_type_id=#{operationTypeId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and operation_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
	
	<select id="showAllOperationTypeOfUser" resultType="OperationType">
		select * from operation_type where operation_type_id in(1,2,3,4,5)
	</select>
	
	<select id="showAllOperationTypeOfAdmin" resultType="OperationType">
		select * from operation_type where operation_type_id in(6,7,8)
	</select>
	
	
	<select id="queryOperationHistoryByAdmin" parameterType="Params" resultType="OperationHistoryAdmin">
		select 
			oha.operation_time,
			oha.operation_type_id,
			oha.operator_id,
			ot.operation_type_name,
			oha.hash,
			u1.person_name as operator_name,
			u2.person_name as be_operated_name,
			u2.username as be_operated_username		
		from 
			operation_history_admin oha
		left join 
			operation_type ot
		on 
			oha.operation_type_id=ot.operation_type_id
		left join 
			users u1
		on 
			oha.operator_id=u1.user_id
		left join 
			users u2
		on 
			oha.be_operated_id=u2.user_id
		<where>
			oha.operator_id=#{userId}
			<if test="operationTypeId!=null">
				and oha.operation_type_id=#{operationTypeId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and oha.operation_time between #{startTime} and #{endTime}
			</if>
		</where>
		order by oha.operation_time desc
		limit #{startPage},#{rowNum}
	</select>
	
	
	<select id="findQueryCountByAdmin" parameterType="Params" resultType="int">
		select
			count(*)
		from 
			operation_history_admin
		<where>
			operator_id=#{userId}
			<if test="operationTypeId!=null">
				and operation_type_id=#{operationTypeId}
			</if>
			<if test="startTime!=null and endTime!=null">
				and operation_time between #{startTime} and #{endTime}
			</if>
		</where>
	</select>
	
</mapper>