<?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.workexample.dao.WorkExampleIndicesDao">
    
	<sql id="workExampleIndicesColumns">
		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.del_flag AS "delFlag",
		a.parent_id AS "parent.id",
		a.name AS "name",
		a.parent_ids AS "parentIds",
		a.order_num AS "orderNum",
		a.company_id AS "companyId",
		a.is_disable AS "isDisable",
		a.one_items AS "oneItems",
		a.item_unit AS "itemUnit",
		a.type AS "type",
		a.decimal_count AS "decimalCount"
	</sql>
	
	<sql id="workExampleIndicesJoins">
	</sql>
	
    
	<select id="get" resultType="WorkExampleIndices" >
		SELECT 
			<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		WHERE a.id = #{id}
	</select>
	<select id="getOrderNum" resultType="java.lang.Integer" >
		SELECT
			max(a.order_num)
		FROM work_example_indices a
		WHERE a.parent_id = #{id}
	</select>
	<select id="exampleIndicesCount" resultType="java.lang.Integer" >
		SELECT
			count(*)
		FROM work_example_indices_relation a
		WHERE a.work_example_id = #{workExampleId} AND a.example_indices_id = #{exampleIndicesId}
	</select>

	<insert id="saveExampleIndices">
		INSERT INTO work_example_indices_relation(
		work_example_id,
		example_indices_id
		) VALUES (
		#{workExampleId},
		#{exampleIndicesId}
		)
	</insert>

	<select id="findList" resultType="WorkExampleIndices" >
		SELECT 
			<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="isDisable != null and isDisable != ''">
				AND a.is_disable = #{isDisable}
			</if>
			<if test="view != null and view != ''">
				AND a.type &lt; 7
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.order_num ASC
			</otherwise>
		</choose>
	</select>
	<select id="findByParentId" resultType="WorkExampleIndices" >
		SELECT
			<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = '0'
			AND find_in_set(#{id}, a.parent_ids)
			AND a.is_disable = '0'
			ORDER BY a.order_num ASC
		</where>
	</select>
	<select id="findByParentIds" resultType="WorkExampleIndices" >
		SELECT
		<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = '0'
			AND find_in_set(#{id}, a.parent_ids)
			AND a.is_disable = '0'
			<if test="name != null and name != ''">
				AND a.name LIKE
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
		</where>
	</select>
	<select id="findListInfo" resultType="WorkExampleIndices" >
		SELECT
			<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		LEFT JOIN work_example_indices_relation r ON a.id = r.example_indices_id
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL} AND a.type &lt; 7
			<if test="name != null and name != ''">
				AND a.name LIKE
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="isDisable != null and isDisable != ''">
				AND a.is_disable = #{isDisable}
			</if>
			<if test="workExample != null and workExample.id != null and workExample.id != ''">
				AND r.work_example_id = #{workExample.id}
			</if>

		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.order_num ASC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="WorkExampleIndices" >
		SELECT 
			<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.order_num ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_example_indices(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			parent_id,
			name,
			parent_ids,
			order_num,
			company_id,
			is_disable,
			one_items,
			item_unit,
			type,
			decimal_count
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{parent.id},
			#{name},
			#{parentIds},
			#{orderNum},
			#{companyId},
			#{isDisable},
			#{oneItems},
			#{itemUnit},
			#{type},
			#{decimalCount}
		)
	</insert>
	
	<update id="update">
		UPDATE work_example_indices SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			parent_id = #{parent.id},
			name = #{name},
			parent_ids = #{parentIds},
			order_num = #{orderNum},
			company_id = #{companyId},
			is_disable = #{isDisable},
			one_items = #{oneItems},
			item_unit = #{itemUnit},
			type = #{type},
			decimal_count = #{decimalCount}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_example_indices
		WHERE id = #{id}
	</update>

	<update id="deleteExampleIndices">
		DELETE FROM work_example_indices_relation
		WHERE work_example_id = #{workExampleId} and example_indices_id = #{exampleIndicesId}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_example_indices SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<!--逻辑删除-->
	<update id="updateStatus">
		UPDATE work_example_indices SET
			is_disable = #{isDisable}
		WHERE id = #{id} OR find_in_set(#{id}, parent_ids)
	</update>

	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkExampleIndices" statementType="STATEMENT">
		select * FROM work_example_indices  where ${propertyName} = '${value}'
	</select>

	<select id="findByName" resultType="WorkExampleIndices" >
		SELECT
		<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = '0'
			AND a.company_id=#{companyId}
			AND a.name=#{name}
		</where>
	</select>
	<select id="find" resultType="WorkExampleIndices" >
		SELECT
		<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = '0'
			AND a.company_id=#{companyId}
			AND a.name=#{name}
			AND a.parent_ids like concat('%',#{parent.id},'%')
			AND a.type in ('8','7')
			and a.is_disable='0'
		</where>
	</select>
	<select id="findExampleIndices" parameterType="java.lang.String" resultType="WorkExampleIndices" >
		SELECT
		<include refid="workExampleIndicesColumns"/>
		FROM work_example_indices a
		<include refid="workExampleIndicesJoins"/>
		<where>
			a.del_flag = '0'
			AND a.parent_ids like concat('%',#{parentId},'%')
			AND a.type in ('8','7')
			and a.is_disable='0'
			order by order_num ASC
		</where>
	</select>
</mapper>