<?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.zfire.mall.manager.commonmapper.stock.StockCommonMapper">

    <select id="list" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        kdm.id as materialId,
        kdm.number AS materialNumber,
        kdm.`name` AS materialName,
        kdm.old_number AS materialOldNumber,
        kdm.specification,
        kdm.base_unit_id,
        kdm.category_name,
        kdm.material_group_name
        FROM
        stock s
        JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
        JOIN king_dee_material kdm ON s.material_id = kdm.id
        JOIN stock_correspond sc on scp.correspond_id = sc.id
        where 1=1
        and scp.stock_id = s.stock_id
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="stockId !=null and stockId != ''">
            and  scp.stock_id = #{stockId}
        </if>
<!--        <if test="categoryId !=null and categoryId != ''">
            and  kdm.category_id = #{categoryId}
        </if>-->

        <if test="categoryId != null and categoryId.size > 0">
            AND kdm.category_id in
            <foreach item="item" index="index" collection="categoryId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="materialOldNumber !=null and materialOldNumber != ''">
            and  kdm.old_number like concat('%',#{materialOldNumber},'%')
        </if>
        <if test="materialName !=null and materialName != ''">
            and kdm.name like concat('%',#{materialName},'%')
        </if>
        <if test="materialNumber !=null and materialNumber != ''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="specification !=null and specification != ''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
        group by kdm.id
        order by kdm.create_time desc
    </select>


    <select id="selectInvoiceCount" resultType="Integer">
        SELECT
        ifnull(SUM(iao.refundable_qty),0)
        FROM
        invoice_application_order iao
        LEFT JOIN invoice_application ia ON iao.invoice_id = ia.id
        where ia.examine_status in ('SAVE','WAIT')
        and iao.material_id=#{materialId}
        and type != 2
        <if test="correspondId != null and correspondId != ''">
            and ia.correspond_id =#{correspondId}
        </if>
    </select>


    <select id="countAll" resultType="Integer">
        SELECT
            ifnull(SUM(iao.refundable_qty),0)
        FROM
            sales_order ia
                LEFT JOIN sales_order_item iao ON ia.id = iao.sales_order_no
        where ia.examine_status != 'OK'
          and iao.material_id =#{materialId}
          and ia.type != 2
        <if test="correspondId != null and correspondId != ''">
            and ia.correspond_id =#{correspondId}
        </if>
    </select>


    <select id="countReserved" resultType="Integer">
        SELECT
            ifnull(sum( roi.reserved_num ),0)
        FROM
        reserved_order_item roi
        WHERE
        roi.STATUS = 1
        and roi.material_id = #{materialId}
        <if test="correspondId != null and correspondId != ''">
            and roi.correspond_id =#{correspondId}
        </if>
    </select>



    <resultMap id="sockPosition" type="com.zfire.mall.manager.bean.k3.StockPositionStock">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="stock_cordon" jdbcType="DECIMAL" property="stockCordon"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <collection property="kingDeeStocks" ofType="com.zfire.mall.manager.plus.entity.KingDeeStock" select="stocks" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="number" jdbcType="VARCHAR" property="number"/>
            <result column="name" jdbcType="VARCHAR" property="name"/>
            <result column="useOrgNumber" jdbcType="VARCHAR" property="useOrgName"/>
            <result column="useOrgName" jdbcType="VARCHAR" property="useOrgName"/>
            <result column="syncTime" jdbcType="TIMESTAMP" property="syncTime"/>
            <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
            <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
            <result column="forbid_status" jdbcType="VARCHAR" property="forbidStatus"/>
            <result column="principal_name" jdbcType="VARCHAR" property="principalName"/>
            <result column="stock_property" jdbcType="VARCHAR" property="stockProperty"/>
            <result column="third_stock_type" jdbcType="VARCHAR" property="thirdStockType"/>
            <result column="document_status" jdbcType="VARCHAR" property="documentStatus"/>
            <result column="allow_minus_qty" jdbcType="VARCHAR" property="allowMinusQty"/>
            <result column="open_location" jdbcType="VARCHAR" property="openLocation"/>
            <result column="not_exp_qty" jdbcType="VARCHAR" property="notExpQty"/>
            <result column="available_alert" jdbcType="VARCHAR" property="availableAlert"/>
            <result column="create_org_name" jdbcType="VARCHAR" property="createOrgName"/>
            <result column="supplier_name" jdbcType="VARCHAR" property="supplierName"/>
            <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
            <result column="address" jdbcType="VARCHAR" property="address"/>
            <result column="tel" jdbcType="VARCHAR" property="tel"/>
            <result column="description" jdbcType="VARCHAR" property="description"/>
            <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
            <result column="gys_stock" jdbcType="VARCHAR" property="gysStock"/>
            <result column="gys_stock_number" jdbcType="VARCHAR" property="gysStockNumber"/>
            <result column="third_stock_no" jdbcType="VARCHAR" property="thirdStockNo"/>
            <result column="stock_status_type" jdbcType="VARCHAR" property="stockStatusType"/>
            <result column="def_stock_status_name" jdbcType="VARCHAR" property="defStockStatusName"/>
            <result column="def_receive_status_name" jdbcType="VARCHAR" property="defReceiveStatusName"/>
            <result column="allow_mrp_plan" jdbcType="VARCHAR" property="allowMrpPlan"/>
            <result column="allow_lock" jdbcType="VARCHAR" property="allowLock"/>
            <result column="available_picking" jdbcType="VARCHAR" property="availablePicking"/>
            <result column="sorting_priority" jdbcType="VARCHAR" property="sortingPriority"/>
            <result column="locList_formatter" jdbcType="VARCHAR" property="loclistFormatter"/>
            <result column="Inquire" jdbcType="TINYINT" property="inquire"/>
        </collection>
    </resultMap>

    <select id="listStock" resultMap="sockPosition">
        SELECT
        sc.*
        FROM
        stock_correspond sc
        LEFT JOIN stock_correspond_position scp ON sc.id = scp.correspond_id
        LEFT JOIN king_dee_stock kds ON scp.stock_id = kds.id
            left join admin_user_stock aus on sc.id = aus.correspond_id
        where 1=1
        <if test="name != null and name != ''">
            and sc.name like  CONCAT('%', #{name},'%')
        </if>
        <if test="stockName != null and stockName != ''">
            and kds.name like  CONCAT('%', #{stockName},'%')
        </if>
        <if test="adminUserId != null and adminUserId != ''">
            and aus.admin_user_id =  #{adminUserId}
        </if>
          <if test="type != null and type != ''">
            and sc.type =  #{type}
        </if>
        group by sc.id
        order by sc.create_time desc
    </select>

    <select id="stocks" resultType="com.zfire.mall.manager.plus.entity.KingDeeStock">
        SELECT
            kds.*
        FROM
            stock_correspond sc
                LEFT JOIN stock_correspond_position scp ON sc.id = scp.correspond_id
                LEFT JOIN king_dee_stock kds ON scp.stock_id = kds.id
        where sc.id=#{id}
    </select>


    <select id="resverdNumber" resultType="Integer">
        SELECT
        IFNULL(SUM(roi.reserved_num),0)
        FROM
        reserved_order_item roi
        where roi.material_id = #{materialId}
        and roi.`status` = 1
        <if test="orderId != null and orderId != ''">
            and (roi.order_id != #{orderId} or roi.order_id is null)
        </if>
        and (roi.customer_id != #{customerId})
        <if test="correspondId == null">
            and roi.correspond_id = null
        </if>
        <if test="correspondId != null and correspondId != ''">
            and roi.correspond_id =#{correspondId}
        </if>
    </select>

    <select id="resverdCustomerNumber" resultType="Integer">
        SELECT
        IFNULL(SUM(roi.reserved_num),0)
        FROM
        reserved_order_item roi
        where roi.material_id = #{materialId}
        and roi.`status` = 1
        <if test="orderId != null and orderId != ''">
        and (roi.order_id != #{orderId} and roi.order_id is not null)
        </if>
        and (roi.customer_id = #{customerId})
        <if test="correspondId == null">
            and roi.correspond_id = null
        </if>
        <if test="correspondId != null and correspondId != ''">
            and roi.correspond_id =#{correspondId}
        </if>
    </select>

    <select id="resverdCustomerNumberRe" resultType="Integer">
        SELECT
        IFNULL(SUM(roi.reserved_num),0)
        FROM
        reserved_order_item roi
        where roi.material_id = #{materialId}
        and roi.`status` = 1
        <if test="orderId != null and orderId != ''">
        and (roi.order_id = #{orderId})
        </if>
        and (roi.customer_id = #{customerId})
        <if test="correspondId == null">
            and roi.correspond_id = null
        </if>
        <if test="correspondId != null and correspondId != ''">
            and roi.correspond_id =#{correspondId}
        </if>
    </select>

    <select id="resverdAllStockNumber" resultType="Integer">
        SELECT
        IFNULL(SUM(roi.reserved_num),0)
        FROM
        reserved_order_item roi
        where roi.material_id = #{materialId}
        and roi.`status` = 1
        <if test="correspondId == null || correspondId == ''">
            and roi.correspond_id is null
        </if>
        <if test="correspondId != null and correspondId != ''">
            and roi.correspond_id =#{correspondId}
        </if>
    </select>
    <select id="sumKingDeeNumber" resultType="Integer">
        SELECT
        IFNULL(SUM(s.stock_qty),0)
        FROM
        stock s
        join stock_correspond_position scp on s.stock_id = scp.stock_id
        join king_dee_stock kds on scp.stock_id = kds.id
        where 1=1
        <if test="correspondId != null and correspondId != ''">
            and scp.correspond_id = #{correspondId}
        </if>
          <if test="correspondId == null or correspondId == ''">
              and kds.not_exp_qty = 0
        </if>
        and s.material_id = #{materialId}
    </select>

    <select id="stockHintUnit" resultType="Integer">
        SELECT
            ifnull(count(pms.id ), 0)
        FROM
            pick_manager_ship pms
                LEFT JOIN pick_manager pm ON pm.id = pms.pick_id
                LEFT JOIN invoice_application_order iao ON pms.invoice_order_id = iao.id
        where pm.correspond_id = #{correspondId}
          and iao.material_id = #{materialId}
          and pm.pick_time BETWEEN #{startOfDay} and #{endOfDay}
    </select>

    <select id="stockHintTower" resultType="Integer">
        SELECT
            ifnull(SUM(iao.refundable_qty), 0)
        FROM
            pick_manager_ship pms
                LEFT JOIN pick_manager pm ON pm.id = pms.pick_id
                LEFT JOIN invoice_application_order iao ON pms.invoice_order_id = iao.id
        where pm.correspond_id = #{correspondId}
          and iao.material_id = #{materialId}
          and pm.pick_time BETWEEN #{startTime} and #{endTime}
    </select>
    <select id="listStockToDay" resultType="com.zfire.mall.manager.bean.stock.StockBeanToDay">
        SELECT
            kdm.`name` as materialName,
            kdm.old_number as materialOldNumber,
            kdm.number as materialNumber,
            kdm.id as materialId,
            kdm.specification,
            s.stock_qty as balanceNumber,
            s.stock_id,
            sc.`name` as correspond_name,
            scp.correspond_id,
               kdm.category_name
        FROM
            stock s
                 JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
                 JOIN king_dee_material kdm ON s.material_id = kdm.id
                 JOIN stock_correspond sc on scp.correspond_id = sc.id
        left join admin_user_stock aus on scp.correspond_id = aus.correspond_id
        where 1=1
        and scp.stock_id = s.stock_id
 <!--       <if test="correspondId != null and correspondId!=''">
            and scp.correspond_id =#{correspondId}
        </if>-->
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
         <if test="categoryId != null and categoryId.size > 0">
            AND kdm.category_id in
            <foreach item="item" index="index" collection="categoryId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="materialNumber != null and materialNumber!=''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="materialOldNumber != null and materialOldNumber!=''">
            and kdm.old_number like concat('%',#{materialOldNumber},'%')
        </if>
          <if test="materialName != null and materialName!=''">
            and kdm.name like concat('%',#{materialName},'%')
        </if>

    <!--   <if test="categoryId != null and categoryId!=''">
            and kdm.category_id =#{categoryId}
        </if>-->

          <if test="specification != null and specification!=''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
      <if test="adminUserId != null and adminUserId!=''">
          and aus.admin_user_id = #{adminUserId}
        </if>
        group by s.id
    </select>


    <select id="sumKingDeeNumberList"  resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        s.stock_qty,
        s.material_id,
        scp.correspond_id
        FROM
        stock s
        join stock_correspond_position scp on s.stock_id = scp.stock_id
        join king_dee_stock kds on scp.stock_id = kds.id
        where 1=1
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>

    </select>
    <select id="sumKingDeeNumberAdequateList" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        s.stock_qty as stockAdequate,
        s.material_id,
        scp.correspond_id
        FROM
        stock s
        join stock_correspond_position scp on s.stock_id = scp.stock_id
        join king_dee_stock kds on scp.stock_id = kds.id
        where 1=1

        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="correspondId == null ">
            and kds.not_exp_qty = 0
        </if>
    </select>

    <select id="selectInvoiceCountList" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        iao.refundable_qty as temporaryNum,
        iao.material_id,
        ia.correspond_id
        FROM
        invoice_application_order iao
        LEFT JOIN invoice_application ia ON iao.invoice_id = ia.id
        where ia.examine_status in ('SAVE','WAIT')
        and type != 2
        <if test="correspondId != null and correspondId.size > 0">
            AND ia.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>

    </select>

    <select id="countAllList" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        iao.refundable_qty as num,
        iao.material_id,
        ia.correspond_id
        FROM
        sales_order ia
        LEFT JOIN sales_order_item iao ON ia.id = iao.sales_order_no
        where ia.examine_status != 'OK'
        and ia.type != 2
        <if test="correspondId != null and correspondId.size > 0">
            AND ia.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>

    </select>

    <select id="countReservedList" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        roi.reserved_num as reservedNum,
        roi.material_id,
        roi.correspond_id
        FROM
        reserved_order_item roi
        WHERE
        roi.STATUS = 1
        <if test="correspondId != null and correspondId.size > 0">
            AND roi.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>

    </select>
    <select id="listCustomer" resultType="com.zfire.mall.manager.bean.stock.StockCustomerReserved">
        SELECT
	 roi.customer_id,
	 roi.customer_name,
	 roi.customer_number,
	 kdm.category_name,
	 roi.material_code as materialNumber,
	 roi.material_old_number,
	 roi.material_id,
	 roi.specification,
	 roi.material_name,
	 roi.unit,
	 sum(roi.reserved_num) as reservedNum
    FROM
	reserved_order ro
	JOIN reserved_order_item roi on roi.reserved_id = ro.id
	join king_dee_material kdm on kdm.id = roi.material_id
	where roi.status = 1



        <if test="materialName != null and materialName!=''">
            and roi.material_name  like concat('%',#{materialName},'%')
        </if>
        <if test="materialNumber != null and materialNumber!=''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="customerName != null and customerName!=''">
            and ro.customer_name like concat('%',#{customerName},'%')
        </if>
        <if test="customerNumber != null and customerNumber!=''">
            and ro.customer_number like concat('%',#{customerNumber},'%')
        </if>
        <if test="categoryId != null and categoryId!=''">
            and kdm.category_id =#{categoryId}
        </if>
        <if test="specification != null and specification!=''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
        GROUP BY roi.customer_id,roi.material_id
        order by roi.create_time
    </select>

    <select id="listPurchaseOrderOut" resultType="com.zfire.mall.manager.bean.stock.KingDeePurchaseStockOutBean">
        SELECT
        	kdpso.id,
        	kdpso.bill_no,
        	kdpso.f_date,
        	kds.`name` as supplierName,
        	kdpso.fd_document_status,
        	kdpsoi.material_name,
        	kdpsoi.material_number,
        	kdm.old_number as materialOldNumber,
        	kdm.specification,
        	kdm.base_unit_id as unit,
        	kdpsoi.rm_real_qty as qty,
        	kdsc.`name` as  stockName

        FROM
        	king_dee_purchase_stock_out kdpso
        	JOIN king_dee_purchase_stock_out_item kdpsoi on kdpso.id = kdpsoi.f_id
        	join king_dee_supplier kds on kdpso.supplier_id = kds.id
        	JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        	join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        where 1=1
        <if test="specification != null and specification!=''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
            <if test="supplierName != null and supplierName!=''">
            and kds.name like concat('%',#{supplierName},'%')
        </if>
         <if test="materialOldNumber != null and materialOldNumber!=''">
            and kdm.old_number like concat('%',#{materialOldNumber},'%')
        </if>
         <if test="materialName != null and materialName!=''">
            and kdpsoi.material_name like concat('%',#{materialName},'%')
        </if>
          <if test="materialNumber != null and materialNumber!=''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="billNo != null and billNo!=''">
            and kdpso.bill_no like concat('%',#{billNo},'%')
        </if>
        <if test="startTime !=null and startTime !='' and endTime !=null and endTime !=''">
            and kdpso.f_date between #{startTime} and #{endTime}
        </if>
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        	order by kdpso.f_date desc
    </select>
    <select id="listOtherStockOut" resultType="com.zfire.mall.manager.bean.stock.KingDeeOtherStockOutBean">
           SELECT
        kdpso.id,
        kdpso.bill_no,
        	kdpso.f_date,
        	kdpso.fd_document_status,
        	kdpsoi.material_name,
        	kdpsoi.material_number,
        	kdm.old_number as materialOldNumber,
        	kdm.specification,
        	kdm.base_unit_id as unit,
        	kdpsoi.qty,
        	kdsc.`name` as  stockName

        FROM
        	king_dee_other_stock_out kdpso
        	JOIN king_dee_other_stock_out_item kdpsoi on kdpso.id = kdpsoi.f_id
        	JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        	join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        where 1=1
        <if test="specification != null and specification!=''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
        <if test="materialOldNumber != null and materialOldNumber!=''">
            and kdm.old_number like concat('%',#{materialOldNumber},'%')
        </if>
        <if test="materialName != null and materialName!=''">
            and kdpsoi.material_name like concat('%',#{materialName},'%')
        </if>
        <if test="billNo != null and billNo!=''">
            and kdpso.bill_no like concat('%',#{billNo},'%')
        </if>
        <if test="materialNumber != null and materialNumber!=''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="startTime !=null and startTime !='' and endTime !=null and endTime !=''">
            and kdpso.f_date between #{startTime} and #{endTime}
        </if>
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        order by kdpso.f_date desc
    </select>
    <select id="listOtherStockIn" resultType="com.zfire.mall.manager.bean.stock.KingDeeOtherStockInBean">
 SELECT
        kdpso.id,
        kdpso.bill_no,
        	kdpso.f_date,
        	kdpso.fd_document_status,
        	kdpsoi.material_name,
        	kdpsoi.material_number,
        	kdm.old_number as materialOldNumber,
        	kdm.specification,
        	kdm.base_unit_id as unit,
        	kdpsoi.qty,
        	kdsc.`name` as  stockName

        FROM
        	king_dee_other_stock_in kdpso
        	JOIN king_dee_other_stock_in_item kdpsoi on kdpso.id = kdpsoi.f_id
        	JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        	join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        where 1=1

        <if test="specification != null and specification!=''">
            and kdm.specification like concat('%',#{specification},'%')
        </if>
        <if test="materialOldNumber != null and materialOldNumber!=''">
            and kdm.old_number like concat('%',#{materialOldNumber},'%')
        </if>
        <if test="materialName != null and materialName!=''">
            and kdpsoi.material_name like concat('%',#{materialName},'%')
        </if>
        <if test="materialNumber != null and materialNumber!=''">
            and kdm.number like concat('%',#{materialNumber},'%')
        </if>
        <if test="billNo != null and billNo!=''">
            and kdpso.bill_no like concat('%',#{billNo},'%')
        </if>
        <if test="startTime !=null and startTime !='' and endTime !=null and endTime !=''">
            and kdpso.f_date between #{startTime} and #{endTime}
        </if>
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        order by kdpso.f_date desc
    </select>
    <select id="listStockToDayV2" resultType="com.zfire.mall.manager.bean.stock.StockBeanToDay">
        SELECT
        kdm.`name` as materialName,
        kdm.old_number as materialOldNumber,
        kdm.number as materialNumber,
        kdm.id as materialId,
        kdm.specification,
        s.stock_qty as balanceNumber,
        s.stock_id,
        scp.correspond_id,
        sc.name as correspondName,
        kdm.category_name
        FROM
        stock s
        JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
        JOIN king_dee_material kdm ON s.material_id = kdm.id
        JOIN stock_correspond sc on scp.correspond_id = sc.id
        left join admin_user_stock aus on scp.correspond_id = aus.correspond_id
        ${ex.query}
        and scp.stock_id = s.stock_id
        <if test="adminUserId != null and adminUserId!=''">
            and aus.admin_user_id = #{adminUserId}
        </if>

        group by s.id
        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by s.sync_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="listStockToDayCustomerV2"
            resultType="com.zfire.mall.manager.bean.stock.StockBeanCustomerToDay">
        SELECT
        kdm.`name` as materialName,
        kdm.old_number as materialOldNumber,
        kdm.number as materialNumber,
        kdm.id as materialId,
        kdm.specification,
        s.stock_qty as balanceNumber,
        s.stock_id,
        scp.correspond_id,
        sc.name as correspondName,
        kdm.category_name
        FROM
        stock s
        JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
        JOIN king_dee_material kdm ON s.material_id = kdm.id
        JOIN stock_correspond sc on scp.correspond_id = sc.id
        left join admin_user_stock aus on scp.correspond_id = aus.correspond_id
        ${ex.query}
        and scp.stock_id = s.stock_id
        <if test="adminUserId != null and adminUserId!=''">
            and aus.admin_user_id = #{adminUserId}
        </if>

        group by s.id
        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by s.sync_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="listPurchaseOrderOutV2"
            resultType="com.zfire.mall.manager.bean.stock.KingDeePurchaseStockOutVO">
          SELECT
        	kdpso.id,
        	kdpso.bill_no,
        	kdpso.f_date as fDate,
        	kds.`name` as supplierName,
        	kdpso.fd_document_status,
        	kdpsoi.material_name,
        	kdpsoi.material_number,
        	kdm.old_number as materialOldNumber,
        	kdm.specification,
        	kdm.base_unit_id as unit,
        	kdpsoi.rm_real_qty as qty,
        	kdsc.`name` as  stockName

        FROM
        	king_dee_purchase_stock_out kdpso
        	JOIN king_dee_purchase_stock_out_item kdpsoi on kdpso.id = kdpsoi.f_id
        	join king_dee_supplier kds on kdpso.supplier_id = kds.id
        	JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        	join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        ${ex.query}

        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by kdpso.sync_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="listOtherStockOutV2" resultType="com.zfire.mall.manager.bean.stock.KingDeeOtherStockOutVO">
          SELECT
        kdpso.id,
        kdpso.bill_no,
        	kdpso.f_date,
        	kdpso.stock_direct,
        	kdpso.cancel_status,
        	kdpsoi.material_name,
        	kdpsoi.material_number,
        	kdm.old_number as materialOldNumber,
        	kdm.specification,
        	kdm.base_unit_id as unit,
        	kdpsoi.qty,
        	kdsc.`name` as  stockName

        FROM
        	king_dee_other_stock_out kdpso
        	JOIN king_dee_other_stock_out_item kdpsoi on kdpso.id = kdpsoi.f_id
        	JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        	join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        ${ex.query}

        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by kdpso.sync_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="listOtherStockInV2" resultType="com.zfire.mall.manager.bean.stock.KingDeeOtherStockInVO">
        SELECT
        kdpso.id,
        kdpso.bill_no,
        kdpso.f_date,
        kdpso.stock_direct,
        kdpso.cancel_status,
        kdpso.fd_document_status,
        kdpsoi.material_name,
        kdpsoi.material_number,
        kdm.old_number as materialOldNumber,
        kdm.specification,
        kdm.base_unit_id as unit,
        kdpsoi.qty,
        kdsc.`name` as  stockName

        FROM
        king_dee_other_stock_in kdpso
        JOIN king_dee_other_stock_in_item kdpsoi on kdpso.id = kdpsoi.f_id
        JOIN king_dee_material kdm on kdpsoi.material_id = kdm.id
        join king_dee_stock kdsc on kdsc.id = kdpsoi.stock_id
        join stock_correspond_position scp on kdsc.id = scp.stock_id
        ${ex.query}

        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by kdpso.sync_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="listV2" resultType="com.zfire.mall.manager.bean.stock.StockVO">
        SELECT
        kdm.id as materialId,
        kdm.number AS materialNumber,
        kdm.`name` AS materialName,
        kdm.old_number AS materialOldNumber,
        kdm.specification,
        kdm.base_unit_id,
        kdm.category_name,
        kdm.material_group_name
        FROM
        stock s
        JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
        JOIN king_dee_material kdm ON s.material_id = kdm.id
        JOIN stock_correspond sc on scp.correspond_id = sc.id
        ${ex.query}
        and scp.stock_id = s.stock_id
        group by kdm.id
        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by kdm.create_time desc
        </if>

        ${ex.orderBy}
    </select>
    <select id="listCustomerV2" resultType="com.zfire.mall.manager.bean.stock.StockCustomerReserved">
        SELECT
        roi.customer_id,
        roi.customer_name,
        roi.customer_number,
        kdm.category_name,
        roi.material_code as materialNumber,
        roi.material_old_number,
        roi.material_id,
        roi.specification,
        roi.material_name,
        roi.unit,
        sum(roi.reserved_num) as reservedNum
        FROM
        reserved_order ro
        JOIN reserved_order_item roi on roi.reserved_id = ro.id
        join king_dee_material kdm on kdm.id = roi.material_id
        ${ex.query}

        and roi.status = 1
        GROUP BY roi.customer_id,roi.material_id
        <if test="ex.orderBy == null or ex.orderBy ==''">
            order by roi.create_time desc
        </if>

        ${ex.orderBy}
    </select>
    <select id="listOne" resultType="com.zfire.mall.manager.bean.stock.StockBean">
        SELECT
        kdm.id as materialId,
        kdm.number AS materialNumber,
        kdm.`name` AS materialName,
        kdm.old_number AS materialOldNumber,
        kdm.specification,
        kdm.base_unit_id,
        kdm.category_name,
        kdm.material_group_name
        FROM
        stock s
        JOIN stock_correspond_position scp on scp.stock_id = s.stock_id
        JOIN king_dee_material kdm ON s.material_id = kdm.id
        JOIN stock_correspond sc on scp.correspond_id = sc.id
        and scp.stock_id = s.stock_id
        <if test="correspondId != null and correspondId.size > 0">
            AND scp.correspond_id in
            <foreach item="item" index="index" collection="correspondId" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
         and kdm.id = #{materialId}
        group by kdm.id
    </select>
</mapper>