<?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.syswarning.dao.SysWarningDao">
    
	<sql id="sysWarningColumns">
		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.warning_type AS "warningType",
		a.name AS "name",
		a.frequency AS "frequency",
		a.warning_date AS "warningDate",
		a.reminding AS "reminding",
		a.scope AS "scope",
		a.state AS "state",
		a.comment AS "comment",
		a.rules AS "rules",
		a.office_id AS "officeId",
		a.company_id AS "companyId",
		a.branch_office AS "branchOffice",
		o.name AS "scopeName",
		a.sql_string AS "sqlString",
		a.modular_id as "modularId"

	</sql>
	
	<sql id="sysWarningJoins">
		left join sys_office o on o.id=a.scope
	</sql>
	
    
	<select id="get" resultType="SysWarning" >
		SELECT 
			<include refid="sysWarningColumns"/>
		FROM sys_warning a
		<include refid="sysWarningJoins"/>
		WHERE a.id = #{id}
	</select>


	<select id="findList" resultType="SysWarning" >
		SELECT 
			<include refid="sysWarningColumns"/>
		FROM sys_warning a
		<include refid="sysWarningJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="warningType != null and warningType != ''">
				AND a.warning_type = #{warningType}
			</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="state != null and state != ''">
				AND a.state = #{state}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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="SysWarning" >
		SELECT 
			<include refid="sysWarningColumns"/>
		FROM sys_warning a
		<include refid="sysWarningJoins"/>
		<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_warning(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			warning_type,
			name,
			frequency,
			warning_date,
			reminding,
			scope,
			state,
			comment,
			rules,
			office_id,
			company_id,
			branch_office,
			sql_string,
			modular_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{warningType},
			#{name},
			#{frequency},
			#{warningDate},
			#{reminding},
			#{scope},
			#{state},
			#{comment},
			#{rules},
			#{officeId},
			#{companyId},
			#{branchOffice},
			#{sqlString},
			#{modularId}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_warning SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			warning_type = #{warningType},
			name = #{name},
			frequency = #{frequency},
			warning_date = #{warningDate},
			reminding = #{reminding},
			scope = #{scope},
			state = #{state},
			comment = #{comment},
			rules = #{rules},
			office_id = #{officeId},
			company_id = #{companyId},
			branch_office = #{branchOffice},
			sql_string=#{sqlString},
			modular_id=#{modularId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_warning
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_warning SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysWarning" statementType="STATEMENT">
		select * FROM sys_warning  where ${propertyName} = '${value}'
	</select>

	<select id="getSqlString" resultType="java.lang.Integer" parameterType="java.lang.String">
		${sqlString}
	</select>

    <select id="find" resultType="SysWarning" >
        SELECT
        <include refid="sysWarningColumns"/>
        FROM sys_warning a
        <include refid="sysWarningJoins"/>
        WHERE a.state='1' and a.del_flag='0'
    </select>


    <update id="removeFlat">
		UPDATE sys_warning SET
			state = #{state}
		WHERE id = #{id}
	</update>
</mapper>