<?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.workperformance.dao.WorkPerformanceDao">
    
	<sql id="workPerformanceColumns">
		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.company_id AS "companyId",
		a.office_id AS "officeId",
		a.num AS "num",
		a.name AS "name",
		a.client AS "client",
		a.content AS "content",
		a.type AS "type",
		a.service_date AS "serviceDate",
		a.contract_price AS "contractPrice",
		a.investment_amount AS "investmentAmount",
		a.scale_quantity AS "scaleQuantity",
		a.linkman AS "linkman",
		a.contract_source AS "contractSource",
		a.office_name AS "officeName",
		a.project_leaders AS "projectLeaders",
		a.record AS "record",
		a.source AS "source"
	</sql>
	
	<sql id="workPerformanceJoins">
		LEFT JOIN sys_office s ON s.id = a.company_id
        LEFT JOIN sys_office o ON o.id = a.office_id
	</sql>
	
    
	<select id="get" resultType="WorkPerformance" >
		SELECT 
			<include refid="workPerformanceColumns"/>
		FROM work_performance a
		<include refid="workPerformanceJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkPerformance" >
		SELECT 
			<include refid="workPerformanceColumns"/>
		FROM work_performance a
		<include refid="workPerformanceJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="client != null and client != ''">
				AND a.client LIKE
				<if test="dbName == 'oracle'">'%'||#{client}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{client}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{client},'%')</if>
			</if>
			<if test="sqlMap.dsf !=null and sqlMap.dsf!=''">
				AND ((s.id = #{currentUser.company.id} or find_in_set( #{currentUser.company.id}, s.parent_ids ) )${sqlMap.dsf} )
			</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="WorkPerformance" >
		SELECT 
			<include refid="workPerformanceColumns"/>
		FROM work_performance a
		<include refid="workPerformanceJoins"/>
		<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_performance(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			company_id,
			office_id,
			num,
			name,
			client,
			content,
			type,
			service_date,
			contract_price,
			investment_amount,
			scale_quantity,
			linkman,
			contract_source,
			office_name,
			project_leaders,
			record,
			source
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{companyId},
			#{officeId},
			#{num},
			#{name},
			#{client},
			#{content},
			#{type},
			#{serviceDate},
			#{contractPrice},
			#{investmentAmount},
			#{scaleQuantity},
			#{linkman},
			#{contractSource},
			#{officeName},
			#{projectLeaders},
			#{record},
			#{source}
		)
	</insert>
	
	<update id="update">
		UPDATE work_performance SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			company_id = #{companyId},
			office_id = #{officeId},
			num = #{num},
			name = #{name},
			client = #{client},
			content = #{content},
			type = #{type},
			service_date = #{serviceDate},
			contract_price = #{contractPrice},
			investment_amount = #{investmentAmount},
			scale_quantity = #{scaleQuantity},
			linkman = #{linkman},
			contract_source = #{contractSource},
			office_name = #{officeName},
			project_leaders = #{projectLeaders},
			record = #{record},
			source = #{source}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_performance
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_performance SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkPerformance" statementType="STATEMENT">
		select * FROM work_performance  where ${propertyName} = '${value}'
	</select>
	
</mapper>