CREATE
    DEFINER = `root`@`%` PROCEDURE `pro_daily_report_update`()
BEGIN
    -- 更新库存-未定(库存状态不是出库0002且不是内销0007、且不是退库0004的。非删除、销售订单未锁定)
    UPDATE daily_report r INNER JOIN (SELECT COUNT(id) AS scount, `useOrgSid`, `modelSid`, `modelConfigSid`
                                      FROM anrui_base.`base_vehicle`
                                      WHERE vehicleState <> '0002'
                                        AND vehicleState <> '0004'
                                        AND vehicleState <> '0007'
                                        AND isDelete <> 1
                                        AND lockedState <> '02'
                                      GROUP BY `useOrgSid`, `modelSid`, `modelConfigSid`) AS s ON
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`stock_unreserve`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 更新库存-已定（库存状态不是出库0002且不是内销0007且不是退库0004的，非删除，锁定状态为销售订单锁定02）
    UPDATE daily_report r INNER JOIN (SELECT COUNT(id) AS scount, `useOrgSid`, `modelSid`, `modelConfigSid`
                                      FROM anrui_base.`base_vehicle`
                                      WHERE vehicleState <> '0002'
                                        AND vehicleState <> '0004'
                                        AND vehicleState <> '0007'
                                        AND isDelete <> 1
                                        AND lockedState = '02'
                                      GROUP BY `useOrgSid`, `modelSid`, `modelConfigSid`) AS s ON
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`stock_reserve`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
    -- 更新库存-小计
    UPDATE daily_report
    SET stock_subtotal=stock_unreserve + stock_reserve
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 更新库存-买断-未定（库存状态不是出库0002且不是内销0007且不是退库0004的，非删除，非销售订单锁定、已买断）
    UPDATE daily_report r INNER JOIN (SELECT COUNT(id) AS scount, `useOrgSid`, `modelSid`, `modelConfigSid`
                                      FROM anrui_base.`base_vehicle`
                                      WHERE vehicleState <> '0002'
                                        AND vehicleState <> '0004'
                                        AND vehicleState <> '0007'
                                        AND isDelete <> 1
                                        AND lockedState <> '02'
                                        AND settlementStatus = '2'
                                      GROUP BY `useOrgSid`, `modelSid`, `modelConfigSid`) AS s ON
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`stock_buyout_unreserve`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
    -- 更新库存-买断-已定（库存状态不是出库0002且不是内销0007且不是退库0004的，非删除，销售订单锁定，已买断）
    UPDATE daily_report r INNER JOIN (SELECT COUNT(id) AS scount, `useOrgSid`, `modelSid`, `modelConfigSid`
                                      FROM anrui_base.`base_vehicle`
                                      WHERE vehicleState <> '0002'
                                        AND vehicleState <> '0004'
                                        AND vehicleState <> '0007'
                                        AND isDelete <> 1
                                        AND lockedState = '02'
                                        AND settlementStatus = '2'
                                      GROUP BY `useOrgSid`, `modelSid`, `modelConfigSid`) AS s ON
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`stock_buyout_reserve`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
    -- 更新库存-买断-小计
    UPDATE daily_report
    SET `stock_buyout_subtotal`=`stock_buyout_unreserve` + `stock_buyout_reserve`
    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, v.carBrand
            from (
                     select ifnull(si.num, 0) as scount,
                            si.useOrgSid,
                            si.modelSid,
                            si.configSid         modelConfigSid,
                            bbm.carBrand
                     from anrui_base.bus_vehicle_apply bo
                              left join anrui_scm.scm_apply_inbound si on bo.sid = si.vehicleOrderSid
                              left join anrui_base.bus_vehicle_order bbo on bbo.purchaseRequisitionSid = bo.sid
                              left join anrui_base.base_vehicle_model bbm on bbm.sid = si.modelSid
                     where (si.nodeState != '已办结' and si.nodeState != '终止')
                       and bo.isDelete <> 1
                       and bo.applyTypeKey = '001'
                       and bo.sid not in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)
                     group by si.applicationCode, bo.createOrgSid, si.modelSid, si.configSid
                     union all
                     select ifnull(sum(bo.platformNo), 0) as scount,
                            bo.useOrgSid,
                            bd.vehicleSid,
                            bd.configSid                     modelConfigSid,
                            bbm.carBrand
                     from anrui_base.bus_vehicle_order bo
                              left join anrui_base.bus_vehicle_apply_detail bd
                                        on bd.sid = bo.purchaseApplyMediumModelSid
                              left join anrui_base.base_vehicle_model bbm on bbm.sid = bd.vehicleSid
                     where bo.isDelete <> 1
                       and LENGTH(offlineDate) = 0
                       and bo.orderStatus != '已作废'
                       and bo.sid not in (select bbbbo.linkSid from anrui_buscenter.bus_sales_vehicle_order bbbbo)
                     group by bo.createOrgSid, bd.vehicleSid, bd.configSid
                     union all
                     select ifnull(si.num, 0) as scount,
                            si.useOrgSid,
                            si.modelSid,
                            si.configSid         modelConfigSid,
                            bbm.carBrand
                     from anrui_base.bus_vehicle_apply bo
                              left join anrui_scm.scm_apply_inbound si on bo.sid = si.vehicleOrderSid
                              left join anrui_base.base_vehicle_model bbm on bbm.sid = si.modelSid
                     where (si.nodeState != '已办结' and si.nodeState != '终止')
                       and bo.isDelete <> 1
                       and bo.applyTypeKey = '004'
                     group by si.applicationCode, bo.createOrgSid, si.modelSid, si.configSid
                 ) 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 si.useOrgSid,
                            si.modelSid,
                            si.configSid            modelConfigSid,
                            bo.sid,
                            ifnull(ab.counts, 0) as scount
                     FROM anrui_base.bus_vehicle_apply bo
                              LEFT JOIN anrui_scm.scm_apply_inbound si ON bo.sid = si.vehicleOrderSid
                              LEFT JOIN (
                         SELECT bbbbo.linkSid,
                                ifnull(count(bbbbo.linkSid), 0) as counts
                         FROM anrui_buscenter.bus_sales_vehicle_order bbbbo
                                  LEFT JOIN anrui_buscenter.bus_sales_order bb ON bb.sid = bbbbo.salesOrderSid
                         WHERE LENGTH(bb.nodeState) > 0
                           AND bb.nodeState != '终止'
                         group by bbbbo.linkSid
                     ) ab ON ab.linkSid = bo.sid
                     WHERE (si.nodeState != '已办结' AND si.nodeState != '终止')
                       AND bo.isDelete <> 1
                     group by si.applicationCode, bo.createOrgSid, si.modelSid, si.configSid
                 ) 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_reserve` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 更新库存- 排产小计

    UPDATE daily_report
    SET pcOrder_subtotal=pcOrder_reserve + pcOrder_unreserve
    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(), '%')
                      and bbm.nodeState = '已办结'
                    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'), '%')
                      and bbm.nodeState = '已办结'
                    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();


-- 销售订单-本日-全款
    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bm.modelSid, bm.modelConfigSid, bo.useOrgSid
                    from anrui_buscenter.bus_sales_order bo
                             left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bo.sid
                             left join anrui_buscenter.bus_sales_order_vehicle bv on bv.salesOrderSid = bo.sid
                    where bo.closingDate like concat('%', CURDATE(), '%')
                      and bo.payTypeKey = 1
                      and bo.nodeState = '已办结'
                    group by useOrgSid, modelSid, modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`saleOrder_full_day` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
-- 销售订单-本日-贷款
    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bm.modelSid, bm.modelConfigSid, bo.useOrgSid
                    from anrui_buscenter.bus_sales_order bo
                             left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bo.sid
                             left join anrui_buscenter.bus_sales_order_vehicle bv on bv.salesOrderSid = bo.sid
                    where bo.closingDate like concat('%', CURDATE(), '%')
                      and bo.payTypeKey = 2
                      and bo.nodeState = '已办结'
                    group by useOrgSid, modelSid, modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`saleOrder_loan_day` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 销售订单本日-小计
    UPDATE daily_report
    SET saleOrder_subtotal_day=saleOrder_loan_day + saleOrder_full_day
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 销售订单-本月-全款
    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bm.modelSid, bm.modelConfigSid, bo.useOrgSid
                    from anrui_buscenter.bus_sales_order bo
                             left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bo.sid
                             left join anrui_buscenter.bus_sales_order_vehicle bv on bv.salesOrderSid = bo.sid
                    where bo.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
                      and bo.payTypeKey = 1
                      and bo.nodeState = '已办结'
                    group by useOrgSid, modelSid, modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`saleOrder_full_month` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
-- 销售订单-本月-贷款
    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bm.modelSid, bm.modelConfigSid, bo.useOrgSid
                    from anrui_buscenter.bus_sales_order bo
                             left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bo.sid
                             left join anrui_buscenter.bus_sales_order_vehicle bv on bv.salesOrderSid = bo.sid
                    where bo.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
                      and bo.payTypeKey = 2
                      and bo.nodeState = '已办结'
                    group by useOrgSid, modelSid, modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`saleOrder_loan_month` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 销售订单-本月-小计
    UPDATE daily_report
    SET saleOrder_subtotal_month=saleOrder_loan_month + saleOrder_full_month
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 销售订单-待交车累计（销售订单已办结且库存状态为库存和在途的）
    /* UPDATE daily_report r
         INNER JOIN (select count(bv.id) as scount, bv.useOrgSid, bv.modelSid, bv.modelConfigSid, bo.contractNo
                     from anrui_base.base_vehicle bv
                              left join anrui_buscenter.bus_sales_order_vehicle bov on bov.linkSid = bv.sid
                              left join anrui_buscenter.bus_sales_order bo on bo.sid = bov.salesOrderSid
                     where bo.nodeState = '已办结'
                       and (bv.vehicleState = '0001' or bv.vehicleState = '0005')
                     group by bo.useOrgSid, bv.modelSid, bv.modelConfigSid
         ) AS s ON r.useOrgSid = s.useOrgSid
             AND r.vehModelSid = s.modelSid
             AND r.vehMConfigSid = s.modelConfigSid
     SET r.`saleOrder_waitVeh_total` = s.scount
     WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();*/

    UPDATE daily_report r
        INNER JOIN (select sum(c.scount) as scount, c.useOrgSid, c.modelSid, c.modelConfigSid
                    from (select (b.scount - b.ssscount1 - b.ssscount) as scount,
                                 b.ssscount1                              s1,
                                 b.ssscount                               s2,
                                 b.useOrgSid,
                                 b.modelConfigSid,
                                 b.modelSid,
                                 b.contractNo
                          from (SELECT a.scount,
                                       a.useOrgSid,
                                       a.modelConfigSid,
                                       a.modelSid,
                                       a.contractNo,
                                       (SELECT count(bov.id) AS scount
                                        from anrui_buscenter.bus_sales_order_vehicle bov
                                                 LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bov.salesOrderSid
                                                 LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                 left join anrui_base.base_vehicle_state bs on bs.busSid = bov.sid
                                        WHERE bo.nodeState = '已办结'
                                          AND (bs.busState = '602' or bs.busState = '501' or bs.busState = '502' or
                                               bs.busState = '503' or bs.busState = '601' or bs.busState = '602')
                                          and bo.useOrgSid = a.useOrgSid
                                          and bm.modelSid = a.modelSid
                                          and bm.modelConfigSid = a.modelConfigSid
                                          and bo.contractNo = a.contractNo) as ssscount1,
                                       (SELECT count(bov.id) AS scount
                                        FROM anrui_base.base_vehicle bv
                                                 LEFT JOIN anrui_buscenter.bus_sales_order_vehicle bov ON bov.linkSid = bv.sid
                                                 LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bov.salesOrderSid
                                                 LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                 left join anrui_base.base_vehicle_state bs on bs.saleOrderSid = bo.sid
                                        WHERE bo.nodeState = '已办结'
                                          AND bv.vehicleState = '0002'
                                          and bo.useOrgSid = a.useOrgSid
                                          and bm.modelSid = a.modelSid
                                          and bm.modelConfigSid = a.modelConfigSid
                                          and bs.saleOrderSid is null
                                          and bo.contractNo = a.contractNo) as ssscount
                                FROM (
                                         SELECT count(bov.id) AS scount,
                                                bo.useOrgSid,
                                                bm.modelSid,
                                                bm.modelConfigSid,
                                                bo.contractNo
                                         FROM anrui_buscenter.bus_sales_order_vehicle bov
                                                  LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bov.salesOrderSid
                                                  LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                         WHERE bo.nodeState = '已办结'
                                         GROUP BY bo.useOrgSid,
                                                  bm.modelSid,
                                                  bm.modelConfigSid,
                                                  bo.contractNo
                                     ) a) b) c
                    group by c.useOrgSid, c.modelSid, c.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`saleOrder_waitVeh_total` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 本日销售全款（销售日期为本日、全款、主车（包含value为空的））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', CURDATE(), '%')
                            and bvoa.saleTypeKey = '001'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_full_day` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
