<?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.xingxi.web.controller.business.ProdInventory.mapper.ProdInventoryOrderVoMapper">
    <resultMap type="ProdInventoryOrderVo" id="ProdInventoryOrderVoResult">
        <result property="inventoryOrderId" column="inventoryOrderId"   />
        <result property="ioFlag"           column="ioFlag"             />
        <result property="ioType"           column="ioType"             />
        <result property="inId"             column="inId"               />
        <result property="outId"            column="outId"              />
        <result property="remark"           column="remark"             />
        <result property="delFlag"          column="delFlag"            />
        <result property="createUser"       column="createUser"         />
        <result property="createTime"       column="createTime"         />
        <result property="updateUser"       column="updateUser"         />
        <result property="updateTime"       column="updateTime"         />
        <result property="stockInName"      column="stockInName"        />
        <result property="stockOutName"     column="stockOutName"       />
        <result property="prodCount"        column="prodCount"          />
    </resultMap>

    <sql id="selectProdInventoryOrderVo">
        select t_prod_inventory_order.inventoryOrderId
             , t_prod_inventory_order.ioFlag
             , t_prod_inventory_order.ioType
             , t_prod_inventory_order.inId
             , t_prod_inventory_order.outId
             , t_prod_inventory_order.remark
             , t_prod_inventory_order.delFlag
             , t_prod_inventory_order.createUser
             , t_prod_inventory_order.createTime
             , t_prod_inventory_order.updateUser
             , t_prod_inventory_order.updateTime
             , stock_in.mercName as stockInName
             , stock_out.mercName as stockOutName
             , prodCount
        from t_prod_inventory_order
        left join m_merchant stock_in on stock_in.mercId = t_prod_inventory_order.inId
        left join m_merchant stock_out on stock_out.mercId = t_prod_inventory_order.outId
        left join (
            select t_prod_inventory_order.inventoryOrderId
                 , count(t_prod_inventory_order_detail.inventoryOrderDetailId) prodCount
            from t_prod_inventory_order
            left join t_prod_inventory_order_detail on t_prod_inventory_order_detail.inventoryOrderId = t_prod_inventory_order.inventoryOrderId
            group by inventoryOrderId
        ) detail on detail.inventoryOrderId = t_prod_inventory_order.inventoryOrderId
    </sql>

    <select id="selectProdInventoryOrderVoById" parameterType="Long" resultMap="ProdInventoryOrderVoResult">
        <include refid="selectProdInventoryOrderVo"/>
        where t_prod_inventory_order.inventoryOrderId = #{inventoryOrderId}
    </select>

    <select id="selectProdInventoryOrderVoList" parameterType="ProdInventoryOrderVo" resultMap="ProdInventoryOrderVoResult">
        <include refid="selectProdInventoryOrderVo"/>
        <where>
            <if test="ioFlag != null  and ioFlag != ''"> and t_prod_inventory_order.ioFlag = #{ioFlag}</if>
            <if test="ioType != null  and ioType != ''"> and t_prod_inventory_order.ioType = #{ioType}</if>
            <if test="inId != null "> and t_prod_inventory_order.inId = #{inId}</if>
            <if test="outId != null "> and t_prod_inventory_order.outId = #{outId}</if>
            <if test="delFlag != null  and delFlag != ''"> and t_prod_inventory_order.delFlag = #{delFlag}</if>
            <if test="createUser != null  and createUser != ''"> and t_prod_inventory_order.createUser = #{createUser}</if>
            <if test="createTime != null "> and t_prod_inventory_order.createTime = #{createTime}</if>
            <if test="updateUser != null  and updateUser != ''"> and t_prod_inventory_order.updateUser = #{updateUser}</if>
            <if test="updateTime != null "> and t_prod_inventory_order.updateTime = #{updateTime}</if>
        </where>
    </select>
</mapper>