<?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.fjhx.mapper.StockExMapper">

    <select id="getInOutPage" resultType="java.util.LinkedHashMap">
        select
        w.name warehouseName,
        sj.type,
        sj.details_type,
        t.name goodsName,
        t.code goodsCode,
        t.goodsType,
        sj.change_quantity quantity,
        sj.create_time,
        sj.create_user
        from stock_journal sj
        left join warehouse w on sj.warehouse_id=w.id
        inner join
        <include refid="unionAllMaterialAndProduct"/>
        t on t.id = sj.goods_id
        ${ew.customSqlSegment}
    </select>

    <select id="getPage" resultType="java.util.Map">
        select
        w.name warehouseName,
        t.name goodsName,
        t.code goodsCode,
        t.goodsType,
        t.unit,
        s.quantity
        from stock s
        left join warehouse w on s.warehouse_id=w.id
        inner join
        <include refid="unionAllMaterialAndProduct"/>
        t on t.id = s.goods_id
        ${ew.customSqlSegment}
    </select>

    <sql id="unionAllMaterialAndProduct">
        (
        select m.id,m.code,m.name,m.unit,1 goodsType from material m
        <where>
            <if test="keyword neq null and keyword neq ''">
                instr(m.code,#{keyword}) > 0 or instr(m.name,#{keyword}) > 0
            </if>
            <if test="goodsId neq null and goodsId neq ''">
                and m.id = #{goodsId}
            </if>
        </where>
        union all
        select p.id,p.code,p.name,p.unit,2 goodsType from product p
        <where>
            <if test="keyword neq null and keyword neq ''">
                instr(p.code,#{keyword}) > 0 or instr(p.name,#{keyword}) > 0
            </if>
            <if test="goodsId neq null and goodsId neq ''">
                and p.id = #{goodsId}
            </if>
        </where>
        )
    </sql>

</mapper>
