<?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.willalp.erms.mapper.HsGoodsStorageMapper">

    <resultMap type="com.willalp.erms.domain.HsGoodsStorage" id="HsGoodsStorageResult">
        <result property="id" column="id"/>
        <result property="recordType" column="record_type"/>
        <result property="typeRoot" column="type_root"/>
        <result property="typeBranch" column="type_branch"/>
        <result property="number" column="number"/>
        <result property="name" column="name"/>
        <result property="productModel" column="product_model"/>
        <result property="quantityInStock" column="quantity_in_stock"/>
        <result property="unit" column="unit"/>
        <result property="averageUnitPrice" column="average_unit_price"/>
        <result property="useStatus" column="use_status"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectHsGoodsStorageVo">
        select id,
               record_type,
               type_root,
               type_branch,
               name,
               number,
               product_model,
               quantity_in_stock,
               unit,
               average_unit_price,
               use_status,
               is_delete,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from hs_goods_storage
    </sql>

    <select id="selectHsGoodsStorageList" parameterType="HsGoodsStorage" resultMap="HsGoodsStorageResult">
        <include refid="selectHsGoodsStorageVo"/>
        <where>
            is_delete = 0
            <if test="recordType != null  and recordType != ''">
                and record_type = #{recordType}
            </if>
            <if test="typeRoot != null  and typeRoot != ''">
                and type_root = #{typeRoot}
            </if>
            <if test="typeBranch != null  and typeBranch != ''">
                and type_branch = #{typeBranch}
            </if>
            <if test="number != null  and number != ''">
                and number = #{number}
            </if>
            <if test="name != null  and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="productModel != null  and productModel != ''">
                and product_model = #{productModel}
            </if>
            <if test="quantityInStock != null ">
                and quantity_in_stock = #{quantityInStock}
            </if>
            <if test="unit != null  and unit != ''">
                and unit = #{unit}
            </if>
            <if test="averageUnitPrice != null ">
                and average_unit_price = #{averageUnitPrice}
            </if>
            <if test="useStatus != null  and useStatus != ''">
                and use_status = #{useStatus}
            </if>
        </where>
    </select>
    <select id="appGetGoodsByType" resultType="com.willalp.erms.domain.HsGoodsStorage">
        select id,
        type_root,
        type_branch,
        number,
        name,
        product_model,
        unit
        from hs_goods_storage
        <where>
            is_delete = 0
            AND use_status = 0
            AND type_branch = #{type}
            <if test="name != '' and name != null">
                AND name like concat('%', #{name}, '%')
            </if>
        </where>
    </select>
    <select id="flowGetGoodsById" resultType="java.util.Map">
        SELECT
            hgs.id,
            (SELECT goods_type_name FROM hs_goods_type t1 WHERE id = type_root) type_root,
            (SELECT goods_type_name FROM hs_goods_type t2 WHERE id = type_branch) type_branch,
            number,
            NAME,
            product_model,
            unit
        FROM
            hs_goods_storage hgs
        WHERE
            hgs.is_delete = 0
          AND id = #{id}
    </select>
</mapper>
