<?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.CtBbtcTypeAreaDao">
    
	<sql id="ctBbtcTypeAreaColumns">
		a.id AS "id",
		b.label AS "group",
		c.name AS "area",
		a.parent_id AS "parentId",
		d.name AS "typeName"
	</sql>
	
	<sql id="ctBbtcTypeAreaJoins">
		LEFT JOIN ct_bbtc_sys_dict b on b.id = a.group_name
		LEFT JOIN CT_BBTC_REGION c on c.id = a.area
		LEFT JOIN CT_BBTC_BUS_TYPE d on d.id = a.parent_id
	</sql>
    
	<select id="get" resultType="com.ejweb.modules.report.entity.CtBbtcTypeArea">
		SELECT 
			<include refid="ctBbtcTypeAreaColumns"/>
		FROM CT_BBTC_TYPE_AREA a
		<include refid="ctBbtcTypeAreaJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByParentId" resultType="com.ejweb.modules.report.entity.CtBbtcTypeArea">
		SELECT
			<include refid="ctBbtcTypeAreaColumns"/>
		FROM CT_BBTC_TYPE_AREA a
		<include refid="ctBbtcTypeAreaJoins"/>
		WHERE a.parent_id = #{parentId}
	</select>

	<select id="getByAreaId" resultType="com.ejweb.modules.report.entity.CtBbtcTypeArea">
		SELECT
		    <include refid="ctBbtcTypeAreaColumns"/>
		FROM CT_BBTC_TYPE_AREA a
		<include refid="ctBbtcTypeAreaJoins"/>
		WHERE a.area = #{area} AND a.group_name=#{group}

	</select>

	<select id="findList" resultType="com.ejweb.modules.report.entity.CtBbtcTypeArea">
		SELECT 
			<include refid="ctBbtcTypeAreaColumns"/>
		FROM CT_BBTC_TYPE_AREA a
		<include refid="ctBbtcTypeAreaJoins"/>
	</select>


	
	<select id="findAllList" resultType="com.ejweb.modules.report.entity.CtBbtcTypeArea">
		SELECT 
			<include refid="ctBbtcTypeAreaColumns"/>
		FROM CT_BBTC_TYPE_AREA a
		<include refid="ctBbtcTypeAreaJoins"/>
	</select>
	
	<insert id="insert">
		INSERT INTO CT_BBTC_TYPE_AREA(
			id,
			group_name,
			area,
			parent_id
		) VALUES (
			#{id},
			#{group},
			#{area},
			#{parentId}
		)
	</insert>
	
	<update id="update">
		UPDATE CT_BBTC_TYPE_AREA SET
			group_name = #{group},
			area = #{area},
			parent_id = #{parentId}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		DELETE FROM CT_BBTC_TYPE_AREA
		WHERE id = #{id}
	</update>

	<update id="deleteByParentId">
		DELETE FROM CT_BBTC_TYPE_AREA
		WHERE parent_id = #{parentId}
	</update>
	
</mapper>