<?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.buydetails.dao.BuyDetailsDao">
    
	<sql id="buyDetailsColumns">
		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.oa_buyid AS "oaBuy.id",
		a.name AS "name",
		a.format AS "format",
		a.amount AS "amount",
		a.unit AS "unit",
		a.price AS "price"
	</sql>
	
	<sql id="buyDetailsJoins">
		LEFT JOIN oa_buy oaBuy ON oaBuy.id = a.oa_buyid
	</sql>
	
    
	<select id="get" resultType="BuyDetails" >
		SELECT 
			<include refid="buyDetailsColumns"/>
		FROM buy_details a
		<include refid="buyDetailsJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="BuyDetails" >
		SELECT 
			<include refid="buyDetailsColumns"/>
		FROM buy_details a
		<include refid="buyDetailsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="oaBuy !=null and oaBuy.id!= null and oaBuy.id!= ''">
				AND a.oa_buyid = #{oaBuy.id}
			</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="BuyDetails" >
		SELECT 
			<include refid="buyDetailsColumns"/>
		FROM buy_details a
		<include refid="buyDetailsJoins"/>
		<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 buy_details(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			oa_buyid,
			name,
			format,
			amount,
			unit,
			price
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{oaBuy.id},
			#{name},
			#{format},
			#{amount},
			#{unit},
			#{price}
		)
	</insert>
	
	<update id="update">
		UPDATE buy_details SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			oa_buyid = #{oaBuy.id},
			name = #{name},
			format = #{format},
			amount = #{amount},
			unit = #{unit},
			price = #{price}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM buy_details
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE buy_details SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="BuyDetails" statementType="STATEMENT">
		select * FROM buy_details  where ${propertyName} = '${value}'
	</select>
	
	<select id="findListByoaBuy" resultType="OaBuy">
		SELECT 
			*
		FROM oa_buy 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>