<?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.yijia.system.mapper.SysDeptMapper">

	<resultMap type="SysDept" id="SysDeptResult">
		<id     property="deptId"     column="dept_id"     />
		<result property="parentId"   column="parent_id"   />
		<result property="ancestors"  column="ancestors"   />
		<result property="deptName"   column="dept_name"   />
		<result property="orderNum"   column="order_num"   />
		<result property="leader"     column="leader"      />
		<result property="phone"      column="phone"       />
		<result property="email"      column="email"       />
		<result property="status"     column="status"      />
		<result property="delFlag"    column="del_flag"    />
		<result property="parentName" column="parent_name" />
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
		<result property="jiBie" column="ji_bie" />
		<result property="deptAddress"    column="dept_address"    />
		<result property="oilGunNum"    column="oil_gun_num"    />
		<result property="stationPic"    column="station_pic"    />
		<result property="stationLongitude"    column="station_longitude"    />
		<result property="stationLatitude"    column="station_latitude"    />
		<result property="mno"    column="mno"    />
		<result property="discountSetting"    column="discount_setting"    />
		<result property="cardEnabledFlag"    column="card_enabled_flag"    />
		<result property="appId"    column="app_id"    />
		<result property="appSecret"    column="app_secret"    />
		<result property="gzhAppId"    column="gzh_app_id"    />
		<result property="gzhAppSecret"    column="gzh_app_secret"    />
		<result property="aliAppId"    column="ali_app_id"    />
		<result property="aliPublicKey"    column="ali_public_key"    />
		<result property="aliPrivateKey"    column="ali_private_key"    />
		<result property="aliEncryptKey"    column="ali_encrypt_key"    />
		<result property="image1"    column="image1"    />
		<result property="image2"    column="image2"    />
		<result property="image3"    column="image3"    />
		<result property="image4"    column="image4"    />
		<result property="image5"    column="image5"    />
		<result property="wsPrintFlag"    column="ws_print_flag"    />
		<result property="printSetting"    column="print_setting"     />
		<result property="notice"    column="notice"    />
		<result property="integralPrintFlag"    column="integral_print_flag"    />
		<result property="integralFlag"    column="integral_flag"    />
		<result property="integralRuleFlag"    column="integral_rule_flag"    />
		<result property="cardRuleFlag"    column="card_rule_flag"    />
		<result property="couponFlag"    column="coupon_flag"    />
		<result property="labelFlag"    column="label_flag"    />
		<result property="printDeviceType"    column="print_device_type" />
	</resultMap>

	<sql id="selectDeptVo">
        select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,
        d.del_flag, d.create_by, d.create_time ,d.ji_bie, d.dept_address, d.oil_gun_num, d.station_pic, d.station_longitude,
        d.station_latitude, d.mno, discount_setting,card_enabled_flag,d.app_id, d.app_secret,gzh_app_id,gzh_app_secret,ali_app_id,gzh_app_secret
        ali_app_id,ali_public_key,ali_private_key,ali_encrypt_key,image1,image2,image3,image4,image5,ws_print_flag,print_setting,notice,integral_print_flag,
        d.integral_flag,d.integral_rule_flag,d.card_rule_flag,d.coupon_flag,d.label_flag,d.print_device_type
        from sys_dept d
    </sql>

	<select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
        <include refid="selectDeptVo"/>
        where d.del_flag = '0'
		<if test="deptId != null">
			AND dept_id = #{deptId}
		</if>
		<if test="jiBie != null">
			AND ji_bie = #{jiBie}
		</if>
        <if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>
		<if test="orDeptId != null">
			AND ( dept_id = #{orDeptId} or  parent_id = #{orDeptId} )
		</if>
		<if test="deptName != null and deptName != ''">
			AND dept_name like concat('%', #{deptName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="deptIdList != null ">
			and dept_id in
			<foreach item="item" index="index" collection="deptIdList"
					 open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by d.parent_id, d.order_num
    </select>
	<select id="queryDeptList" parameterType="SysDept" resultMap="SysDeptResult">
        <include refid="selectDeptVo"/>
        where d.del_flag = '0'
		<if test="deptId != null or parentId!=null">
			<trim prefixOverrides="or" prefix=" and (" suffix=")">
				<if test="parentId != null and parentId != null">
					or parent_id = #{parentId}
				</if>
				<if test="deptId != null and deptId != null">
					or dept_id = #{deptId}
				</if>
			</trim>
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by d.parent_id, d.order_num
    </select>

    <select id="selectDeptListByRoleId" resultType="Integer">
		select d.dept_id
		from sys_dept d
            left join sys_role_dept rd on d.dept_id = rd.dept_id
        where rd.role_id = #{roleId}
            <if test="deptCheckStrictly">
              and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
            </if>
		order by d.parent_id, d.order_num
	</select>

    <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
		<include refid="selectDeptVo"/>
		where dept_id = #{deptId}
	</select>

    <select id="checkDeptExistUser" parameterType="Long" resultType="int">
		select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
	</select>

	<select id="hasChildByDeptId" parameterType="Long" resultType="int">
		select count(1) from sys_dept
		where del_flag = '0' and parent_id = #{deptId} limit 1
	</select>

	<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
		select * from sys_dept where find_in_set(#{deptId}, ancestors)
	</select>

	<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
		select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
	</select>

	<select id="checkDeptNameUnique" resultMap="SysDeptResult">
	    <include refid="selectDeptVo"/>
		where dept_name=#{deptName} and parent_id = #{parentId} limit 1
	</select>
    <!--调用存储过程-->
	<select id="getGroupInfo" resultMap="SysDeptResult">
		<include refid="selectDeptVo"/>
		<where>
			<if test="deptId != null ">
				AND dept_id = #{deptId}
			</if>
			<if test="parentId != null ">
				AND parent_id = #{parentId}
			</if>
		</where>
	</select>
<!--添加代理人集团信息查询-->
	<select id="selectSysAgentList" parameterType="SysDept" resultMap="SysDeptResult">
		SELECT dept_id,dept_name from  sys_dept where ji_bie = 1
		and dept_id not in (
			SELECT group_id from sys_agent
			<where>
				<if test="userId != null "> and user_id != #{userId}</if>
			</where>
		)
	</select>

<!--查询代理人对应的集团下的站点信息-->
	<select id="selectDeptAgentList" parameterType="SysDept" resultMap="SysDeptResult">
		SELECT dept_id from sys_dept t
		where t.parent_id in (
		SELECT dept_id from sys_dept d join  sys_agent a on d.dept_id =a.group_id
		where d.del_flag = '0'
		<if test="userId != null "> and a.user_id = #{userId}</if>)
		and  t.del_flag = '0'
	</select>
	<!--查询代理人对应的集团信息-->
	<select id="selectGroupAgentList" parameterType="SysDept" resultMap="SysDeptResult">
		SELECT dept_id,dept_name,integral_rule_flag,card_rule_flag,coupon_flag,label_flag
		from sys_dept d join  sys_agent a on d.dept_id =a.group_id
		where d.del_flag = '0'
		<if test="userId != null "> and a.user_id = #{userId}</if>
	</select>

	<!--根据集团查询油站id-->
	<select id="searchDeptList" parameterType="SysDept" resultMap="SysDeptResult">
		SELECT dept_id,dept_name from sys_dept
		where del_flag = '0'
		<if test="parentId != null and parentId != 0">
			AND parent_id = #{parentId}
		</if>
	</select>
	<select id="selectGroupAndDeptList" parameterType="SysDept" resultMap="SysDeptResult">
		SELECT dept_id,dept_name
		from sys_dept
		where del_flag = '0'
		<if test="orDeptId != null">
			AND ( dept_id = #{orDeptId} or  parent_id = #{orDeptId} )
		</if>
	</select>

	<!--查询集团及共享信息-->
	<select id="selectShareGroup" parameterType="SysDept" resultMap="SysDeptResult">
		select d.dept_id, d.parent_id, d.ancestors, d.dept_name,card_enabled_flag,d.app_id,d.integral_flag,d.integral_rule_flag,d.card_rule_flag,
		d.coupon_flag,d.label_flag,d.print_device_type
        from sys_dept d
		where d.del_flag = '0'
		<if test="integralRuleFlag != null">
			AND integral_rule_flag = #{integralRuleFlag}
		</if>
		<if test="couponFlag != null">
			AND coupon_flag = #{couponFlag}
		</if>
		<if test="cardRuleFlag != null">
			AND card_rule_flag = #{cardRuleFlag}
		</if>
		<if test="cardRuleFlag != null">
			AND card_rule_flag = #{cardRuleFlag}
		</if>
		<if test="deptId != null">
			AND dept_id = #{deptId}
		</if>
		<if test="deptId != null">
			AND dept_id = #{deptId}
		</if>
		<if test="jiBie != null">
			AND ji_bie = #{jiBie}
		</if>
		<if test="orDeptId != null">
			AND ( dept_id = #{orDeptId} or  parent_id = #{orDeptId} )
		</if>
		<if test="deptName != null and deptName != ''">
			AND dept_name like concat('%', #{deptName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND status = #{status}
		</if>
		<if test="deptIdList != null ">
			and dept_id in
			<foreach item="item" index="index" collection="deptIdList"
					 open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>

	<!--查询所有不共享的站点-->
	<select id="selectNotShareDept" parameterType="SysDept" resultMap="SysDeptResult">
		select d.dept_id, d.parent_id, d.ancestors, d.dept_name,card_enabled_flag,d.app_id,d.integral_flag,d.integral_rule_flag,d.card_rule_flag,
		d.coupon_flag,d.label_flag,d.print_device_type
		from sys_dept d
		where d.del_flag = "0"
		and d.parent_id in (
			SELECT dept_id from sys_dept t
			where t.ji_bie ='1' and del_flag = "0"
			<if test="integralRuleFlag != null">
				AND integral_rule_flag = #{integralRuleFlag}
			</if>
			<if test="couponFlag != null">
				AND coupon_flag = #{couponFlag}
			</if>
			<if test="cardRuleFlag != null">
				AND card_rule_flag = #{cardRuleFlag}
			</if>
		)
	</select>

    <insert id="insertDept" parameterType="SysDept">
 		insert into sys_dept(
 			<if test="deptId != null and deptId != 0">dept_id,</if>
 			<if test="parentId != null and parentId != 0">parent_id,</if>
 			<if test="deptName != null and deptName != ''">dept_name,</if>
 			<if test="ancestors != null and ancestors != ''">ancestors,</if>
 			<if test="orderNum != null and orderNum != ''">order_num,</if>
 			<if test="leader != null and leader != ''">leader,</if>
 			<if test="phone != null and phone != ''">phone,</if>
 			<if test="email != null and email != ''">email,</if>
 			<if test="status != null">status,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			<if test="jiBie != null">ji_bie,</if>
			<if test="deptAddress != null">dept_address,</if>
			<if test="oilGunNum != null">oil_gun_num,</if>
			<if test="stationPic != null">station_pic,</if>
			<if test="stationLongitude!= null">station_longitude,</if>
			<if test="stationLatitude != null">station_latitude,</if>
			<if test="mno != null">mno,</if>
			<if test="appId != null">app_id,</if>
			<if test="appSecret != null">app_secret,</if>
			<if test="discountSetting != null">discount_setting,</if>
			<if test="cardEnabledFlag != null">card_enabled_flag,</if>
			<if test="gzhAppId != null">gzh_app_id,</if>
			<if test="gzhAppSecret != null">gzh_app_secret,</if>
			<if test="aliAppId != null">ali_app_id,</if>
			<if test="aliPublicKey != null">ali_public_key,</if>
			<if test="aliPrivateKey != null">ali_private_key,</if>
			<if test="aliEncryptKey != null">ali_encrypt_key,</if>
			<if test="image1 != null">image1,</if>
			<if test="image2 != null">image2,</if>
			<if test="image3 != null">image3,</if>
			<if test="image4 != null">image4,</if>
			<if test="image5 != null">image5,</if>
			<if test="wsPrintFlag != null">ws_print_flag,</if>
			<if test="printSetting != null">print_setting,</if>
			<if test="notice != null">notice,</if>
			<if test="integralPrintFlag != null">integral_print_flag,</if>
			<if test="integralFlag != null">integral_flag,</if>
			<if test="integralRuleFlag != null">integral_rule_flag,</if>
			<if test="cardRuleFlag != null">card_rule_flag,</if>
			<if test="couponFlag != null">coupon_flag,</if>
			<if test="labelFlag != null">label_flag,</if>
			<if test="printDeviceType != null">print_device_type,</if>
 			create_time
 		)values(
 			<if test="deptId != null and deptId != 0">#{deptId},</if>
 			<if test="parentId != null and parentId != 0">#{parentId},</if>
 			<if test="deptName != null and deptName != ''">#{deptName},</if>
 			<if test="ancestors != null and ancestors != ''">#{ancestors},</if>
 			<if test="orderNum != null and orderNum != ''">#{orderNum},</if>
 			<if test="leader != null and leader != ''">#{leader},</if>
 			<if test="phone != null and phone != ''">#{phone},</if>
 			<if test="email != null and email != ''">#{email},</if>
 			<if test="status != null">#{status},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			<if test="jiBie != null">#{jiBie},</if>
			<if test="deptAddress != null">#{deptAddress},</if>
			<if test="oilGunNum != null">#{oilGunNum},</if>
			<if test="stationPic != null">#{stationPic},</if>
			<if test="stationLongitude!= null">#{stationLongitude},</if>
			<if test="stationLatitude != null">#{stationLatitude},</if>
			<if test="mno != null">#{mno},</if>
			<if test="appId != null">#{appId},</if>
			<if test="appSecret != null">#{appSecret},</if>
			<if test="discountSetting != null">#{discountSetting},</if>
			<if test="cardEnabledFlag != null">#{cardEnabledFlag},</if>
			<if test="gzhAppId != null">#{gzhAppId},</if>
			<if test="gzhAppSecret != null">#{gzhAppSecret},</if>
			<if test="aliAppId != null">#{aliAppId},</if>
			<if test="aliPublicKey != null">#{aliPublicKey},</if>
			<if test="aliPrivateKey != null">#{aliPrivateKey},</if>
			<if test="aliEncryptKey != null">#{aliEncryptKey},</if>
			<if test="image1 != null">#{image1},</if>
			<if test="image2 != null">#{image2},</if>
			<if test="image3 != null">#{image3},</if>
			<if test="image4 != null">#{image4},</if>
			<if test="image5 != null">#{image5},</if>
			<if test="wsPrintFlag != null">#{wsPrintFlag},</if>
			<if test="printSetting != null">#{printSetting},</if>
			<if test="notice != null">#{notice},</if>
			<if test="integralPrintFlag != null">#{integralPrintFlag},</if>
			<if test="integralFlag != null">#{integralFlag},</if>
			<if test="integralRuleFlag != null">#{integralRuleFlag},</if>
			<if test="cardRuleFlag != null">#{cardRuleFlag},</if>
			<if test="couponFlag != null">#{couponFlag},</if>
			<if test="labelFlag != null">#{labelFlag},</if>
			<if test="printDeviceType != null">#{printDeviceType},</if>
 			sysdate()
 		)
	</insert>

	<update id="updateDept" parameterType="SysDept">
 		update sys_dept
 		<set>
 			<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
 			<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
 			<if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
 			<if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
 			<if test="leader != null">leader = #{leader},</if>
 			<if test="phone != null">phone = #{phone},</if>
 			<if test="email != null">email = #{email},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="jiBie != null">ji_bie = #{jiBie},</if>
			<if test="deptAddress!= null">dept_address = #{deptAddress},</if>
			<if test="oilGunNum != null">oil_gun_num = #{oilGunNum},</if>
			<if test="stationPic != null">station_pic = #{stationPic},</if>
			<if test="stationLongitude!= null">station_longitude = #{stationLongitude},</if>
			<if test="stationLatitude != null">station_latitude = #{stationLatitude},</if>
			<if test="mno != null">mno = #{mno},</if>
			<if test="appId != null">app_id = #{appId},</if>
			<if test="appSecret != null">app_secret = #{appSecret},</if>
			<if test="discountSetting != null">discount_setting= #{discountSetting},</if>
			<if test="cardEnabledFlag != null">card_enabled_flag= #{cardEnabledFlag},</if>
			<if test="gzhAppId != null">gzh_app_id= #{gzhAppId},</if>
			<if test="gzhAppSecret != null">gzh_app_secret= #{gzhAppSecret},</if>
			<if test="aliAppId != null">ali_app_id= #{aliAppId},</if>
			<if test="aliPublicKey != null">ali_public_key= #{aliPublicKey},</if>
			<if test="aliPrivateKey != null">ali_private_key= #{aliPrivateKey},</if>
			<if test="aliEncryptKey != null">ali_encrypt_key= #{aliEncryptKey},</if>
			<if test="image1 != null">image1= #{image1},</if>
			<if test="image2 != null">image2= #{image2},</if>
			<if test="image3 != null">image3= #{image3},</if>
			<if test="image4 != null">image4= #{image4},</if>
			<if test="image5 != null">image5= #{image5},</if>
			<if test="wsPrintFlag != null">ws_print_flag= #{wsPrintFlag},</if>
			<if test="printSetting != null">print_setting= #{printSetting},</if>
			<if test="notice != null">notice= #{notice},</if>
			<if test="integralPrintFlag != null">integral_print_flag= #{integralPrintFlag},</if>
			<if test="integralFlag != null">integral_flag= #{integralFlag},</if>
			<if test="integralRuleFlag != null">integral_rule_flag= #{integralRuleFlag},</if>
			<if test="cardRuleFlag != null">card_rule_flag= #{cardRuleFlag},</if>
			<if test="couponFlag != null">coupon_flag= #{couponFlag},</if>
			<if test="labelFlag != null">label_flag= #{labelFlag},</if>
			<if test="printDeviceType != null">print_device_type= #{printDeviceType},</if>
 			update_time = sysdate()
 		</set>
 		where dept_id = #{deptId}
	</update>

	<update id="updateDeptChildren" parameterType="java.util.List">
	    update sys_dept set ancestors =
	    <foreach collection="depts" item="item" index="index"
	        separator=" " open="case dept_id" close="end">
	        when #{item.deptId} then #{item.ancestors}
	    </foreach>
	    where dept_id in
	    <foreach collection="depts" item="item" index="index"
	        separator="," open="(" close=")">
	        #{item.deptId}
	    </foreach>
	</update>

	 <update id="updateDeptStatus" parameterType="SysDept">
 	    update sys_dept
 	    <set>
 	        <if test="status != null and status != ''">status = #{status},</if>
 	        <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 	        update_time = sysdate()
        </set>
 	    where dept_id in (${ancestors})
	</update>

	<delete id="deleteDeptById" parameterType="Long">
		update sys_dept set del_flag = '2' where dept_id = #{deptId}
	</delete>

</mapper>
