<?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.workaftermath.dao.WorkAftermathDao">
    
	<sql id="workAftermathColumns">
		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.project_id AS "workBidingProject.id",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		a.bid_amount AS "bidAmount",
		a.date_start AS "dateStart",
		a.date_end AS "dateEnd",
		a.project_master AS "projectMaster",
		a.agency_type AS "agencyType",
		a.pay_name AS "payName",
		a.invoice_number AS "invoiceNumber",
		a.invoice_date AS "invoiceDate",
		workBidingProject.id AS "workBidingProject.id"
	</sql>
	
	<sql id="workAftermathJoins">
		JOIN sys_office o ON o.id = a.office_id
		JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN work_biding_project workBidingProject ON workBidingProject.id = a.project_id
	</sql>
	
    
	<select id="get" resultType="WorkAftermath" >
		SELECT
			<include refid="workAftermathColumns"/>
		FROM work_aftermath a
		<include refid="workAftermathJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getBypid" resultType="WorkAftermath" >
		SELECT
		<include refid="workAftermathColumns"/>
		FROM work_aftermath a
		<include refid="workAftermathJoins"/>
		WHERE a.project_id = #{pid}
	</select>
	
	<select id="findList" resultType="WorkAftermath" >
		SELECT 
			<include refid="workAftermathColumns"/>
		FROM work_aftermath a
		<include refid="workAftermathJoins"/>
		<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>
	
	<select id="findAllList" resultType="WorkAftermath" >
		SELECT 
			<include refid="workAftermathColumns"/>
		FROM work_aftermath a
		<include refid="workAftermathJoins"/>
		<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_aftermath(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			project_id,
			company_id,
			office_id,
			bid_amount,
			date_start,
			date_end,
			project_master,
			agency_type,
			pay_name,
			invoice_number,
			invoice_date
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{workBidingProject.id},
			#{companyId},
			#{officeId},
			#{bidAmount},
			#{dateStart},
			#{dateEnd},
			#{projectMaster},
			#{agencyType},
			#{payName},
			#{invoiceNumber},
			#{invoiceDate}
		)
	</insert>
	
	<update id="update">
		UPDATE work_aftermath SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			project_id = #{workBidingProject.id},
			company_id = #{companyId},
			office_id = #{officeId},
			bid_amount = #{bidAmount},
			date_start = #{dateStart},
			date_end = #{dateEnd},
			project_master = #{projectMaster},
			agency_type = #{agencyType},
			pay_name = #{payName},
			invoice_number = #{invoiceNumber},
			invoice_date = #{invoiceDate}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_aftermath
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_aftermath SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkAftermath" statementType="STATEMENT">
		select * FROM work_aftermath  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByworkBidingProject" resultType="WorkBidingProject">
		SELECT 
			*
		FROM work_biding_peoject a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="id != null and id != ''">
				AND a.id LIKE 
				<if test="dbName == 'oracle'">#{id}||'%'</if>
				<if test="dbName == 'mssql'">#{id}+'%'</if>
				<if test="dbName == 'mysql'">concat(#{id},'%')</if>
			</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>
</mapper>