<?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.fastbee.system.mapper.SysDictDataMapper">

	<resultMap type="com.fastbee.common.extend.core.domin.entity.SysDictData" id="SysDictDataResult">
		<id     property="dictCode"   column="dict_code"   />
		<result property="dictSort"   column="dict_sort"   />
		<result property="dictLabel"  column="dict_label"  />
		<result property="dictLabel_zh_CN"  column="dict_label_zh_cn"  />
		<result property="dictLabel_en_US"  column="dict_label_en_us"  />
		<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="status"     column="status"      />
		<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>

	<resultMap type="com.fastbee.system.domain.vo.SysDictDataVO" id="SysDictDataVOResult">
		<id     property="dictCode"   column="dict_code"   />
		<result property="dictSort"   column="dict_sort"   />
		<result property="dictLabel"  column="dict_label"  />
		<result property="dictLabel_zh_CN"  column="dict_label_zh_cn"  />
		<result property="dictLabel_en_US"  column="dict_label_en_us"  />
		<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="status"     column="status"      />
		<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 dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark
		from sys_dict_data
	</sql>

	<select id="selectSysDictDataVoPage" parameterType="com.fastbee.system.domain.vo.SysDictDataVO" resultMap="SysDictDataVOResult">
		select d.dict_code, d.dict_sort,
		case
		when #{sysDictDataVO.language} = 'zh-CN' then d.dict_label
		when #{sysDictDataVO.language} = 'en-US' then t.en_us
		else d.dict_label
		end as dict_label,
		d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
		left join sys_dict_data_translate t on d.dict_code = t.id
		<where>
			<if test="sysDictDataVO.dictType != null and sysDictDataVO.dictType != ''">
				AND d.dict_type = #{sysDictDataVO.dictType}
			</if>
			<if test="sysDictDataVO.dictLabel != null and sysDictDataVO.dictLabel != ''">
				AND d.dict_label like concat('%', #{sysDictDataVO.dictLabel}, '%')
			</if>
			<if test="sysDictDataVO.status != null">
				AND d.status = #{sysDictDataVO.status}
			</if>
		</where>
		order by d.dict_sort asc
	</select>

	<select id="selectDictDataList" parameterType="com.fastbee.common.extend.core.domin.entity.SysDictData" resultMap="SysDictDataResult">
		select d.dict_code, d.dict_sort,
		case
		when #{language} = 'zh-CN' then d.dict_label
		when #{language} = 'en-US' then t.en_us
		else d.dict_label
		end as dict_label,
		d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
		left join sys_dict_data_translate t on d.dict_code = t.id
		<where>
			<if test="dictType != null and dictType != ''">
				AND d.dict_type = #{dictType}
			</if>
			<if test="dictLabel != null and dictLabel != ''">
				AND d.dict_label like concat('%', #{dictLabel}, '%')
			</if>
			<if test="status != null">
				AND d.status = #{status}
			</if>
		</where>
		order by d.dict_sort asc
	</select>

	<select id="selectDictDataListAll" parameterType="com.fastbee.common.extend.core.domin.entity.SysDictData" resultMap="SysDictDataResult">
		select d.dict_code, d.dict_sort, d.dict_label, t.zh_cn as dict_label_zh_cn, t.en_us as dict_label_en_us,
		d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
		left join sys_dict_data_translate t on d.dict_code = t.id
		<where>
			<if test="dictType != null and dictType != ''">
				AND d.dict_type = #{dictType}
			</if>
			<if test="dictLabel != null and dictLabel != ''">
				AND d.dict_label like concat('%', #{dictLabel}, '%')
			</if>
			<if test="status != null">
				AND d.status = #{status}
			</if>
		</where>
		order by d.dict_sort asc
	</select>

	<select id="selectDictDataByType" parameterType="com.fastbee.common.extend.core.domin.entity.SysDictData" resultMap="SysDictDataResult">
		select d.dict_code, d.dict_sort, d.dict_label, t.zh_cn as dict_label_zh_cn, t.en_us as dict_label_en_us,
			   d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
				 left join sys_dict_data_translate t on d.dict_code = t.id
		where d.status = 0 and d.dict_type = #{dictType} order by d.dict_sort asc
	</select>

	<select id="selectDictLabel" resultType="java.lang.String">
		select dict_label from sys_dict_data
		where dict_type = #{dictType} and dict_value = #{dictValue}
	</select>

	<select id="selectDictDataById" resultMap="SysDictDataResult">
		select d.dict_code, d.dict_sort,
			   case
				   when #{language} = 'zh-CN' then d.dict_label
				   when #{language} = 'en-US' then t.en_us
				   else d.dict_label
				   end as dict_label,
			   d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
				 left join sys_dict_data_translate t on d.dict_code = t.id
		where d.dict_code = #{dictCode}
	</select>

	<select id="countDictDataByType" resultType="Integer">
		select count(1) from sys_dict_data where dict_type=#{dictType}
	</select>

	<select id="selectDictDataListByDictTypes" resultType="com.fastbee.common.extend.core.domin.entity.SysDictData">
		select d.dict_code, d.dict_sort,
		case
		when #{language} = 'zh-CN' then d.dict_label
		when #{language} = 'en-US' then t.en_us
		else d.dict_label
		end as dict_label,
		d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
		left join sys_dict_data_translate t on d.dict_code = t.id
		where d.dict_type in
		<foreach collection="dictTypeList" item="dictType" open="(" separator="," close=")">
			#{dictType}
		</foreach>
		and d.status = 0
	</select>

	<update id="updateDictData" parameterType="com.fastbee.common.extend.core.domin.entity.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="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 = CURRENT_TIMESTAMP
		</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="com.fastbee.common.extend.core.domin.entity.SysDictData">
		insert into sys_dict_data(
		<if test="dictSort != null">dict_sort,</if>
		<if test="dictLabel != null and dictLabel != ''">dict_label,</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="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>
		CURRENT_TIMESTAMP
		)
	</insert>

	<select id="selectByDictTypeAndDictValue" resultType="com.fastbee.common.extend.core.domin.entity.SysDictData">
		select d.dict_code, d.dict_sort,
			   case
				   when #{language} = 'zh-CN' then d.dict_label
				   when #{language} = 'en-US' then t.en_us
				   else d.dict_label
				   end as dict_label,
			   d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
				 left join sys_dict_data_translate t on d.dict_code = t.id
		where d.dict_type = #{dictType} and d.dict_value = #{dictValue}
	</select>

	<select id="listByDictTypeAndDictValue" resultType="com.fastbee.common.extend.core.domin.entity.SysDictData">
		select d.dict_code, d.dict_sort,
		case
		when #{language} = 'zh-CN' then d.dict_label
		when #{language} = 'en-US' then t.en_us
		else d.dict_label
		end as dict_label,
		d.dict_value, d.dict_type, d.css_class, d.list_class, d.is_default, d.status, d.create_by, d.create_time, d.remark
		from sys_dict_data d
		left join sys_dict_data_translate t on d.dict_code = t.id
		where d.dict_type = #{dictType}
		and d.dict_value in
		<foreach collection="dictValueList" item="dictValue" open="(" separator="," close=")">
			#{dictValue}
		</foreach>
	</select>

</mapper>
