<?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.WorkHandBillDetailDao">
    
	<sql id="workHandBillDetailColumns">
		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.material_number AS "materialNumber",
		a.material_name AS "materialName",
		a.material_flag AS "materialFlag",
		a.material_pages AS "materialPages",
		a.bill_id AS "billId"
	</sql>
	
	<sql id="workHandBillDetailJoins">
	</sql>
	
    
	<select id="get" resultType="WorkHandBillDetail" >
		SELECT 
			<include refid="workHandBillDetailColumns"/>
		FROM work_hand_bill_detail a
		<include refid="workHandBillDetailJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkHandBillDetail" >
		SELECT 
			<include refid="workHandBillDetailColumns"/>
		FROM work_hand_bill_detail a
		<include refid="workHandBillDetailJoins"/>
		<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="WorkHandBillDetail" >
		SELECT 
			<include refid="workHandBillDetailColumns"/>
		FROM work_hand_bill_detail a
		<include refid="workHandBillDetailJoins"/>
		<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_detail(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			material_number,
			material_name,
			material_flag,
			material_pages,
			bill_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{materialNumber},
			#{materialName},
			#{materialFlag},
			#{materialPages},
			#{billId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_hand_bill_detail SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			material_name = #{materialName},
			material_flag = #{materialFlag},
			material_pages = #{materialPages},
			bill_id = #{billId},
			material_number = #{materialNumber}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_hand_bill_detail
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_hand_bill_detail SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkHandBillDetail" statementType="STATEMENT">
		select * FROM work_hand_bill_detail  where ${propertyName} = '${value}'
	</select>


	<!-- 依据交接单查询资料列表 -->
	<select id="findListByBillId" resultType="WorkHandBillDetail" >
		SELECT
		<include refid="workHandBillDetailColumns"/>
		FROM work_hand_bill_detail a
		<include refid="workHandBillDetailJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and a.bill_id = #{billId}
		</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="deleteByBillId">
		DELETE FROM work_hand_bill_detail
		WHERE bill_id = #{billId}
	</delete>
</mapper>