<?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.sg.financial.erpcredit.mapper.ErpCreditMaterialMapper">
    
	<sql id="erpChild2Columns">
		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.erp_credit_id As "erpCredit.id",
		a.eqm_material_details As "eqmMaterialDetails2",
		a.mtl_booked_fee As "mtlBookedFee2",
		a.mtl_unit As "mtlUnit",
		a.mtl_numbers As "mtlNumbers",
		a.mtl_supplier_name As "mtlSupplierName"
	</sql>

	<select id="get" resultType="erpCreditMaterial" >
		SELECT 
			<include refid="erpChild2Columns"/>
		FROM js_erp_credit_material a
		WHERE a.erp_credit_id = #{erpCredit.id}
	</select>
	
	<select id="findList" resultType="erpCreditMaterial" >
		SELECT 
			<include refid="erpChild2Columns"/>
		FROM js_erp_credit_material a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="erpCredit != null and erpCredit.itemId != null and erpCredit.itemId != ''">
				AND a.erp_credit_id = #{erpCredit.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="erpCreditMaterial" >
		SELECT 
			<include refid="erpChild2Columns"/>
		FROM js_erp_credit_material a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO js_erp_credit_material(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			erp_credit_id,
			eqm_material_details,
			mtl_booked_fee,
			mtl_unit,
			mtl_numbers,
			mtl_supplier_name
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{erpCredit.id},
			#{eqmMaterialDetails2},
			#{mtlBookedFee2},
			#{mtlUnit},
			#{mtlNumbers},
			#{mtlSupplierName}
		)
	</insert>

	<insert id="insertList">
		insert INTO js_erp_credit_material(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			erp_credit_id,
			eqm_material_details,
			mtl_booked_fee,
			mtl_unit,
			mtl_numbers,
			mtl_supplier_name
		) VALUES
		<foreach collection="list" item="item" index="index" separator="," >
			(
				#{item.id},
				#{item.createBy.id},
				#{item.createDate},
				#{item.updateBy.id},
				#{item.updateDate},
				#{item.remarks},
				#{item.delFlag},
				#{item.erpCredit.id},
				#{item.eqmMaterialDetails2},
				#{item.mtlBookedFee2},
				#{item.mtlUnit},
				#{item.mtlNumbers},
				#{item.mtlSupplierName}
			)
		</foreach>
	</insert>

	<update id="update">
		UPDATE js_erp_credit_material SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			del_flag = #{delFlag},
			eqm_material_details = #{eqmMaterialDetails2},
			mtl_booked_fee = #{mtlBookedFee2},
			mtl_supplier_name=#{mtlSupplierName}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM js_erp_credit_material
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE erp_credit_id = #{erpCredit.id}
			</otherwise>
		</choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE js_erp_credit_material SET
			del_flag = #{DEL_FLAG_DELETE}
		<choose>
			<when test="id !=null and id != ''">
				WHERE id = #{id}
			</when>
			<otherwise>
				WHERE erp_credit_id = #{erpCredit.id}
			</otherwise>
		</choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="erpCreditMaterial" statementType="STATEMENT">
		select * FROM js_erp_credit_material  where ${propertyName} = '${value}'
	</select>
	<!--查找多个条件-->
	<select id="findErpCreditChild2List" parameterType="java.util.List" resultType="erpCreditMaterial">
		select
		<include refid="erpChild2Columns"/>
		FROM js_erp_credit_material a
		WHERE a.del_flag = '0'
		and a.erp_credit_id in
		<if test="list !=null">
			<foreach collection="list" index="index" item="item" open="("
					 separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>

	<delete id="deleteAll">
		delete
		FROM js_erp_credit_material
	</delete>
</mapper>