INSERT INTO daily_report(busOrgSid, busOrgName, useOrgSid, useOrgName, brandSid, brandName, vehModelSid, vehMConfigSid)
SELECT DISTINCT o.`psid`            AS busOrgSid,
                op.`name`           AS busOrgName,
                o.`sid`             AS useOrgSid,
                o.`name`            AS useOrgName,
                m.`carBrand`        AS brandSid,
                m.`brandName`       AS brandName,
                bv.`modelSid`       AS vehModelSid,
                bv.`modelConfigSid` AS vehMConfigSid
FROM anrui_base.`base_vehicle` bv
         LEFT JOIN anrui_base.`base_vehicle_model` m ON m.sid = bv.`modelSid`
         LEFT JOIN anrui_portal.`sys_organization` o ON bv.`useOrgSid` = o.`sid`
         JOIN anrui_portal.`sys_organization` op ON op.sid = o.`psid`
WHERE bv.vehicleState != '0004'
  AND bv.isDelete != 1;



-- 修改
-- 更新库存-未定
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();
-- 更新库存-已定
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();
-- 更新库存-买断-未定
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();
-- 更新库存-买断-已定
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();