高章伟
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
public without sharing class OpportunitySpecialApplyHandler extends Oly_TriggerHandler
{
    // 询价预测特殊对应
    private Map<Id, OpportunitySpecialApply__c> newMap;
    private Map<Id, OpportunitySpecialApply__c> oldMap;
    private List<OpportunitySpecialApply__c> newList;
    private List<OpportunitySpecialApply__c> oldList;
 
    Id recordTypeOpp =
    Schema.SObjectType.OpportunitySpecialApply__c.getRecordTypeInfosByDeveloperName().get('OpportunityRecordType').getRecordTypeId();
 
    public OpportunitySpecialApplyHandler() {
        this.newMap = (Map<Id, OpportunitySpecialApply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, OpportunitySpecialApply__c>) Trigger.oldMap;
        this.newList = (List<OpportunitySpecialApply__c>) Trigger.new;
        this.oldList = (List<OpportunitySpecialApply__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        // 批准后,将内容和理由赋值到询价
        updateWindow();
    }
    // taoqz 2020/07/02 start
    protected override void afterInsert() {
        updateOpp();
    }
    // taoqz 2020/07/02 end
    protected override void beforeUpdate() {
        // 批准后,将内容和理由赋值到询价
        updateWindow();
    }
 
    protected override void afterUpdate() {
        // 批准后,将内容和理由赋值到询价
        updateOpp();
    }
 
    private void updateWindow() {
 
        List<OCM_Management_Province__c> mpList = 
                    [select id, Name, Window1__c from OCM_Management_Province__c];
 
        // 取得OCSM管理省的营业窗口
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        for (OCM_Management_Province__c mp : mpList) {
            mpMap.put(mp.Name, mp);
        }
 
        // 设置审批人
        for (OpportunitySpecialApply__c n : newList) {
            OCM_Management_Province__c omp = mpMap.get(n.OCM_man_province_no_dealer__c);
            if (omp != null) {
                n.OCM_Window__c = omp.Window1__c;
            }
        }
 
    }
 
    // taoqz 2020/07/02 start
    // 更新注残预测
    
    // taoqz 2020/07/02 end
 
    private void updateOpp() {
        // 有重复更新询价 最新特殊对应 的情况,list 变map  Gzw 20201030 update
        // List<Opportunity> oppList = new List<Opportunity>();
        Map<String,Opportunity> oppMap = new Map<String,Opportunity>();
        List<Statu_Achievements__c> achList = new List<Statu_Achievements__c>();
        for (OpportunitySpecialApply__c n : newList) {
 
            Opportunity opp = new Opportunity();
            Statu_Achievements__c ach = new Statu_Achievements__c();
            //if (oldMap.get(n.Id).get('Status__c') != n.Status__c && n.Status__c == '已批准') {
            if ((Trigger.isInsert && n.Status__c == '已批准') 
                    || (Trigger.isUpdate && oldMap.get(n.Id).get('Status__c') != n.Status__c 
                        && n.Status__c == '已批准')) {    
                String strMemo;
                strMemo = '特殊对应:' + n.Apply_Content__c + ' ' + n.NMApply_Content_c__c;
                strMemo += '; 理由:' + n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);
                strMemo += '; 批准日期:' + n.Approval_Date__c.format();
 
                //if (n.recordType.DeveloperName == 'OpportunityRecordType') {
                if (n.RecordTypeId == recordTypeOpp) {    
                    // 询价特殊申请记录类型
                    opp.Id = n.Opportunity__c;
                    opp.SpecialApply__c = n.Id;
                    opp.SpecialApplyMemo__c = strMemo;
                    opp.Special_Apply_Content__c = n.Apply_Content__c;
                    opp.Special_Approval_Date__c = n.Approval_Date__c;
                    opp.Special_Apply_Reason__c = n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);
 
 
                    if (n.Apply_Content__c == '删除本月订货预测' || n.Apply_Content__c == '删除本月订发货预测') {
                        opp.Forecast_this_month__c = '';
 
                    } else if (n.Apply_Content__c == '新增本月订货预测') {
                        opp.Forecast_this_month__c = '本月订货预测';
 
                    } else if (n.Apply_Content__c == '新增本月订发货预测') {
                        opp.Forecast_this_month__c = '本月订发货预测';
                    }
                    // oppList.add(opp);
                    oppMap.put(opp.Id, opp);
 
                } else {
                    // 注残特殊申请记录类型
                    ach.Id = n.Achievements__c;
                    ach.SpecialApply__c = n.Id;
                    ach.Special_Apply_Content__c = n.Apply_Content__c;
                    ach.Special_NMApply_Content__c = n.NMApply_Content_c__c;
                    ach.Special_Approval_Date__c = n.Approval_Date__c;
                    ach.Special_Apply_Reason__c = n.Apply_Reason__c + ((n.Apply_Memo__c == null) ? '' : n.Apply_Memo__c);
                    ach.SpecialApplyMemo__c = strMemo;
                    // 2020/07/06 taoqz start 与月末判断有关,表明注残为手动审批,月末判断时跳过
                    ach.isManualApprove__c = true;
                    // 2020/07/06 taoqz end
                    if (n.Apply_Content__c == '新增本月发货预测') {
                        ach.Monthly_forecast_shipping__c = '本月发货预测';
 
                    } else if (n.Apply_Content__c == '删除本月发货预测') {
                        ach.Monthly_forecast_shipping__c = '';
                    }
                    //
                    if (n.NMApply_Content_c__c == '新增次月发货预测') {
                        ach.NM_forecast_shipping__c = '次月发货预测';
 
                    } else if (n.NMApply_Content_c__c == '删除次月发货预测') {
                        ach.NM_forecast_shipping__c = '';
                    }
                    //
                    achList.add(ach);
 
                    opp.Id = n.Opportunity__c;
                    opp.SpecialApply__c = n.Id;
                    // oppList.add(opp);
                    oppMap.put(opp.Id, opp);
                }
                
            }
        }
 
        // if (oppList.size() > 0) {
        //     update oppList;
        // }
        if (oppMap.size() > 0) {
            update oppMap.values();
        }
        if (achList.size() > 0) {
            update achList;
        }
    } 
}