trigger LostCancelReport2Asset on Lost_cancel_report__c (after delete, after insert, after update) { 
 | 
/* 
 | 
    List<Lost_cancel_report__c> targetInsertLcrs = new List<Lost_cancel_report__c>(); 
 | 
    List<Lost_cancel_report__c> targetUpdateLcrs = new List<Lost_cancel_report__c>(); 
 | 
    List<Id> delAstIds = new List<Id>(); 
 | 
    List<String> OppoList = new List<String>(); 
 | 
    if (Trigger.isInsert) { 
 | 
        for (Lost_cancel_report__c lcr : Trigger.new) { 
 | 
            if (lcr.RecordType_DevName__c == 'PCL_Lost_report' && lcr.HP_RecordType_DevName__c == 'HP') { 
 | 
                targetInsertLcrs.add(lcr); 
 | 
            } 
 | 
            OppoList.add(lcr.Opportunity__c); 
 | 
        } 
 | 
        //ControllerUtil.updateSIodcList(OppoList);//询价失单,SI需求表无效化 
 | 
    } 
 | 
    else if (Trigger.isUpdate) { 
 | 
        for (Lost_cancel_report__c lcr : Trigger.new) { 
 | 
            if (lcr.RecordType_DevName__c == 'PCL_Lost_report' && lcr.HP_RecordType_DevName__c == 'HP' 
 | 
                && (lcr.CompetitorProduct1__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct1__c') 
 | 
                    || lcr.CompetitorProduct2__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct2__c') 
 | 
                    || lcr.CompetitorProduct3__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct3__c') 
 | 
                    || lcr.CompetitorProduct4__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct4__c') 
 | 
                ) 
 | 
            ) { 
 | 
                targetUpdateLcrs.add(lcr); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
    else if (Trigger.isDelete) { 
 | 
        for (Lost_cancel_report__c lcr : Trigger.old) { 
 | 
            if (lcr.CompetitorAsset1__c != null) { 
 | 
                delAstIds.add(lcr.CompetitorAsset1__c); 
 | 
            } 
 | 
            if (lcr.CompetitorAsset2__c != null) { 
 | 
                delAstIds.add(lcr.CompetitorAsset2__c); 
 | 
            } 
 | 
            if (lcr.CompetitorAsset3__c != null) { 
 | 
                delAstIds.add(lcr.CompetitorAsset3__c); 
 | 
            } 
 | 
            if (lcr.CompetitorAsset4__c != null) { 
 | 
                delAstIds.add(lcr.CompetitorAsset4__c); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
    if (targetInsertLcrs.size() > 0 || targetUpdateLcrs.size() > 0) { 
 | 
        Map<String, Asset> upsertAstMap = new Map<String, Asset>();   // lcr.id + 枝番 ==> Asset  
 | 
        for (Lost_cancel_report__c lcr : [ 
 | 
                 Select CompetitorAsset1__c, CompetitorProduct1__c, 
 | 
                        CompetitorAsset2__c, CompetitorProduct2__c, 
 | 
                        CompetitorAsset3__c, CompetitorProduct3__c, 
 | 
                        CompetitorAsset4__c, CompetitorProduct4__c, 
 | 
                        Opportunity__c, Submit_Day__c, 
 | 
                        Lost_By_Product__c, 
 | 
                        Lost_By_Brand__c, 
 | 
                        Opportunity__r.Hospital__c, Opportunity__r.Department_Class__c, Opportunity__r.AccountId 
 | 
                   From Lost_cancel_report__c 
 | 
                  Where (id IN :targetInsertLcrs or id IN :targetUpdateLcrs) 
 | 
                ] 
 | 
        ) { 
 | 
            if (lcr.CompetitorProduct1__c != null) { 
 | 
                Asset upsertAssert = new Asset( 
 | 
                    Id = lcr.CompetitorAsset1__c, 
 | 
                    Name = '*', 
 | 
                    Product2Id = lcr.CompetitorProduct1__c, 
 | 
                    Opportunity__c = lcr.Opportunity__c, 
 | 
                    InstallDate = lcr.Submit_Day__c, 
 | 
                    Hospital__c = lcr.Opportunity__r.Hospital__c, 
 | 
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c, 
 | 
                    AccountId = lcr.Opportunity__r.AccountId, 
 | 
                    Status = '使用中', 
 | 
                    Asset_Owner__c = '病院資産', 
 | 
                    IsCompetitorProduct = True 
 | 
                ); 
 | 
                if(lcr.Lost_By_Product__c != null){ 
 | 
                    upsertAssert.Manual_competitor_product_code__c = lcr.Lost_By_Product__c; 
 | 
                } 
 | 
                if(lcr.Lost_By_Brand__c != null){ 
 | 
                    upsertAssert.Manual_competitor_product_company__c = lcr.Lost_By_Brand__c; 
 | 
                } 
 | 
  
 | 
                upsertAstMap.put(lcr.Id + '_1', upsertAssert); 
 | 
            } 
 | 
            if (lcr.CompetitorProduct2__c != null) { 
 | 
                Asset upsertAssert = new Asset( 
 | 
                    Id = lcr.CompetitorAsset2__c, 
 | 
                    Name = '*', 
 | 
                    Product2Id = lcr.CompetitorProduct2__c, 
 | 
                    Opportunity__c = lcr.Opportunity__c, 
 | 
                    InstallDate = lcr.Submit_Day__c, 
 | 
                    Hospital__c = lcr.Opportunity__r.Hospital__c, 
 | 
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c, 
 | 
                    AccountId = lcr.Opportunity__r.AccountId, 
 | 
                    Status = '使用中', 
 | 
                    Asset_Owner__c = '病院資産', 
 | 
                    IsCompetitorProduct = True 
 | 
                ); 
 | 
                upsertAstMap.put(lcr.Id + '_2', upsertAssert); 
 | 
            } 
 | 
            if (lcr.CompetitorProduct3__c != null) { 
 | 
                Asset upsertAssert = new Asset( 
 | 
                    Id = lcr.CompetitorAsset3__c, 
 | 
                    Name = '*', 
 | 
                    Product2Id = lcr.CompetitorProduct3__c, 
 | 
                    Opportunity__c = lcr.Opportunity__c, 
 | 
                    InstallDate = lcr.Submit_Day__c, 
 | 
                    Hospital__c = lcr.Opportunity__r.Hospital__c, 
 | 
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c, 
 | 
                    AccountId = lcr.Opportunity__r.AccountId, 
 | 
                    Status = '使用中', 
 | 
                    Asset_Owner__c = '病院資産', 
 | 
                    IsCompetitorProduct = True 
 | 
                ); 
 | 
                upsertAstMap.put(lcr.Id + '_3', upsertAssert); 
 | 
            } 
 | 
            if (lcr.CompetitorProduct4__c != null) { 
 | 
                Asset upsertAssert = new Asset( 
 | 
                    Id = lcr.CompetitorAsset4__c, 
 | 
                    Name = '*', 
 | 
                    Product2Id = lcr.CompetitorProduct4__c, 
 | 
                    Opportunity__c = lcr.Opportunity__c, 
 | 
                    InstallDate = lcr.Submit_Day__c, 
 | 
                    Hospital__c = lcr.Opportunity__r.Hospital__c, 
 | 
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c, 
 | 
                    AccountId = lcr.Opportunity__r.AccountId, 
 | 
                    Status = '使用中', 
 | 
                    Asset_Owner__c = '病院資産', 
 | 
                    IsCompetitorProduct = True 
 | 
                ); 
 | 
                upsertAstMap.put(lcr.Id + '_4', upsertAssert); 
 | 
            } 
 | 
        } 
 | 
        system.debug('233332'+upsertAstMap.values()); 
 | 
        ControllerUtil.upsertAsset(upsertAstMap.values()); 
 | 
        List<Lost_cancel_report__c> updateLcrs = new List<Lost_cancel_report__c>(); 
 | 
        for (Lost_cancel_report__c lcr : targetInsertLcrs) { 
 | 
            if (upsertAstMap.containsKey(lcr.Id + '_1') || upsertAstMap.containsKey(lcr.Id + '_2') 
 | 
                    || upsertAstMap.containsKey(lcr.Id + '_3') || upsertAstMap.containsKey(lcr.Id + '_4') 
 | 
            ) { 
 | 
                Lost_cancel_report__c updLcr = new Lost_cancel_report__c(Id = lcr.Id); 
 | 
                updateLcrs.add(updLcr); 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_1')) { 
 | 
                    updLcr.CompetitorAsset1__c = upsertAstMap.get(lcr.Id + '_1').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_2')) { 
 | 
                    updLcr.CompetitorAsset2__c = upsertAstMap.get(lcr.Id + '_2').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_3')) { 
 | 
                    updLcr.CompetitorAsset3__c = upsertAstMap.get(lcr.Id + '_3').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_4')) { 
 | 
                    updLcr.CompetitorAsset4__c = upsertAstMap.get(lcr.Id + '_4').Id; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        for (Lost_cancel_report__c lcr : targetUpdateLcrs) { 
 | 
            if (upsertAstMap.containsKey(lcr.Id + '_1') || upsertAstMap.containsKey(lcr.Id + '_2') 
 | 
                    || upsertAstMap.containsKey(lcr.Id + '_3') || upsertAstMap.containsKey(lcr.Id + '_4') 
 | 
            ) { 
 | 
                Lost_cancel_report__c updLcr = new Lost_cancel_report__c(Id = lcr.Id); 
 | 
                updateLcrs.add(updLcr); 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_1')) { 
 | 
                    updLcr.CompetitorAsset1__c = upsertAstMap.get(lcr.Id + '_1').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_2')) { 
 | 
                    updLcr.CompetitorAsset2__c = upsertAstMap.get(lcr.Id + '_2').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_3')) { 
 | 
                    updLcr.CompetitorAsset3__c = upsertAstMap.get(lcr.Id + '_3').Id; 
 | 
                } 
 | 
                if (upsertAstMap.containsKey(lcr.Id + '_4')) { 
 | 
                    updLcr.CompetitorAsset4__c = upsertAstMap.get(lcr.Id + '_4').Id; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        update updateLcrs; 
 | 
    } 
 | 
    if (delAstIds.size() > 0) { 
 | 
        ControllerUtil.deleteAsset(delAstIds); 
 | 
    }*/ 
 | 
} 
 |