游畅
2022-12-08 54e9fce17758136f2f5223038d30eeb32439c603
force-app/main/default/classes/OpportunityAndContactDailyUpdateBatch.cls
@@ -59,7 +59,7 @@
        system.debug(runTarget);
    }
   global Database.QueryLocator start(Database.BatchableContext BC) {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        if (salesdepartment == null || salesdepartment == '') {
            query = ' select Id,FSE_Main__c,GI_Main__c,SP_Main__c,GI_Product_Leader__c,GS_Product_Leader__c,GYN_Product_Leader__c,BF_owner__c, ';
            query += 'ET_Product_Leader__c,BF_Product_Leader__c,ENT_Product_Leader__c,FSE_GI_Vice_Leader__c,FSE_SP_Vice_Leader__c,GYN_owner__c,';
@@ -69,7 +69,7 @@
            query += 'ENT_owner_ID__c,Original_Date_Flag__c,ET_owner__c,URO_owner_ID__c ,URO_Produc_Leader__c,FSE_SP_Main_Leader__c, FSE_GI_Main_Leader__c, ParentId, OwnerId,Energy_LeaderStr__c,FSE_Extend_viceLeaderStr__c,Extend_Leader_Str__c,';
            query += 'FSE_Main__r.IsActive ,GI_Main__r.IsActive ,URO_owner_ID__r.IsActive ,ET_owner__r.IsActive ,';
            query += 'ENT_owner_ID__r.IsActive ,BF_owner__r.IsActive ,SP_Main__r.IsActive ,GYN_owner__r.IsActive ';
            query += ' from Account where Is_Active__c = \'有効\' and RecordType.DeveloperName = \'HP\'';
            query += ' from Account where Is_Active__c = \'有効\' and RecordType.DeveloperName = \'HP\' and Id=\'0011000000V9UKl\'';
            if (runTarget != null && runTarget.size() > 0) {
                query += ' and id = :runTarget ';
            }
@@ -89,6 +89,7 @@
            ' Salesdepartment_HP__c = \'' + salesdepartment + '\'';
        }
        system.debug('=====query:' + query);
        //Id=\'0011000000V9OGP\' and  测试用 给一条客户id
        return Database.getQueryLocator(query);
    
    }
