<?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="codeClassSqlMapper">
	
	<sql id="base_column">
		          id,
	advertiser_id as advertiserId,
	          name as codeName,
	          para as codePara,
	   script_code as script,
	      pic_code as picture,
	            updated
	            	
	</sql>
	
	<select id="selectByName"  parameterType="string" resultType="com.cloudcross.ssp.model.CodeClass">
		select 
		<include refid="base_column" />
		from t_code_class
		<where>
			name = #{codeName}
		</where>
		order by id  desc
	</select>
	
		<!--  根据id从数据库中查询代码分类对象-->
	 <select id="selectById" parameterType="int" resultType="com.cloudcross.ssp.model.CodeClass">
		select 
		<include refid="base_column" />
		from t_code_class
		<where>
			id = #{id} 			
		</where>
	</select>  
	
	<!--  搜索 -->
	<select id="selectByParamsadvertiser"  parameterType="map" resultType="com.cloudcross.ssp.model.CodeClass">
		select 
		<include refid="base_column" />
		from t_code_class 
		<where>
		<if test="advertiserId!=null and advertiserId != '' and advertiserId!=-1 ">
		advertiser_id=#{advertiserId}
		</if>	
		
	    <if test="codeName!=null">
		and name like #{codeName}
		</if>
		
		
		
		<!--  
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='codeName'">
					and name like #{value}
				</when>
				<when test="key=='id'">
					and id like #{value}
				</when>
			</choose>	
		</if>-->
		</where>
		
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="selectByParamsoperator"  parameterType="map" resultType="com.cloudcross.ssp.model.CodeClass">
		select 
		<include refid="base_column" />
		from t_code_class where id>0	
		<if test="key!=null and value!=null">
			<choose>
				<when test="key=='codeName'">
					and name like #{value}
				</when>
				<when test="key=='id'">
					and id like #{value}
				</when>
			</choose>	
		</if>
		
		order by id  desc
		limit #{pager.offset}, #{pager.limit}
	</select>
	
	
	<!--  计算总行数获取列表 -->
	
	<select id="countByParamsadvertiser" parameterType="map" resultType="int">
		select count(id)
		from t_code_class 
		<where>
			1=1
			<if test="advertiserId != null and advertiserId != '' and advertiserId!=-1 ">
				and advertiser_Id = #{advertiserId}
			</if>
			
			<if test="codeName != null and codeName != ''">
				and name like #{codeName}
			</if>
		</where>	
	</select>
	
		<select id="countByParamsoperator" parameterType="map" resultType="int">
		select count(id)
		from t_code_class where id>0	
			<if test="key!=null and value!=null">
			<choose>
				<when test="key=='id'">
					and id like #{value}
				</when>
				<when test="key=='codeName'">
					and name like #{value}
				</when>
			</choose>	
		</if>
	</select>
	 
	
	
	<update id="editCodeClass" parameterType="com.cloudcross.ssp.model.CodeClass">
		update t_code_class 
		set name=#{codeName}, para=#{codePara}, updated=#{updated} , script_code=#{script}, pic_code=#{picture}
		where id = #{id}
	</update>
	
	  <insert id="addCodeClass" parameterType="com.cloudcross.ssp.model.CodeClass">
		insert into t_code_class(advertiser_id, name, para, updated )
		values(#{advertiserId},#{codeName},#{codePara},#{updated})
	</insert>
	
	

	
	<!-- 根据id查询代码值 -->
	<select id="getScriptById"  parameterType="int" resultType="String">
		select 
		script_code
		from t_code_class where id = #{id}
	</select>
	<!-- 根据id查询图片值 -->
	<select id="getPicyureById"  parameterType="int" resultType="String">
		select 
		pic_code
		from t_code_class where id = #{id}
	</select>
	
</mapper>