<?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.jeeplus.modules.sys.dao.MainDictDetailDao">
    
	<sql id="mainDictDetailColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.type_id AS "typeId",
		a.del_flag AS "delFlag",
		a.del_flag AS "delFlags",
		a.detail_key AS "detailKey",
		a.detail_name AS "detailName",
		a.detail_key AS "value",
		a.detail_name AS "label",
		a.parent_id AS "parentId",
		(select detail_name from main_dict_detail where id = a.parent_id) parentName,
		a.parent_ids AS "parentIds",
		a.parent_names AS "parentNames",
		a.end_flag AS "endFlag",
		a.company_id AS "companyId",
		a.sort AS "sort",
		b.id AS "company.id",
		b.name AS "company.name",
		c.id AS "mainDict.id",
		c.type_name AS "mainDict.typeName",
		c.type_name as "type",
		a.branch_office as "branchOffice",
		a.level as "level"
	</sql>
	
	<sql id="mainDictDetailJoins">
		LEFT JOIN sys_office b on a.company_id = b.id
		LEFT JOIN main_dict c on a.type_id = c.id
	</sql>
	
    
	<select id="getByDetailKey" resultType="MainDictDetail" >
		SELECT 
			<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		WHERE a.company_id = #{company.id} AND a.detail_key = #{detailKey}
	</select>
	<select id="get" resultType="MainDictDetail" >
		SELECT
			<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="MainDictDetail" >
		SELECT 
			<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		<where>
			<!--a.del_flag = #{DEL_FLAG_NORMAL}-->
			1=1 and a.type_id = #{typeId}
			<if test="company != null and company.id != '' and company.id != null">
				and a.company_id = #{company.id}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				and a.branch_office = #{branchOffice}
			</if>
			<if test="level != null and level != ''">
				and a.level = #{level}
			</if>

		</where>
		ORDER BY a.parent_ids ASC,a.sort*1 ASC
	</select>

	<select id="findAllList" resultType="MainDictDetail" >
		SELECT 
			<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="type != null and type != ''">
				and c.type_name = #{type}
			</if>
			<if test="company != null and company.id != '' and company.id != null">
				and a.company_id = #{company.id}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				and a.branch_office = #{branchOffice}
			</if>
			<if test="level != null and level != ''">
				and a.level = #{level}
			</if>
		</where>
		ORDER BY a.parent_ids ASC ,a.sort*1 ASC
	</select>
	<select id="findAllListView" resultType="MainDictDetail" >
		SELECT
		<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		<where>
			1=1
			<if test="type != null and type != ''">
				and c.type_name = #{type}
			</if>
			<if test="company != null and company.id != '' and company.id != null">
				and a.company_id = #{company.id}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				and a.branch_office = #{branchOffice}
			</if>
			<if test="level != null and level != ''">
				and a.level = #{level}
			</if>
		</where>
		ORDER BY a.sort*1 ASC

	</select>
	<insert id="insert">
		INSERT INTO main_dict_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			detail_key,
			detail_name,
			parent_id,
			parent_ids,
			parent_names,
			end_flag,
			company_id,
			sort,
			type_id,
			branch_office,
			level
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{detailKey},
			#{detailName},
			#{parentId},
			#{parentIds},
			#{parentNames},
			#{endFlag},
			#{company.id},
			#{sort},
			#{typeId},
			#{branchOffice},
			#{level}
		)
	</insert>
	
	<update id="update">
		UPDATE main_dict_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			detail_key = #{detailKey},
			detail_name = #{detailName},
			parent_id = #{parentId},
			parent_ids = #{parentIds},
			end_flag = #{endFlag},
			company_id = #{company.id},
			parent_names = #{parentNames},
			sort = #{sort},
			type_id=#{typeId},
			branch_office=#{branchOffice},
			level=#{level}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM main_dict_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE main_dict_detail SET 
			del_flag = #{delFlag}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="MainDictDetail" statementType="STATEMENT">
		select * FROM main_dict_detail  where ${propertyName} = '${value}'
	</select>

	<!-- 修改末级标志 -->
	<update id="upateEndFlag">
		UPDATE main_dict_detail SET
			end_flag = #{endFlag}
		WHERE id = #{id}
	</update>
	<!-- 获取当前节点是否有子节点 -->
	<select id="getChildNode" resultType="java.lang.Integer">
		select count(1) from main_dict_detail
		where parent_ids like concat('%',#{id},'%')
	</select>

	<!-- 依据类型id和键获取值 -->
	<select id="getNameByIdAndKey" resultType="MainDictDetail">
		select * from main_dict_detail where detail_key = #{detailKey} and type_id = #{typeId} and branch_office = #{branchOffice}
	</select>

	<select id="findListOther" resultType="MainDictDetail" >
		SELECT
		<include refid="mainDictDetailColumns"/>
		FROM main_dict_detail a
		<include refid="mainDictDetailJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			 and a.type_id = #{typeId}
			<if test="company != null and company.id != '' and company.id != null">
				and a.company_id = #{company.id}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				and a.branch_office = #{branchOffice}
			</if>
			<if test="level != null and level != ''">
				and a.level = #{level}
			</if>
		</where>
		ORDER BY a.parent_ids ASC ,a.sort*1 ASC

	</select>
	<select id="findSys" resultType="MainDictDetail" parameterType="java.lang.String">
		SELECT d.id as "id",
        d.`value` as "detailKey",
        d.label as "detailName",
        d.parent_id as "parentId",
        d.sort,
        d.description as  "remarks",
        (select m.id from main_dict m where m.type_name=#{typeName}  LIMIT 1) as "typeId"
 FROM sys_dict d where d.type=#{typeName}
	</select>
	<select id="getByAndKey" resultType="MainDictDetail">
		select * from main_dict_detail where detail_key = #{detailKey} and type_id = #{typeId} and branch_office = #{branchOffice}
	</select>

	<select id="getDetailKey" resultType="MainDictDetail" parameterType="java.lang.String">
		select * from main_dict_detail where detail_key = #{detailKey} and type_id = #{typeId} and id !=#{id} and branch_office = #{branchOffice}
	</select>

	<select id="getId" resultType="MainDictDetail" parameterType="java.lang.String">
		select * from main_dict_detail a LEFT JOIN main_dict c on a.type_id = c.id where c.type_name = #{typeName} and a.branch_office = #{companyId} and a.detail_name =#{name} LIMIT 1
	</select>



    <select id="findJedisAllList" resultType="MainDictDetail" >
        SELECT
		c.type_name as "type",
        a.detail_key AS "value",
        a.detail_name AS "label"
        FROM main_dict_detail a
		LEFT JOIN main_dict c on a.type_id = c.id
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="type != null and type != ''">
                and c.type_name = #{type}
            </if>
            <if test="company != null and company.id != '' and company.id != null">
                and a.company_id = #{company.id}
            </if>
            <if test="branchOffice != null and branchOffice != ''">
                and a.branch_office = #{branchOffice}
            </if>
            <if test="level != null and level != ''">
                and a.level = #{level}
            </if>
        </where>
        ORDER BY a.parent_ids ASC ,a.sort*1 ASC
    </select>
</mapper>