高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
public without sharing class Solution_ProgrammeTrigger extends Oly_TriggerHandler{
    private Map<Id,  Solution_Programme__c> newMap;
    private Map<Id, Solution_Programme__c> oldMap;
    private List<Solution_Programme__c> newList;
    private List<Solution_Programme__c> oldList;
    public Set<id> solProjectId = new Set<id>();
 
    public Solution_ProgrammeTrigger() {
        this.newMap = (Map<Id, Solution_Programme__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Solution_Programme__c>) Trigger.oldMap;
        this.newList = (List<Solution_Programme__c>) Trigger.new;
        this.oldList = (List<Solution_Programme__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
 
        // getSolutionProjectId();
        // Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
        // List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c from SolutionProjectRequirements__c where id = :solProjectId];
        // for(SolutionProjectRequirements__c solProject : solutionProJectList){
        //     solutionProJectMap.put(solProject.Id, solProject);
        // }
        // List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        for(Solution_Programme__c solutionPro : newList){
            if(solutionPro.Scheme_Type__c == '初次'){
                solutionPro.RecordTypeId = '01210000000gUGGAA2';
                // solutionPro.RecordTypeId = '012p0000000Rr3KAAS';
            }
            if(solutionPro.Scheme_Type__c == '结案'){
                solutionPro.RecordTypeId = '01210000000gUGFAA2';
                // solutionPro.RecordTypeId = '012p0000000Rr3PAAS';
            }
        }
        // if(solProjectUpdateList.size() > 0){
        //     update solProjectUpdateList;
        // }
 
    }
 
    protected override void beforeUpdate() {
        // 提交solution方案审批时,点击审批之后,更新solution方案之前,更新solution方案的状态以及solution项目需求的状态
        // 获取solution项目需求的id值
        getSolutionProjectId();
        updateSolutionProgrammeAndProjectStatus();
    }
 
    protected override void afterUpdate() {
        // 为solution项目需求的第一次提交日期赋值。
        getSolutionProjectId();
        updateSolutionProjectFirstSubDate();
    }
 
    protected override void afterInsert() {
    }
 
    protected override void afterDelete() {
    }
    public void getSolutionProjectId(){
        for(Solution_Programme__c sol : newList){
            solProjectId.add(sol.SolutionProjectRequirements__c);
        }
    }
 
    public void updateSolutionProjectFirstSubDate(){
        System.debug('进入after trigger ');
        Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
        List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c,Confirm_Result__c from SolutionProjectRequirements__c where id = :solProjectId];
        
 
        // 更新项目需求的确认结果 start
        List<SolutionProjectRequirements__c> solProjectUpdateResultList = new List<SolutionProjectRequirements__c>();
        List<Solution_Programme__c> solutionProgrammeList = [select id,SolutionProjectRequirements__c,Confirmation_Result__c from Solution_Programme__c where SolutionProjectRequirements__c = :solProjectId and Confirmation_Result__c != '' order by CreatedDate];
        Map<String,String> confirmResultMap = new Map<String,String>();
        for(Solution_Programme__c soll : solutionProgrammeList){
            confirmResultMap.put(soll.SolutionProjectRequirements__c, soll.Confirmation_Result__c);
        }
 
 
        for(SolutionProjectRequirements__c solProject : solutionProJectList){
            solutionProJectMap.put(solProject.Id, solProject);
            if(confirmResultMap.containsKey(solProject.Id)){
                if(solProject.Confirm_Result__c != confirmResultMap.get(solProject.Id)){
                    solProject.Confirm_Result__c = confirmResultMap.get(solProject.Id);
                    solProjectUpdateResultList.add(solProject);
                }
            }
        }
        // 更新项目需求的确认结果 end
        List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        
        for(Solution_Programme__c sol : newList){
            if(sol.Proposal_Date__c != null || sol.Submint_TF__c == true){
                System.debug('solution方案的提交日期不为空');
                if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).First_Proposal_Time__c == null){
                    System.debug('solution项目需求的第一次提交日期为空');
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).First_Proposal_Time__c = Datetime.now();
                    // solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
                }
                if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '03方案制作中'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '04方案提出';
                    
                }
                // 自动结案
                if(sol.ApprovalClosingProgramme__c == true && solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c != '07结案'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '07结案';
                }
                // 2020-09-25 mzy  新增 CHAN-BTJCPQ  方案类型为初案时,调回以后,项目需求的状态变回 start
               if(sol.Submint_TF__c == false && sol.Scheme_Type__c == '初次'){
                    if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '04方案提出'){
                    solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '03方案制作中';
                  }
                }
                // 2020-09-25 mzy 新增 CHAN-BTJCPQ  方案类型为初案时,调回以后,项目需求的状态变回 end
                solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
                        
            }
 
            // if(sol.Confirmation_Result__c != '' && sol.Confirmation_Result__c != null){
            //     if(){
 
            //     }
            // }
            // if(sol.Submint_TF__c == true ){
                // if(solutionProJectMap.containsKey(sol.SolutionProjectRequirements__c)){
                //     if(solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c == '03方案制作中')
                //         solutionProJectMap.get(sol.SolutionProjectRequirements__c).Status__c = '04方案提出';
                //         solProjectUpdateList.add(solutionProJectMap.get(sol.SolutionProjectRequirements__c));
                // }
            // }
        }
 
        if(solProjectUpdateList.size() > 0){
            update solProjectUpdateList;
        }
        if(solProjectUpdateResultList.size() > 0 ){
            update solProjectUpdateResultList;
        }
    }
 
    public void updateSolutionProgrammeAndProjectStatus(){
        Map<Id,SolutionProjectRequirements__c> solutionProJectMap = new Map<Id,SolutionProjectRequirements__c>();
        List<SolutionProjectRequirements__c> solutionProJectList = [select id,Status__c,First_Proposal_Time__c from SolutionProjectRequirements__c where id = :solProjectId];
        for(SolutionProjectRequirements__c solProject : solutionProJectList){
            solutionProJectMap.put(solProject.Id, solProject);
        }
        List<SolutionProjectRequirements__c> solProjectUpdateList = new List<SolutionProjectRequirements__c>();
        for(Solution_Programme__c solutionPro : newList){
            // 驳回无效
            if(solutionPro.RejectedInvalid__c == true){
                solutionPro.addError('请填写反馈结果,并按审批按钮');
            }
            if(solutionPro.ProcessOfApproval__c == true && (solutionPro.Confirmation_Result__c == null || solutionPro.Confirmation_Result__c == '')){
                solutionPro.addError('批准之前必须填写反馈结果!');
            }
            if(solutionPro.ProcessOfApproval__c == true && solutionPro.Confirmation_Result__c != null && solutionPro.Confirmation_Result__c != ''){
                if(solutionPro.Confirmation_Result__c == '01采纳/部分采纳'){
                    solutionPro.Status__c = '03采纳/部分采纳';
                    // solutionPro.Scheme_Type__c = '结案';
                    solutionPro.ProcessOfApproval__c = false;
                    if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
                        solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
                        solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
                    }
                }
                if(solutionPro.Confirmation_Result__c == '02未采纳'){
                    solutionPro.Status__c = '04未采纳';
                    // solutionPro.Scheme_Type__c = '结案';
                    solutionPro.ProcessOfApproval__c = false;
                    if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
                        solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
                        solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
                    }
                }
                if(solutionPro.Confirmation_Result__c == '04其他'){
                    solutionPro.Status__c = '06其他';
                    // solutionPro.Scheme_Type__c = '结案';
                    solutionPro.ProcessOfApproval__c = false;
                    if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
                        solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '06已反馈';
                        solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
                    }
                }
                if(solutionPro.Confirmation_Result__c == '03跟进中'){
                    solutionPro.Status__c = '05营业跟进中';
                    // solutionPro.Scheme_Type__c = '初次';
                    solutionPro.ProcessOfApproval__c = false;
                    if(solutionProJectMap.containsKey(solutionPro.SolutionProjectRequirements__c)){
                        solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c).Status__c = '05方案修改中';
                        solProjectUpdateList.add(solutionProJectMap.get(solutionPro.SolutionProjectRequirements__c));
                    }
                }
                
            }
        }
        if(solProjectUpdateList.size() > 0){
            update solProjectUpdateList;
        }
    }
}