D C
2023-05-26 9a0ef802a678ffc421fc1d416f7f867e89e5536a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
trigger SIUploadFile on SI_Attachment__c (before insert,after insert,after update) {
     List<Id> copyUserIds = new List<Id>();
    List<SI_Attachment__c> newList = new List<SI_Attachment__c>();
    Map<String,Integer> countNumber = new  Map<String,Integer>();
    Map<id,id> SODOwnerMap = new Map<id,id>();
    Map<id,id> SODOwnerManMap = new Map<id,id>();
    Map<id,id> SODOwnerMinisterMap = new Map<id,id>();
    Map<id,id> SODOwnerMajorMap = new Map<id,id>();//总监
    Map<id,id> SODOOpporAssisMap = new Map<id,id>();//询价助理
    if(trigger.isInsert&&trigger.isAfter){
        List<id> SI_attidList = new List<id>();
        for(SI_Attachment__c sia : trigger.new){
            if( sia.Type__c == 'CAD图纸'||
                    sia.Type__c =='手术室净化平面图(含设备定位)'||
                        sia.Type__c == '目标术间吊塔定位图' ||
                            sia.Type__c =='目标术间净化立体图' ||
                                sia.Type__c =='示教点楼层平面图' ||
                                    sia.Type__c =='楼层建筑平面图' ||
                                        sia.Type__c =='其他工程文件清单'||
                                            sia.Type__c == '招标参数'||
                                                sia.Type__c == '标书'||
                                                    sia.Type__c == '项目方案书'){
                SI_attidList.add(sia.id);
            }
        }
        if(SI_attidList.size()>0){
            ControllerUtil.UpdateSIFunctionStatus(SI_attidList);
            system.debug(SI_attidList);
        }
    }
 
    if(trigger.isInsert && trigger.isBefore){
        
        List<id> oppsId = new List<id>();
        for(SI_Attachment__c siAc : trigger.new){
            oppsId.add(siAc.Opportunity_ID__c);
            siAc.Opportunity_sub_owner__c = siAc.Opportunity_sub_ownerID__c;
            newList.add(siAc);
            copyUserIds.add(siAc.OwnerId);
        }
        AggregateResult[] CountSAIList = [select count(id) countID,Opportunity_ID__c oppoid
                                                from SI_Attachment__c 
                                                where Opportunity_ID__c in:oppsId
                                                group by Opportunity_ID__c];
        for(AggregateResult siA : CountSAIList){
            countNumber.put((Id)siA.get('oppoid'),(Integer)siA.get('countID'));
        }
        for(SI_Attachment__c siAc : trigger.new){
            siAc.Last_Two_Number__c = 1+(countNumber.get(siAc.Opportunity_ID__c)==null?0:countNumber.get(siAc.Opportunity_ID__c));
        }
        List<IS_Opportunity_Demand__c> sodList = [select OwnerId,id,Opportunity_ID__c,SI_SalesManager__c,
                                SI_Majordomo__c,SI_Minister_Sell__c,Opportunity_ID__r.Sales_assistant_ID__c from IS_Opportunity_Demand__c where Opportunity_ID__c in:oppsId 
                                and Abort_Date__c = null ];
        for(IS_Opportunity_Demand__c sod: sodList){
            SODOwnerMap.put(sod.Opportunity_ID__c, sod.OwnerId);
            SODOwnerManMap.put(sod.Opportunity_ID__c, sod.SI_SalesManager__c);
            SODOwnerMinisterMap.put(sod.Opportunity_ID__c, sod.SI_Minister_Sell__c);
            SODOwnerMajorMap.put(sod.Opportunity_ID__c, sod.SI_Majordomo__c);
            SODOOpporAssisMap.put(sod.Opportunity_ID__c, sod.Opportunity_ID__r.Sales_assistant_ID__c);
        }
        
    }
    if(trigger.isUpdate){
        //=============================================================================================================
        if(trigger.isBefore){
            List<id> oppsId = new List<id>();
        
            for(SI_Attachment__c siAc : trigger.new){
                oppsId.add(siAc.Opportunity_ID__c);
                newList.add(siAc);
                copyUserIds.add(siAc.OwnerId);
            }
             List<IS_Opportunity_Demand__c> sodList = [select OwnerId,id,Opportunity_ID__c,SI_SalesManager__c,
                                    SI_Majordomo__c,SI_Minister_Sell__c,Opportunity_ID__r.Sales_assistant_ID__c from IS_Opportunity_Demand__c where Opportunity_ID__c in:oppsId 
                                    and Abort_Date__c = null ];
            for(IS_Opportunity_Demand__c sod: sodList){
                SODOwnerMap.put(sod.Opportunity_ID__c, sod.OwnerId);
                SODOwnerManMap.put(sod.Opportunity_ID__c, sod.SI_SalesManager__c);
                SODOwnerMinisterMap.put(sod.Opportunity_ID__c, sod.SI_Minister_Sell__c);
                SODOwnerMajorMap.put(sod.Opportunity_ID__c, sod.SI_Majordomo__c);
                SODOOpporAssisMap.put(sod.Opportunity_ID__c, sod.Opportunity_ID__r.Sales_assistant_ID__c);
            }
        }
        
       
        //=============================================================================================================
        List<id> oppid = new  List<id>();
        Map<id,Date> idMapDate = new Map<id,Date>();
        Map<id,Date> BidMapDate = new Map<id,Date>();
        for(SI_Attachment__c Sa : trigger.new){
            oppid.add(Sa.Opportunity_ID__c);
            idMapDate.put(Sa.Opportunity_ID__c, Sa.ProJect_Confirm_Date__c);
            BidMapDate.put(Sa.Opportunity_ID__c, Sa.bid_Confrim_Date__c);
        }
        List<Opportunity> OppList = [select Project_decide_date__c,bid_Confrim_Date__c from Opportunity where id In:oppid];
        for(Opportunity op : OppList){
            if(idMapDate.get(op.id)!=null){
                op.Project_decide_date__c = idMapDate.get(op.id);
            }
            if(BidMapDate.get(op.id)!=null){
                op.bid_Confrim_Date__c = BidMapDate.get(op.id);
            }
            
        }
        update OppList;
    }
    if (copyUserIds.size() > 0) {
            Map<Id, User> copyUserMap = new Map<Id, User>([
                SELECT Id, Name, SalesManager__c, BuchangApprovalManagerSales__c, JingliApprovalManager__c, BuchangApprovalManager__c, ZongjianApprovalManager__c FROM User WHERE Id IN :copyUserIds
            ]);
            
            for (Integer i = 0; i < copyUserIds.size(); i++) {
                SI_Attachment__c local = newList[i];
                User loginUser = copyUserMap.get(copyUserIds[i]);
                local.SI_File_Manager_Sell__c = loginUser.SalesManager__c == null ? loginUser.Id : loginUser.SalesManager__c;
                local.SI_File_Minister_Sell__c = loginUser.BuchangApprovalManagerSales__c == null ? loginUser.Id : loginUser.BuchangApprovalManagerSales__c;
                local.SI_File_Minister_Service__c = loginUser.BuchangApprovalManager__c == null ? loginUser.Id : loginUser.BuchangApprovalManager__c;
                local.SI_File_Majordomo_Sell__c = loginUser.ZongjianApprovalManager__c == null ? loginUser.Id : loginUser.ZongjianApprovalManager__c;
                local.File_Upload_Date__c = Date.today();
                local.SI_SOD_Owner__c = SODOwnerMap.get(local.Opportunity_ID__c);
                local.SI_SOD_Owner_Manager__c = SODOwnerManMap.get(local.Opportunity_ID__c);
                local.Oppor_Assistant__c = SODOOpporAssisMap.get(local.Opportunity_ID__c);
            }
        }
    
}