<?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.securitycontrol.system.mapper.DictMapper">

    <resultMap id="MenuMap" type="com.securitycontrol.entity.system.vo.DictVo">
        <id property="dictId" column="dict_id"/>
        <result property="dictCode" column="dict_code"/>
        <result property="pidCode" column="p_code"/>
        <result property="dictName" column="dict_name"/>
        <result property="dictValue" column="dict_sort"/>
        <result property="createTime" column="create_time"/>
        <result property="remarks" column="remarks"/>
    </resultMap>

    <!--查询  -->
    <select id="getDictList" resultMap="MenuMap">
        select dict_id,dict_code,dict_name,dict_sort,p_code,create_time,remarks
        from sys_dict
        where del_flag=0
        <if test="keyWord!=null and keyWord!=''">
            and(
            dict_name like concat('%',#{keyWord},'%')
            or dict_code =#{keyWord}
            )
        </if>
        order by dict_sort
    </select>
    <!--依据字典查询-->
    <select id="getNumByCode" resultType="java.lang.Integer">
        select count(1)
        from sys_dict
        where del_flag=0
        <if test="dictCode!=null and dictCode!='' and dictCode!=0 ">
            and dict_code=#{dictCode}
        </if>
        <if test="dictName!=null and dictName!=''">
            and dict_name=#{dictName} and p_code=#{pidCode}
        </if>
        <if test="dictId!=null and dictId!=''">
            and dict_id!=#{dictId}
        </if>
    </select>
    <!--子节点数量-->
    <select id="getChildNum" resultType="java.lang.Integer">
        select count(sd1.dict_id)
        from sys_dict sd1
        left join sys_dict sd2 on sd1.p_code=sd2.dict_code
        where sd1.del_flag=0 and sd2.dict_id=#{dictId}
    </select>
    <select id="getDetails" resultMap="MenuMap">
        select dict_id,dict_code,dict_name,dict_sort,p_code,create_time,remarks
        from sys_dict
        where del_flag=0 and dict_id=#{id}
    </select>
    <!--新增字典-->
    <insert id="addDict">
        insert into sys_dict (dict_code, dict_name, dict_sort, create_time, del_flag, remarks,p_code) value (
        #{dictCode},#{dictName},#{dictValue},now(),0,#{remarks},#{pidCode}
        )
    </insert>
    <!--修改-->
    <update id="updateDict">
        update sys_dict
        set dict_code=#{dictCode},dict_name=#{dictName},dict_sort=#{dictValue},remarks=#{remarks},p_code=#{pidCode}
        where dict_id=#{dictId};
    </update>

    <update id="updateChildCode">
        update sys_dict
        set p_code=#{pidCode}
        where p_code=#{dictCode};
    </update>
    <!--删除-->
    <delete id="delDict">
        update sys_dict
        set del_flag=1
        where dict_id=#{dictId};
    </delete>
</mapper>