<?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="accountSqlMapper">

	<!--mybatis ehcache缓存配置 -->
	<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 <cache type="org.mybatis.caches.ehcache.LoggingEhcache" 
		/> -->
	<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
	<!-- 以下与实体类的中字段一致 -->
	<sql id="selectId">
		id,
		name as accountName,
		(select group_concat(name) from t_ly_role
		where t_ly_role.id
		in (SELECT role_id FROM t_acc_role WHERE
		acc_id=t_account.id) ) roleName,
		password,
		description,
		state,
		create_time as createTime,
		real_name as realName,
		tel,
		type,
		operator_id as operatorId,
		agent_id as agentId,
		advertiser_id as advertiserId,
		sys_type as sysType
	</sql>

	<!--resultType="Account" 每返回一条结果封装到Account里 -->
	<select id="queryAll" resultType="com.cloudcross.ssp.model.Account" parameterType="com.cloudcross.ssp.model.Account">
		select
		<include refid="selectId" />
		from t_account
		<where>
			<if test="accountName != null and accountName != ''">
				name like '%${accountName}%'
			</if>
		</where>
	</select>
	
	<select id="selectByParams"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and operator_id = 0
		and agent_id = 0
		and advertiser_id = 0
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
		order by id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>


		<select id="selectByParamsOperator"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and operator_id = #{loginAccount.operatorId}
		and id != #{loginAccount.id}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and sys_type = #{loginAccount.sysType}
		order by id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>


<select id="selectByParamsAdvertiser"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and agent_id = #{loginAccount.agentId}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
		<if test="loginAccount.advertiserId != 0 ">
		and advertiser_id = #{loginAccount.advertiserId}
		</if>
		order by id   desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	<select id="countByParams" parameterType="map" resultType="int">
		select count(id) 
		from t_account
		where deletestatus != -1 
		and operator_id = 0
		and agent_id = 0
		and advertiser_id = 0
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
	</select>
	
<select id="countByParamsOperator" parameterType="map" resultType="int">
		select count(id) 
		from t_account
		where deletestatus != -1 
		and operator_id = #{loginAccount.operatorId}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
	</select>

