<?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.zcxk.dao.DictMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.entity.Dict" id="DictMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="dictName" column="DICT_NAME" jdbcType="VARCHAR"/>
        <result property="dictCode" column="DICT_CODE" jdbcType="VARCHAR"/>
        <result property="dictValue" column="DICT_VALUE" jdbcType="VARCHAR"/>
        <result property="parentDictId" column="PARENT_DICT_ID" jdbcType="INTEGER"/>
        <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
        <result property="status" column="STATUS" jdbcType="INTEGER"/>
        <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
        <result property="dateCreate" column="DATE_CREATE" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
        <result property="dateUpdate" column="DATE_UPDATE" jdbcType="TIMESTAMP"/>
    </resultMap>
    <!-- 二层结果集 -->
    <resultMap type="com.zcxk.dto.DictDto" id="DictDtoMap">
        <result property="parentDictId" column="PARENT_DICT_ID" jdbcType="INTEGER"/>
        <result property="parentDictName" column="DICT_NAME" jdbcType="VARCHAR"/>
        <result property="parentDictCode" column="PARENT_DICT_CODE" jdbcType="VARCHAR"/>
        <result property="remark" column="REMARK" jdbcType="VARCHAR"/>
        <collection property="dictList" ofType="java.util.Map" javaType="list">
            <result property="id" column="ID" jdbcType="INTEGER"/>
            <result property="dictCode" column="DICT_CODE" jdbcType="VARCHAR"/>
            <result property="dictValue" column="DICT_VALUE" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
         ID,DICT_NAME,DICT_CODE,DICT_VALUE,	PARENT_DICT_ID,	REMARK,	STATUS,	DATE_CREATE,CREATE_BY,DATE_UPDATE,UPDATE_BY</sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="DictMap">
        select
        <include refid="Base_Column_List"/>
        from uims_dict
        where ID = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="DictMap">
        select
        <include refid="Base_Column_List"/>
        from uims_dict
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="DictMap">
        select
        <include refid="Base_Column_List"/>
        from uims_dict
        <where>
            <if test="id != null">
                and ID = #{id}
            </if>
            <if test="dictName != null and dictName != ''">
                and DICT_NAME = #{dictName}
            </if>
            <if test="dictCode != null and dictCode != ''">
                and (DICT_CODE LIKE CONCAT('%',#{dictCode},'%')
                or DICT_VALUE LIKE CONCAT('%',#{dictCode},'%'))
            </if>
            <if test="parentDictId != null and parentDictId != ''">
                and PARENT_DICT_ID = #{parentDictId}
            </if>
            <if test="parentDictCode != null and parentDictCode != ''">
                and PARENT_DICT_ID in (select ID from uims_dict where DICT_CODE=#{parentDictCode})
            </if>
            and STATUS =1
            and DICT_CODE!='-999'
        </where>
        order by DATE_CREATE
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into uims_dict(ID,DICT_NAME,DICT_CODE,DICT_VALUE,PARENT_DICT_ID,	REMARK,	STATUS,	CREATE_BY,DATE_CREATE,UPDATE_BY,DATE_UPDATE)
        values ( #{id}, #{dictName}, #{dictCode}, #{dictValue}, #{parentDictId}, #{remark}, 1, #{createBy}, #{dateCreate}, #{updateBy}, #{dateUpdate})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into uims_dict(ID,DICT_NAME,DICT_CODE,DICT_VALUE,PARENT_DICT_ID,	REMARK,	STATUS,	CREATE_BY,DATE_CREATE,UPDATE_BY,DATE_UPDATE)
        values
        <foreach collection="dicts" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.dictName}, #{item.dictCode}, #{item.dictValue}, #{item.parentDictId}, #{item.remark}, 1,
            #{item.createBy}, #{item.dateCreate}, #{item.updateBy}, #{item.dateUpdate})
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update uims.uims_dict
        <set>
            <if test="dictName != null and dictName != ''">
                DICT_NAME = #{dictName},
            </if>
            <if test="dictCode != null and dictCode != ''">
                DICT_CODE = #{dictCode},
            </if>
            <if test="dictValue != null and dictValue != ''">
                DICT_VALUE = #{dictValue},
            </if>
            <if test="parentDictId != null and parentDictId != ''">
                PARENT_DICT_ID = #{parentDictId},
            </if>
            <if test="remark != null and remark != ''">
                REMARK = #{remark},
            </if>
            <if test="status != null">
                STATUS = #{status},
            </if>
            <if test="createBy != null and createBy != ''">
                CREATE_BY = #{createBy},
            </if>
            <if test="dateCreate != null">
                DATE_CREATE = #{dateCreate},
            </if>
            <if test="updateBy != null and updateBy != ''">
                UPDATE_BY = #{updateBy},
            </if>
            <if test="dateUpdate != null">
                DATE_UPDATE = #{dateUpdate},
            </if>
        </set>
        where ID = #{id}
    </update>

    <!--通过主键删除-物理删除-->
    <!--<delete id="deleteById" parameterType="java.util.List">
        delete from uims_dict where ID IN
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
         or  PARENT_DICT_ID in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </delete>-->
    <!--通过主键删除-逻辑删除-->
    <update id="deleteById" parameterType="java.util.List">
        update uims.uims_dict
        set STATUS=0
        where ID in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
            or  PARENT_DICT_ID in
        <foreach item="item" index="index" collection="ids" open="("
                 separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from uims_dict
    </select>
    <select id="selectPage" resultMap="DictMap">
        select
        <include refid="Base_Column_List"/>
        from uims_dict
        <where>
            <if test="dict.id != null">
                and ID = #{dict.id}
            </if>
            <if test="dict.parentDictId == null and dict.dictCode != null and dict.dictCode != ''">
                and (DICT_NAME LIKE CONCAT('%',#{dict.dictCode},'%')
                or DICT_CODE LIKE CONCAT('%',#{dict.dictCode},'%'))
            </if>
            <if test="dict.parentDictId != null and dict.dictCode != null and dict.dictCode != ''">
                and (DICT_VALUE LIKE CONCAT('%',#{dict.dictCode},'%')
                or DICT_CODE LIKE CONCAT('%',#{dict.dictCode},'%'))
            </if>
            <if test="dict.parentDictId != null ">
                and PARENT_DICT_ID = #{dict.parentDictId}
            </if>
            <if test="dict.parentDictId == null">
                and PARENT_DICT_ID IS NULL
            </if>
            and STATUS =1
            and DICT_CODE!='-999'
        </where>
        order by DATE_CREATE
    </select>
    <!-- 字典编码和父配置ID数量 -->
    <select id="countByCodePid" resultType="int">
        select count(*) from uims_dict
        <where>
            <if test="dictCode != null and dictCode != ''">
                and DICT_CODE = #{dictCode}
            </if>
            <if test="parentDictId != null and parentDictId != ''">
                and PARENT_DICT_ID = #{parentDictId}
            </if>
            <if test="parentDictId == null">
                and PARENT_DICT_ID IS NULL
            </if>
            and STATUS =1
        </where>
    </select>
    <!--查询父列表-->
    <select id="selectParentList" resultMap="DictDtoMap">
        select t2.parent_dict_id,t1.dict_code parent_dict_code,t1.dict_name,t1.remark,t2.id,t2.dict_code,t2.dict_value
        from uims_dict t1
        inner join uims_dict t2 on t1.id=t2.parent_dict_id
        where t1.`status`=1 and t2.`status`=1
        <if test="remark != null and remark != ''">
            and t1.remark = #{remark}
        </if>
        order by t1.date_create,t2.date_create
    </select>
</mapper>