OrganizationMapper.xml 20 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
<?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="cn.runsa.portal.base.dao.OrganizationMapper">
	<resultMap id="BaseResultMap" type="cn.runsa.portal.base.entity.Organization">
		<id column="orId" property="id" jdbcType="INTEGER" />
		<result column="beId" property="beid" jdbcType="INTEGER" />
		<result column="orName" property="name" jdbcType="VARCHAR" />
		<result column="orCode" property="code" jdbcType="VARCHAR" />
		<result column="orDesc" property="desc" jdbcType="VARCHAR" />
		<result column="orType" property="type" jdbcType="INTEGER" />
		<result column="orlId" property="lid" jdbcType="INTEGER" />
		<result column="orParent" property="parent" jdbcType="INTEGER" />
		<result column="ptRemark1" property="remark1" jdbcType="VARCHAR" />
		<result column="ptRemark2" property="remark2" jdbcType="VARCHAR" />
		<result column="ptUpTime" property="uptime" jdbcType="TIMESTAMP" />
		<result column="ptUpdater" property="updater" jdbcType="INTEGER" />
		<result column="orSort" property="sort" jdbcType="INTEGER" />
		<result column="orlName" property="orlName" jdbcType="VARCHAR" />
		<result column="beOrgType" property="beOrgType" jdbcType="INTEGER" />
		<result column="beName" property="beName" jdbcType="VARCHAR" />
		
		<collection property="setList" javaType="ArrayList"
			ofType="cn.runsa.portal.base.entity.OrgSetOfBook">
			<id column="orId" property="orId" jdbcType="INTEGER" />
			<id column="chId" property="chId" jdbcType="INTEGER" />
			<result column="bebId" property="bebId" jdbcType="INTEGER" />
			<result column="orbSort" property="orbSort" jdbcType="INTEGER" />
			<result column="orbDefault" property="orbDefault" jdbcType="BIT" />
			<result column="orbEnable" property="orbEnable" jdbcType="BIT" />
			<result column="chName" property="chName" jdbcType="VARCHAR" />
			<result column="bebName" property="bebName" jdbcType="VARCHAR" />
		</collection>
	</resultMap>
	
	<resultMap id="ChResultMap" type="cn.runsa.portal.base.entity.Channel" >
	    <id column="chId" property="id" jdbcType="INTEGER" />
	    <result column="chCode" property="code" jdbcType="VARCHAR" />
	    <result column="chName" property="name" jdbcType="VARCHAR" />
	    <result column="ptValid" property="valid" jdbcType="SMALLINT" />
    </resultMap>
    
    <resultMap id="OrgSetOfBookResultMap" type="cn.runsa.portal.base.entity.OrgSetOfBook" >
	    <id column="chId" property="chId" jdbcType="INTEGER" />
	    <id column="orId" property="orId" jdbcType="INTEGER" />
	    <result column="beId" property="beId" jdbcType="INTEGER" />
		<result column="orName" property="orName" jdbcType="VARCHAR" />
		<result column="orlId" property="orlId" jdbcType="INTEGER" />
		<result column="orParent" property="orParent" jdbcType="INTEGER" />
		<result column="bebId" property="bebId" jdbcType="INTEGER" />
		<result column="orbSort" property="orbSort" jdbcType="INTEGER" />
		<result column="orbDefault" property="orbDefault" jdbcType="BIT" />
		<result column="orbEnable" property="orbEnable" jdbcType="BIT" />
    </resultMap>
    
     <resultMap id="BrResultMap" type="cn.runsa.portal.base.entity.Brand">
	    <id column="brId" jdbcType="INTEGER" property="id" />
	    <result column="brName" jdbcType="VARCHAR" property="name" />
	  </resultMap>

	<sql id="Base_Column_List">
		orId, beId, orName, orCode, orDesc, orType, orlId, orParent, ptRemark1, ptRemark2, ptUpTime, ptUpdater,orSort
	</sql>

	<select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
		select
		<include refid="Base_Column_List" />
		from mtorganization
		where orId = #{orid,jdbcType=INTEGER}
	</select>

	<delete id="deleteById" parameterType="java.lang.Integer">
		delete from mtorganization
		where orId = #{orid,jdbcType=INTEGER}
	</delete>

	<insert id="insertOrganization" parameterType="cn.runsa.portal.base.entity.Organization">
		<selectKey resultType="java.lang.Integer" keyProperty="id"
			order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into mtorganization (beId, orName, 
	      orCode, orDesc, orType, 
	      orlId, orParent, ptRemark1, 
	      ptRemark2, ptUpTime, ptUpdater,orSort
		)
		values (#{beid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
		#{code,jdbcType=VARCHAR}, #{desc,jdbcType=VARCHAR},
		#{type,jdbcType=INTEGER},
		#{lid,jdbcType=INTEGER}, #{parent,jdbcType=INTEGER}, #{remark1,jdbcType=VARCHAR},
		#{remark2,jdbcType=VARCHAR}, now(), #{updater,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}
		)
	</insert>

	<update id="updateById" parameterType="cn.runsa.portal.base.entity.Organization">
		update mtorganization set ptUpTime = now()
		<if test="beid != null">
			,beId = #{beid,jdbcType=INTEGER}
		</if>
		<if test="name != null">
			,orName = #{name,jdbcType=VARCHAR}
		</if>
		<if test="code != null">
			,orCode = #{code,jdbcType=VARCHAR}
		</if>
		<if test="desc != null">
			,orDesc = #{desc,jdbcType=VARCHAR}
		</if>
		<if test="type != null">
			,orType = #{type,jdbcType=INTEGER}
		</if>
		<if test="lid != null">
			,orlId = #{lid,jdbcType=INTEGER}
		</if>
		<if test="parent != null">
			,orParent = #{parent,jdbcType=INTEGER}
		</if>
		<if test="remark1 != null">
			,ptRemark1 = #{remark1,jdbcType=VARCHAR}
		</if>
		<if test="remark2 != null">
			,ptRemark2 = #{remark2,jdbcType=VARCHAR}
		</if>
		<if test="updater != null">
			,ptUpdater = #{updater,jdbcType=INTEGER}
		</if>
		<if test="sort != null">
			,orSort = #{sort,jdbcType=INTEGER}
		</if>
		where orId = #{id,jdbcType=INTEGER}
	</update>
	
	<select id="selectOrgsByPid" resultMap="BaseResultMap" parameterType="java.lang.Integer">
		select
		<include refid="Base_Column_List" />
		from mtorganization
		where beId = #{beId,jdbcType=INTEGER}
		<if test="pid!=null and pid!=''">
			AND orParent = #{pid,jdbcType=INTEGER}
		</if>
		<if test="pid==null or pid==''">
			AND orParent IS NULL
		</if>
		ORDER BY orSort
	</select>
	
	<select id="selectOrgsByPlId" resultMap="BaseResultMap">
		SELECT
			t1.orId,
			t1.beId,
			t1.orName,
			t1.orParent,
			t1.orSort,
			t1.orlId
		FROM
			mtOrganization t1
		INNER JOIN (
			SELECT DISTINCT
				s2.orParent
			FROM
				mtOrganization s1
			INNER JOIN mtOrgInfo s2 ON s1.orId = s2.orId
			INNER JOIN smPlatform s3 ON s1.beId = s3.beId
			INNER JOIN mtBizEntity s4 ON s1.beId = s4.beId
			WHERE
				s3.plId = #{plId,jdbcType=INTEGER} AND s4.ptValid = 1
			<if test="orgName!=null and orgName!=''">
				AND s1.orName LIKE CONCAT('%',#{orgName,jdbcType=VARCHAR},'%')
			</if>
			<if test="parentId!=null">
				AND s2.orId IN (SELECT orId FROM mtOrgInfo WHERE orParent = #{parentId,jdbcType=INTEGER})
			</if>
		) t2 ON t1.orId = t2.orParent
		ORDER BY
			t1.orParent,
			t1.orSort
	</select>
	
	<select id="selectAllHQOrgs" resultMap="BaseResultMap">
		SELECT
			t1.orId,
			t1.beId,
			t1.orName,
			t1.orParent,
			t1.orSort,
			t1.orlId
		FROM
			mtOrganization t1
		INNER JOIN (
			SELECT DISTINCT
				s2.orParent
			FROM
				mtOrganization s1
			INNER JOIN mtOrgInfo s2 ON s1.orId = s2.orId
			WHERE
				1 = 1
			<if test="orgName!=null and orgName!=''">
				AND s1.orName LIKE CONCAT('%',#{orgName,jdbcType=VARCHAR},'%')
			</if>
			<if test="parentId!=null">
				AND s2.orId IN (SELECT orId FROM mtOrgInfo WHERE orParent = #{parentId,jdbcType=INTEGER})
			</if>
		) t2 ON t1.orId = t2.orParent
		WHERE
			t1.beId IN (
				SELECT
					beId
				FROM
					mtBizEntity
				WHERE
					beOrgType = 6
					AND ptValid = 1
			)
		ORDER BY
			t1.orParent,
			t1.orSort
	</select>
	
	<select id="selectOrgDetailById" resultMap="BaseResultMap" parameterType="java.lang.Integer">
		SELECT
			t1.orId,
			t1.orCode,
			t1.orDesc,
			t1.beId,
			t1.orName,
			t1.orType,
			t1.orParent,
			t1.orSort,
			t3.orlName,
			t2.orbDefault,
			t2.orbEnable,
			t2.bebId,
			t2.chId,
			t5.chName,
			t4.beOrgType,
			t4.beName,
			CONCAT(t6.bebCode, '_', t6.bebName) bebName
		FROM
			mtOrganization t1
		LEFT JOIN vOrgSetOfBook t2 ON t1.orId = t2.orId
		LEFT JOIN mtOrgLevel t3 ON t1.orlId = t3.orlId
		AND t3.beId = t1.beId
		LEFT JOIN mtBizEntity t4 ON t1.beId = t4.beId
		LEFT JOIN mtChannel t5 ON t2.chId = t5.chId
		LEFT JOIN mtBizSetOfBook t6 ON t2.bebId = t6.bebId
		WHERE t1.orId = #{orid,jdbcType=INTEGER}
	</select>
	
	<select id="existsOrgName" resultType="Boolean"
		parameterType="cn.runsa.portal.base.entity.Organization">
		SELECT
			COUNT(*)
		FROM
			mtorganization
		WHERE
			orName = #{name,jdbcType=VARCHAR}
		AND orId IN (
			SELECT DISTINCT
				orId
			FROM
				mtorginfo
			WHERE
				orParent = (
					SELECT
						MIN(orParent)
					FROM
						mtorginfo
					WHERE 1=1
						<if test="id!=null and id!=''">
							AND orId = #{id,jdbcType=INTEGER}
						</if>
						<if test="id==null or id==''">
							AND orId = #{parent,jdbcType=INTEGER}
						</if>
				)
				<if test="id!=null and id!=''">
					AND orId != #{id,jdbcType=INTEGER}
				</if>
		)
	</select>
	
	<insert id="insertOrgInfo" parameterType="cn.runsa.portal.base.entity.Organization">
		insert into mtorginfo
		(orId,orParent,orLevel) 
		SELECT #{id},orParent,orLevel FROM mtorginfo WHERE orId = #{parent}
		UNION select #{id},#{id},IFNULL(MAX(orLevel)+1,1) AS orLevel FROM mtorginfo where orId = #{parent}
	</insert>
	
	<select id="selectMaxSort" resultType="java.lang.Integer">
		SELECT MAX(orSort) FROM mtorganization WHERE 1 = 1
		<if test="pid!=null and pid!=''">
			AND orParent = #{pid,jdbcType=INTEGER}
		</if>
		<if test="pid==null or pid==''">
			AND orParent IS NULL
		</if> 
	</select>
	
	<select id="selectPreviousOrg" resultMap="BaseResultMap"
		parameterType="cn.runsa.portal.base.entity.Organization">
		select
		<include refid="Base_Column_List" />
		from mtorganization
		where orSort = (SELECT MAX(orSort) FROM mtorganization where orSort <![CDATA[<]]>
		#{sort,jdbcType=INTEGER}
		<if test="parent!=null and parent!=''">
			AND orParent = #{parent,jdbcType=INTEGER}
		</if>
		<if test="parent==null or parent==''">
			AND orParent IS NULL
		</if>
		ORDER BY orParent)
		<if test="parent!=null and parent!=''">
			AND orParent = #{parent,jdbcType=INTEGER}
		</if>
		<if test="parent==null or parent==''">
			AND orParent IS NULL
		</if>
		AND beId = #{beid,jdbcType=INTEGER}
	</select>
	
	<select id="selectNextOrg" resultMap="BaseResultMap"
		parameterType="cn.runsa.portal.base.entity.Organization">
		select
		<include refid="Base_Column_List" />
		from mtorganization
		where orSort = (SELECT MIN(orSort) FROM mtorganization where orSort <![CDATA[>]]>
		#{sort,jdbcType=INTEGER}
		<if test="parent!=null and parent!=''">
			AND orParent = #{parent,jdbcType=INTEGER}
		</if>
		<if test="parent==null or parent==''">
			AND orParent IS NULL
		</if>
		ORDER BY orParent)
		<if test="parent!=null and parent!=''">
			AND orParent = #{parent,jdbcType=INTEGER}
		</if>
		<if test="parent==null or parent==''">
			AND orParent IS NULL
		</if>
		AND beId = #{beid,jdbcType=INTEGER}
	</select>
	
	<select id="findRootOrgSetOfBooksByBeId" resultMap="OrgSetOfBookResultMap" parameterType="java.lang.Integer">
		SELECT
			t1.chId,t2.bebId
		FROM
			mtBizChannel t1
		LEFT JOIN mtBizSetOfBook t2 ON t1.beId = t2.beId AND t2.bebDefault = 1
		WHERE
			t1.beId = #{beId,jdbcType=INTEGER}
	</select>
	
	<insert id="insertOrgLevel" parameterType="cn.runsa.portal.base.entity.Organization">
		insert into mtorglevel ( orlId,beId,orlName,ptUpTime,ptUpdater )
		values (#{lid,jdbcType=INTEGER},#{beid,jdbcType=INTEGER},
		#{orlName,jdbcType=VARCHAR}, now(), #{updater,jdbcType=INTEGER}
		)
	</insert>
	
	<update id="updateOrgLevel" parameterType="cn.runsa.portal.base.entity.Organization">
		update mtorglevel set ptUpTime = now(),orlName = #{orlName,jdbcType=VARCHAR}
		where orlId = #{lid,jdbcType=INTEGER} AND beId = #{beid,jdbcType=INTEGER}
	</update>
	
	<select id="existsOrgLevel" resultType="Boolean" parameterType="java.lang.Integer">
		SELECT COUNT(*) FROM mtorglevel
		WHERE orlId = #{orlId,jdbcType=INTEGER} AND beId = #{beId,jdbcType=INTEGER}
	</select>
	
	<insert id="insertOrgChannel" parameterType="java.lang.Integer">
		insert into mtorgchannel ( orId,chId )
		values (#{orId,jdbcType=INTEGER},#{chId,jdbcType=INTEGER})
	</insert>
	
	<delete id="deleteOrgChannelByOrId" parameterType="java.lang.Integer">
		delete from mtorgchannel
		where orId = #{orId,jdbcType=INTEGER}
	</delete>
	
	<select id="isQuotedByOrId" resultType="Boolean" parameterType="java.lang.Integer">
		SELECT
			COUNT(*) num
		FROM
			mtSiteRange
		WHERE
			orId = #{orId,jdbcType=INTEGER}
	</select>
	
	<delete id="deleteOrgInfoByOrId" parameterType="java.lang.Integer">
		delete from mtorginfo
		where orId IN 
		(SELECT a.orId FROM (select DISTINCT orId from mtorginfo where orParent = #{orId,jdbcType=INTEGER})a)
	</delete>
	
	<select id="selectByBeId" resultMap="BaseResultMap" parameterType="map">
		select
		<include refid="Base_Column_List" />
		from mtorganization
		where beId = #{beid,jdbcType=INTEGER} 
		<if test="type!=null and type!=''">
		 and orType=#{type,jdbcType=INTEGER} 
		</if>
	</select>
	
	<select id="selectRootDisByUsId" resultMap="ChResultMap">
		select 
			t1.chId,
			t1.chCode,
			t1.chName,
			t1.ptValid
		from mtChannel t1
		INNER JOIN smUsPermission t2 ON t1.chId = t2.chId and t2.usId =  #{usId,jdbcType=INTEGER}
		WHERE exists(
			select 1 from mtBizChannel s1 
			inner join mtBizEntity s2 on s1.beId=s2.beId AND s2.ptValid = 1
			WHERE s1.chId=t1.chId
		)
		<if test="orgName!=null and orgName!=''">
			AND (EXISTS(
				select 1 from vOrgSetOfBook s1
				INNER JOIN mtOrganization s2 ON s1.orId = s2.orId 
				WHERE t1.chId = s1.chId AND s1.orbEnable = 1 AND s1.orbDefault = 1
				AND locate(#{orgName,jdbcType=VARCHAR},s2.orName)>0
			)
			OR locate(#{orgName,jdbcType=VARCHAR},t1.chName)>0)
		</if>
	</select>
	
	<select id="selectAllHQRootDis" resultMap="ChResultMap">
		SELECT 
			t1.chId,
			t1.chCode,
			t1.chName,
			t1.ptValid
		FROM
			mtChannel t1
		where exists(
			select 1 from mtBizChannel s1 
			inner join mtBizEntity s2 on s1.beId=s2.beId and s2.beOrgType = 6 AND s2.ptValid = 1
			WHERE s1.chId=t1.chId
		)
		<if test="orgName!=null and orgName!=''">
		AND (exists(
			select 1 from vOrgSetOfBook s1 
			INNER JOIN mtOrganization s2 on s1.orId=s2.orId 
			INNER JOIN mtBizEntity s3 on s3.beId=s2.beId and s3.beOrgType = 6
			WHERE s1.chId = t1.chId AND s1.orbEnable = 1 AND s1.orbDefault = 1
			AND locate(#{orgName,jdbcType=VARCHAR},s2.orName)>0
		)
		OR locate(#{orgName,jdbcType=VARCHAR},t1.chName)>0)
		</if>
	</select>
	
	<select id="selectDisOrgsByChIdsAndPlId" resultMap="OrgSetOfBookResultMap">
		SELECT
			t1.orId,
			t1.beId,
			t1.orName,
			t1.orParent,
			t1.orlId,
			t2.chId
		FROM
			mtOrganization t1
		INNER JOIN (
			SELECT DISTINCT r1.orParent,r3.chId FROM mtOrgInfo r1 
			INNER JOIN mtOrganization r2 ON r1.orId = r2.orId
			INNER JOIN vOrgSetOfBook r3 ON r2.orId = r3.orId AND r3.orbEnable = 1 AND r3.orbDefault = 1
			INNER JOIN mtBizEntity r4 ON r2.beId = r4.beId
			WHERE r4.ptValid = 1 AND r3.orId IN (
				SELECT s1.orParent FROM mtOrgInfo s1 LEFT JOIN smUsPermission s2 ON s1.orId = s2.moId WHERE s2.usId = #{usId,jdbcType=INTEGER} AND s2.chId = r3.chId
				UNION
				SELECT s3.orId FROM mtOrgInfo s3 LEFT JOIN smUsPermission s4 ON s3.orParent = s4.moId WHERE s4.usId = #{usId,jdbcType=INTEGER} AND s4.chId = r3.chId
			)
		<if test="orgName!=null and orgName!=''">
			AND r2.orName LIKE CONCAT('%',#{orgName,jdbcType=VARCHAR},'%')
		</if>
		) t2 ON t1.orId = t2.orParent
		ORDER BY
			t1.orParent,
			t1.orSort
	</select>
	
	<select id="selectAllHQDisOrgs" resultMap="OrgSetOfBookResultMap">
		SELECT
			t1.orId,
			t1.beId,
			t1.orName,
			t1.orParent,
			t1.orlId,
			t2.chId
		FROM
			mtOrganization t1
		INNER JOIN (
			SELECT DISTINCT
				s2.orParent,s4.chId
			FROM
				mtOrganization s1
			INNER JOIN mtOrgInfo s2 ON s1.orId = s2.orId
			INNER JOIN vOrgSetOfBook s4 ON s1.orId = s4.orId AND s4.orbEnable = 1 AND s4.orbDefault = 1
			WHERE 1 = 1
			<if test="orgName!=null and orgName!=''">
				AND s1.orName LIKE CONCAT('%',#{orgName,jdbcType=VARCHAR},'%')
			</if>
		) t2 ON t1.orId = t2.orParent
		WHERE
			t1.beId IN (
				SELECT
					beId
				FROM
					mtBizEntity
				WHERE
					beOrgType = 6
					AND ptValid = 1
			)
		ORDER BY
			t1.orParent,
			t1.orSort
	</select>
	
	<insert id="insertOrgSetOfBook" parameterType="cn.runsa.portal.base.entity.OrgSetOfBook">
		insert into mtOrgSetOfBook ( orId,chId,bebId,orbDefault,orbSort )
		values (#{orId,jdbcType=INTEGER},#{chId,jdbcType=INTEGER},#{bebId,jdbcType=INTEGER},#{orbDefault,jdbcType=INTEGER},#{orbSort,jdbcType=INTEGER})
	</insert>
	
	<insert id="insertChildrenOrgSetOfBook" parameterType="cn.runsa.portal.base.entity.OrgSetOfBook">
		insert into mtOrgSetOfBook ( orId,chId,bebId,orbDefault )
		SELECT DISTINCT orId,#{chId,jdbcType=INTEGER},#{bebId,jdbcType=INTEGER},#{orbDefault,jdbcType=INTEGER} 
		FROM mtOrgSetOfBook WHERE orId IN (SELECT orId FROM mtOrgInfo WHERE orParent = #{orId,jdbcType=INTEGER})
	</insert>
	
	<delete id="deleteOrgSetOfBook" parameterType="java.lang.Integer">
		delete from mtOrgSetOfBook
		where orId = #{orId,jdbcType=INTEGER} 
		<if test="chId!=null">
			AND chId = #{chId,jdbcType=INTEGER}
		</if>
	</delete>
	
	<delete id="deleteExtraOrgSetOfBook">
		delete from mtOrgSetOfBook
		where orId IN (SELECT orId FROM mtOrgInfo WHERE orParent = #{orId,jdbcType=INTEGER})
			AND chId NOT IN 
			<foreach item="item" collection="list" separator=","
				open="(" close=")" index="">
				#{item.chId, jdbcType=INTEGER}
			</foreach>
	</delete>
	
	<update id="forbiddenChildrenOrgSetOfBook" parameterType="java.lang.Integer">
		update mtOrgSetOfBook set bebId = null,orbDefault = 0
		where orId IN (SELECT orId FROM mtOrgInfo WHERE orId<![CDATA[ <> ]]>orParent AND orParent = #{orId,jdbcType=INTEGER}) 
			AND chId = #{chId,jdbcType=INTEGER}
	</update>
	
	<select id="existsOrgSetOfBook" resultType="Boolean" parameterType="java.lang.Integer">
		SELECT COUNT(*) FROM mtOrgSetOfBook
		WHERE orId = #{orId,jdbcType=INTEGER} AND chId = #{chId,jdbcType=INTEGER}
	</select>
	
	<update id="updateOrgSetOfBook" parameterType="cn.runsa.portal.base.entity.OrgSetOfBook">
		update mtOrgSetOfBook set bebId = #{bebId,jdbcType=INTEGER},orbDefault = #{orbDefault,jdbcType=INTEGER}
		where orId = #{orId,jdbcType=INTEGER} AND chId = #{chId,jdbcType=INTEGER}
	</update>
	
	<select id="isUsedChId" resultType="Boolean" parameterType="java.lang.Integer">
		SELECT COUNT(*) FROM mtOrgSetOfBook
		WHERE orId IN (SELECT orId FROM mtOrgInfo WHERE orId<![CDATA[ <> ]]>orParent AND orParent = #{orId,jdbcType=INTEGER}) AND chId = #{chId,jdbcType=INTEGER}
	</select>
	
	<select id="selectParentByOrIdAndChId" resultMap="OrgSetOfBookResultMap" parameterType="java.lang.Integer">
		SELECT
			*
		FROM
			vOrgSetOfBook t1
		WHERE
			t1.orId = (SELECT IFNULL(orParent,orId) FROM mtOrganization WHERE orId = #{orId,jdbcType=INTEGER})
			AND t1.chId = #{chId,jdbcType=INTEGER}
	</select>
	
	<select id="selectBrandsByOrgChannel" resultMap="BrResultMap" parameterType="java.lang.Integer">
		SELECT
			brId,
			brName
		FROM
			mtbrand
		WHERE
			brId IN (
				SELECT DISTINCT
					t2.brId
				FROM
					mtSiteRange t1
				LEFT JOIN mtsitebrand t2 ON t1.sirId = t2.sirId
				WHERE
					t1.orId = #{orId,jdbcType=INTEGER}
				AND t1.chId = #{chId,jdbcType=INTEGER}
			)
	</select>
	
	<delete id="deleteSiteRangeByIds" parameterType="list">
		delete from mtSiteRange
		where sirId IN 
		<foreach item="item" index="index" collection="list" open="(" separator="," close=")" >
	      #{item,jdbcType=INTEGER}
	    </foreach>
	</delete>
	
	<update id="updateSiteByIds">
		UPDATE mtSite SET orId = #{orId,jdbcType=INTEGER}
		WHERE siId IN 
		<foreach item="item" index="index" collection="list" open="(" separator="," close=")" >
	      #{item,jdbcType=INTEGER}
	    </foreach>
	</update>
	
	<update id="updateSiteBySirIds">
		UPDATE mtSite SET orId = #{orId,jdbcType=INTEGER}
		WHERE siId IN (
			SELECT DISTINCT siId FROM mtSiteRange WHERE sirId IN 
			<foreach item="item" index="index" collection="list" open="(" separator="," close=")" >
		      #{item,jdbcType=INTEGER}
		    </foreach>
		)
	</update>
	
	<select id="findChannelsByBeId" resultMap="ChResultMap" parameterType="java.lang.Integer">
		SELECT
			t1.chId,
			t1.chName
		FROM
			mtChannel t1
		LEFT JOIN mtBizChannel t2 ON t1.chId = t2.chId
		WHERE
			t2.beId = #{beId,jdbcType=INTEGER}
		AND t2.ptValid = 1
		AND t1.ptValid = 1
	</select>
</mapper>