<?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.ym.mec.biz.dal.dao.GoodsProcurementDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.GoodsProcurement" id="GoodsProcurement">
		<result column="id_" property="id" />
		<result column="goods_id_" property="goodsId" />
		<result column="goods_category_id_" property="goodsCategoryId" />
		<result column="supply_channel_" property="supplyChannel" />
		<result column="discount_price_" property="discountPrice" />
		<result column="agree_cost_price_" property="agreeCostPrice" />
		<result column="stock_count_" property="stockCount" />
		<result column="tax_stock_count_" property="taxStockCount" />
		<result column="operator_id_" property="operatorId" />
		<result column="batch_no_" property="batchNo" />
		<result column="stock_sold_num_" property="stockSoldNum"/>
		<result column="tax_stock_sold_num_" property="taxStockSoldNum"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="GoodsProcurement" >
		SELECT * FROM goods_procurement WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="GoodsProcurement">
		SELECT * FROM goods_procurement ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.GoodsProcurement" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,stock_count_,tax_stock_count_,operator_id_,batch_no_,create_time_,update_time_)
		VALUES(#{goodsId},#{goodsCategoryId},#{supplyChannel},#{discountPrice},#{agreeCostPrice},#{stockCount},#{taxStockCount},#{operatorId},#{batchNo},NOW(),NOW())
	</insert>

	<insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.GoodsProcurement" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO goods_procurement (goods_id_,goods_category_id_,supply_channel_,discount_price_,agree_cost_price_,stock_count_,tax_stock_count_,operator_id_,batch_no_,create_time_,update_time_)
		VALUE
		<foreach collection="goodsProcurements" item="gp" separator=",">
			(#{gp.goodsId},#{gp.goodsCategoryId},#{gp.supplyChannel},#{gp.discountPrice},#{gp.agreeCostPrice},#{gp.stockCount},#{gp.taxStockCount},#{gp.operatorId},#{gp.batchNo},NOW(),NOW())
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.GoodsProcurement">
		UPDATE goods_procurement
		<set>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="agreeCostPrice != null">
				agree_cost_price_ = #{agreeCostPrice},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="goodsCategoryId != null">
				goods_category_id_ = #{goodsCategoryId},
			</if>
			<if test="taxStockCount != null">
				tax_stock_count_ = #{taxStockCount},
			</if>
			<if test="supplyChannel != null">
				supply_channel_ = #{supplyChannel},
			</if>
			<if test="discountPrice != null">
				discount_price_ = #{discountPrice},
			</if>
			<if test="stockCount != null">
				stock_count_ = #{stockCount},
			</if>
			<if test="goodsId != null">
				goods_id_ = #{goodsId},
			</if>
			<if test="batchNo != null">
				batch_no_ = #{batchNo},
			</if>
			<if test="stockSoldNum != null">
				stock_sold_num_ = #{stockSoldNum},
			</if>
			<if test="taxStockSoldNum != null">
				tax_stock_sold_num_ = #{taxStockSoldNum},
			</if>
				update_time_ = NOW(),
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM goods_procurement WHERE id_ = #{id} 
	</delete>

	<sql id="queryCondition">
		<where>
			1=1
			<if test="goodsId!=null">
				AND goods_id_ = #{goodsId}
			</if>
			<if test="enterStorageStartTime!=null and enterStorageEndTime!=null">
				AND DATE_FORMAT(create_time_, '%Y-%m-%d') BETWEEN #{enterStorageStartTime} AND #{enterStorageEndTime}
			</if>
			<if test="search!=null and search!=''">
				AND supply_channel_ LIKE CONCAT('%', #{search}, '%')
			</if>
		</where>
	</sql>

	<select id="queryGoodsProcurements" resultMap="GoodsProcurement" parameterType="map">
		SELECT * FROM goods_procurement
		<include refid="queryCondition"></include>
		ORDER BY id_ DESC <include refid="global.limit"/>
	</select>

	<select id="countGoodsProcurements" resultType="int">
		SELECT COUNT(*) FROM goods_procurement
		<include refid="queryCondition"></include>
	</select>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="GoodsProcurement" parameterType="map">
		SELECT * FROM goods_procurement
		ORDER BY id_ DESC <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM goods_procurement
	</select>

	<select id="getWithStockSurplusProcurement" resultMap="GoodsProcurement" useCache="false" flushCache="true">
		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND stock_count_>stock_sold_num_ ORDER BY create_time_ LIMIT 1
	</select>

	<select id="getWithTaxStockSurplusProcurement" resultMap="GoodsProcurement" useCache="false" flushCache="true">
		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND tax_stock_count_>tax_stock_sold_num_ ORDER BY create_time_ LIMIT 1
	</select>

	<select id="getWithGoodsAndBatchNo" resultMap="GoodsProcurement">
		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND batch_no_=#{batchNo}
	</select>
</mapper>
