<?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.willalp.code.mapper.HsSysCodeMapper">

	<resultMap type="HsSysCode" id="HsSysCodeResult">
		<id     property="id"     column="id"     />
		<result property="jgid"   column="jgid"   />
		<result property="levelid"  column="levelid"   />
		<result property="codeid1"   column="codeid1"   />
		<result property="length1"   column="length1"   />
		<result property="name1"   column="name1"   />
		<result property="codeid2"   column="codeid2"   />
		<result property="length2"   column="length2"   />
		<result property="name2"   column="name2"   />
		<result property="codeid3"   column="codeid3"   />
		<result property="length3"   column="length3"   />
		<result property="name3"   column="name3"   />
		<result property="codeid4"   column="codeid4"   />
		<result property="length4"   column="length4"   />
		<result property="name4"   column="name4"   />
		<result property="codeid5"   column="codeid5"   />
		<result property="length5"   column="length5"   />
		<result property="name5"   column="name5"   />
		<result property="codeid6"   column="codeid6"   />
		<result property="length6"   column="length6"   />
		<result property="name6"   column="name6"   />
		<result property="codeid7"   column="codeid7"   />
		<result property="length7"   column="length7"   />
		<result property="name7"   column="name7"   />
		<result property="codeid8"   column="codeid8"   />
		<result property="length8"   column="length8"   />
		<result property="name8"   column="name8"   />
		<result property="codeid9"   column="codeid9"   />
		<result property="length9"   column="length9"   />
		<result property="name9"   column="name9"   />
		<result property="summary"   column="summary"   />
		<result property="approved"   column="approved"   />
		<result property="createBy"   column="create_by"   />
		<result property="createTime" column="create_time" />
		<result property="updateBy"   column="update_by"   />
		<result property="updateTime" column="update_time" />
	</resultMap>

	<resultMap type="HsSysLevels" id="HsSysLevelResult">
		<result property="id"    column="id"    />
		<result property="levelcode"    column="levelcode"    />
		<result property="levelname"    column="levelname"    />
		<result property="parentId"    column="parent_id"    />
		<result property="ancestors"    column="ancestors"    />
		<result property="xh"    column="xh"    />
		<result property="jgid"    column="jgid"    />
		<result property="sfzx"    column="sfzx"    />
		<result property="createTime"    column="create_time"    />
		<result property="createBy"    column="create_by"    />
		<result property="updateTime"    column="update_time"    />
		<result property="updateBy"    column="update_by"    />
		<result property="remark"    column="remark"    />
	</resultMap>

	<sql id="selectLevelVo">
        select *
        from hs_sys_level d
    </sql>

	<select id="selectLevelList" parameterType="HsSysLevels" resultMap="HsSysLevelResult">
        <include refid="selectLevelVo"/>
        where  d.sfzx = '0'

		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by d.parent_id
    </select>

	<select id="selectCodeList" parameterType="HsSysCode" resultMap="HsSysCodeResult">
		select *   from hs_sys_code u
		where 1=1
		<if test="levelid != null ">and u.levelid = #{levelid} </if>
		<if test="name1 != null ">and u.name1 like CONCAT(CONCAT('%',#{name1}),'%')
		 or  u.name2 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name3 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name4 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name5 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name6 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name7 like CONCAT(CONCAT('%',#{name1}),'%')
			or  u.name8 like CONCAT(CONCAT('%',#{name1}),'%')</if>

		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<insert id="insertCode" parameterType="HsSysCode" >
		insert into hs_sys_code(
		<if test="levelid != null ">levelid,</if>
		<if test="codeid1 != null and codeid1 != '' ">codeid1,</if>
		<if test="length1 != null and length1 != '' ">length1,</if>
		<if test="name1 != null and name1 != ''">name1,</if>
		<if test="codeid2 != null and codeid2 != ''">codeid2,</if>
		<if test="length2 != null and length2 != '' ">length2,</if>
		<if test="name2 != null and name2 != ''">name2,</if>
		<if test="codeid3 != null and codeid3 != ''">codeid3,</if>
		<if test="length3 != null and length3 != '' ">length3,</if>
		<if test="name3 != null and name3 != ''">name3,</if>
		<if test="codeid4 != null and codeid4 != ''">codeid4,</if>
		<if test="length4 != null and length4 != '' ">length4,</if>
		<if test="name4 != null and name4 != ''">name4,</if>
		<if test="codeid5 != null and codeid5 != ''">codeid5,</if>
		<if test="length5 != null and length5 != '' ">length5,</if>
		<if test="name5 != null and name5 != ''">name5,</if>
		<if test="codeid6 != null and codeid6 != ''">codeid6,</if>
		<if test="length6 != null and length6 != '' ">length6,</if>
		<if test="name6 != null and name6 != ''">name6,</if>
		<if test="codeid7 != null and codeid7 != ''">codeid7,</if>
		<if test="length7 != null and length7 != '' ">length7,</if>
		<if test="name7 != null and name7 != ''">name7,</if>
		<if test="codeid8 != null and codeid8 != ''">codeid8,</if>
		<if test="length8 != null and length8 != '' ">length8,</if>
		<if test="name8 != null and name8 != ''">name8,</if>
		<if test="summary != null and summary != ''">summary,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		<if test="remark != null and remark != ''">remark,</if>
		<if test="sfzx != null and sfzx != ''">sfzx,</if>
		create_time,
		id
		)values(
		<if test="levelid != null and levelid != ''">#{levelid},</if>
		<if test="codeid1 != null and codeid1 != ''">#{codeid1},</if>
		<if test="length1 != null and length1 != '' ">#{length1},</if>
		<if test="name1 != null and name1 != ''">#{name1},</if>
		<if test="codeid2 != null and codeid2 != ''">#{codeid2},</if>
		<if test="length2 != null and length2 != '' ">#{length2},</if>
		<if test="name2 != null and name2 != ''">#{name2},</if>
		<if test="codeid3 != null and codeid3 != ''">#{codeid3},</if>
		<if test="length3 != null and length3 != '' ">#{length3},</if>
		<if test="name3 != null and name3 != ''">#{name3},</if>
		<if test="codeid4 != null and codeid4 != ''">#{codeid4},</if>
		<if test="length4 != null and length4 != '' ">#{length4},</if>
		<if test="name4 != null and name4 != ''">#{name4},</if>
		<if test="codeid5 != null and codeid5 != ''">#{codeid5},</if>
		<if test="length5 != null and length5 != '' ">#{length5},</if>
		<if test="name5 != null and name5 != ''">#{name5},</if>
		<if test="codeid6 != null and codeid6 != ''">#{codeid6},</if>
		<if test="length6 != null and length6 != '' ">#{length6},</if>
		<if test="name6 != null and name6 != ''">#{name6},</if>
		<if test="codeid7 != null and codeid7 != ''">#{codeid7},</if>
		<if test="length7 != null and length7 != '' ">#{length7},</if>
		<if test="name7 != null and name7 != ''">#{name7},</if>
		<if test="codeid8 != null and codeid8 != ''">#{codeid8},</if>
		<if test="length8 != null and length8 != '' ">#{length8},</if>
		<if test="name8 != null and name8 != ''">#{name8},</if>
		<if test="summary != null and summary != ''">#{summary},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		<if test="remark != null and remark != ''">#{remark},</if>
		<if test="sfzx != null and sfzx != ''">#{sfzx},</if>
		sysdate(),
		#{id}
		)
	</insert>

	<select id="getByMaxId" resultType="java.lang.Long">
        Select ifnull(max(id),0)+1  from hs_sys_code
            </select>

	<select id="selectCodeById" parameterType="Long" resultMap="HsSysCodeResult">
		select *   from hs_sys_code u
		where u.id = #{id}
	</select>

	<select id="getIsParentid"  resultType="java.lang.Long" >
		select  count(id) from hs_sys_level where parent_id = #{levelid}
	</select>

	<update id="updateCode" parameterType="HsSysCode">
		update hs_sys_code
		<set>
			<if test="levelid != null and levelid != ''">levelid = #{levelid},</if>
			<if test="codeid1 != null and codeid1 != ''">
				codeid1 = #{codeid1},</if>
			<if test="length1 != null and length1 != ''">
				length1 = #{length1},</if>
			<if test="name1 != null and name1 != ''">
				name1 = #{name1},</if>
			<if test="codeid2 != null and codeid2 != ''">
				codeid2 = #{codeid2},</if>
			<if test="length2 != null and length2 != ''">
				length2 = #{length2},</if>
			<if test="name2 != null and name2 != ''">
				name2 = #{name2},</if>
			<if test="codeid3 != null and codeid3 != ''">
				codeid3 = #{codeid3},</if>
			<if test="length3 != null and length3 != ''">
				length3 = #{length3},</if>
			<if test="name3 != null and name3 != ''">
				name3 = #{name3},</if>
			<if test="codeid4 != null and codeid4 != ''">
				codeid4 = #{codeid4},</if>
			<if test="length4 != null and length4 != ''">
				length4 = #{length4},</if>
			<if test="name4 != null and name4 != ''">
				name4 = #{name4},</if>
			<if test="codeid5 != null and codeid5 != ''">
				codeid5 = #{codeid5},</if>
			<if test="length5 != null and length5 != ''">
				length5 = #{length5},</if>
			<if test="name5 != null and name5 != ''">
				name5 = #{name5},</if>
			<if test="codeid6 != null and codeid6 != ''">
				codeid6 = #{codeid6},</if>
			<if test="length6 != null and length6 != ''">
				length6 = #{length6},</if>
			<if test="name6 != null and name6 != ''">
				name6 = #{name6},</if>
			<if test="codeid7 != null and codeid7 != ''">
				codeid7 = #{codeid7},</if>
			<if test="length7 != null and length7 != ''">
				length7 = #{length7},</if>
			<if test="name7 != null and name7 != ''">
				name7 = #{name7},</if>
			<if test="codeid8 != null and codeid8 != ''">
				codeid8 = #{codeid8},</if>
			<if test="length8 != null and length8 != ''">
				length8 = #{length8},</if>
			<if test="name8 != null and name8 != ''">
				name8 = #{name8},</if>
			<if test="summary != null and summary != ''">
				summary = #{summary},</if>

			update_time = sysdate()
		</set>
		where id = #{id}
	</update>

<!--	<delete id="removeByIds" parameterType="Long">
		update hs_sys_code  set del_flag = '2' where user_id in
		<foreach collection="array" item="userId" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>-->

	<delete id="removeByIds" parameterType="Long">
		delete from  hs_sys_code  where id in
		<foreach collection="array" item="id" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

	<select id="selectCodeListOfAll" parameterType="HsSysCode" resultMap="HsSysCodeResult">
		select id,levelid,concat(ifnull(name1,''),ifnull(name2,''),ifnull(name3,''),ifnull(name4,''),ifnull(name5,''),ifnull(name6,''),ifnull(name7,''),ifnull(name8,'')) as name1,summary from hs_sys_code u
		where 1=1
		<if test="summary != null ">and summary  like CONCAT(CONCAT('%',#{summary}),'%')
			</if>

		<!-- 数据范围过滤 -->
		${params.dataScope}
	</select>

	<select id="selectHsSysLevelList" parameterType="HsSysCode" resultMap="HsSysLevelResult">
			select * from hs_sys_level where id=#{levelid}
	</select>

	<select id="getLevelcode" parameterType="Long"  resultMap="HsSysLevelResult">
		select levelcode,parent_id  from hs_sys_level u
		where u.id = #{id}
	</select>

	<select id="checkCodeApply" parameterType="Long"  resultType="java.util.Map">
		select codeapply from hs_sys_apply where codeid=(select levelid from hs_sys_code where id= #{id})
	</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>

    <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>
			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>
			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>
 			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="updateDeptStatusNormal" parameterType="Long">
 	    update sys_dept set status = '0' where dept_id in
 	    <foreach collection="array" item="deptId" open="(" separator="," close=")">
        	#{deptId}
        </foreach>
	</update>

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

</mapper>
