<?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.ejweb.modules.report.dao.CtBbtcBusCategoryDao">
    
	<sql id="ctBbtcBusCategoryColumns">
		a.id AS "id",
		a.fnumber AS "number",
		a.name AS "name",
		a.type AS "type.id",
		b.name AS "typeName",
		b2.label AS "ctBbtcGroup",
		b3.name AS "supplementArea"
	</sql>
	
	<sql id="ctBbtcBusCategoryJoins">
		LEFT JOIN ct_bbtc_bus_type b on b.id = a.type
		LEFT JOIN ct_bbtc_sys_dict b2 on b2.id = a.CTBBTCGROUP
		LEFT JOIN CT_BBTC_REGION b3 on b3.id = a.supplementArea
	</sql>
    
	<select id="get" resultType="CtBbtcBusCategory">
		SELECT 
			<include refid="ctBbtcBusCategoryColumns"/>
		FROM ct_bbtc_bus_category a
		<include refid="ctBbtcBusCategoryJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByTypeId" resultType="CtBbtcBusCategory">
		SELECT
		<include refid="ctBbtcBusCategoryColumns"/>
		FROM ct_bbtc_bus_category a
		<include refid="ctBbtcBusCategoryJoins"/>
		WHERE a.type = #{typeId}

	</select>
	
	<select id="findList" resultType="CtBbtcBusCategory">
		SELECT 
			<include refid="ctBbtcBusCategoryColumns"/>
		FROM ct_bbtc_bus_category a
		<include refid="ctBbtcBusCategoryJoins"/>
		<where>
			<if test="ctBbtcGroup != null and ctBbtcGroup != ''">
				AND a.ctBbtcGroup = #{ctBbtcGroup}
			</if>
			<if test="typeName != null and typeName != ''">
				AND a.type = #{typeName}
			</if>
			<if test="supplementArea != null and supplementArea != ''">
				AND a.supplementArea = #{supplementArea}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="CtBbtcBusCategory">
		SELECT 
			<include refid="ctBbtcBusCategoryColumns"/>
		FROM ct_bbtc_bus_category a
		<include refid="ctBbtcBusCategoryJoins"/>
		<where>
			
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO ct_bbtc_bus_category(
			id,
			fnumber,
			name,
			type,
			ctBbtcGroup,
			supplementArea
		) VALUES (
			#{id},
			#{number},
			#{name},
			#{type.id},
			#{ctBbtcGroup},
			#{supplementArea}
		)
	</insert>
	
	<update id="update">
		UPDATE ct_bbtc_bus_category SET 	
			fnumber = #{number},
			name = #{name},
			type = #{type.id},
			ctBbtcGroup=#{ctBbtcGroup},
			supplementArea=#{supplementArea}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		DELETE FROM ct_bbtc_bus_category
		WHERE id = #{id}
	</update>
	
</mapper>