<?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.ruoyi.project.system.mapper.SysDictDataMapper">
	
	<resultMap type="SysDictData" id="SysDictDataResult">
		<id     property="dictCode"   column="dict_code"   />
		<result property="dictSort"   column="dict_sort"   />
		<result property="dictLabel"  column="dict_label"  />
		<result property="dictLabelUS"  column="dict_labelUS"  />
		<result property="dictLabelRU"  column="dict_labelRU"  />
		<result property="dictLabelES"  column="dict_labelES"  />
		<result property="dictValue"  column="dict_value"  />
		<result property="dictType"   column="dict_type"   />
		<result property="cssClass"   column="css_class"   />
		<result property="listClass"  column="list_class"  />
		<result property="isDefault"  column="is_default"  />
		<result property="isDefaultK"  column="is_default_k"  />
		<result property="status"     column="status"      />
		<result property="statusK"     column="status_k"      />
		<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>
	
	<sql id="selectDictDataVo">
		SELECT
			T1.dict_code,
			T1.dict_sort,
			T1.dict_label,
			T1.dict_labelUS,
			T1.dict_labelRU,
			T1.dict_labelES,
			T1.dict_value,
			T1.dict_type,
			T1.css_class,
			T1.list_class,
			T1.is_default,
			CASE #{language}
				WHEN 'english' THEN T3.dict_labelUS
				WHEN 'russia'  THEN T3.dict_labelRU
				WHEN 'spanish' THEN T3.dict_labelES
				ELSE T3.dict_label
			END AS IS_DEFAULT_K,
			T1.STATUS,
			CASE #{language}
				WHEN 'english' THEN T2.dict_labelUS
				WHEN 'russia'  THEN T2.dict_labelRU
				WHEN 'spanish' THEN T2.dict_labelES
				ELSE T2.dict_label
			END AS STATUS_K,
			T1.create_by,
			T1.create_time,
			T1.remark
		FROM
			sys_dict_data T1
				LEFT JOIN sys_dict_data T2 ON T2.dict_type = 'sys_normal_disable' AND t1.STATUS = t2.dict_value
				LEFT JOIN sys_dict_data T3 ON T3.dict_type = 'sys_yes_no' AND t1.is_default = T3.dict_value
    </sql>

	<select id="selectDictDataList" parameterType="SysDictData" resultMap="SysDictDataResult">
	    <include refid="selectDictDataVo"/>
		<where>
		    <if test="dictType != null and dictType != ''">
				AND T1.dict_type = #{dictType}
			</if>
			<if test="dictLabel != null and dictLabel != ''">
				AND T1.dict_label like concat('%', #{dictLabel}, '%')
			</if>
			<if test="dictLabelUS != null and dictLabelUS != ''">
				AND T1.dict_labelUS like concat('%', #{dictLabelUS}, '%')
			</if>
			<if test="dictLabelRU != null and dictLabelRU != ''">
				AND T1.dict_labelRU like concat('%', #{dictLabelRU}, '%')
			</if>
			<if test="dictLabelES != null and dictLabelES != ''">
				AND T1.dict_labelES like concat('%', #{dictLabelES}, '%')
			</if>
			<if test="status != null and status != ''">
				AND T1.status = #{status}
			</if>
			<if test="beginTime != null and beginTime != ''">
				and date_format(T1.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
			</if>
			<if test="endTime != null and endTime != ''">
				and date_format(T1.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
			</if>
		</where>
		order by T1.dict_sort asc
	</select>
	
	<select id="selectDictDataByType" parameterType="SysDictData" resultMap="SysDictDataResult">
		<include refid="selectDictDataVo"/>
		where T1.status = '0' and T1.dict_type = #{dictType} order by T1.dict_sort asc
	</select>
	
	<select id="selectDictLabel" resultType="String">
		select dict_label from sys_dict_data
		where dict_type = #{dictType} and dict_value = #{dictValue}
	</select>
	
	<select id="selectDictDataById" parameterType="Long" resultMap="SysDictDataResult">
		<include refid="selectDictDataVo"/>
		where T1.dict_code = #{dictCode}
	</select>
	
	<select id="countDictDataByType" resultType="Integer">
	    select count(1) from sys_dict_data where dict_type=#{dictType}  
	</select>
	
	<delete id="deleteDictDataById" parameterType="Long">
 		delete from sys_dict_data where dict_code = #{dictCode}
 	</delete>
 	
 	<delete id="deleteDictDataByIds" parameterType="Long">
 		delete from sys_dict_data where dict_code in
 		<foreach collection="array" item="dictCode" open="(" separator="," close=")">
 			#{dictCode}
        </foreach> 
 	</delete>
	
	<update id="updateDictData" parameterType="SysDictData">
 		update sys_dict_data
 		<set>
 			<if test="dictSort != null">dict_sort = #{dictSort},</if>
 			<if test="dictLabel != null and dictLabel != ''">dict_label = #{dictLabel},</if>
			<if test="dictLabelUS != null and dictLabelUS != ''">dict_labelUS = #{dictLabelUS},</if>
			<if test="dictLabelRU != null and dictLabelRU != ''">dict_labelRU = #{dictLabelRU},</if>
			<if test="dictLabelES != null and dictLabelES != ''">dict_labelES = #{dictLabelES},</if>
 			<if test="dictValue != null and dictValue != ''">dict_value = #{dictValue},</if>
 			<if test="dictType != null and dictType != ''">dict_type = #{dictType},</if>
 			<if test="cssClass != null">css_class = #{cssClass},</if>
 			<if test="listClass != null">list_class = #{listClass},</if>
 			<if test="isDefault != null and isDefault != ''">is_default = #{isDefault},</if>
 			<if test="status != null">status = #{status},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			update_time = sysdate()
 		</set>
 		where dict_code = #{dictCode}
	</update>
	
	<update id="updateDictDataType" parameterType="String">
 		update sys_dict_data set dict_type = #{newDictType} where dict_type = #{oldDictType}
	</update>
 	
 	<insert id="insertDictData" parameterType="SysDictData">
 		insert into sys_dict_data(
 			<if test="dictSort != null">dict_sort,</if>
 			<if test="dictLabel != null and dictLabel != ''">dict_label,</if>
			<if test="dictLabelUS != null and dictLabelUS != ''">dict_labelUS,</if>
			<if test="dictLabelRU != null and dictLabelRU != ''">dict_labelRU,</if>
			<if test="dictLabelES != null and dictLabelES != ''">dict_labelES,</if>
 			<if test="dictValue != null and dictValue != ''">dict_value,</if>
 			<if test="dictType != null and dictType != ''">dict_type,</if>
 			<if test="cssClass != null and cssClass != ''">css_class,</if>
 			<if test="listClass != null and listClass != ''">list_class,</if>
 			<if test="isDefault != null and isDefault != ''">is_default,</if>
 			<if test="status != null">status,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			create_time
 		)values(
 		    <if test="dictSort != null">#{dictSort},</if>
 		    <if test="dictLabel != null and dictLabel != ''">#{dictLabel},</if>
			<if test="dictLabelUS != null and dictLabelUS != ''">#{dictLabelUS},</if>
			<if test="dictLabelRU != null and dictLabelRU != ''">#{dictLabelRU},</if>
			<if test="dictLabelES != null and dictLabelES != ''">#{dictLabelES},</if>
 			<if test="dictValue != null and dictValue != ''">#{dictValue},</if>
 			<if test="dictType != null and dictType != ''">#{dictType},</if>
 			<if test="cssClass != null and cssClass != ''">#{cssClass},</if>
 			<if test="listClass != null and listClass != ''">#{listClass},</if>
 			<if test="isDefault != null and isDefault != ''">#{isDefault},</if>
 			<if test="status != null">#{status},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			sysdate()
 		)
	</insert>
	
</mapper> 