<?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.workeffect.dao.WorkEffectDao">
    
	<sql id="workEffectColumns">
		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.month AS "month",
		a.company_id AS "companyId",
		a.office_id AS "office.id",
		o.name AS "office.name",
		a.office_num AS "officeNum",
		a.effect_num AS "effectNum",
		a.effect_money AS "effectMoney",
		su.name AS "createBy.name",
		a.status AS "status"
	</sql>
	
	<sql id="workEffectJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		left JOIN sys_user su on su.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkEffect" >
		SELECT 
			<include refid="workEffectColumns"/>
		FROM work_effect a
		<include refid="workEffectJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkEffect" >
		SELECT 
			<include refid="workEffectColumns"/>
		FROM work_effect a
		<include refid="workEffectJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="status != null and status != ''">
				AND a.status = #{status}
			</if>
			<if test="month != null and month != ''">
				AND a.month = #{month}
			</if>
			<if test="office != null and office.id !=null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			${sqlMap.dsf}
		</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="WorkEffect" >
		SELECT 
			<include refid="workEffectColumns"/>
		FROM work_effect a
		<include refid="workEffectJoins"/>
		<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(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			month,
			company_id,
			office_id,
			office_num,
			effect_num,
			effect_money,
			status
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{month},
			#{companyId},
			#{office.id},
			#{officeNum},
			#{effectNum},
			#{effectMoney},
			#{status}
		)
	</insert>
	
	<update id="update">
		UPDATE work_effect SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			month = #{month},
			company_id = #{companyId},
			office_id = #{office.id},
			office_num = #{officeNum},
			effect_num = #{effectNum},
			effect_money = #{effectMoney},
			status = #{status}
		WHERE id = #{id}
	</update>
	<update id="updateEffectNum">
		UPDATE work_effect SET
		effect_num = #{effectNum}
		WHERE id = #{id}
	</update>
	<update id="updateMoney">
		UPDATE work_effect SET
		effect_money = #{effectMoney}
		WHERE id = #{id}
	</update>

	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_effect
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_effect SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkEffect" statementType="STATEMENT">
		select * FROM work_effect  where ${propertyName} = '${value}'
	</select>
	
</mapper>