<?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.energy.manage.service.mappers.dict.DictConstantsMapper">



    <insert id="batchSave">

        INSERT INTO dict_constants (

                `contents_type`,
                `contents_value`,
                `contents_name`,
        )
        VALUES
        <foreach collection="data" index="index" item="item" separator=",">
            (
                    #{item.contentsType},
                    #{item.contentsValue},
                    #{item.contentsName},
            )
        </foreach>
    </insert>


    <update id="batchUpdate" parameterType="java.util.List">

        <foreach collection="data" item="item" index="index" separator=";">
            UPDATE dict_constants
            <set>
                    <if test="item.contentsType != null">
                        contents_type=#{item.contentsType},
                    </if>
                    <if test="item.contentsValue != null">
                        contents_value=#{item.contentsValue},
                    </if>
                    <if test="item.contentsName != null">
                        contents_name=#{item.contentsName},
                    </if>
            </set>
            WHERE id = #{item.id}
        </foreach>
    </update>



    <resultMap id="enableDictPMap" type="com.energy.manage.service.domain.vo.dict.DictParentVo">
        <id property="pId" column="pid" jdbcType="INTEGER"/>
        <result property="pName" column="pname" jdbcType="VARCHAR"/>
        <collection property="dictChainVoList" ofType="com.energy.manage.service.domain.vo.dict.DictChainVo">
            <id property="cId" column="cid" jdbcType="INTEGER"/>
            <result property="cName" column="cname" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <!-- 查询树结构字典 -->
    <select id="getDictReachChain" resultMap="enableDictPMap">
        select
        a.id as pid,
        a.name as pname,
        b.id as cid,
        b.name as cname
        from dict_chain_constants a
        inner join dict_chain_constants b on a.id = b.parent_id
        where 1=1 and a.type = #{type}
    </select>

</mapper>
