CREATE DEFINER=`root`@`%` PROCEDURE `pro_as_daily_report_create`()
BEGIN
    -- 插入当天日期数据前，先删除当天的旧数据
    delete from as_daily_report WHERE DATE_FORMAT(createTime, '%Y-%m-%d') = CURDATE();
    -- 从维修工单中生成数据，到临时表中
    INSERT INTO as_daily_report_temp(busOrgSid,busOrgName,createOrgSid,createOrgName,useOrgSid,useOrgName)	SELECT DISTINCT
                                                                                                                  so.sid as busOrgSid,
                                                                                                                  so.`name` as busOrgName,
                                                                                                                  op.sid as createOrgSid,
                                                                                                                  op.`name` as createOrgName,
                                                                                                                  o.sid as useOrgSid,
                                                                                                                  o.`name` as useOrgName
    FROM
        yxt_4sas.as_busrepair_bill as b
            LEFT JOIN anrui_portal.`sys_organization` o ON b.deptSid = o.`sid`
            JOIN anrui_portal.`sys_organization` op ON op.sid = o.`psid`
            JOIN anrui_portal.`sys_organization` so ON so.sid = op.`psid`;
    -- 从销售单中生成数据，到临时表中
    INSERT INTO as_daily_report_temp(busOrgSid,busOrgName,createOrgSid,createOrgName,useOrgSid,useOrgName)	SELECT DISTINCT
                                                                                                                  so.sid as busOrgSid,
                                                                                                                  so.`name` as busOrgName,
                                                                                                                  op.sid as createOrgSid,
                                                                                                                  op.`name` as createOrgName,
                                                                                                                  o.sid as useOrgSid,
                                                                                                                  o.`name` as useOrgName
    FROM
        yxt_sms.sms_sales_bill as b
            LEFT JOIN anrui_portal.`sys_organization` o ON b.deptSid = o.`sid`
            JOIN anrui_portal.`sys_organization` op ON op.sid = o.`psid`
            JOIN anrui_portal.`sys_organization` so ON so.sid = op.`psid`;

    -- 把临时表中数据去重后，插入到正式表
    INSERT INTO as_daily_report(busOrgSid,busOrgName,createOrgSid,createOrgName,useOrgSid,useOrgName) SELECT DISTINCT busOrgSid,busOrgName,createOrgSid,createOrgName,useOrgSid,useOrgName from as_daily_report_temp;

END