<?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.extension.mapper.FormDefinitionMapper">

	<sql id="formDefinitionColumns">
		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.category_id AS "category.id",
		a.name AS "name",
		b.name AS "category.name"
	</sql>

	<sql id="formDefinitionJoins">
			LEFT JOIN act_extension_form_category b ON b.id = a.category_id
	</sql>


	<select id="get" resultType="FormDefinition" >
		SELECT
			<include refid="formDefinitionColumns"/>
		FROM act_extension_form_definition a
		<include refid="formDefinitionJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="FormDefinition" >
		SELECT
			<include refid="formDefinitionColumns"/>,
			c.id AS "formDefinitionJson.id",
			c.json AS "formDefinitionJson.json",
			c.version AS "formDefinitionJson.version",
			c.status AS "formDefinitionJson.status",
			c.is_primary AS "formDefinitionJson.isPrimary"
		FROM act_extension_form_definition a
		<include refid="formDefinitionJoins"/>
		LEFT JOIN act_extension_form_definition_json c ON c.form_definition_id = a.id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
                    <if test="category != null  and category.id != null and category.id != ''">
                        AND (b.id = #{category.id} OR b.parent_ids LIKE
                        <if test="_databaseId == 'postgre'">'%,'||#{category.id}||',%')</if>
                        <if test="_databaseId == 'oracle'">'%,'||#{category.id}||',%')</if>
                        <if test="_databaseId == 'mysql'">CONCAT('%,', #{category.id}, ',%'))</if>
                        <if test="_databaseId == 'mssql'">'%'+#{category.id}+'%')</if>
                    </if>
		   AND ( c.is_primary = '1' or c.is_primary is null)
			<if test="name != null and name != ''">
				AND a.name = #{name}
			</if>
			<if test="formDefinitionJson != null and formDefinitionJson.status != null and formDefinitionJson.status != ''">
				AND c.status = #{formDefinitionJson.status}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="FormDefinition" >
		SELECT
			<include refid="formDefinitionColumns"/>
		FROM act_extension_form_definition a
		<include refid="formDefinitionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>

	<insert id="insert"  parameterType="FormDefinition" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
		INSERT INTO act_extension_form_definition(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			category_id,
			name
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{category.id},
			#{name}
		)
	</insert>

	<update id="update">
		UPDATE act_extension_form_definition SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			category_id = #{category.id},
			name = #{name}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
		DELETE FROM act_extension_form_definition
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE category_id = #{category.id}
			</otherwise>
		</choose>
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE act_extension_form_definition SET
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE category_id = #{category.id}
			</otherwise>
		</choose>
	</update>


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

</mapper>
