<?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.zcloud.mapper.datasource.bus.TradingRecordMapper">
	
	<!--表名 -->
	<sql id="tableName">
		BUS_TRADINGRECORD
	</sql>
	
	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		f.OUTPUT_USER_ID,	
		f.OUTPUT_USER_NAME,	
		f.INPUT_USER_ID,	
		f.INPUT_USER_NAME,	
		f.MONEY,	
		f.SERVICECHARGE,	
		f.ACTUALAMOUNT,	
		f.RELEVANCE_ID,	
		f.RECORD_TYPE,	
		f.ISDELETE,	
		f.CREATOR,	
		f.CREATTIME,	
		f.OPERATOR,	
		f.OPERATTIME,	
		f.TRADINGRECORD_ID
	</sql>
	
	<!-- 字段用于新增 -->
	<sql id="Field2">
		OUTPUT_USER_ID,	
		OUTPUT_USER_NAME,	
		INPUT_USER_ID,	
		INPUT_USER_NAME,	
		MONEY,	
		SERVICECHARGE,	
		ACTUALAMOUNT,	
		RELEVANCE_ID,	
		RECORD_TYPE,	
		ISDELETE,	
		CREATOR,	
		CREATTIME,	
		OPERATOR,	
		OPERATTIME,	
		TRADINGRECORD_ID
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{OUTPUT_USER_ID},	
		#{OUTPUT_USER_NAME},	
		#{INPUT_USER_ID},	
		#{INPUT_USER_NAME},	
		#{MONEY},	
		#{SERVICECHARGE},	
		#{ACTUALAMOUNT},	
		#{RELEVANCE_ID},	
		#{RECORD_TYPE},	
		#{ISDELETE},	
		#{CREATOR},	
		#{CREATTIME},	
		#{OPERATOR},	
		#{OPERATTIME},	
		#{TRADINGRECORD_ID}
	</sql>
	
	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into 
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where 
			TRADINGRECORD_ID = #{TRADINGRECORD_ID}
	</delete>
	
	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			OUTPUT_USER_ID = #{OUTPUT_USER_ID},
			OUTPUT_USER_NAME = #{OUTPUT_USER_NAME},
			INPUT_USER_ID = #{INPUT_USER_ID},
			INPUT_USER_NAME = #{INPUT_USER_NAME},
			MONEY = #{MONEY},
			SERVICECHARGE = #{SERVICECHARGE},
			ACTUALAMOUNT = #{ACTUALAMOUNT},
			RELEVANCE_ID = #{RELEVANCE_ID},
			RECORD_TYPE = #{RECORD_TYPE},
			TRADINGRECORD_ID = TRADINGRECORD_ID
		where 
			TRADINGRECORD_ID = #{TRADINGRECORD_ID}
	</update>
	
	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select 
		<include refid="Field"></include>
		from 
		<include refid="tableName"></include> f
		where 
			f.TRADINGRECORD_ID = #{TRADINGRECORD_ID}
	</select>
	
	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		case f.RECORD_TYPE when '1' then '提现'
			when '2' then '交易'  when '3'  then '充值' end as RECORD_TYPENAME
		from 
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.OUTPUT_USER_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or 
					f.INPUT_USER_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') 
					 or 
					f.MONEY LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') 
					 or 
					f.SERVICECHARGE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') 
					 or 
					f.ACTUALAMOUNT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') 
				)	
		</if>
		<if test="pd.RECORD_TYPE != null and pd.RECORD_TYPE != ''"><!-- 关键词检索 -->
			and f.RECORD_TYPE = #{pd.RECORD_TYPE}
		</if>
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 关键词检索 -->
			and f.CREATTIME &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
			and f.CREATTIME &lt;= #{pd.ENDTIME}
		</if>	
		<if test="pd.USER_ID != null and pd.USER_ID != ''"><!-- 关键词检索 -->
			and ( 
				f.OUTPUT_USER_ID = #{pd.USER_ID}
				or
				f.INPUT_USER_ID = #{pd.USER_ID}
			)
		</if>
		order by f.OPERATTIME desc
	</select>
	
	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		bc.NAME RECORDNAME
		from 
		<include refid="tableName"></include> f
		left join bus_order bo on bo.ISDELETE = '0' and bo.ORDER_ID = f.RELEVANCE_ID and f.RECORD_TYPE = '2'
		left join bus_classify bc on bc.ISDELETE = '0' and bc.CLASSIFY_ID = bo.CLASSIFY_ID
		where f.ISDELETE = '0'
		<if test="USER_ID != null and USER_ID != ''"><!-- 关键词检索 -->
			and ( 
				f.OUTPUT_USER_ID = #{USER_ID}
				or
				f.INPUT_USER_ID = #{USER_ID}
			)
		</if>
		order by f.OPERATTIME desc
	</select>
	
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where 
			TRADINGRECORD_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
