<?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.workstaffachiveslog.dao.WorkStaffAchivesLogDao">
    
	<sql id="workStaffAchivesLogColumns">
		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.staff_id AS "staffId",
		a.module AS "module",
		a.describes AS "describes",
		a.fields AS "fields",
		a.old_value AS "oldValue",
		a.new_value AS "newValue",
		a.applyer AS "applyer",
		a.apply_date AS "applyDate",
		a.old_key AS "oldKey",
		a.new_key AS "newKey",
		a.son_id AS "sonId",
		a.type AS "type",
		a.state as "state"
	</sql>
	
	<sql id="workStaffAchivesLogJoins">
	</sql>
	
    
	<select id="get" resultType="WorkStaffAchivesLog" >
		SELECT 
			<include refid="workStaffAchivesLogColumns"/>
		FROM work_staff_achives_log a
		<include refid="workStaffAchivesLogJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkStaffAchivesLog" >
		SELECT 
			<include refid="workStaffAchivesLogColumns"/>
		FROM work_staff_achives_log a
		<include refid="workStaffAchivesLogJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="staffId != null and staffId != ''">
				AND a.staff_id = #{staffId}
			</if>
			<if test="module != null and module != ''">
				AND a.module = #{module}
			</if>
			<if test="describes != null and describes != ''">
				AND a.describes = #{describes}
			</if>
			<if test="fields != null and fields != ''">
				AND a.fields = #{fields}
			</if>
			<if test="oldValue != null and oldValue != ''">
				AND a.old_value = #{oldValue}
			</if>
			<if test="newValue != null and newValue != ''">
				AND a.new_value = #{newValue}
			</if>
			<if test="applyer != null and applyer != ''">
				AND a.applyer = #{applyer}
			</if>
			<if test="applyDate != null and applyDate != ''">
				AND a.apply_date = #{applyDate}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="sonId != null and sonId != ''">
				AND a.son_id = #{sonId}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</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="WorkStaffAchivesLog" >
		SELECT 
			<include refid="workStaffAchivesLogColumns"/>
		FROM work_staff_achives_log a
		<include refid="workStaffAchivesLogJoins"/>
		<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 work_staff_achives_log(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			staff_id,
			module,
			describes,
			fields,
			old_value,
			new_value,
			applyer,
			apply_date,
			old_key,
			new_key,
			son_id,
			type,
			state
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{staffId},
			#{module},
			#{describes},
			#{fields},
			#{oldValue},
			#{newValue},
			#{applyer},
			#{applyDate},
			#{oldKey},
			#{newKey},
			#{sonId},
			#{type},
			#{state}
		)
	</insert>
	
	<update id="update">
		UPDATE work_staff_achives_log SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			staff_id = #{staffId},
			module = #{module},
			describes = #{describes},
			fields = #{fields},
			old_value = #{oldValue},
			new_value = #{newValue},
			applyer = #{applyer},
			apply_date = #{applyDate},
			old_key = #{oldKey},
			new_key = #{newKey},
			son_id = #{sonId},
			type = #{type},
			state = #{state}
		WHERE id = #{id}
	</update>

	<update id="updateState">
		UPDATE work_staff_achives_log SET
			applyer = #{applyer},
			apply_date = #{applyDate},
			state = '2'
		WHERE staff_id = #{staffId} and state='1'
	</update>
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_staff_achives_log
		WHERE id = #{id}
	</update>
	<update id="deleteBack">
		DELETE FROM work_staff_achives_log
		WHERE staff_id = #{staffId} and state='1'
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_staff_achives_log SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkStaffAchivesLog" statementType="STATEMENT">
		select * FROM work_staff_achives_log  where ${propertyName} = '${value}'
	</select>

	<select id="findSonId" resultType="WorkStaffAchivesLog" >
		SELECT
		<include refid="workStaffAchivesLogColumns"/>
		FROM work_staff_achives_log a
		<include refid="workStaffAchivesLogJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="staffId != null and staffId != ''">
				AND a.staff_id = #{staffId}
			</if>
			<if test="module != null and module != ''">
				AND a.module = #{module}
			</if>
			<if test="describes != null and describes != ''">
				AND a.describes = #{describes}
			</if>
			<if test="fields != null and fields != ''">
				AND a.fields = #{fields}
			</if>
			<if test="oldValue != null and oldValue != ''">
				AND a.old_value = #{oldValue}
			</if>
			<if test="newValue != null and newValue != ''">
				AND a.new_value = #{newValue}
			</if>
			<if test="applyer != null and applyer != ''">
				AND a.applyer = #{applyer}
			</if>
			<if test="applyDate != null and applyDate != ''">
				AND a.apply_date = #{applyDate}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="sonId != null and sonId != ''">
				AND a.son_id = #{sonId}
			</if>
			<if test="state != null and state != ''">
				AND a.state = #{state}
			</if>
		</where>
		group by a.son_id
	</select>
</mapper>