-- 本日销售贷款（贷款、销售日期为本日、主车（包含value为空的））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', CURDATE(), '%')
                            and bvoa.saleTypeKey = '002'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_loan_day` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 本日销售小计
    UPDATE daily_report
    SET sale_subtotal_day=sale_loan_day + sale_full_day
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 本月销售全款（全款、销售日期本月、主车（包含value为空的））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
                            and bvoa.saleTypeKey = '001'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_full_month` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
-- 本月销售贷款（贷款、销售日期为本月、主车（包含value为空的））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
                            and bvoa.saleTypeKey = '002'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_loan_month` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 本月销售小计
    UPDATE daily_report
    SET sale_subtotal_month=sale_loan_month + sale_full_month
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 本年销售全款（全款、销售日期为本年、主车（包含value为空的））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
                            and bvoa.saleTypeKey = '001'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_full_year` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
-- 本年销售贷款（贷款、销售日期本年、主车（value为空的）））
    UPDATE daily_report r
        INNER JOIN (select sum(ss.scount) as scount, ss.modelSid, ss.modelConfigSid, ss.useOrgSid
                    from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                          from anrui_base.base_vehicle bv
                                   left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                                   left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                          where bv.salesDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
                            and bvoa.saleTypeKey = '002'
                            and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                                 length(bvo.materialTypeValue) = 0)
                          group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                    group by ss.useOrgSid, ss.modelSid, ss.modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`sale_loan_year` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 本年销售小计
    UPDATE daily_report
    SET sale_subtotal_year=sale_loan_year + sale_full_year
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 本日买断的台数

    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.solidDate 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.`buyout_day` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 本月买断的台数

    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.solidDate 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.`buyout_month` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 本年买断的台数
    UPDATE daily_report r
        INNER JOIN (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.solidDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
                    group by useOrgSid, modelSid, modelConfigSid
        ) AS s ON r.useOrgSid = s.useOrgSid
            AND r.vehModelSid = s.modelSid
            AND r.vehMConfigSid = s.modelConfigSid
    SET r.`buyout_year` = s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 交付-贷款-本日
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', CURDATE(), '%')
          and bo.payTypeKey = '2'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_loan_day = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 交付-全款-本日
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', CURDATE(), '%')
          and bo.payTypeKey = '1'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_full_day = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 交付-小计-本日
    UPDATE daily_report
    SET deliver_subtotal_day=deliver_full_day + deliver_loan_day
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 交付-贷款-本月
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
          and bo.payTypeKey = '2'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_loan_month = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 交付-全款-本月
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y-%m'), '%')
          and bo.payTypeKey = '1'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_full_month = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 交付-小计-本月
    UPDATE daily_report
    SET deliver_subtotal_month=deliver_full_month + deliver_loan_month
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 交付-贷款-本年
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
          and bo.payTypeKey = '2'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_loan_year = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 交付-全款-本年
    update daily_report r inner join (
        select count(bhi.id) as scount, bm.modelSid, bm.modelConfigSid, bh.useOrgSid
        from anrui_buscenter.bus_handover_items bhi
                 left join anrui_buscenter.bus_handover bh on bh.sid = bhi.handoverSid
                 left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bh.orderSid
                 left join anrui_buscenter.bus_sales_order_vehicle bv on bhi.vinSid = bv.linkSid
                 left join anrui_buscenter.bus_sales_order bo on bo.sid = bh.orderSid
        where bh.closingDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
          and bo.payTypeKey = '1'
        group by bh.useOrgSid, bm.modelSid, modelConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.modelSid
            and r.vehMConfigSid = s.modelConfigSid
    set deliver_full_year = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

    -- 交付-小计-本年
    UPDATE daily_report
    SET deliver_subtotal_year=deliver_full_year + deliver_loan_year
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 存销比
    update daily_report r inner join (
        select ddr.useOrgSid,
               ddr.vehModelSid,
               ddr.vehMConfigSid,
               (select ifnull(sum(ss.scount), 0) as scount
                from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                      from anrui_base.base_vehicle bv
                               left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                               left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                      where (DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m')
                          OR DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), '%Y-%m')
                          OR DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m')
                          )
                        and (bvoa.saleTypeKey = '001' or bvoa.saleTypeKey = '002')
                      group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                where ss.useOrgSid = ddr.useOrgSid
                  and ss.modelSid = ddr.vehModelSid
                  and ss.modelConfigSid = ddr.vehMConfigSid) as subtotal
        from daily_report ddr
        where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE()
        group by ddr.useOrgSid, ddr.vehModelSid, ddr.vehMConfigSid
    ) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set saleOfStockRatio =if(ROUND(s.subtotal / 3, 2) = 0,
                             stock_subtotal + pcOrder_subtotal,
                             ROUND((stock_subtotal + pcOrder_subtotal) / ROUND(s.subtotal / 3, 2), 2))
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 前三个月的销售台数之和
    update daily_report r inner join (
        select ddr.useOrgSid,
               ddr.vehModelSid,
               ddr.vehMConfigSid,
               (select ifnull(sum(ss.scount), 0) as scount
                from (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                      from anrui_base.base_vehicle bv
                               left join anrui_base.base_vehicle_out bvo on bv.sid = bvo.vinSid
                               left join anrui_base.base_vehicle_out_apply bvoa on bvoa.sid = bvo.mainSid
                      where (DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m')
                          OR DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), '%Y-%m')
                          OR DATE_FORMAT(bv.salesDate, '%Y-%m') LIKE
                             DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m')
                          )
                        and (bvoa.saleTypeKey = '001' or bvoa.saleTypeKey = '002')
                        and (bvo.materialTypeKey = '001' or bvo.materialTypeValue is null or
                             length(bvo.materialTypeValue) = 0)
                      group by bv.useOrgSid, bv.modelSid, bv.modelConfigSid) ss
                where ss.useOrgSid = ddr.useOrgSid
                  and ss.modelSid = ddr.vehModelSid
                  and ss.modelConfigSid = ddr.vehMConfigSid) as subtotal
        from daily_report ddr
        where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE()
        group by ddr.useOrgSid, ddr.vehModelSid, ddr.vehMConfigSid
    ) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set saleAllToThree =s.subtotal
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 库存小计
    update daily_report
    set stock_total = (stock_subtotal + pcOrder_subtotal)
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 欠款出库数量--截止当日欠款出库申请通过且未回清款的台数（欠款出库申请通过，且未认完款的台数总和加上欠款提车申请通过，且全款已出库未认完款的台数总和）
    update daily_report r inner join (SELECT SUM(cc.scount) AS scount,
                                             cc.useOrgSid,
                                             cc.vehMConfigSid,
                                             cc.vehModelSid
                                      FROM (
                                               SELECT COUNT(a.id) AS scount,
                                                      a.useOrgSid,
                                                      a.NAME,
                                                      a.contractNo,
                                                      a.vehMConfigSid,
                                                      a.vehModelSid
                                               FROM (
                                                        SELECT bdad.id,
                                                               ba.useOrgSid,
                                                               bm.modelConfigSid AS vehMConfigSid,
                                                               bm.modelSid       AS vehModelSid,
                                                               so.NAME,
                                                               SUM(s.reveivableMoney),
                                                               SUM(s.subscriptionMoney),
                                                               bo.contractNo
                                                        FROM anrui_buscenter.bus_delivered_apply_details bdad
                                                                 LEFT JOIN anrui_buscenter.bus_delivered_apply ba ON ba.sid = bdad.applySid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_vehicle bv ON bv.sid = bdad.busVinSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bv.salesOrderSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                                 LEFT JOIN (
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs
                                                                               ON fs.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              AND fs.auditState = 3
                                                            GROUP BY fd.sid
                                                            UNION ALL
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   0 AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     left join (select fs.*
                                                                                from anrui_fin.fin_selected_receivables_detailed fs
                                                                                where fs.auditState = 3) s
                                                                               on s.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              and s.id is null
                                                            GROUP BY fd.sid
                                                        ) s ON s.busVinSid = bv.sid
                                                                 LEFT JOIN anrui_portal.sys_organization so ON so.sid = ba.useOrgSid
                                                        WHERE ba.nodeState = '已办结'
                                                          AND ba.typeState = 1
                                                        GROUP BY bv.sid,
                                                                 ba.useOrgSid,
                                                                 bm.modelSid,
                                                                 bm.modelConfigSid
                                                        HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
                                               GROUP BY a.useOrgSid,
                                                        a.vehMConfigSid,
                                                        a.vehModelSid
                                               UNION ALL
                                               SELECT COUNT(a.id) AS scount,
                                                      a.useOrgSid,
                                                      a.name,
                                                      a.contractNo,
                                                      a.vehMConfigSid,
                                                      a.vehModelSid
                                               FROM (SELECT bdad.id,
                                                            ba.useOrgSid,
                                                            bm.modelConfigSid AS vehMConfigSid,
                                                            bm.modelSid       AS vehModelSid,
                                                            so.name,
                                                            SUM(s.reveivableMoney),
                                                            SUM(s.subscriptionMoney),
                                                            bo.contractNo
                                                     FROM anrui_buscenter.bus_arrears_carry_vehicle_details bdad
                                                              LEFT JOIN anrui_buscenter.bus_arrears_carry_vehicle_apply ba
                                                                        ON ba.sid = bdad.applySid
                                                              LEFT JOIN anrui_buscenter.bus_sales_order_vehicle bv ON bv.linkSid = bdad.vinSid
                                                              LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bv.salesOrderSid
                                                              LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                              LEFT JOIN anrui_base.base_vehicle bbv ON bbv.sid = bv.linkSid
                                                              LEFT JOIN (SELECT fd.reveivableMoney,
                                                                                fd.busVinSid,
                                                                                IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                                                                         FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                                  LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs
                                                                                            ON fs.receivablesSid = fd.sid
                                                                         WHERE fd.kxState = '01'
                                                                           AND fs.auditState = 3
                                                                         GROUP BY fd.sid
                                                                         UNION ALL
                                                                         SELECT fd.reveivableMoney,
                                                                                fd.busVinSid,
                                                                                0 AS subscriptionMoney
                                                                         FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                                  left join (select fs.*
                                                                                             from anrui_fin.fin_selected_receivables_detailed fs
                                                                                             where fs.auditState = 3) s
                                                                                            on s.receivablesSid = fd.sid
                                                                         WHERE fd.kxState = '01'
                                                                           and s.id is null
                                                                         GROUP BY fd.sid
                                                     ) s ON s.busVinSid = bv.sid
                                                              LEFT JOIN anrui_portal.sys_organization so ON so.sid = ba.useOrgSid
                                                     WHERE ba.nodeState = '已办结'
                                                       AND ba.paymentMethodKey = 1
                                                       AND bbv.vehicleState = '0002'
                                                     GROUP BY bv.sid,
                                                              ba.useOrgSid,
                                                              bm.modelSid,
                                                              bm.modelConfigSid
                                                     HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
                                               GROUP BY a.useOrgSid,
                                                        a.vehMConfigSid, a.vehModelSid) cc
                                      GROUP BY cc.useOrgSid,
                                               cc.vehMConfigSid,
                                               cc.vehModelSid
    ) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set arrears_carry_veh_count = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 欠款出库金融--截止当日欠款出库申请通过且未回清款的台数欠款总额（欠款出库申请通过，且未认完款的剩余认款总和加上欠款提车申请通过，且全款已出库未认完款的剩余认款总和）
    update daily_report r inner join (SELECT sum(cc.scount) AS scount,
                                             cc.useOrgSid,
                                             cc.vehMConfigSid,
                                             cc.vehModelSid
                                      FROM (
                                               SELECT SUM(a.scount) AS scount,
                                                      a.useOrgSid,
                                                      a.NAME,
                                                      a.contractNo,
                                                      a.vehMConfigSid,
                                                      a.vehModelSid
                                               FROM (
                                                        SELECT bdad.id,
                                                               ba.useOrgSid,
                                                               bm.modelConfigSid                                      AS vehMConfigSid,
                                                               bm.modelSid                                            AS vehModelSid,
                                                               so.NAME,
                                                               SUM(s.reveivableMoney),
                                                               SUM(s.subscriptionMoney),
                                                               bo.contractNo,
                                                               (
                                                                   SUM(s.reveivableMoney) - SUM(s.subscriptionMoney)) AS scount
                                                        FROM anrui_buscenter.bus_delivered_apply_details bdad
                                                                 LEFT JOIN anrui_buscenter.bus_delivered_apply ba ON ba.sid = bdad.applySid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_vehicle bv ON bv.sid = bdad.busVinSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bv.salesOrderSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                                 LEFT JOIN (
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs
                                                                               ON fs.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              AND fs.auditState = 3
                                                            GROUP BY fd.sid
                                                            UNION ALL
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   0 AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     left join (select fs.*
                                                                                from anrui_fin.fin_selected_receivables_detailed fs
                                                                                where fs.auditState = 3) s
                                                                               on s.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              and s.id is null
                                                            GROUP BY fd.sid
                                                        ) s ON s.busVinSid = bv.sid
                                                                 LEFT JOIN anrui_portal.sys_organization so ON so.sid = ba.useOrgSid
                                                        WHERE ba.nodeState = '已办结'
                                                          AND ba.typeState = 1
                                                        GROUP BY bv.sid,
                                                                 ba.useOrgSid,
                                                                 bm.modelSid,
                                                                 bm.modelConfigSid
                                                        HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
                                               GROUP BY a.useOrgSid,
                                                        a.vehMConfigSid,
                                                        a.vehModelSid
                                               UNION ALL
                                               SELECT SUM(a.scount) AS scount,
                                                      a.useOrgSid,
                                                      a.NAME,
                                                      a.contractNo,
                                                      a.vehMConfigSid,
                                                      a.vehModelSid
                                               FROM (
                                                        SELECT bdad.id,
                                                               ba.useOrgSid,
                                                               bm.modelConfigSid                                      AS vehMConfigSid,
                                                               bm.modelSid                                            AS vehModelSid,
                                                               so.NAME,
                                                               SUM(s.reveivableMoney),
                                                               SUM(s.subscriptionMoney),
                                                               bo.contractNo,
                                                               (
                                                                   SUM(s.reveivableMoney) - SUM(s.subscriptionMoney)) AS scount
                                                        FROM anrui_buscenter.bus_arrears_carry_vehicle_details bdad
                                                                 LEFT JOIN anrui_buscenter.bus_arrears_carry_vehicle_apply ba
                                                                           ON ba.sid = bdad.applySid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_vehicle bv ON bv.linkSid = bdad.vinSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bv.salesOrderSid
                                                                 LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bo.sid
                                                                 LEFT JOIN anrui_base.base_vehicle bbv ON bbv.sid = bv.linkSid
                                                                 LEFT JOIN (
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs
                                                                               ON fs.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              AND fs.auditState = 3
                                                            GROUP BY fd.sid
                                                            UNION ALL
                                                            SELECT fd.reveivableMoney,
                                                                   fd.busVinSid,
                                                                   0 AS subscriptionMoney
                                                            FROM anrui_fin.fin_uncollected_receivables_detailed fd
                                                                     left join (select fs.*
                                                                                from anrui_fin.fin_selected_receivables_detailed fs
                                                                                where fs.auditState = 3) s
                                                                               on s.receivablesSid = fd.sid
                                                            WHERE fd.kxState = '01'
                                                              and s.id is null
                                                            GROUP BY fd.sid
                                                        ) s ON s.busVinSid = bv.sid
                                                                 LEFT JOIN anrui_portal.sys_organization so ON so.sid = ba.useOrgSid
                                                        WHERE ba.nodeState = '已办结'
                                                          AND ba.paymentMethodKey = 1
                                                          AND bbv.vehicleState = '0002'
                                                        GROUP BY bv.sid,
                                                                 ba.useOrgSid,
                                                                 bm.modelSid,
                                                                 bm.modelConfigSid
                                                        HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
                                               GROUP BY a.useOrgSid,
                                                        a.vehMConfigSid,
                                                        a.vehModelSid
                                           ) cc
                                      GROUP BY cc.useOrgSid,
                                               cc.vehMConfigSid,
                                               cc.vehModelSid
    ) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set arrears_carry_veh_amount = ROUND(s.scount / 10000, 2)
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 金融未放款数量--- 截止当日贷款业务已出库且融资放款未认款的台数。
    update daily_report r inner join (
        SELECT count(a.id) AS scount,
               a.useOrgSid,
               a.NAME,
               a.contractNo,
               a.vehMConfigSid,
               a.vehModelSid
        FROM (SELECT bv.id,
                     bo.useOrgSid,
                     bm.modelConfigSid AS vehMConfigSid,
                     bm.modelSid       AS vehModelSid,
                     so.NAME,
                     SUM(s.reveivableMoney),
                     SUM(s.subscriptionMoney),
                     bo.contractNo
              from anrui_buscenter.bus_sales_order_vehicle bv
                       left join anrui_buscenter.bus_sales_order bo on bo.sid = bv.salesOrderSid
                       left join anrui_base.base_vehicle bbv on bbv.sid = bv.linkSid
                       left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bv.salesOrderSid
                       LEFT JOIN (
                  SELECT fd.reveivableMoney,
                         fd.busVinSid,
                         IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                  FROM anrui_fin.fin_uncollected_receivables_detailed fd
                           LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs ON fs.receivablesSid = fd.sid
                  WHERE fd.kxState = '01'
                    AND fs.auditState = 3
                    and fd.receivablesName = '融资放款'
                  GROUP BY fd.sid
                  UNION ALL
                  SELECT fd.reveivableMoney,
                         fd.busVinSid,
                         0 AS subscriptionMoney
                  FROM anrui_fin.fin_uncollected_receivables_detailed fd
                  WHERE fd.kxState = '01'
                    AND fd.receivablesName = '融资放款'
                    AND fd.sid NOT IN (SELECT fs.`receivablesSid`
                                       FROM anrui_fin.fin_selected_receivables_detailed fs
                                       WHERE fs.`auditState` = 3
                                         AND fs.`receivablesName` = '融资放款')
                  GROUP BY fd.sid
              ) s ON s.busVinSid = bv.sid
                       LEFT JOIN anrui_portal.sys_organization so ON so.sid = bo.useOrgSid
              where bo.payTypeKey = 2

                and bbv.vehicleState = '0002'
              group by bv.sid, bo.useOrgSid, bm.modelSid, bm.modelConfigSid
              HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
        GROUP BY a.useOrgSid,
                 a.vehMConfigSid,
                 a.vehModelSid) as s on
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.vehModelSid AND r.vehMConfigSid = s.vehMConfigSid
    SET r.`loan_not_count`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 金融未放款金额-- 截止当日贷款业务已出库且融资放款未认款的台数融资放款金额之和

    update daily_report r inner join (
        SELECT SUM(a.scount) AS scount,
               a.useOrgSid,
               a.NAME,
               a.contractNo,
               a.vehMConfigSid,
               a.vehModelSid
        FROM (SELECT bv.id,
                     bo.useOrgSid,
                     bm.modelConfigSid                                      AS vehMConfigSid,
                     bm.modelSid                                            AS vehModelSid,
                     so.NAME,
                     SUM(s.reveivableMoney),
                     SUM(s.subscriptionMoney),
                     bo.contractNo,
                     (
                         SUM(s.reveivableMoney) - SUM(s.subscriptionMoney)) AS scount
              FROM anrui_buscenter.bus_sales_order_vehicle bv
                       LEFT JOIN anrui_buscenter.bus_sales_order bo ON bo.sid = bv.salesOrderSid
                       LEFT JOIN anrui_base.base_vehicle bbv ON bbv.sid = bv.linkSid
                       LEFT JOIN anrui_buscenter.bus_sales_order_model bm ON bm.salesOrderSid = bv.salesOrderSid
                       LEFT JOIN (
                  SELECT fd.reveivableMoney,
                         fd.busVinSid,
                         IFNULL(SUM(fs.subscriptionMoney), 0) AS subscriptionMoney
                  FROM anrui_fin.fin_uncollected_receivables_detailed fd
                           LEFT JOIN anrui_fin.fin_selected_receivables_detailed fs ON fs.receivablesSid = fd.sid
                  WHERE fd.kxState = '01'
                    AND fs.auditState = 3
                    AND fd.receivablesName = '融资放款'
                  GROUP BY fd.sid
                  UNION ALL
                  SELECT fd.reveivableMoney,
                         fd.busVinSid,
                         0 AS subscriptionMoney
                  FROM anrui_fin.fin_uncollected_receivables_detailed fd
                  WHERE fd.kxState = '01'
                    AND fd.receivablesName = '融资放款'
                    AND fd.sid NOT IN (SELECT fs.`receivablesSid`
                                       FROM anrui_fin.fin_selected_receivables_detailed fs
                                       WHERE fs.`auditState` = 3
                                         AND fs.`receivablesName` = '融资放款')
                  GROUP BY fd.sid
              ) s ON s.busVinSid = bv.sid
                       LEFT JOIN anrui_portal.sys_organization so ON so.sid = bo.useOrgSid
              WHERE bo.payTypeKey = 2

                AND bbv.vehicleState = '0002'
              GROUP BY bv.sid, bo.useOrgSid, bm.modelSid, bm.modelConfigSid
              HAVING SUM(s.reveivableMoney) > SUM(s.subscriptionMoney)) a
        GROUP BY a.useOrgSid,
                 a.vehMConfigSid,
                 a.vehModelSid) as s on
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.vehModelSid AND r.vehMConfigSid = s.vehMConfigSid
    SET r.`loan_not_amount`=ROUND(s.scount / 10000, 2)
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 全年厂家开票数
    UPDATE daily_report r INNER JOIN (SELECT COUNT(id) AS scount, `useOrgSid`, `modelSid`, `modelConfigSid`
                                      FROM anrui_base.`base_vehicle`
                                      WHERE manDate like concat('%', DATE_FORMAT(CURDATE(), '%Y'), '%')
                                      GROUP BY `useOrgSid`, `modelSid`, `modelConfigSid`) AS s ON
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`manufacturer_invoicing_year`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


END