CREATE DEFINER=`root`@`%` PROCEDURE `pro_daily_report_create`()
BEGIN
    -- 生成记录，从车辆台账中获取非厂家退车和删除的车型、配置
    -- INSERT INTO daily_report(busOrgSid,busOrgName,useOrgSid,useOrgName,brandSid,brandName,vehModelSid,vehMConfigSid,vehModeName,vehMConfigName ) SELECT o.`psid` AS busOrgSid,op.`name` AS busOrgName,mc.`useOrgSid`,o.`name` AS useOrgName,m.`carBrand` AS brandSid,m.`brandName`,mc.`modelSid` AS vehModelSid,mc.`configurationItemsSid` AS vehMConfigSid,m.`modelName` AS vehModeName,c.`configName` AS vehMConfigName FROM anrui_base.`base_vehmodel_config` mc JOIN anrui_base.`base_model_config` c ON c.sid=mc.`configurationItemsSid` JOIN anrui_base.`base_vehicle_model` m ON m.sid=mc.`modelSid` JOIN anrui_portal.`sys_organization` o  ON mc.`useOrgSid`=o.`sid` JOIN anrui_portal.`sys_organization` op ON op.sid=o.`psid`;
    -- INSERT INTO daily_report(busOrgSid,busOrgName,useOrgSid,useOrgName,brandSid,brandName,vehModelSid,vehMConfigSid) SELECT DISTINCT o.`psid` AS busOrgSid,op.`name` AS busOrgName,bv.`useOrgSid`,bv.`useOrgName`,bv.`carBrandSid` AS brandSid,bv.`carBrandName` AS brandName,bv.`modelSid` AS vehModelSid,bv.`modelConfigSid` AS vehMConfigSid FROM anrui_base.`base_vehicle` bv 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;
    -- 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,bv.`carBrandSid` AS brandSid,bv.`carBrandName` AS brandName,bv.`modelSid` AS vehModelSid,bv.`modelConfigSid` AS vehMConfigSid FROM anrui_base.`base_vehicle` bv 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;
    -- 插入当天日期数据前，先删除当天的旧数据
    DELETE FROM daily_report WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
    -- 从车辆台账中生成数据，到临时表中
    INSERT INTO daily_report_temp(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;
    -- 再从厂家采购订单中获取排产中的数据到临时表中
    INSERT INTO daily_report_temp(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,va.`carBrandSid`AS brandSid,va.`carBrandName` AS brandName,vad.`vehicleSid` AS vehModelSid,vad.`configSid` AS vehMConfigSid FROM anrui_base.`bus_vehicle_apply_detail` vad  JOIN anrui_base.`bus_vehicle_apply` va ON va.sid=vad.`applySid` JOIN anrui_portal.`sys_organization` o  ON va.`createOrgSid`=o.`sid` JOIN anrui_portal.`sys_organization` op ON op.sid=o.`psid`  WHERE va.`nodeState`='已办结';
    -- 把临时表中数据去重后，插入到正式表
    INSERT INTO daily_report(busOrgSid,busOrgName,useOrgSid,useOrgName,brandSid,brandName,vehModelSid,vehMConfigSid) SELECT DISTINCT busOrgSid,busOrgName,useOrgSid,useOrgName,brandSid,brandName,vehModelSid,vehMConfigSid FROM daily_report_temp;

END