<?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.workdevicerecord.dao.WorkDeviceRecordDao">
    
	<sql id="workDeviceRecordColumns">
		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.device_name AS "deviceName",
		a.device_kind AS "deviceKind",
		a.device_unit AS "deviceUnit",
		a.device_creator AS "deviceCreator",
		a.device_phone AS "devicePhone",
		a.device_price AS "devicePrice",
		a.device_xj_id AS "deviceXjId"
 	</sql>
	
	<sql id="workDeviceRecordJoins">
	</sql>
	
    
	<select id="get" resultType="WorkDeviceRecord" >
		SELECT 
			<include refid="workDeviceRecordColumns"/>
		FROM work_device_record a
		<include refid="workDeviceRecordJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkDeviceRecord" >
		SELECT 
			<include refid="workDeviceRecordColumns"/>
		FROM work_device_record a
		<include refid="workDeviceRecordJoins"/>
		<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="WorkDeviceRecord" >
		SELECT 
			<include refid="workDeviceRecordColumns"/>
		FROM work_device_record a
		<include refid="workDeviceRecordJoins"/>
		<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_device_record(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			device_name,
			device_kind,
			device_unit,
			device_creator,
			device_phone,
			device_price,
			device_xj_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{deviceName},
			#{deviceKind},
			#{deviceUnit},
			#{deviceCreator},
			#{devicePhone},
			#{devicePrice},
			#{deviceXjId}
		)
	</insert>
	
	<update id="update">
		UPDATE work_device_record SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			device_name = #{deviceName},
			device_kind = #{deviceKind},
			device_unit = #{deviceUnit},
			device_creator = #{deviceCreator},
			device_phone = #{devicePhone},
			device_price = #{devicePrice},
			device_xj_id= #{deviceXjId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_device_record
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_device_record SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkDeviceRecord" statementType="STATEMENT">
		select * FROM work_device_record  where ${propertyName} = '${value}'
	</select>

	<!-- 依据主表id获取从表的列表数据 -->
	<select id="findListByXjId" resultType="WorkDeviceRecord" >
		SELECT
		<include refid="workDeviceRecordColumns"/>
		FROM work_device_record a
		<include refid="workDeviceRecordJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			and a.device_xj_id = #{deviceXjId}
		</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_device_record
		WHERE device_xj_id = #{deviceXjId}
	</delete>
</mapper>