<?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.WalletMapper">
	
	<!--表名 -->
	<sql id="tableName">
		BUS_WALLET
	</sql>
	
	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		f.USER_ID,	
		f.BALANCE,	
		f.ISDELETE,	
		f.CREATOR,	
		f.CREATTIME,	
		f.OPERATOR,	
		f.OPERATTIME,	
		f.USER_TYPE,
		f.WALLET_ID
	</sql>
	
	<!-- 字段用于新增 -->
	<sql id="Field2">
		USER_ID,	
		BALANCE,	
		ISDELETE,	
		CREATOR,	
		CREATTIME,	
		OPERATOR,	
		OPERATTIME,		
		USER_TYPE,
		WALLET_ID
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{USER_ID},	
		#{BALANCE},	
		#{ISDELETE},	
		#{CREATOR},	
		#{CREATTIME},	
		#{OPERATOR},	
		#{OPERATTIME},		
		#{USER_TYPE},
		#{WALLET_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 
			WALLET_ID = #{WALLET_ID}
	</delete>
	
	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			USER_ID = #{USER_ID},
			BALANCE = #{BALANCE},		
			OPERATOR = #{OPERATOR},	
			OPERATTIME = #{OPERATTIME},	
			WALLET_ID = WALLET_ID
		where 
			WALLET_ID = #{WALLET_ID}
	</update>
	
	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select 
		<include refid="Field"></include>
		from 
		<include refid="tableName"></include> f
		where 
			f.WALLET_ID = #{WALLET_ID}
	</select>
	
	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		if(f.USER_TYPE = '1','客户','服务人员') as USER_TYPENAME,
		u.NAME
		from 
		<include refid="tableName"></include> f
		left join USER_VIEW u on u.APPID = f.USER_ID and u.USER_TYPE = f.USER_TYPE
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				
					f.BALANCE LIKE CONCAT(CONCAT('%', #{pd.BALANCE}),'%')
				<!--	根据需求自己加检索条件
					 or 
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%') 
				-->
				)
		</if>
		order by f.OPERATTIME desc
	</select>
	
	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from 
		<include refid="tableName"></include> f
	</select>
	
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where 
			WALLET_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
	
		<!-- 通过ID获取数据 -->
	<select id="findByUser" parameterType="pd" resultType="pd">
		select 
		<include refid="Field"></include>,
		u.NAME
		from 
		<include refid="tableName"></include> f
		left join USER_VIEW u on u.APPID = f.USER_ID and u.USER_TYPE = f.USER_TYPE
		where 
			f.USER_ID = #{USER_ID}
		<if test="USER_TYPE != null and USER_TYPE != ''"><!-- 关键词检索 -->
			and f.USER_TYPE = #{USER_TYPE}
		</if>
	</select>
	
	<!-- 修改余额 -->
	<update id="editBalance" parameterType="pd">
		update
		<include refid="tableName"></include>
		set 
			BALANCE = #{BALANCE},		
			OPERATOR = #{OPERATOR},	
			OPERATTIME = #{OPERATTIME},	
			WALLET_ID = WALLET_ID
		where 
			WALLET_ID = #{WALLET_ID}
	</update>

</mapper>
