<?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="operatorBalanceSqlMapper">
	<sql id="base_column">
		id,
		operator_id as operatorId,
		act_time as actTime,
		num,
		amount,
		state,
		balance,
		income
	</sql>
	
	<insert id="addOperatorBalance" parameterType="com.cloudcross.ssp.model.OperatorBalance"  useGeneratedKeys="true"
		keyProperty="id">
		insert into t_operator_balance(operator_id,act_time,num,amount,state,balance)
		values(#{operatorId},#{actTime},#{num},#{amount},#{state},#{balance})	
	</insert>
	<select id="findSumAmountByOperatorId" parameterType="long" resultType="float">
		select sum(amount) from t_operator_balance where
		operator_id = #{operatorId} and state != 2
	</select>
	
	<select id="countByParams" parameterType="map" resultType="int">
		select count(id) from t_operator_balance
		<where> 
		amount > 0
		<if test="num != null">
			and num like #{num}
		</if>
		</where>
	</select>
	<!--从operatorsql中copy  countByParams -> countByParamsOperator  -->
	<select id="countByParamsOperator" parameterType="map" resultType="int">
		select count(id) from t_operator_balance
		where operator_id = #{operatorId} and amount > 0
	</select>
	<!--从operatorsql中copy  findFinancial -> findFinancial  -->
	<select id="findFinancial" parameterType="map" resultType="com.cloudcross.ssp.model.OperatorBalance">
		select 
		impression,click,ecpm,income
		from t_rpt_zone
	</select>
	
	<select id="findByParams" parameterType="map" resultType="com.cloudcross.ssp.model.OperatorBalance">
		select <include refid="base_column"/> from t_operator_balance
		<where> 
		amount > 0
		<if test="num != null">
			and num like #{num}
		</if>
		</where>
		order by act_time desc
		limit #{pager.offset}, #{pager.limit} 
	</select>
	<!--从operatorsql中copy  findByParams -> findByParamsOperator  -->
	<select id="findByParamsOperator" parameterType="map" resultType="com.cloudcross.ssp.model.OperatorBalance">
		select <include refid="base_column"/> from t_operator_balance
		where operator_id = #{operatorId} and amount > 0
		order by act_time desc
		limit #{pager.offset}, #{pager.limit} 
	</select>
	<!--从operatorsql中copy  findByParams1 -> findByParams1 -->
	<select id="findByParams1" parameterType="map" resultType="com.cloudcross.ssp.model.OperatorBalance">
		select <include refid="base_column"/> from t_operator_balance
		where operator_id = #{operatorId}  and amount > 0
		order by act_time desc
	</select>
	<!--从operatorsql中copy  findAvailableMoney -> findAvailableMoney -->
	<select id="findAvailableMoney" parameterType="long" resultType="float">
		select balance from t_operator_balance where operator_id = #{operatorId}
        order by id desc limit 1
	</select>
	
	<update id="updateOperatorBalanceStatus" parameterType="HashMap">
		update t_operator_balance 
		set state=#{state}
		<if test="reason != null">
			  ,memo=#{reason}
		</if>
		where id in
		<foreach item="id" collection="idList" open="(" separator=","
			close=")">
			#{id}
		</foreach>
	</update>
	
	<update id="moneyBackToOperator" parameterType="long">
		update t_operator_balance  set balance = balance + (select amount from (select amount from t_operator_balance where id = #{id}) as a) where id = (select id from 
(select id from t_operator_balance where operator_id = (select operator_id from t_operator_balance where id = #{id}) order by id desc limit 1) as foo )
	</update>
	
</mapper>