<?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.sysparameter.dao.SysParameterDao">
    
	<sql id="sysParameterColumns">
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.branch_office AS "branchOffice",
		a.type AS "type",
		a.name AS "name",
		a.parameter AS "parameter",
		a.value_type AS "valueType",
		a.is_force AS "isForce"

	</sql>
	
	<sql id="sysParameterJoins">

	</sql>
	
    
	<select id="get" resultType="SysParameter" >
		SELECT 
			<include refid="sysParameterColumns"/>
		FROM sys_parameter a
		<include refid="sysParameterJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findListisAdmin" resultType="SysParameter" >
		SELECT
		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.office_id AS "officeId",
		a.company_id AS "companyId",
		a.branch_office AS "branchOffice",
		a.type AS "type",
		a.name AS "name",
		a.parameter AS "parameter",
		a.value_type AS "valueType",
		a.is_force AS "isForce",
		ifnull(s.VALUE, a.parameter_value) as "parameterValue"
		FROM sys_parameter a LEFT JOIN (SELECT s.parameter_id,s.VALUE FROM sys_parameter_value s
		JOIN sys_parameter a ON s.parameter_id = a.id
		WHERE s.falt='1'
		) s ON a.id = s.parameter_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<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="parameter != null and parameter != ''">
				AND a.parameter LIKE
				<if test="dbName == 'oracle'">'%'||#{parameter}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{parameter}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{parameter},'%')</if>
			</if>
			<if test="parameterValue != null and parameterValue != ''">
				AND a.parameter_value = #{parameterValue}
			</if>
			<if test="valueType != null and valueType != ''">
				AND a.value_type = #{valueType}
			</if>
			<if test="isForce != null and isForce != ''">
				AND a.is_force = #{isForce}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findList" resultType="SysParameter" >
		SELECT
		<include refid="sysParameterColumns"/>
		from sys_parameter a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<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="parameter != null and parameter != ''">
				AND a.parameter LIKE
				<if test="dbName == 'oracle'">'%'||#{parameter}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{parameter}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{parameter},'%')</if>
			</if>
			<if test="parameterValue != null and parameterValue != ''">
				AND a.parameter_value = #{parameterValue}
			</if>
			<if test="valueType != null and valueType != ''">
				AND a.value_type = #{valueType}
			</if>
			<if test="isForce != null and isForce != ''">
				AND a.is_force = #{isForce}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findAllList" resultType="SysParameter" >
		SELECT 
			<include refid="sysParameterColumns"/>
		FROM sys_parameter a
		<include refid="sysParameterJoins"/>
		<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.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO sys_parameter(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			branch_office,
			type,
			name,
			parameter,
			parameter_value,
			value_type,
			is_force
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{branchOffice},
			#{type},
			#{name},
			#{parameter},
			#{parameterValue},
			#{valueType},
			#{isForce}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_parameter SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			branch_office = #{branchOffice},
			type = #{type},
			name = #{name},
			parameter = #{parameter},
			parameter_value = #{parameterValue},
			value_type = #{valueType},
			is_force = #{isForce}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_parameter
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_parameter SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysParameter" statementType="STATEMENT">
		select * FROM sys_parameter  where ${propertyName} = '${value}'
	</select>



	<select id="findByParameter" resultType="SysParameter" >
		SELECT
		a.value as "parameterValue"
		FROM sys_parameter_value a
		WHERE a.parameter_id=#{parameterId} and a.falt = '1' and del_flag = '0'
	</select>
	<select id="findByParameterFalt" resultType="SysParameter" >
		SELECT
		a.value as "parameterValue"
		FROM sys_parameter_value a
		WHERE a.parameter_id=#{parameterId} and a.branch_office = #{branchOffice} and falt='0' and del_flag = '0'
	</select>

	<select id="findIsforce" resultType="SysParameter" parameterType="java.lang.String">
		SELECT
		<include refid="sysParameterColumns"/>
		FROM sys_parameter a
		<include refid="sysParameterJoins"/>
		WHERE a.parameter=#{parameter} and a.del_flag = '0'
	</select>
</mapper>