<?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.ho.user.mapper.SysDictMapper">

    <sql id="Base_Column_DictList">
        id, `type`, type_name
    </sql>

    <sql id="Base_Column_DictSubList">
        id,label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList1">
        id,label_en label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList2">
        id,label_fra label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList3">
        id,label_spa label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList4">
        id,label_de label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList5">
        id,label_pl label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>
    <sql id="Base_Column_DictSubList6">
        id,label_ara label, type,`value`, sort, `desc`,create_time, update_time, deleted
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByType" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        where
        type = #{type}
    </select>


    <select id="selectAll" resultType="com.ho.user.vo.resp.DictRespVo">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        <where>
            <if test="record.type != null and record.type!= ''">
                type LIKE concat('%',#{record.type},'%')
            </if>
        </where>
    </select>

    <select id="selectSubDictByValue" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        where
        deleted = 1
        and value = #{value}
    </select>

    <select id="selectById" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        where
        id = #{id}
    </select>

    <select id="selectBySubId" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        where
        id = #{id}
    </select>

    <select id="selectSubDictByTypeList" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        <where>
            <if test="typeList!=null and typeList.size() >0">
                and type in
                <foreach collection="typeList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            and
            deleted = 1
        </where>
        order by sort
    </select>

    <select id="selectSubDictByType" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_DictSubList1"></include>
            </when>
            <when test="lang != null and lang=='fr_FR' ">
                <include refid="Base_Column_DictSubList2"/>
            </when>
            <when test="lang != null and lang=='es_ES' ">
                <include refid="Base_Column_DictSubList3"/>
            </when>
            <when test="lang != null and lang=='de_DE' ">
                <include refid="Base_Column_DictSubList4"/>
            </when>
            <when test="lang != null and lang=='pl_PL' ">
                <include refid="Base_Column_DictSubList5"/>
            </when>
            <when test="lang != null and lang=='ar_EG' ">
                <include refid="Base_Column_DictSubList6"/>
            </when>
            <otherwise>
                <include refid="Base_Column_DictSubList"></include>
            </otherwise>
        </choose>
        from sys_sub_dict
        where
        type = #{type}
        and
        deleted = 1
        order by sort
    </select>

    <select id="selectDictByTypeAndName" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        where
        type = #{record.type}
        and
        type_name = #{record.typeName}
    </select>
    <select id="selectTypeAll" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
    </select>
    <select id="selectSubDictByTV" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        where
        label = #{value}
        and type = #{type}
        and deleted = 1
        limit 1
    </select>
    <select id="selectDictListByType" resultType="com.ho.user.entity.SysDict">
        select
        <include refid="Base_Column_DictList"/>
        from sys_dict
        where
        type LIKE concat('%',#{type},'%')
    </select>




    <select id="selectSubDictByKey" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        <where>
            <if test="record.type != null">
                and type = #{record.type}
            </if>
            <if test="record.label != null">
                and label = #{record.label}
            </if>
            <if test="record.id != null">
                and id != #{record.id}
            </if>
            and deleted = 1
        </where>
    </select>


    <select id="selectSubDictBySort" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        <where>
            <if test="type != null">
                and type = #{type}
            </if>
            <if test="sort != null">
                and sort = #{sort}
            </if>
            and deleted = 1
        </where>
    </select>

    <select id="getDictByIds" resultType="com.ho.user.api.entity.SysSubDict">
        select
        <include refid="Base_Column_DictSubList"/>
        from sys_sub_dict
        <where>
            <if test="ids != null and ids.size != 0">
                and id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <insert id="insertDict" parameterType="com.ho.user.entity.SysDict">
        insert into sys_dict (`type`, type_name)
        values (#{record.type}, #{record.typeName})
    </insert>
    <insert id="insertSubDict">
        insert into sys_sub_dict (type, label, `value`, sort, `desc`, create_time, update_time, deleted)
        values (#{record.type}, #{record.label}, #{record.value}, #{record.sort}, #{record.desc}, now(), now(), 1)
    </insert>

    <update id="updateByPrimaryKey">
        update sys_sub_dict
        <set>
            <if test="record.label != null">
                label = #{record.label,jdbcType=VARCHAR},
            </if>
            <if test="record.value != null">
                value = #{record.value,jdbcType=VARCHAR},
            </if>
            <if test="record.sort != null">
                sort = #{record.sort,jdbcType=INTEGER},
            </if>
            <if test="record.desc != null">
                `desc` = #{record.desc,jdbcType=VARCHAR},
            </if>
            <if test="record.deleted != null">
                deleted = #{record.deleted,jdbcType=INTEGER},
            </if>
            update_time = now()
        </set>
        where type = #{record.type,jdbcType=VARCHAR}
    </update>

    <update id="updateDict">
        update sys_dict
        <set>
            <if test="record.type != null">
                type = #{record.type,jdbcType=VARCHAR},
            </if>
            <if test="record.typeName != null">
                type_name = #{record.typeName,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{record.id,jdbcType=INTEGER}
    </update>

    <update id="updateSubDict">
        update sys_sub_dict
        <set>
            <if test="record.type != null">
                type = #{record.type,jdbcType=VARCHAR},
            </if>

            <if test="record.label != null">
                label = #{record.label,jdbcType=VARCHAR},
            </if>
            <if test="record.labelEn != null">
                label_en = #{record.labelEn},
            </if>
            <if test="record.value != null">
                value = #{record.value,jdbcType=VARCHAR},
            </if>
            <if test="record.sort != null">
                sort = #{record.sort,jdbcType=INTEGER},
            </if>
            <if test="record.desc != null">
                `desc` = #{record.desc,jdbcType=VARCHAR},
            </if>
            <if test="record.deleted != null">
                deleted = #{record.deleted,jdbcType=INTEGER},
            </if>
            update_time = now()
        </set>
        where id = #{record.id,jdbcType=INTEGER}
    </update>


    <delete id="deleteByPrimaryKey">
        delete
        from sys_dict
        where id = #{id}
    </delete>

    <delete id="deleteByType">
        delete
        from sys_dict
        where type = #{type}
    </delete>


</mapper>