@@ -97,27 +98,52 @@
         List<Account> deptClassUpdateList = [select Id, RecordType.DeveloperName, Parent.Original_Date_Flag__c, ParentId, Parent.ownerId, Parent.owner.Alias__c, OwnerId from Account where Parent.Is_Active__c = '有効' and Parent.RecordType.DeveloperName = 'HP' and Hospital_Department_Class__c = :hpList order by Hospital_Department_Class__c, Account_Sort__c];
         List<Account> deptUpdateList = [select Id, RecordType.DeveloperName, Parent.Parent.Original_Date_Flag__c, Parent.owner.Alias__c, Parent.ParentId, Parent.Parent.ownerId, OwnerId from Account where Parent.Parent.Is_Active__c = '有効' and Parent.Parent.RecordType.DeveloperName = 'HP' and Hospital__c = :hpList order by Hospital__c, Account_Sort__c];
         AccountDailyUpdateBatch.updateFromHosToContact(deptUpdateList, iflog);
         updateFromHosToContact(deptUpdateList, iflog);
         //20220415 you SWAG-CBUB2W start 
         updateFromHosToInquiryForm(deptUpdateList, iflog);
         //20220415 you SWAG-CBUB2W end
         //20220509 you SWAG-CBUB2W start
         updateFromHosToInquiryFormFSEMain(hpList, iflog);
         //20220509 you SWAG-CBUB2W end
    }
     //同步所属的询价(业务机会)的所有人
    public static void updateFromHosToContact(List<Account> deptUpdateList, BatchIF_Log__c iflog) {
       List<Opportunity> updateOpportunityList =
         List<Opportunity> updateOpportunityList =
            [select id, Ownerid, Account.ownerId from Opportunity
        where Accountid in:deptUpdateList
        and isOwnerDiffWithAccount__c = true
            AND owner_not_automatically_update__c = FALSE ];
        //20220928 you SWAG-CJP7TL start
        system.debug('deptUpdateList==='+deptUpdateList);
        Map<Id,String> mapoppid = new Map<Id,String>();
        for (Opportunity opp : updateOpportunityList) {
            if (opp.Ownerid != opp.Account.ownerId) {
                opp.Ownerid = opp.Account.ownerId;
                mapoppid.put(opp.id,opp.Account.ownerId);
            }
        }
        List<Task__c> taskList = [
                SELECT Id,OpportunityId__c,taskStatus__c
                FROM task__c
                WHERE OpportunityId__c in :mapoppid.keySet()
                AND (RecordType.Name = '中标结果确认' OR RecordType.Name = '失单报告任务')
                AND taskStatus__c = '02 接受'];
        system.debug(mapoppid.keySet()+'test1-----'+taskList);
        for(Task__c t: taskList){
            if(mapoppid.containsKey(t.OpportunityId__c)){
               t.Ownerid = mapoppid.get(t.OpportunityId__c);
               t.assignee__c = mapoppid.get(t.OpportunityId__c);
            }
        }
        Integer indexCon = 0;
        Database.SaveResult[] lsrUpdateCon = Database.update(updateOpportunityList, false);
        Database.SaveResult[] lsrUpdatetask = Database.update(taskList, false);
        //20220928 you SWAG-CJP7TL end
        for (Database.SaveResult lsrChild : lsrUpdateCon) {
            if (!lsrChild.isSuccess()) {
                iflog.Is_Error__c = 3;
@@ -134,12 +160,17 @@
     //同步所属的产品咨询单的所有人
    public static void updateFromHosToInquiryForm(List<Account> deptUpdateList, BatchIF_Log__c iflog) {
         List<Inquiry_form__c> updateInquiryFormList =
            [select id, Ownerid, Hospital_Name__r.ownerId from Inquiry_form__c
            [select id, Ownerid,Depart_Owner__c,Product_Owner__c, Hospital_Name__r.ownerId from Inquiry_form__c
        where Hospital_Name__c in:deptUpdateList
        and isOwnerDiffWithAccount__c = true ];
        for (Inquiry_form__c ifo : updateInquiryFormList) {
            if (ifo.Ownerid != ifo.Hospital_Name__r.ownerId) {
                ifo.Ownerid = ifo.Hospital_Name__r.ownerId;
                ifo.Depart_Owner__c = ifo.Hospital_Name__r.ownerId;
                //20221128 you DB202211530484  当勾选 转产品担当时,所有人可以手动更改,不需要同步跟科室所有人一致
                if(ifo.Product_Owner__c == false){
                    ifo.Ownerid = ifo.Hospital_Name__r.ownerId;
                }
            }
        }
        Integer indexCon = 0;
@@ -156,7 +187,40 @@
        }
    }
    //20220415 you SWAG-CBUB2W end 
    //20220509 you SWAG-CBUB2W start
    //FSE主担当变化
     public static void updateFromHosToInquiryFormFSEMain(List<SObject> hpList, BatchIF_Log__c iflog) {
         List<Inquiry_form__c> updateInquiryFormFSEList =
            [select id, Ownerid,Hospital_Name__c,Hospital_Name__r.Name, Hospital_Name__r.ownerId,FSE_Owner__c,
            Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c,Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c from Inquiry_form__c
        where Hospital__c in:hpList
        and isFSEDiffWithAccount__c = true ];
        for (Inquiry_form__c ifo : updateInquiryFormFSEList) {
            if (String.isNotBlank(ifo.Hospital_Name__r.Name) && (ifo.Hospital_Name__r.Name.contains('消化') || ifo.Hospital_Name__r.Name.contains('呼吸') || ifo.Hospital_Name__r.Name.contains('ET')) && ifo.FSE_Owner__c != ifo.Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c) {
                ifo.FSE_Owner__c = ifo.Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c;
            }
            if(String.isNotBlank(ifo.Hospital_Name__r.Name) && !ifo.Hospital_Name__r.Name.contains('消化')  && !ifo.Hospital_Name__r.Name.contains('呼吸') && !ifo.Hospital_Name__r.Name.contains('ET') &&  ifo.FSE_Owner__c != ifo.Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c){
               ifo.FSE_Owner__c = ifo.Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c;
            }
        }
        Integer indexCon = 0;
        Database.SaveResult[] lsrUpdateInq = Database.update(updateInquiryFormFSEList, false);
        for (Database.SaveResult lsrChild : lsrUpdateInq) {
            if (!lsrChild.isSuccess()) {
                iflog.Is_Error__c = 3;
                Database.Error emsg = lsrChild.getErrors()[0];
                iflog.ErrorLog__c += 'ownerId: ' + updateInquiryFormFSEList.get(indexCon).ownerId + ' \n'
                                     + 'InquiryFormID: ' + updateInquiryFormFSEList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n';
            }
            indexCon ++ ;
        }
    }
    //20220509 you SWAG-CBUB2W end
    global void finish(Database.BatchableContext BC) {
        system.debug('=====iflog:' + iflog.id);
        iflog.Log__c += 'OpportunityAndContactDailyUpdateBatch finish()\n';
@@ -191,4 +255,306 @@
        //Database.executeBatch(new ConsumablesApplyOrLendBatch(), 50);
        //20200819 ljh add end
    }
     @TestVisible private static void test() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;//可以多写点
    }
}