CREATE
    DEFINER = `root`@`%` PROCEDURE `pro_daily_report_update`()
BEGIN
    -- 更新库存-未定
    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 <> '04'
                                      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();
    -- 更新库存-已定
    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 = '04'
                                      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();
    -- 更新库存-买断-未定
    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 <> '04'
                                        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();
    -- 更新库存-买断-已定
    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 = '04'
                                        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
            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 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_reserve` = 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_subtotal` = 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();

-- 销售订单-本日-小计
    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(), '%')
                    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_subtotal_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 = 1
                    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
                    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 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'), '%')
                    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_subtotal_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 = 1
                    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
                    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 r
        INNER JOIN (select count(bs.id) as scount, bo.useOrgSid, bm.modelSid, bm.modelConfigSid
                    from anrui_base.base_vehicle_state bs
                             left join anrui_buscenter.bus_sales_order bo on bo.sid = bs.saleOrderSid
                             left join anrui_buscenter.bus_sales_order_model bm on bm.salesOrderSid = bo.sid
                    where bs.busState = '401'
                      and (bs.finState = 1 or bs.carryVehicleState = 1)
                    group by bo.useOrgSid, bm.modelSid, bm.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 count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.salesDate 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.`sale_subtotal_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
                             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'
                    group by useOrgSid, modelSid, 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();
-- 本日销售贷款
    UPDATE daily_report r
        INNER JOIN (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'
                    group by useOrgSid, modelSid, 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 r
        INNER JOIN (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.salesDate 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.`sale_subtotal_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
                             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'
                    group by useOrgSid, modelSid, 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();
-- 本月销售贷款
    UPDATE daily_report r
        INNER JOIN (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'
                    group by useOrgSid, modelSid, 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 r
        INNER JOIN (select count(bv.id) as scount, bv.modelSid, bv.modelConfigSid, bv.useOrgSid
                    from anrui_base.base_vehicle bv
                    where bv.salesDate 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.`sale_subtotal_year` = 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
                             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'
                    group by useOrgSid, modelSid, 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();
-- 本年销售贷款
    UPDATE daily_report r
        INNER JOIN (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'
                    group by useOrgSid, modelSid, 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 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 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(), '%')
        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_subtotal_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('%', 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 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'), '%')
        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_subtotal_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'), '%')
          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 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'), '%')
        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_subtotal_year = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 存销比
    update daily_report r inner join (
        select ifnull(sum(dr.sale_subtotal_month), 0) as subtotal, dr.useOrgSid, dr.vehModelSid, dr.vehMConfigSid
        from daily_report dr
        where (DATE_FORMAT(createTime, '%Y-%m') LIKE DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m')
            OR DATE_FORMAT(createTime, '%Y-%m') LIKE DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 2 MONTH), '%Y-%m')
            OR DATE_FORMAT(createTime, '%Y-%m') LIKE DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m')
                  )
        group by dr.useOrgSid, dr.vehModelSid, dr.vehMConfigSid) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set saleOfStockRatio =if(s.subtotal = 0, stock_subtotal + pcOrder_subtotal,
                             ROUND((stock_subtotal + pcOrder_subtotal) / ROUND(s.subtotal / 3, 2), 2)),
        stock_total      = (stock_subtotal + pcOrder_subtotal)
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

-- 欠款出库数量--截止当日欠款出库申请通过且未回清款的台数
    update daily_report r inner join (
        select count(bdad.id) as scount, ba.useOrgSid, bm.modelConfigSid as vehMConfigSid, bm.modelSid as vehModelSid
        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.sid
                 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_fin.fin_uncollected_receivables_detailed fd on fd.busVinSid = bdad.busVinSid
                 left join anrui_fin.fin_selected_receivables_detailed fs on fs.receivablesSid = fd.sid
        where ba.nodeState = '已办结'
          and ba.typeState = 1
        group by bdad.busVinSid, ba.useOrgSid, bm.modelSid, bm.modelConfigSid
        having sum(fd.reveivableMoney) > ifnull(sum(fs.subscriptionMoney), 0)) 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(fd.reveivableMoney) - sum(fs.subscriptionMoney)) as scount,
               ba.useOrgSid,
               bm.modelConfigSid                                     as vehMConfigSid,
               bm.modelSid                                           as vehModelSid
        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.sid
                 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_state bs on bs.busSid = bdad.busVinSid
                 left join anrui_fin.fin_uncollected_receivables_detailed fd on fd.busVinSid = bdad.busVinSid
                 left join anrui_fin.fin_selected_receivables_detailed fs on fs.receivablesSid = fd.sid
        where ba.nodeState = '已办结'
          and ba.typeState = 1
        group by bdad.busVinSid, ba.useOrgSid, bm.modelSid, bm.modelConfigSid
        having sum(fd.reveivableMoney) > ifnull(sum(fs.subscriptionMoney), 0)
    ) s
        on r.useOrgSid = s.useOrgSid
            and r.vehModelSid = s.vehModelSid
            and r.vehMConfigSid = s.vehMConfigSid
    set arrears_carry_veh_amount = s.scount
    where DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();


-- 金融未放款数量--- 截止当日贷款业务已出库且融资放款未认款的台数
    update daily_report r inner join (
        select count(bv.id) as scount, bo.useOrgSid, bm.modelSid, bm.modelConfigSid, fd.reveivableMoney
        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 anrui_fin.fin_uncollected_receivables_detailed fd on fd.busVinSid = bv.sid
                 left join anrui_fin.fin_selected_receivables_detailed fs on fs.receivablesSid = fd.sid
        where bo.payTypeKey = 2
          and fd.receivablesName = '融资放款'
          and bbv.vehicleState = '0002'
        group by bv.sid, bo.useOrgSid, bm.modelSid, bm.modelConfigSid
        having fd.reveivableMoney > ifnull(sum(fs.subscriptionMoney), 0)) as s on
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`loan_not_count`=s.scount
    WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();

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

    update daily_report r inner join (
        select sum(fd.reveivableMoney) as scount, bo.useOrgSid, bm.modelSid, bm.modelConfigSid, fd.reveivableMoney
        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 anrui_fin.fin_uncollected_receivables_detailed fd on fd.busVinSid = bv.sid
                 left join anrui_fin.fin_selected_receivables_detailed fs on fs.receivablesSid = fd.sid
        where bo.payTypeKey = 2
          and fd.receivablesName = '融资放款'
          and bbv.vehicleState = '0002'
        group by bv.sid, bo.useOrgSid, bm.modelSid, bm.modelConfigSid
        having fd.reveivableMoney > ifnull(sum(fs.subscriptionMoney), 0)) as s on
                r.useOrgSid = s.useOrgSid AND r.vehModelSid = s.modelSid AND r.vehMConfigSid = s.modelConfigSid
    SET r.`loan_not_amount`=s.scount
    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