<?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="org.jeecg.modules.fbsMomOrder.mapper.FbsMomOrderdetailMapper">
    <select id="getFbsMomOrderdetailSum" resultType="java.util.HashMap">
        SELECT
            sum( modee.QualifiedInQty ) AS '已完成',
            sum( modee.Qty ) - sum( modee.QualifiedInQty ) AS '未完成'
        FROM
            mom_orderdetail
            modee LEFT JOIN mom_order mo ON modee.MoId = mo.MoId
        <!--WHERE-->
            <!--substring( mo.MoCode, 3, 4 ) = substring( CONVERT ( VARCHAR ( 100 ), GETDATE ( ), 112 ), 3, 4 )-->
        <!--DATE_FORMAT(mo.CreateTime, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m' )
        AND modee.Status <![CDATA[ <>]]> 5-->
</select>

<select id="getQualifiedInQty" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
    SELECT
    i.cInvName ,
    sum(mo.QualifiedInQty) as qualifiedinqty
    FROM
    mom_orderdetail mo
    LEFT JOIN Inventory i ON mo.InvCode = i.cInvCode
    where mo.Status <![CDATA[ <>]]> 5
    GROUP BY
    i.cInvName
</select>

<select id="getQualifiedInQtyWeek" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
   <!-- SELECT

        i.cInvName,
        sum( QualifiedInQty ) as QualifiedInQty
    FROM
        mom_orderdetail mo
        LEFT JOIN Inventory i ON mo.InvCode = i.cInvCode
        left join mom_morder mm on mo.modid=mm.modid
    WHERE
        datediff( WEEK, mm.DueDate, getdate ( ) ) = 0
    GROUP BY
        i.cInvName-->
        SELECT
            i.cInvName,
            sum( iQuantity ) AS QualifiedInQty
        FROM
          rdrecords10 mo
        LEFT JOIN Inventory i ON mo.cInvCode = i.cInvCode
        LEFT JOIN rdrecord10 mm ON mo.ID = mm.ID
        WHERE
            mm.cWhCode = '01'
           <!-- AND datediff( WEEK, mm.dDate, getdate ( ) ) = 0 sql修改-->
            AND YEARWEEK(date_format(mm.dDate,'%Y-%m-%d'),1) = YEARWEEK(now(),1)
        GROUP BY
            i.cInvName
    </select>

    <select id="getMonthList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            m.MoId,
            m.MoCode,
            i.cInvCode,
            i.cInvStd,
            i.cInvName,
            i.cInvDefine13,
            mo.Qty,
            mo.QualifiedInQty,
            m.Define12
        FROM
            mom_orderdetail mo
            LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
            left join mom_morder mm on mo.MoDId=mm.MoDId
        WHERE
            <!--datediff(
            MONTH,
            mm.StartDate,
            getdate()) =0-->
        <!-- substring( m.MoCode, 3, 4 ) = substring( CONVERT ( VARCHAR ( 100 ), GETDATE ( ), 112 ), 3, 4 )
         and mo.Status in ('3','4')-->
        mo.Status <![CDATA[ <>]]> 4
        AND mo.Status <![CDATA[ <>]]> 5
     ORDER BY
         m.createtime DESC
 </select>

    <select id="getMonthListStatreWork" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
        mo.cost_item_code as itemCode,
        ftp.memo as workCenter,
        m.MoId,
        m.MoCode,
        i.cInvCode,
        i.cInvStd,
        i.cInvName,
        i.cInvDefine13,
        mo.Qty,
        mo.QualifiedInQty,
        m.Define12,
        m.cost_item_code as costItemCode,
        mm.StartDate,
        mm.DueDate,
        mo.SortSeq as sortSeq,
        mo.SAPStatus as SAPStatus,
        ftp.id as fbsTechnologicalProcessId,
        ftp.name as fbsTechnologicalProcessName,
        (select group_concat(name ORDER BY sort separator '-->') from fbs_technological_process_item where ftp.id=fbs_technological_process_id) as processSequence
        FROM
        mom_orderdetail mo
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        left join mom_morder mm on mo.MoDId=mm.MoDId
        left join fbs_technological_process ftp on mo.InvCode=ftp.name
        WHERE

        <!--datediff(
        MONTH,
        mm.StartDate,
        getdate()) =0-->
        mo.Status = 3 and ftp.del_flag='0'
        <if test="fbsMomOrderdetail.mocode!=null and fbsMomOrderdetail.mocode!=''">
            AND m.MoCode=#{fbsMomOrderdetail.mocode}
        </if>
        <if test="fbsMomOrderdetail.itemCode!=null and fbsMomOrderdetail.itemCode!=''">
            AND mo.cost_item_code=#{fbsMomOrderdetail.itemCode}
        </if>
        <if test="fbsMomOrderdetail.costitemname!=null and fbsMomOrderdetail.costitemname!=''">
            AND mo.CostItemName LIKE "%" #{fbsMomOrderdetail.costitemname} "%"
        </if>
        <if test="fbsMomOrderdetail.workCenter!=null and fbsMomOrderdetail.workCenter!=''">
            AND ftp.memo LIKE  "%" #{fbsMomOrderdetail.workCenter} "%"
        </if>
        <if test="fbsMomOrderdetail.cinvcode!=null and fbsMomOrderdetail.cinvcode!=''">
            AND i.cInvCode=#{fbsMomOrderdetail.cinvcode}
        </if>
        <if test="fbsMomOrderdetail.cinvname!=null and fbsMomOrderdetail.cinvname!=''">
            AND i.cInvName=#{fbsMomOrderdetail.cinvname}
        </if>
        <if test="fbsMomOrderdetail.startDate!=null">
            AND  DATE_FORMAT(mm.StartDate,'%Y-%m-%d')=DATE_FORMAT(#{fbsMomOrderdetail.startDate},'%Y-%m-%d')
        </if>
        <if test="fbsMomOrderdetail.dueDate!=null">
            AND DATE_FORMAT(mm.DueDate,'%Y-%m-%d') =DATE_FORMAT(#{fbsMomOrderdetail.dueDate},'%Y-%m-%d')
        </if>
        ORDER BY
        m.createtime DESC
    </select>

 <select id="getMonthListStatre" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
     SELECT
     mo.cost_item_code as itemCode,
     ftp.memo as workCenter,
     m.MoId,
     m.MoCode,
     i.cInvCode,
     i.cInvStd,
     i.cInvName,
     i.cInvDefine13,
     mo.Qty,
     mo.QualifiedInQty,
     m.Define12,
     m.cost_item_code as costItemCode,
     mm.StartDate,
     mm.DueDate
     FROM
     mom_orderdetail mo
     LEFT JOIN mom_order m ON mo.MoId= m.MoId
     LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
     left join mom_morder mm on mo.MoDId=mm.MoDId
     left join fbs_technological_process ftp on mo.InvCode=ftp.name
     WHERE

     <!--datediff(
     MONTH,
     mm.StartDate,
     getdate()) =0-->
     ftp.del_flag='0' and
        mo.Status = 3
        <if test="fbsMomOrderdetail.mocode!=null and fbsMomOrderdetail.mocode!=''">
            AND m.MoCode=#{fbsMomOrderdetail.mocode}
        </if>
         <if test="fbsMomOrderdetail.itemCode!=null and fbsMomOrderdetail.itemCode!=''">
             AND mo.cost_item_code=#{fbsMomOrderdetail.itemCode}
         </if>
         <if test="fbsMomOrderdetail.costitemname!=null and fbsMomOrderdetail.costitemname!=''">
             AND mo.CostItemName LIKE "%" #{fbsMomOrderdetail.costitemname} "%"
         </if>
         <if test="fbsMomOrderdetail.workCenter!=null and fbsMomOrderdetail.workCenter!=''">
             AND ftp.memo LIKE  "%" #{fbsMomOrderdetail.workCenter} "%"
         </if>
         <if test="fbsMomOrderdetail.cinvcode!=null and fbsMomOrderdetail.cinvcode!=''">
             AND i.cInvCode=#{fbsMomOrderdetail.cinvcode}
         </if>
         <if test="fbsMomOrderdetail.cinvname!=null and fbsMomOrderdetail.cinvname!=''">
             AND i.cInvName=#{fbsMomOrderdetail.cinvname}
         </if>
        ORDER BY
        m.createtime DESC
    </select>

    <select id="getMonthListStatreReviewed" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
        mo.cost_item_code as itemCode,
        ftp.memo as workCenter,
        m.MoId,
        m.MoCode,
        i.cInvCode,
        i.cInvStd,
        i.cInvName,
        i.cInvDefine13,
        mo.Qty,
        mo.QualifiedInQty,
        m.Define12,
        mo.Status,
        mm.StartDate,
        mm.DueDate
        FROM
        mom_orderdetail mo
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        left join mom_morder mm on mo.MoDId=mm.MoDId
        left join fbs_technological_process ftp on mo.InvCode=ftp.name
        WHERE
        <!--datediff(
        MONTH,
        mm.StartDate,
        getdate()) =0-->
        <!--substring(m.MoCode, 3, 4)=SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )-->

        (

            <if test="fbsMomOrderdetail.yearMonth!=null and fbsMomOrderdetail.yearMonth!=''">
                <!--SUBSTRING ( CONVERT ( VARCHAR ( 100 ), mo.CloseTime, 112 ), 3, 4 )=#{fbsMomOrderdetail.yearMonth}
                or
                substring(m.MoCode, 3, 4)=#{fbsMomOrderdetail.yearMonth} sql修改-->
                SUBSTRING(DATE_FORMAT(mo.CloseTime,'%Y%m%d'),3,4)=#{fbsMomOrderdetail.yearMonth}
                OR
                substring(DATE_FORMAT(m.CreateTime,'%Y%m%d'),3,4)=#{fbsMomOrderdetail.yearMonth}
            </if>
            <if test="fbsMomOrderdetail.yearMonth==null or fbsMomOrderdetail.yearMonth==''">
                <!--datediff(
                MONTH,
                mo.CloseTime,
                getdate()) =0 sql
                OR
                substring(m.MoCode, 3, 4)=SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )sql修改-->
                DATE_FORMAT(mo.CloseTime,'%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m' )
                OR
                substring(DATE_FORMAT(m.CreateTime,'%Y%m%d'), 3, 4)=SUBSTRING(DATE_FORMAT(NOW(),'%Y%m%d'), 3, 4 )
    </if>
)
AND mo.Status = 4 and ftp.del_flag='0'
<if test="fbsMomOrderdetail.mocode!=null and fbsMomOrderdetail.mocode!=''">
    AND m.MoCode=#{fbsMomOrderdetail.mocode}
</if>
<if test="fbsMomOrderdetail.itemCode!=null and fbsMomOrderdetail.itemCode!=''">
    AND mo.cost_item_code=#{fbsMomOrderdetail.itemCode}
</if>
<if test="fbsMomOrderdetail.costitemname!=null and fbsMomOrderdetail.costitemname!=''">
    AND mo.CostItemName LIKE "%" #{fbsMomOrderdetail.costitemname} "%"
</if>
<if test="fbsMomOrderdetail.workCenter!=null and fbsMomOrderdetail.workCenter!=''">
    AND ftp.memo LIKE  "%" #{fbsMomOrderdetail.workCenter} "%"
</if>
<if test="fbsMomOrderdetail.cinvcode!=null and fbsMomOrderdetail.cinvcode!=''">
    AND i.cInvCode=#{fbsMomOrderdetail.cinvcode}
</if>
<if test="fbsMomOrderdetail.cinvname!=null and fbsMomOrderdetail.cinvname!=''">
    AND i.cInvName=#{fbsMomOrderdetail.cinvname}
</if>
ORDER BY
m.createtime DESC
</select>

<select id="getMonthListNotStatre" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
SELECT
m.MoId,
m.MoCode,
i.cInvCode,
i.cInvStd,
i.cInvName,
i.cInvDefine13,
mo.Qty,
mo.QualifiedInQty,
m.Define12
FROM
mom_orderdetail mo
LEFT JOIN mom_order m ON mo.MoId= m.MoId
LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
left join mom_morder mm on mo.MoDId=mm.MoDId
WHERE

<!--datediff(
MONTH,
mm.StartDate,
getdate()) =0-->
        substring(m.MoCode, 3, 4)=#{month}
        AND mo.Status <![CDATA[ <>]]> 5
        ORDER BY
        m.createtime DESC
    </select>

    <select id="getMonthListGroupDefine12" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
        Round(SUM( (mo.Qty*ifnull(i.cInvDefine13,0))  ),2) cInvDefine13,
            SUM( mo.Qty ) Qty,
            SUM( mo.QualifiedInQty ) QualifiedInQty,
            m.Define12
        FROM
            mom_orderdetail mo
            LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        WHERE
            <!--SUBSTRING ( m.MoCode, 3, 4 ) = SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )-->
            <!--mo.Status <![CDATA[ <>]]> 4-->
            m.Define12 IS NOT NULL
            AND mo.Status <![CDATA[ <>]]> 5
        GROUP BY
            m.Define12
        HAVING Qty <![CDATA[ <>]]> QualifiedInQty
    </select>

    <select id="getDayList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            m.MoCode,
            i.cInvCode,
            i.cInvStd,
            i.cInvName,
            mo.Qty,
            mo.QualifiedInQty
        FROM
            mom_orderdetail mo
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        left join mom_morder mm on mo.MoDId=mm.MoDId
        WHERE
            DateDiff(
            dd,
            mm.StartDate,
            getdate()) =0
            AND mo.Status <![CDATA[ <>]]> 5
    </select>

    <select id="getListByOrderSheetCurrentStock" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT SUM(iQuantity) as materialPreparationTotal,
            cInvCode
        FROM
            CurrentStock
        WHERE
            cWhCode in ('02','10')
        GROUP BY
            cInvCode
    </select>

    <select id="getMomMoallocate" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            mm.Qty AS Qtys,
            mm.IssQty AS IssQty,
            mm.InvCode as mInvCode,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd,
            mi.cinvname mcinvname,
            mi.cinvcode mcinvcode
        FROM
            mom_moallocate mm
            LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
            LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
            LEFT JOIN Inventory mi ON mm.InvCode= mi.cInvCode
        WHERE
            mo.RelsUser IS NOT NULL
          <!--AND SUBSTRING ( m.MoCode, 3, 4 ) = SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )-->
          AND mo.Status <![CDATA[ <>]]> 4
          AND mo.Status <![CDATA[ <>]]> 5
          ORDER BY m.MoCode,mo.InvCode DESC
    </select>

    <select id="getMomMoallocateByMoCode" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            mm.Qty AS Qtys,
            mm.IssQty AS IssQty,
            mm.InvCode as mInvCode,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        FROM
            mom_moallocate mm
            LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
            LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
            where m.MoCode=#{moCode} AND mo.Status <![CDATA[ <>]]> 5
    </select>

    <select id="">

    </select>

    <select id="getListByOrderSheetMomMoallocate" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        <!--
        SELECT
            SUM ( mm.Qty ) as materialPreparationNumber,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        FROM
            mom_moallocate mm
            LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
            left join mom_order m on mo.MoId=m.MoId
            left join Inventory i on mo.InvCode=i.cInvCode
        GROUP BY
            mo.MoId,mo.InvCode,m.MoCode,i.cinvname,i.cinvcode,i.cinvstd -->
        SELECT SUM
        ( mm.Qty ) AS Qtys,
        sum(mm.IssQty) as IssQty,
        mo.InvCode,
        m.MoCode,
        i.cinvname,
        i.cinvcode,
        i.cinvstd
        FROM
        mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        WHERE mo.RelsUser is NOT NULL
        AND SUBSTRING ( m.MoCode, 3, 4 ) = SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )
        AND mo.Status <![CDATA[ <>]]> 5
        GROUP BY
        mo.MoId,
        mo.InvCode,
        m.MoCode,
        i.cinvname,
        i.cinvcode,
        i.cinvstd
       <!-- HAVING
            SUM( mm.Qty )-SUM(mm.IssQty)>0 -->
    </select>

    <select id="getListByOrderSheetEnough" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT SUM
            ( mm.Qty ) AS Qtys,
            sum(mm.IssQty) as IssQty,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        FROM
          mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        WHERE SUBSTRING ( m.MoCode, 3, 4 ) = SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )
        mo.Status <![CDATA[ <>]]> 5
        GROUP BY
            mo.MoId,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        HAVING
            SUM( mm.Qty )-SUM(mm.IssQty) <![CDATA[ <=]]>  0
    </select>

    <select id="getListByOrderSheetNotEnough" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
         SELECT
            mm.Qty AS Qtys,
            mm.IssQty as IssQty,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd,
            moi.cinvname mocinvname
        FROM
          mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mm.InvCode= i.cInvCode
        LEFT JOIN Inventory moi on moi.cInvCode=mo.InvCode
				where mm.Qty-mm.IssQty>0 and mo.RelsUser is NOT NULL
				AND mo.Status <![CDATA[ <>]]> 5
				AND SUBSTRING ( m.MoCode, 3, 4 ) = SUBSTRING ( CONVERT ( VARCHAR ( 100 ), GETDATE( ), 112 ), 3, 4 )
				ORDER BY m.MoCode
    </select>




    <select id="getApplyList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
       <!-- SELECT SUM
            ( mm.RequisitionQty ) - SUM ( RequisitionIssQty ) AS unfinishedQuantity,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        FROM
            mom_moallocate mm
            LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
            LEFT JOIN mom_order m ON mo.MoId= m.MoId
            LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        GROUP BY
            mo.MoId,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            i.cinvcode,
            i.cinvstd
        HAVING
            SUM ( mm.RequisitionQty )-sum(RequisitionIssQty)>0 -->
        SELECT
        mas.cInvCode,
        i.cInvName,
        ma.cMaker,
        (mas.iQuantity - mas.fOutQuantity) as unfinishedQuantity,
        <!--d.cDepName,sql修改-->
        ma.cDepName,
        ma.dDate
        FROM
        MaterialAppVouchs mas
        LEFT JOIN MaterialAppVouch ma ON mas.ID= ma.ID
        <!--LEFT JOIN Department d ON ma.cDepCode= d.cDepCode sql修改-->
        LEFT JOIN Inventory i ON mas.cInvCode= i.cInvCode
        WHERE
        <!--isnull(mas.iQuantity,0) - isnull(mas.fOutQuantity,0) >0 sql修改-->
        ifnull(mas.iQuantity,0) - ifnull(mas.fOutQuantity,0) >0
        AND (mas.cBCloser is NULL or mas.cBCloser='')
        <!--AND datediff( MONTH, ma.dDate, getdate ( ) ) = 0 sql修改-->
        AND DATE_FORMAT(ma.dDate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
    </select>

    <select id="getApplyCompleteSum" resultType="java.lang.Integer">
        SELECT
        count(mas.ID)
        FROM
        MaterialAppVouchs mas
        LEFT JOIN MaterialAppVouch ma ON mas.ID= ma.ID
        <!--LEFT JOIN Department d ON ma.cDepCode= d.cDepCode修改-->
        LEFT JOIN Inventory i ON mas.cInvCode= i.cInvCode
        WHERE
        mas.iQuantity - mas.fOutQuantity <![CDATA[ <=]]>0
    </select>

    <select id="getLatelyDayMomOrderList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            mo.MoCode,
            mod.QualifiedInQty,
            mo.CreateTime,
            mod.invcode
        FROM
          mom_order mo
        LEFT JOIN mom_orderdetail MOD ON mo.MoId = MOD.MoId
        WHERE
           <!-- DateDiff( dd, mo.CreateTime, getdate ( ) ) <![CDATA[ <=]]> 90 sql修改-->
            DATE_SUB(CURDATE(), INTERVAL 90 DAY)  <![CDATA[ <=]]> date(mo.CreateTime)
        AND MOD.Status <![CDATA[ <>]]> 5
    </select>

    <select id="getMomOrderDetailByMoCode" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            *
        FROM
            mom_order mo
            LEFT JOIN mom_orderdetail MOD ON mo.MoId = MOD.MoId
        WHERE
            mo.MoCode = #{moCode}
            AND MOD.Status <![CDATA[ <>]]> 5
    </select>


    <select id="getMoallocateList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            cs.materialPreparationTotal,
            mm.Qty AS Qtys,
            mm.IssQty AS IssQty,
            mm.InvCode AS mInvCode,
            mo.InvCode,
            m.MoCode,
            i.cinvname,
            mi.cinvname mcinvname,
            mo.Qty as modQty
        FROM
          mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        LEFT JOIN Inventory mi ON mm.InvCode= mi.cInvCode
        LEFT JOIN ( SELECT SUM(iQuantity) AS materialPreparationTotal, cInvCode FROM CurrentStock WHERE cWhCode IN ( '02', '10' ) GROUP BY cInvCode ) cs ON mm.InvCode= cs.cInvCode
        WHERE
            mo.RelsUser IS NOT NULL
            AND mo.Status  <![CDATA[ <> ]]> 4
            AND mo.Status <![CDATA[ <>]]> 5
            AND mm.Qty  <![CDATA[ <> ]]> mm.IssQty
            ORDER BY
            m.MoCode,
            mo.InvCode DESC
    </select>

    <select id="getMoallocateListGroupCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM
        (
        SELECT
        <!--TOP
        100 PERCENT--> mo.InvCode,
        m.MoCode,
        i.cinvname,
        mo.Qty
        FROM
        mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        LEFT JOIN Inventory mi ON mm.InvCode= mi.cInvCode
        LEFT JOIN ( SELECT SUM(iQuantity) AS materialPreparationTotal, cInvCode FROM CurrentStock WHERE cWhCode IN ( '02', '10' ) GROUP BY cInvCode ) cs ON mm.InvCode= cs.cInvCode
        WHERE
        mo.RelsUser IS NOT NULL
        AND mo.Status   <![CDATA[ <> ]]> 4
        AND mo.Status <![CDATA[ <>]]> 5
        AND mm.Qty <![CDATA[ <> ]]> mm.IssQty
        GROUP BY
        mo.InvCode,
        m.MoCode,
        i.cinvname,
        mo.Qty
        ORDER BY
        m.MoCode,
        mo.InvCode DESC
        ) a
    </select>

    <select id="getOrderDetailList" resultType="org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail">
        SELECT
            SUM ( mm.Qty ) AS Qtys,
            SUM ( mm.IssQty ) AS IssQty,
            mo.InvCode,
            m.MoCode,
            i.cinvname
        FROM
          mom_moallocate mm
        LEFT JOIN mom_orderdetail mo ON mm.MoDId= mo.MoDId
        LEFT JOIN mom_order m ON mo.MoId= m.MoId
        LEFT JOIN Inventory i ON mo.InvCode= i.cInvCode
        LEFT JOIN Inventory mi ON mm.InvCode= mi.cInvCode
        WHERE
            mo.RelsUser IS NOT NULL
            AND mo.Status  <![CDATA[ <> ]]> 4
            AND mo.Status <![CDATA[ <>]]> 5
            GROUP BY
            m.MoCode,
            mo.InvCode,
            i.cinvname
            ORDER BY
            m.MoCode,
            mo.InvCode DESC
    </select>


    <!-- 单个新增生产订单明细 -->
    <insert id="insertMomOrderDetailSap">
        insert into mom_orderdetail
        <foreach item="value" collection="map" index="key" open="("   separator="," close=")">
            ${key}
        </foreach>
        values
        <foreach item="value" collection="map" index="key"  open="("   separator="," close=")">
            #{value}
        </foreach>
    </insert>

    <!-- 单个新增生产订单资料 -->
    <insert id="insertMomMorderSap">
        insert into mom_morder
        <foreach item="value" collection="map" index="key" open="("   separator="," close=")">
            ${key}
        </foreach>
        values
        <foreach item="value" collection="map" index="key"  open="("   separator="," close=")">
            #{value}
        </foreach>
    </insert>



    <insert id="insertSAPMomOrderdetail" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            insert into mom_orderdetail (id,MoDId,MoId,InvCode,CostItemName,cost_item_code,Qty,SortSeq,QualifiedInQty,Status,SAPStatus,CloseTime,RelsUser,pull_date) values
            (
            #{item.id},
            #{item.MoDId},
            #{item.MoId},
            #{item.InvCode},
            #{item.CostItemName},
            #{item.cost_item_code},
            #{item.Qty},
            #{item.SortSeq},
            #{item.QualifiedInQty},
            #{item.Status},
            #{item.SAPStatus},
            #{item.CloseTime},
            #{item.RelsUser},
            #{item.pull_date}
            )
        </foreach>
    </insert>

    <!-- 获取SAP数据删除订单明细表 -->
    <delete id="deleteSAPMomOrderdetail" parameterType="java.util.List">
        delete from mom_orderdetail where MoId in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.moid}
        </foreach>
    </delete>

    <insert id="insertSAPMomMorder" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            insert into mom_morder (id,MoDId,StartDate,DueDate,pull_date) values
            (
            #{item.id},
            #{item.MoDId},
            #{item.StartDate},
            #{item.DueDate},
            #{item.pull_date}
            )
        </foreach>
    </insert>

    <!-- 获取SAP数据删除订单资料表 -->
    <delete id="deleteSAPMomMorder" parameterType="java.util.List">
        delete from mom_morder where MoDId in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.MoDId}
        </foreach>
    </delete>

    <insert id="insertMomMoallocateBatchSap" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            insert into mom_moallocate (id,MoDId,Qty,IssQty,InvCode,pull_date) values
            (
            #{item.id},
            #{item.MoDId},
            #{item.Qty},
            #{item.IssQty},
            #{item.InvCode},
            #{item.pull_date}
            )
        </foreach>
    </insert>

    <!-- 获取SAP数据删除订单子件表 -->
    <delete id="deleteMomMoallocateBatchSap" parameterType="java.util.List">
        delete from mom_moallocate where MoDId in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.MoDId}
        </foreach>
    </delete>

    <!-- 获取SAP数据删除订单表 -->
    <delete id="deleteSAPMomOrder" parameterType="java.util.List">
        delete from mom_order where MoId in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.moid}
        </foreach>
    </delete>

    <update id="updateOrderdetailStatus" parameterType="java.util.List">
        update mom_orderdetail set STATUS='5' where MoId in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>
</mapper>