<?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.huatek.frame.base.mapper.DictionnaireDetailMapper">

	<resultMap id="BaseResultMap" type="com.huatek.frame.base.vo.DictionnaireDetailVO">
		<id column="id" jdbcType="VARCHAR" property="id" />
		<result column="pid" jdbcType="VARCHAR" property="pid" />
		<result column="dname" jdbcType="VARCHAR" property="dname" />
		<result column="dcode" jdbcType="VARCHAR" property="dcode" />
	</resultMap>
	<sql id="Base_Column_List">
		pid as pid,
		id as id,
		dname as dname,
		dcode as dcode
	</sql>

	<select id="selectDictionnaireDetailPage" parameterType="com.huatek.frame.base.dto.DictionnaireDetailDTO"
		resultMap="BaseResultMap">
		select
			<include refid="Base_Column_List" />
		from
			sys_dictionnaire_detail
		where
			deleted=#{deleted}
		and
			 pid=#{pid}
		order by dcode asc
	</select>

	<select id="findByUnit" parameterType="java.lang.String" resultMap="BaseResultMap">
		SELECT
		A.dcode AS dcode,
		A.dname as dname
		FROM
		sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
		B.`code` = #{dictKey}
		and A.dname=#{unit}
		AND A.deleted = 0
		AND B.deleted = 0
	</select>

	<select id="findByDCode" parameterType="java.lang.String" resultMap="BaseResultMap">
		SELECT
		A.dcode AS dcode,
		A.dname as dname
		FROM
		sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
		B.`code` = #{dictKey}
		and A.dcode=#{dCode}
		AND A.deleted = 0
		AND B.deleted = 0
	</select>

	<select id="findMaxDcodeByCode" parameterType="java.lang.String" resultType="com.huatek.frame.base.dto.DictionnaireDetailDTO">
		SELECT
		  A.pid as pid,
		  MAX(CONVERT(A.dcode, UNSIGNED)) AS dcode
		FROM
			sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
			B.`code` = #{dictKey}
		AND A.deleted = 0
		AND B.deleted = 0
	</select>

	<select id="findMaxDcodeByPId" parameterType="java.lang.String" resultType="com.huatek.frame.base.dto.DictionnaireDetailDTO">
		SELECT
		  A.pid as pid,
		  MAX(CONVERT(A.dcode, UNSIGNED)) AS dcode
		FROM
			sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
			A.pid = #{pId}
		AND A.deleted = 0
		AND B.deleted = 0

	</select>

	<select id="findByValue" parameterType="java.lang.String" resultType="com.huatek.frame.base.vo.DictionnaireDetailVO">
		SELECT
		  *
		FROM
			sys_dictionnaire_detail
		WHERE
			pid = #{pId}
		AND dname = #{value}
		AND deleted = 0
		limit 1
	</select>

	<select id="findByCode" parameterType="java.lang.String" resultType="com.huatek.frame.base.vo.DictionnaireDetailVO">
		SELECT
			A.dcode,
			A.dname
		FROM
			sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
			A.deleted = 0
		AND A.deleted = 0
		AND CODE = #{code}
	</select>

	<select id="findUnit" parameterType="java.lang.String" resultType="java.lang.String">
		SELECT
			A.dname
		FROM
			sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
			A.deleted = 0
		AND A.deleted = 0
		AND CODE = #{code}
		AND A.dcode = #{dCode}
	</select>

	<select id="findByLikeDName" parameterType="java.lang.String" resultType="com.huatek.frame.base.vo.DictionnaireDetailVO">
		SELECT
			A.dcode AS dcode,
			A.dname AS dname
		FROM
			sys_dictionnaire_detail A
		LEFT JOIN sys_dictionnaire B ON A.pid = B.id
		WHERE
			A.dname like concat('%',#{dName},'%')
		AND B.`code` = #{dictKey}
		AND A.deleted = 0
		AND B.deleted = 0
	</select>

	<insert id="insert" parameterType="com.huatek.frame.base.entity.DictionnaireDetail">
		insert into sys_dictionnaire_detail
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				id,
			</if>
			<if test="pid != null">
				pid,
			</if>
			<if test="deleted != null">
				deleted,
			</if>
			<if test="dcode != null">
				dcode,
			</if>
			<if test="dname != null">
				dname
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id, jdbcType=VARCHAR},
			</if>
			<if test="pid != null">
				#{pid, jdbcType=VARCHAR},
			</if>
			<if test="deleted != null">
				#{deleted, jdbcType=VARCHAR},
			</if>
			<if test="dcode != null">
				#{dcode, jdbcType=VARCHAR},
			</if>
			<if test="dname != null">
				#{dname, jdbcType=VARCHAR}
			</if>
		</trim>
	</insert>
</mapper>
