<?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.workhandbill.dao.WorkHandBillDao">
    
	<sql id="workHandBillColumns">
		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.bill_rec_user AS "billRecUser.id",
		billRecUser.name AS "billRecUser.name",
		a.bill_send_user AS "billSendUser.id",
		billSendUser.name AS "billSendUser.name",
		a.bill_hand_time AS "billHandTime",
		a.project_id AS "projectId"
	</sql>
	
	<sql id="workHandBillJoins">
		LEFT JOIN sys_user billRecUser ON billRecUser.id = a.bill_rec_user
		LEFT JOIN sys_user billSendUser ON billSendUser.id = a.bill_send_user
	</sql>
	
    
	<select id="get" resultType="WorkHandBill" >
		SELECT 
			<include refid="workHandBillColumns"/>
		FROM work_hand_bill a
		<include refid="workHandBillJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkHandBill" >
		SELECT 
			<include refid="workHandBillColumns"/>
		FROM work_hand_bill a
		<include refid="workHandBillJoins"/>
		<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="WorkHandBill" >
		SELECT 
			<include refid="workHandBillColumns"/>
		FROM work_hand_bill a
		<include refid="workHandBillJoins"/>
		<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_hand_bill(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			bill_rec_user,
			bill_send_user,
			bill_hand_time,
			project_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{billRecUser.id},
			#{billSendUser.id},
			#{billHandTime},
			#{projectId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_hand_bill SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			bill_rec_user = #{billRecUser.id},
			bill_send_user = #{billSendUser.id},
			bill_hand_time = #{billHandTime},
			project_id = #{projectId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete" >
		DELETE FROM work_hand_bill
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_hand_bill SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkHandBill" statementType="STATEMENT">
		select * FROM work_hand_bill  where ${propertyName} = '${value}'
	</select>

	<!-- 依据控制表格编号获取交接单编号 -->
	<select id="findListByProjectId" resultType="WorkHandBill" >
		SELECT
		<include refid="workHandBillColumns"/>
		FROM work_hand_bill a
		<include refid="workHandBillJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and a.project_id = #{projectId}
		</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>

	<!--物理删除-->
	<delete id="deleteByProId">
		DELETE FROM work_hand_bill
		WHERE project_id = #{projectId}
	</delete>
</mapper>