<select id="countByParamsAdvertiser" parameterType="map" resultType="int">
		select count(id) 
		from t_account
		where deletestatus != -1 
		and agent_id = #{loginAccount.agentId}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
		<if test="loginAccount.advertiserId != 0 ">
		and advertiser_id = #{loginAccount.advertiserId}
			</if>
	</select>
	<select id="isExist" resultType="com.cloudcross.ssp.model.Account" parameterType="String">
		select
		<include refid="selectId" />
		from t_account
		where name = #{accountName}
	</select>

	<!--resultType="Account" 每返回一条结果封装到Account里 -->
	<select id="query" resultType="com.cloudcross.ssp.model.Account" parameterType="java.util.HashMap">
		select
		<include refid="selectId" />
		from t_account
		<where>
			<if test="t.accountName != null and t.accountName != ''">
				name like '%${t.accountName}%'
			</if>
		</where>
	</select>

	<select id="queryNoMatch" resultType="com.cloudcross.ssp.model.Account" parameterType="java.util.HashMap">
		select
		a.id,
		a.name,
		a.password,
		a.accountType,
		a.description,
		a.state,
		a.createTime,
		(SELECT dp.name from department dp where dp.id =
		d.subdep_id) depName
		from account a LEFT JOIN dep_account d on
		a.id=d.account_id
		<where>
			<if test="t.accountName != null and t.accountName != ''">
				name like '%${t.accountName}%'
			</if>
		</where>
	</select>
	
		<!-- 增加用户 -->
	<insert id="add" parameterType="com.cloudcross.ssp.model.Account">
		insert into t_account (
		name,
		password,
		description,
		state )
		values (#{accountName},
		#{password}, #{description},
		#{state})
	</insert>
	<!-- 增加用户 -->
	<insert id="addAccount" parameterType="com.cloudcross.ssp.model.Account" useGeneratedKeys="true" keyProperty="id">
		insert into t_account
		 (name,
		 password,
		 real_name,
		 tel,
		 description, 
		 state,
		 sys_type,
		 type) values (#{accountName}, #{password},#{realName}, 
    	#{tel},#{description},#{state},#{sysType},#{type}) 
	</insert>


	<!-- 增加用户 -->
	<insert id="addAccountOperator" parameterType="com.cloudcross.ssp.model.Account">
		insert into t_account
		 (name,
		 password,
		 real_name,
		 tel,
		 operator_id,
		 description, 
		 state,
		 sys_type ) values (#{accountName}, #{password},#{realName}, 
    	#{tel},#{operatorId},#{description},#{state},#{sysType}) 
	</insert>


<!-- 增加用户 -->
	<insert id="addAccountAdvertiser" parameterType="com.cloudcross.ssp.model.Account" useGeneratedKeys="true" keyProperty="id">
		insert into t_account
		 (name,
		 password,
		 real_name,
		 tel,
		 agent_id,
		 advertiser_id,
		 operator_id,
		 description, 
		 state,
		 sys_type ) values (#{accountName}, #{password},#{realName}, 
    	#{tel},#{agentId},#{advertiserId},#{operatorId},#{description},#{state},#{sysType}) 
	</insert>

	<delete id="delete" parameterType="String">
		delete from t_account where
		id=#{id}
	</delete>

	<select id="getById" parameterType="String" resultType="com.cloudcross.ssp.model.Account">
		select
		id,
		name as accountName,
		(select group_concat(name) from t_ly_role
		where t_ly_role.id
		in (SELECT role_id FROM t_acc_role WHERE
		acc_id=t_account.id) ) roleName,
		password,
		real_name as realName,
		tel,
		description,
		state,
		create_time as createTime,
		type,
		operator_id as operatorId,
		agent_id as agentId,
		advertiser_id as advertiserId,
		sys_type as sysType
		from t_account where id=#{id}
	</select>

	<update id="updateAccountStatus" parameterType="HashMap">
		update t_account
				set state=#{state}
		where t_account.id in
			<foreach item="accountId" collection="accountIdList"
		      open="(" separator="," close=")">
		        #{accountId}
			</foreach>
	</update>
	
	<update id="editAccount" parameterType="com.cloudcross.ssp.model.Account">
		update t_account
		<set>
			<if test="accountName != null and accountName != ''">
				name=#{accountName},
			</if>
			<if test="password != null and password != ''">
				password=#{password},
			</if>
			<if test="description != null and description != ''">
				description=#{description},
			</if>
			<if test="state != null and state != ''">
				state=#{state},
			</if>
			<if test="createTime != null and createTime != ''">
				create_time=#{createTime},
			</if>
			<if test="realName != null and realName != ''">
				real_name=#{realName},
			</if>
			<if test="tel != null and tel != ''">
				tel=#{tel},
			</if>
			<if test="operatorId != null and operatorId != 0">
				operator_id=#{operatorId},
			</if>
			<if test="agentId != null and agentId !=0">
				agent_id=#{agentId},
			</if>
			<if test="advertiserId != null and advertiserId !=0">
				advertiser_id=#{advertiserId},
			</if>
		</set>
		where id=#{id}
	</update>
	
	
	<update id="editAccountOperator" parameterType="com.cloudcross.ssp.model.Account">
		update t_account
		<set>
			<if test="accountName != null and accountName != ''">
				name=#{accountName},
			</if>
			<if test="password != null and password != ''">
				password=#{password},
			</if>
			<if test="description != null and description != ''">
				description=#{description},
			</if>
			<if test="state != null and state != ''">
				state=#{state},
			</if>
			<if test="createTime != null and createTime != ''">
				createTime=#{createTime},
			</if>
			<if test="realName != null and realName != ''">
				real_name=#{realName},
			</if>
			<if test="tel != null and tel != ''">
				tel=#{tel}
			</if>
		</set>
		where id=#{id}
	</update>
	
		<update id="editAccountAdvertiser" parameterType="com.cloudcross.ssp.model.Account">
		update t_account
		<set>
			<if test="accountName != null and accountName != ''">
				name=#{accountName},
			</if>
			<if test="password != null and password != ''">
				password=#{password},
			</if>
			<if test="description != null and description != ''">
				description=#{description},
			</if>
			<if test="state != null and state != ''">
				state=#{state},
			</if>
			<if test="createTime != null and createTime != ''">
				createTime=#{createTime},
			</if>
			<if test="realName != null and realName != ''">
				real_name=#{realName},
			</if>
			<if test="tel != null and tel != ''">
				tel=#{tel},
			</if>
			<if test="advertiserId != null">
				advertiser_id=#{advertiserId}
			</if>
		</set>
		where id=#{id}
	</update>
	
	
	<update id="update" parameterType="com.cloudcross.ssp.model.Account">
		update t_account
		<set>
			<if test="accountName != null and accountName != ''">
				name=#{accountName},
			</if>
			<if test="password != null and password != ''">
				password=#{password},
			</if>
			<if test="description != null and description != ''">
				description=#{description},
			</if>
			<if test="state != null and state != ''">
				state=#{state},
			</if>
			<if test="createTime != null and createTime != ''">
				create_time=#{createTime},
			</if>
			<if test="realName != null and realName != ''">
				real_name=#{realName},
			</if>
			<if test="tel != null and tel != ''">
				tel=#{tel}
			</if>
		</set>
		where id=#{id}
	</update>

	<!-- 验证用户登陆 -->
	<select id="countAccount" parameterType="com.cloudcross.ssp.model.Account" resultType="com.cloudcross.ssp.model.Account">
		select
		<include refid="selectId" />
		from t_account where
		name=#{accountName} and password=#{password}
	</select>

	<!-- 根据用户名查出id -->
	<select id="querySingleAccount" parameterType="String"
		resultType="com.cloudcross.ssp.model.Account">
		select
		<include refid="selectId" />
		from t_account where name=#{accountName}
	</select>
	
	<select id="findExcelAccount"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and operator_id = 0
		and agent_id = 0
		and advertiser_id = 0
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and id != #{loginAccount.id}
		and sys_type = #{loginAccount.sysType}
		order by id   desc
	</select>
	
	
		<select id="findExcelAccountsOperator"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and operator_id = #{loginAccount.operatorId}
		and id != #{loginAccount.id}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and sys_type = #{loginAccount.sysType}
		order by id   desc
	</select>
	
	
	<select id="findExcelAccountsAdvertiser"  parameterType="map" resultType="com.cloudcross.ssp.model.Account">
		select id,
		name as accountName,
		real_name as realName,tel,state,create_time as createTime
		from t_account
		where deletestatus != -1 
		and agent_id = #{loginAccount.agentId}
		and id != #{loginAccount.id}
		<if test="value!=null">
			and ( t_account.name like #{value}
			or t_account.real_name like #{value} )
		</if>
		and sys_type = #{loginAccount.sysType}
		<if test="loginAccount.advertiserId != 0 ">
			and advertiser_id = #{loginAccount.advertiserId}
		</if>
		order by id   desc
	</select>
</mapper>