<?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.workeffectdetail.dao.WorkEffectDetailDao">
    
	<sql id="workEffectDetailColumns">
		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.effect_id AS "effectId",
		a.no AS "no",
		a.name AS "name",
		a.achives_id AS "workStaffBasicInfo.id",
		a.job_grade AS "jobGrade",
		sa.no AS "workStaffBasicInfo.no",
		sa.name AS "workStaffBasicInfo.name",
		jg.name AS "workStaffBasicInfo.jobGrade.name",
		a.money AS "money"
	</sql>
	
	<sql id="workEffectDetailJoins">
		LEFT JOIN work_staff_achives sa on sa.id = a.achives_id
		LEFT JOIN work_job_grade jg ON jg.id = sa.job_grade
	</sql>
	
    
	<select id="get" resultType="WorkEffectDetail" >
		SELECT 
			<include refid="workEffectDetailColumns"/>
		FROM work_effect_detail a
		<include refid="workEffectDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkEffectDetail" >
		SELECT 
			<include refid="workEffectDetailColumns"/>
		FROM work_effect_detail a
		<include refid="workEffectDetailJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="effectId != null and effectId != ''">
				AND a.effect_id = #{effectId}
			</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="WorkEffectDetail" >
		SELECT 
			<include refid="workEffectDetailColumns"/>
		FROM work_effect_detail a
		<include refid="workEffectDetailJoins"/>
		<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_effect_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			effect_id,
			no,
			name,
			achives_id,
			job_grade,
			money
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{effectId},
			#{no},
			#{name},
			#{workStaffBasicInfo.id},
			#{jobGrade},
			#{money}
		)
	</insert>
	
	<update id="update">
		UPDATE work_effect_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			effect_id = #{effectId},
			no = #{no},
			name = #{name},
			achives_id = #{workStaffBasicInfo.id},
			job_grade = #{jobGrade},
			money = #{money}
		WHERE id = #{id}
	</update>

	<update id="updateMoney">
		UPDATE work_effect_detail SET
		money = #{money}
		WHERE id = #{id}
	</update>
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_effect_detail
		WHERE id = #{id}
	</update>

	<update id="deleteByEffectId">
		DELETE FROM work_effect_detail
		WHERE effect_id = #{effectId}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_effect_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>

	<select id="effectCount" resultType="java.lang.Integer" >
		SELECT
		count(*)
		FROM work_effect_detail a
		WHERE a.effect_id = #{effectId} AND a.achives_id = #{workStaffBasicInfo.id}
	</select>
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkEffectDetail" statementType="STATEMENT">
		select * FROM work_effect_detail  where ${propertyName} = '${value}'
	</select>
	
</mapper>