<?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.SysParameterValueDao">
    
	<sql id="sysParameterValueColumns">
		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.parameter_id AS "parameterId",
		a.falt AS "falt",
		a.value as "value"
	</sql>
	
	<sql id="sysParameterValueJoins">
	</sql>
	
    
	<select id="get" resultType="SysParameterValue" >
		SELECT 
			<include refid="sysParameterValueColumns"/>
		FROM sys_parameter_value a
		<include refid="sysParameterValueJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="SysParameterValue" >
		SELECT 
			<include refid="sysParameterValueColumns"/>
		FROM sys_parameter_value a
		<include refid="sysParameterValueJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="branchOffice != null and branchOffice != ''">
				AND a.branch_office = #{branchOffice}
			</if>
			<if test="parameterId != null and parameterId != ''">
				AND a.parameter_id = #{parameterId}
			</if>
			<if test="falt != null and falt != ''">
				AND a.falt = #{falt}
			</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="SysParameterValue" >
		SELECT 
			<include refid="sysParameterValueColumns"/>
		FROM sys_parameter_value a
		<include refid="sysParameterValueJoins"/>
		<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_value(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			office_id,
			company_id,
			branch_office,
			parameter_id,
			falt,
			value
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{officeId},
			#{companyId},
			#{branchOffice},
			#{parameterId},
			#{falt},
			#{value}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_parameter_value SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			office_id = #{officeId},
			company_id = #{companyId},
			branch_office = #{branchOffice},
			parameter_id = #{parameterId},
			falt = #{falt},
			value=#{value}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_parameter_value
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_parameter_value SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysParameterValue" statementType="STATEMENT">
		select * FROM sys_parameter_value  where ${propertyName} = '${value}'
	</select>
	<select id="findByParameter" resultType="SysParameterValue" >
		SELECT
		<include refid="sysParameterValueColumns"/>
		FROM sys_parameter_value a
		<include refid="sysParameterValueJoins"/>
		WHERE a.parameter_id=#{parameterId} and a.falt = '1' and del_flag = '0'
	</select>
	<select id="findByParameterFalt" resultType="SysParameterValue" >
		SELECT
		<include refid="sysParameterValueColumns"/>
		FROM sys_parameter_value a
		<include refid="sysParameterValueJoins"/>
		WHERE a.parameter_id=#{parameterId} and a.branch_office = #{branchOffice} and falt='0' and del_flag = '0'
	</select>

	<select id="findSysValue" resultType="SysParameterValue" >
		SELECT
	    a.*
		FROM sys_parameter_value a
		WHERE a.parameter_id=(select s.id from sys_parameter s where s.parameter=#{parameter})
	    and a.falt = '1' and del_flag = '0'
	</select>
	<select id="findValueByParameter" resultType="SysParameterValue" >
		SELECT
		a.*
		FROM sys_parameter_value a
		WHERE a.parameter_id=(select s.id from sys_parameter s where s.parameter=#{parameter})
		 and a.branch_office = #{branchOffice}
		 and falt='0'
		 and del_flag = '0'
	</select>
</mapper>