<?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.neusoft.smart.pos.inventory.persistent.mapper.InventoryStoreBatchMapper">
    <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="goods_name" jdbcType="VARCHAR" property="goodsName"/>
        <result column="goods_id" jdbcType="INTEGER" property="goodsId"/>
        <result column="org_id" jdbcType="INTEGER" property="orgId"/>
        <result column="shop_id" jdbcType="INTEGER" property="shopId"/>
        <result column="storage_num" jdbcType="INTEGER" property="storageNum"/>
        <result column="locked_storage_count" jdbcType="INTEGER" property="lockedStorageCount"/>
        <result column="in_price" jdbcType="DECIMAL" property="inPrice"/>
        <result column="supplier" jdbcType="INTEGER" property="supplier"/>
        <result column="made_date" jdbcType="DATE" property="madeDate"/>
        <result column="quality_date" jdbcType="DATE" property="qualityDate"/>
        <result column="status" jdbcType="VARCHAR" property="status"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
    </resultMap>
    <sql id="Example_Where_Clause">
        <where>
            <foreach collection="oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Update_By_Example_Where_Clause">
        <where>
            <foreach collection="example.oredCriteria" item="criteria" separator="or">
                <if test="criteria.valid">
                    <trim prefix="(" prefixOverrides="and" suffix=")">
                        <foreach collection="criteria.criteria" item="criterion">
                            <choose>
                                <when test="criterion.noValue">
                                    and ${criterion.condition}
                                </when>
                                <when test="criterion.singleValue">
                                    and ${criterion.condition} #{criterion.value}
                                </when>
                                <when test="criterion.betweenValue">
                                    and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue">
                                    and ${criterion.condition}
                                    <foreach close=")" collection="criterion.value" item="listItem" open="("
                                             separator=",">
                                        #{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>
    <sql id="Base_Column_List">
        id,
        goods_name,
        goods_id,
        org_id,
        shop_id,
        storage_num,
        locked_storage_count,
        in_price,
        supplier,
        made_date,
        quality_date,
        status,
        created_time,
        created_by,
        updated_time,
        updated_by,
        version
    </sql>
    <select id="selectByExample"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        <if test="offset != null and limit != null">
            limit ${offset}, ${limit}
        </if>
    </select>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from inventory_store_batch
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from inventory_store_batch
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <delete id="deleteByExample"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample">
        delete from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch"
            useGeneratedKeys="true">
        insert into inventory_store_batch (goods_name, goods_id, org_id,
                                           shop_id, storage_num, locked_storage_count,
                                           in_price, supplier, made_date,
                                           quality_date, status, created_time,
                                           created_by, updated_time, updated_by,
                                           version)
        values (#{goodsName,jdbcType=VARCHAR}, #{goodsId,jdbcType=INTEGER}, #{orgId,jdbcType=INTEGER},
                #{shopId,jdbcType=INTEGER}, #{storageNum,jdbcType=INTEGER}, #{lockedStorageCount,jdbcType=INTEGER},
                #{inPrice,jdbcType=DECIMAL}, #{supplier,jdbcType=INTEGER}, #{madeDate,jdbcType=DATE},
                #{qualityDate,jdbcType=DATE}, #{status,jdbcType=VARCHAR}, #{createdTime,jdbcType=TIMESTAMP},
                #{createdBy,jdbcType=VARCHAR}, #{updatedTime,jdbcType=TIMESTAMP}, #{updatedBy,jdbcType=VARCHAR},
                0)
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch"
            useGeneratedKeys="true">
        insert into inventory_store_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsName != null">
                goods_name,
            </if>
            <if test="goodsId != null">
                goods_id,
            </if>
            <if test="orgId != null">
                org_id,
            </if>
            <if test="shopId != null">
                shop_id,
            </if>
            <if test="storageNum != null">
                storage_num,
            </if>
            <if test="lockedStorageCount != null">
                locked_storage_count,
            </if>
            <if test="inPrice != null">
                in_price,
            </if>
            <if test="supplier != null">
                supplier,
            </if>
            <if test="madeDate != null">
                made_date,
            </if>
            <if test="qualityDate != null">
                quality_date,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="createdTime != null">
                created_time,
            </if>
            <if test="createdBy != null">
                created_by,
            </if>
            <if test="updatedTime != null">
                updated_time,
            </if>
            <if test="updatedBy != null">
                updated_by,
            </if>
            version,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsName != null">
                #{goodsName,jdbcType=VARCHAR},
            </if>
            <if test="goodsId != null">
                #{goodsId,jdbcType=INTEGER},
            </if>
            <if test="orgId != null">
                #{orgId,jdbcType=INTEGER},
            </if>
            <if test="shopId != null">
                #{shopId,jdbcType=INTEGER},
            </if>
            <if test="storageNum != null">
                #{storageNum,jdbcType=INTEGER},
            </if>
            <if test="lockedStorageCount != null">
                #{lockedStorageCount,jdbcType=INTEGER},
            </if>
            <if test="inPrice != null">
                #{inPrice,jdbcType=DECIMAL},
            </if>
            <if test="supplier != null">
                #{supplier,jdbcType=INTEGER},
            </if>
            <if test="madeDate != null">
                #{madeDate,jdbcType=DATE},
            </if>
            <if test="qualityDate != null">
                #{qualityDate,jdbcType=DATE},
            </if>
            <if test="status != null">
                #{status,jdbcType=VARCHAR},
            </if>
            <if test="createdTime != null">
                #{createdTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createdBy != null">
                #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="updatedTime != null">
                #{updatedTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updatedBy != null">
                #{updatedBy,jdbcType=VARCHAR},
            </if>
            0,
        </trim>
    </insert>
    <select id="countByExample"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultType="java.lang.Integer">
        select count(*) from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update inventory_store_batch
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=INTEGER},
            </if>
            <if test="record.goodsName != null">
                goods_name = #{record.goodsName,jdbcType=VARCHAR},
            </if>
            <if test="record.goodsId != null">
                goods_id = #{record.goodsId,jdbcType=INTEGER},
            </if>
            <if test="record.orgId != null">
                org_id = #{record.orgId,jdbcType=INTEGER},
            </if>
            <if test="record.shopId != null">
                shop_id = #{record.shopId,jdbcType=INTEGER},
            </if>
            <if test="record.storageNum != null">
                storage_num = #{record.storageNum,jdbcType=INTEGER},
            </if>
            <if test="record.lockedStorageCount != null">
                locked_storage_count = #{record.lockedStorageCount,jdbcType=INTEGER},
            </if>
            <if test="record.inPrice != null">
                in_price = #{record.inPrice,jdbcType=DECIMAL},
            </if>
            <if test="record.supplier != null">
                supplier = #{record.supplier,jdbcType=INTEGER},
            </if>
            <if test="record.madeDate != null">
                made_date = #{record.madeDate,jdbcType=DATE},
            </if>
            <if test="record.qualityDate != null">
                quality_date = #{record.qualityDate,jdbcType=DATE},
            </if>
            <if test="record.status != null">
                status = #{record.status,jdbcType=VARCHAR},
            </if>
            <if test="record.createdTime != null">
                created_time = #{record.createdTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.createdBy != null">
                created_by = #{record.createdBy,jdbcType=VARCHAR},
            </if>
            <if test="record.updatedTime != null">
                updated_time = #{record.updatedTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.updatedBy != null">
                updated_by = #{record.updatedBy,jdbcType=VARCHAR},
            </if>
            version = version + 1,
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update inventory_store_batch
        set id = #{record.id,jdbcType=INTEGER},
        goods_name = #{record.goodsName,jdbcType=VARCHAR},
        goods_id = #{record.goodsId,jdbcType=INTEGER},
        org_id = #{record.orgId,jdbcType=INTEGER},
        shop_id = #{record.shopId,jdbcType=INTEGER},
        storage_num = #{record.storageNum,jdbcType=INTEGER},
        locked_storage_count = #{record.lockedStorageCount,jdbcType=INTEGER},
        in_price = #{record.inPrice,jdbcType=DECIMAL},
        supplier = #{record.supplier,jdbcType=INTEGER},
        made_date = #{record.madeDate,jdbcType=DATE},
        quality_date = #{record.qualityDate,jdbcType=DATE},
        status = #{record.status,jdbcType=VARCHAR},
        created_time = #{record.createdTime,jdbcType=TIMESTAMP},
        created_by = #{record.createdBy,jdbcType=VARCHAR},
        updated_time = #{record.updatedTime,jdbcType=TIMESTAMP},
        updated_by = #{record.updatedBy,jdbcType=VARCHAR},
        version = version + 1
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch">
        update inventory_store_batch
        <set>
            <if test="goodsName != null">
                goods_name = #{goodsName,jdbcType=VARCHAR},
            </if>
            <if test="goodsId != null">
                goods_id = #{goodsId,jdbcType=INTEGER},
            </if>
            <if test="orgId != null">
                org_id = #{orgId,jdbcType=INTEGER},
            </if>
            <if test="shopId != null">
                shop_id = #{shopId,jdbcType=INTEGER},
            </if>
            <if test="storageNum != null">
                storage_num = #{storageNum,jdbcType=INTEGER},
            </if>
            <if test="lockedStorageCount != null">
                locked_storage_count = #{lockedStorageCount,jdbcType=INTEGER},
            </if>
            <if test="inPrice != null">
                in_price = #{inPrice,jdbcType=DECIMAL},
            </if>
            <if test="supplier != null">
                supplier = #{supplier,jdbcType=INTEGER},
            </if>
            <if test="madeDate != null">
                made_date = #{madeDate,jdbcType=DATE},
            </if>
            <if test="qualityDate != null">
                quality_date = #{qualityDate,jdbcType=DATE},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=VARCHAR},
            </if>
            <if test="createdTime != null">
                created_time = #{createdTime,jdbcType=TIMESTAMP},
            </if>
            <if test="createdBy != null">
                created_by = #{createdBy,jdbcType=VARCHAR},
            </if>
            <if test="updatedTime != null">
                updated_time = #{updatedTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updatedBy != null">
                updated_by = #{updatedBy,jdbcType=VARCHAR},
            </if>
            version = version + 1,
        </set>
        where id = #{id,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch">
        update inventory_store_batch
        set goods_name           = #{goodsName,jdbcType=VARCHAR},
            goods_id             = #{goodsId,jdbcType=INTEGER},
            org_id               = #{orgId,jdbcType=INTEGER},
            shop_id              = #{shopId,jdbcType=INTEGER},
            storage_num          = #{storageNum,jdbcType=INTEGER},
            locked_storage_count = #{lockedStorageCount,jdbcType=INTEGER},
            in_price             = #{inPrice,jdbcType=DECIMAL},
            supplier             = #{supplier,jdbcType=INTEGER},
            made_date            = #{madeDate,jdbcType=DATE},
            quality_date         = #{qualityDate,jdbcType=DATE},
            status               = #{status,jdbcType=VARCHAR},
            created_time         = #{createdTime,jdbcType=TIMESTAMP},
            created_by           = #{createdBy,jdbcType=VARCHAR},
            updated_time         = #{updatedTime,jdbcType=TIMESTAMP},
            updated_by           = #{updatedBy,jdbcType=VARCHAR},
            version              = version + 1
        where id = #{id,jdbcType=INTEGER}
          and version = #{version,jdbcType=INTEGER}
    </update>
    <insert id="batchInsert">
        <if test="items.get(0) != null">
            insert into inventory_store_batch (goods_name, goods_id, org_id,
            shop_id, storage_num, locked_storage_count,
            in_price, supplier, made_date,
            quality_date, status, created_time,
            created_by, updated_time, updated_by,
            version)
            values
            <foreach collection="items" item="item" index="index" separator=",">
                (#{item.goodsName,jdbcType=VARCHAR}, #{item.goodsId,jdbcType=INTEGER}, #{item.orgId,jdbcType=INTEGER},
                #{item.shopId,jdbcType=INTEGER}, #{item.storageNum,jdbcType=INTEGER},
                #{item.lockedStorageCount,jdbcType=INTEGER},
                #{item.inPrice,jdbcType=DECIMAL}, #{item.supplier,jdbcType=INTEGER}, #{item.madeDate,jdbcType=DATE},
                #{item.qualityDate,jdbcType=DATE}, #{item.status,jdbcType=VARCHAR},
                #{item.createdTime,jdbcType=TIMESTAMP},
                #{item.createdBy,jdbcType=VARCHAR}, #{item.updatedTime,jdbcType=TIMESTAMP},
                #{item.updatedBy,jdbcType=VARCHAR},
                0)
            </foreach>
        </if>
    </insert>
    <update id="deductionStorage">
        update inventory_store_batch
        <set>
            <if test="!isLock">
                storage_num = storage_num - #{count,jdbcType=INTEGER},
            </if>
            locked_storage_count = locked_storage_count - #{count,jdbcType=INTEGER},
            status = #{status,jdbcType=VARCHAR},
            version = version + 1,
        </set>
        where id = #{batchId,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER}
    </update>
    <update id="handleLockStorage">
        update inventory_store_batch
        <set>
            <if test="deductLockedCount != null">
                storage_num = storage_num - #{ deductLockedCount,jdbcType=INTEGER},
            </if>
            <if test="releaseCount != null">
                locked_storage_count = locked_storage_count + #{releaseCount,jdbcType=INTEGER},
            </if>
            <if test="!canNegativeStorage">
                status = if(storage_num-#{ deductLockedCount,jdbcType=INTEGER} &lt;= 0, 0, status ),
            </if>
            version = version + 1,
        </set>
        where id = #{batchId,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER}
    </update>
    <update id="returnStorage">
        update inventory_store_batch
        <set>
            <if test="returnCount != null">
                storage_num = storage_num + #{ returnCount,jdbcType=INTEGER},
                locked_storage_count = locked_storage_count + #{returnCount,jdbcType=INTEGER},
                status = if(storage_num &lt;= 0, 0, 1)
            </if>
            version = version + 1,
        </set>
        where id = #{batchId,jdbcType=INTEGER} and version = #{version,jdbcType=INTEGER}
    </update>
    <select id="sumByExample"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultType="java.math.BigDecimal">
        select sum(storage_num) from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <select id="getAvgPrice"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultType="java.math.BigDecimal">
        select sum(storage_num * in_price) / sum(storage_num) from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <select id="selectMaxIdBatchByGoodsId"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from inventory_store_batch
        where id = (select max(id) from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        )
    </select>

    <select id="selectMinIdBatchByGoodsId"
            parameterType="com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from inventory_store_batch
        where id = (select min(id) from inventory_store_batch
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        )
    </select>
</mapper>