<?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.HsGoodsStorageRecordMapper">

    <resultMap type="com.willalp.erms.domain.HsGoodsStorageRecord" id="HsGoodsStorageRecordResult">
        <result property="id" column="id"/>
        <result property="recordType" column="record_type"/>
        <result property="orderNo" column="order_no"/>
        <result property="title" column="title"/>
        <result property="applyDepartment" column="apply_department"/>
        <result property="storageName" column="storage_name"/>
        <result property="goodsId" column="goods_id"/>
        <result property="unitPrice" column="unit_price"/>
        <result property="amount" column="amount"/>
        <result property="applyUserId" column="apply_user_id"/>
        <result property="applyUserName" column="apply_user_name"/>
        <result property="sumPrice" column="sum_price"/>
        <result property="applyReason" column="apply_reason"/>
        <result property="applyDate" column="apply_date"/>
        <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="selectHsGoodsStorageRecordVo">
        select hgsr.id,
               hgsr.record_type,
               order_no,
               title,
               (select dept_name from sys_dept where dept_id = apply_department) apply_department,
               storage_name,
               goods_id,
               unit_price,
               amount,
               apply_user_id,
               apply_user_name,
               sum_price,
               apply_reason,
               apply_date,
               hgsr.is_delete,
               hgsr.create_by,
               hgsr.create_time,
               hgsr.update_by,
               hgsr.update_time,
               hgsr.remark,
               hgs.type_root,
               hgs.type_branch,
               hgs.name
        from hs_goods_storage_record hgsr
                 left join hs_goods_storage hgs on hgsr.goods_id = hgs.id
    </sql>

    <select id="selectHsGoodsStorageRecordList" parameterType="HsGoodsStorageRecord"
            resultMap="HsGoodsStorageRecordResult">
        <include refid="selectHsGoodsStorageRecordVo"/>
        <where>
            hgsr.is_delete = 0
            and goods_id = #{goodsId}
            <if test="recordType != null  and recordType != ''">
                and hgsr.record_type = #{recordType}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="title != null  and title != ''">
                and title = #{title}
            </if>
            <if test="applyDepartment != null  and applyDepartment != ''">
                and apply_department = #{applyDepartment}
            </if>
            <if test="storageName != null  and storageName != ''">
                and storage_name like concat('%', #{storageName}, '%')
            </if>
            <if test="unitPrice != null ">
                and unit_price = #{unitPrice}
            </if>
            <if test="amount != null ">
                and amount = #{amount}
            </if>
            <if test="applyUserId != null  and applyUserId != ''">
                and apply_user_id = #{applyUserId}
            </if>
            <if test="applyUserName != null  and applyUserName != ''">
                and apply_user_name like concat('%', #{applyUserName}, '%')
            </if>
            <if test="sumPrice != null ">
                and sum_price = #{sumPrice}
            </if>
            <if test="applyReason != null  and applyReason != ''">
                and apply_reason = #{applyReason}
            </if>
            <if test="applyDate != null ">
                and apply_date = #{applyDate}
            </if>
        </where>
    </select>
</mapper>
