-- 更新库存-排产-未定
UPDATE daily_report r
    INNER JOIN (
        select sum(v.scount) as scount, v.useOrgSid, v.modelSid, v.modelConfigSid
        from (
                 select bo.platformNo as scount, si.useOrgSid, si.modelSid, si.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_scm.scm_apply_inbound si on bo.purchaseRequisitionSid = si.vehicleOrderSid
                 where si.nodeState != '已办结'
                   and bo.isDelete <> 1
                   and LENGTH(offlineDate) > 0
                   and bo.sid not in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)
                 union all
                 select bo.platformNo as scount, bo.useOrgSid, bd.vehicleSid modelSid, bd.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_base.bus_vehicle_apply_detail bd on bd.sid = bo.purchaseApplyMediumModelSid
                 where LENGTH(offlineDate) < 1
                   and bo.sid not in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)) as v
        GROUP BY v.`useOrgSid`,
                 v.`modelSid`,
                 v.`modelConfigSid`
    ) AS s ON r.useOrgSid = s.useOrgSid
        AND r.vehModelSid = s.modelSid
        AND r.vehMConfigSid = s.modelConfigSid
SET r.`pcOrder_unreserve` = s.scount
WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 更新库存-排产已定

UPDATE daily_report r
    INNER JOIN (
        select sum(v.scount) as scount, v.useOrgSid, v.modelSid, v.modelConfigSid
        from (
                 select bo.platformNo as scount, si.useOrgSid, si.modelSid, si.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_scm.scm_apply_inbound si on bo.purchaseRequisitionSid = si.vehicleOrderSid
                 where si.nodeState != '已办结'
                   and bo.isDelete <> 1
                   and LENGTH(offlineDate) > 0
                   and bo.sid in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)
                 union all
                 select bo.platformNo as scount, bo.useOrgSid, bd.vehicleSid modelSid, bd.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_base.bus_vehicle_apply_detail bd on bd.sid = bo.purchaseApplyMediumModelSid
                 where LENGTH(offlineDate) < 1
                   and bo.sid in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)) as v
        GROUP BY v.`useOrgSid`,
                 v.`modelSid`,
                 v.`modelConfigSid`
    ) AS s ON r.useOrgSid = s.useOrgSid
        AND r.vehModelSid = s.modelSid
        AND r.vehMConfigSid = s.modelConfigSid
SET r.`pcOrder_unreserve` = s.scount
WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 更新库存- 排产小计

UPDATE daily_report r
    INNER JOIN (
        select sum(v.scount) as scount, v.useOrgSid, v.modelSid, v.modelConfigSid
        from (
                 select bo.platformNo as scount, si.useOrgSid, si.modelSid, si.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_scm.scm_apply_inbound si on bo.purchaseRequisitionSid = si.vehicleOrderSid
                 where si.nodeState != '已办结'
                   and bo.isDelete <> 1
                   and LENGTH(offlineDate) > 0
                 union all
                 select bo.platformNo as scount, bo.useOrgSid, bd.vehicleSid modelSid, bd.configSid modelConfigSid
                 from anrui_base.bus_vehicle_order bo
                          left join anrui_base.bus_vehicle_apply_detail bd on bd.sid = bo.purchaseApplyMediumModelSid
                 where LENGTH(offlineDate) < 1) as v
        GROUP BY v.`useOrgSid`,
                 v.`modelSid`,
                 v.`modelConfigSid`
    ) AS s ON r.useOrgSid = s.useOrgSid
        AND r.vehModelSid = s.modelSid
        AND r.vehMConfigSid = s.modelConfigSid
SET r.`pcOrder_unreserve` = s.scount
WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 简易订单-每日

UPDATE daily_report r
    INNER JOIN (select count(bbv.id) as scount, bbv.modelSid, bbv.modelConfigSid, bbm.orgSid useOrgSid
                from anrui_buscenter.bus_main_deposit bbm
                         left join anrui_buscenter.bus_deposit_vehicle bbv on bbv.billSid = bbm.sid
                where bbm.createTime like concat('%', CURDATE(), '%')
                GROUP BY `useOrgSid`,
                         `modelSid`,
                         `modelConfigSid`
    ) AS s ON r.useOrgSid = s.useOrgSid
        AND r.vehModelSid = s.modelSid
        AND r.vehMConfigSid = s.modelConfigSid
SET r.`simpleOrder_day` = s.scount
WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 简易订单-每月

UPDATE daily_report r
    INNER JOIN (select count(bbv.id) as scount, bbv.modelSid, bbv.modelConfigSid, bbm.orgSid useOrgSid
                from anrui_buscenter.bus_main_deposit bbm
                         left join anrui_buscenter.bus_deposit_vehicle bbv on bbv.billSid = bbm.sid
                where bbm.createTime like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
                GROUP BY `useOrgSid`,
                         `modelSid`,
                         `modelConfigSid`
    ) AS s ON r.useOrgSid = s.useOrgSid
        AND r.vehModelSid = s.modelSid
        AND r.vehMConfigSid = s.modelConfigSid
SET r.`simpleOrder_month` = s.scount
WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();