binxie
2023-06-26 de9565270a88b0749d17c1961cd41399c8483c96
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 * 保有機材に項目履歴ができるようになる
 */
trigger MakeAssetHistory on Asset (after insert, after update, after delete) {
    //add by gzw 20270718 start
    if (StaticParameter.EscapeVMCTrigger) {
        return ;
    }
    //add by gzw 20270718 end
    List<String> accIdList = new List<String>();
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
    List<Id> userIdList = new List<String>();
    List<String> newInspection_report_number_List = new List<String>();
// 20150119 TODO 削除  点検ロジックは 点検報告書にて実装 by katsu end
 
    if (Trigger.isDelete) {
        OFSInsReportAssetHistoryController.delAssetHistory();
    } else {
        String columnstr = System.Label.AssetHistory_Columns;
        List<String> columnList = columnstr.split(',');
        Schema.DescribeSObjectResult assetDesc = Asset.SObjectType.getDescribe();
        Map<String, Schema.SObjectField> fsMap = assetDesc.fields.getMap();
        List<AssetHistory__c> ahList = new List<AssetHistory__c>();
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
        List<String> ahDelExtIdList = new List<String>();
        List<AssetHistory__c> ahUpsertList = new List<AssetHistory__c>();
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
        for (Asset a : Trigger.new) {
            if (Trigger.isInsert) {
                Map<String, AssetHistory__c> historyMap = OFSInsReportAssetHistoryController.astHistoryMap.get(a.Id);
                if (historyMap == null || historyMap.containsKey('Id') == false) {
                    AssetHistory__c ah = new AssetHistory__c();
                    ah.AssetId__c = a.Id;
                    ah.Operator__c = System.Userinfo.getUserId();
                    ah.Field_Api_Name__c = 'Id';
                    ah.Field_Lable_Name__c = 'Id';
                    ahList.add(ah);
                    if (historyMap == null) {
                        historyMap = new Map<String, AssetHistory__c>();
                        OFSInsReportAssetHistoryController.astHistoryMap.put(a.Id, historyMap);
                    }
                    historyMap.put('Id', ah);
                }
            } else {
                Map<String, AssetHistory__c> historyMap = OFSInsReportAssetHistoryController.astHistoryMap.get(a.Id);
                for (String colApiName : columnList) {
                    colApiName = colApiName.trim();
                    if (colApiName == 'Inspection_report_number__c') {
                        // 定義しないはず、念のため、ここでcontinue;
                        continue;
                    }
                    if (Trigger.oldMap.get(a.Id).get(colApiName) != Trigger.newMap.get(a.Id).get(colApiName)) {
                        AssetHistory__c ah = new AssetHistory__c();
                        if (historyMap == null || historyMap.containsKey(colApiName) == false) {
                            if (colApiName == 'AccountId') {
                                accIdList.add(String.valueOf(Trigger.oldMap.get(a.Id).get(colApiName)));
                                accIdList.add(String.valueOf(Trigger.newMap.get(a.Id).get(colApiName)));
                            }
                            ah.AssetId__c = a.Id;
                            ah.Operator__c = System.Userinfo.getUserId();
                            ah.Field_Api_Name__c = colApiName;
                            ah.Field_Lable_Name__c = fsMap.get(colApiName).getDescribe().getLabel();
                            ah.OldValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.oldMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.oldMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.oldMap.get(a.Id).get(colApiName));
                            ah.NewValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.newMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.newMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.newMap.get(a.Id).get(colApiName));
                            if (historyMap == null) {
                                historyMap = new Map<String, AssetHistory__c>();
                                OFSInsReportAssetHistoryController.astHistoryMap.put(a.Id, historyMap);
                            }
                            historyMap.put(colApiName, ah);
                        } else {
                            // newValueを更新
                            ah = historyMap.get(colApiName);
                            if (colApiName == 'AccountId') {
                                accIdList.add(String.valueOf(Trigger.newMap.get(a.Id).get(colApiName)));
                            }
                            ah.NewValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.newMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.newMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.newMap.get(a.Id).get(colApiName));
                        }
                        ahList.add(ah);
                    }
                }
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
                // 更新の場合、点検の履歴はupsertにします、1レコードにまとめます
                // upserMapのキーは Inspection_report_number__c です。
                Map<String, AssetHistory__c> historyUpsertMap = OFSInsReportAssetHistoryController.astHistoryUpsertMap.get(a.Id);
                Boolean isNeedUpsert = false;
                Boolean isNeedDel = false;
//System.debug('old Inspection_report_number__c' + Trigger.oldMap.get(a.Id).get('Inspection_report_number__c'));
//System.debug('new Inspection_report_number__c' + a.Inspection_report_number__c);
                if (Trigger.oldMap.get(a.Id).get('Inspection_report_number__c') != a.Inspection_report_number__c
                        && isNeedUpsert == false) {
                    isNeedUpsert = true;
                    if (String.isBlank(a.Inspection_report_number__c)) {
                        isNeedDel = true;
                    }
                }
                if (Trigger.oldMap.get(a.Id).get('Inspection_Result__c') != a.Inspection_Result__c
                        && isNeedUpsert == false) {
                    isNeedUpsert = true;
                }
                if (Trigger.oldMap.get(a.Id).get('Last_Inspection_staff__c') != a.Last_Inspection_staff__c
                        && isNeedUpsert == false) {
                    isNeedUpsert = true;
                }
                if (Trigger.oldMap.get(a.Id).get('Final_Examination_Date__c') != a.Final_Examination_Date__c
                        && isNeedUpsert == false) {
                    isNeedUpsert = true;
                }
                if (Trigger.oldMap.get(a.Id).get('Inspection_Comment__c') != a.Inspection_Comment__c
                        && isNeedUpsert == false) {
                    isNeedUpsert = true;
                }
                if (isNeedDel) {
                    ahDelExtIdList.add(a.Id + '_' + Trigger.oldMap.get(a.Id).get('Inspection_report_number__c'));
                } else if (isNeedUpsert
                        && !String.isBlank(a.Inspection_report_number__c) && a.Inspection_report_number__c != 'OCM' && a.Inspection_report_number__c != 'OCM-'
                 && a.Inspection_report_number__c != 'OCSM' && a.Inspection_report_number__c != 'OCSM-' // CHAN-BBLATV
                ) {
                    AssetHistory__c ah = new AssetHistory__c();
                    if (historyUpsertMap == null || historyUpsertMap.containsKey('Inspection_report_number__c') == false) {
                        newInspection_report_number_List.add(a.Inspection_report_number__c);
                        System.debug('newInspection_report_number_List.add:' + a.Inspection_report_number__c);
                        ah.AssetId__c = a.Id;
                        ah.Operator__c = System.Userinfo.getUserId();
                        ah.Asset_Inspection_ExtID__c = a.Id + '_' + a.Inspection_report_number__c;
                        ah.NewValue__c = a.Inspection_report_number__c;
                        if (a.Inspection_report_number__c == a.Inspection_Report__r.Name) {
                            ah.Inspection_Report__c = a.Inspection_Report__c;
                        }
                        ah.Field_Api_Name__c = 'Inspection_report_number__c';
                        ah.Field_Lable_Name__c = fsMap.get('Inspection_report_number__c').getDescribe().getLabel();
                        if (historyUpsertMap == null) {
                            historyUpsertMap = new Map<String, AssetHistory__c>();
                            OFSInsReportAssetHistoryController.astHistoryUpsertMap.put(a.Id, historyUpsertMap);
                        }
                    } else {
                        // newValueを更新
                        ah = historyUpsertMap.get('Inspection_report_number__c').clone();
                    }
                    historyUpsertMap.put('Inspection_report_number__c', ah);
                    ahUpsertList.add(ah);
                    ah.Account__c = a.AccountId;
                    ah.AssetId__c = a.Id;
                    ah.Asset_Inspection_ExtID__c = a.Id + '_' + a.Inspection_report_number__c;
                    ah.NewValue__c = a.Inspection_report_number__c;
                    if (a.Inspection_report_number__c == a.Inspection_Report__r.Name) {
                        ah.Inspection_Report__c = a.Inspection_Report__c;
                    }
                    if (String.isBlank(a.Inspection_Result__c) == false) {
                        ah.Inspection_Result__c = a.Inspection_Result__c;
                    }
                    if (String.isBlank(a.Last_Inspection_staff__c) == false) {
                        ah.Inspection_name__c = a.Last_Inspection_staff__c;
                        userIdList.add(a.Last_Inspection_staff__c);
                    }
                    if (a.Final_Examination_Date__c != null) {
                        ah.Examination_Date__c = a.Final_Examination_Date__c;
                    }
                    if (String.isBlank(a.Inspection_Comment__c) == false) {
                        ah.Inspection_Comment__c = a.Inspection_Comment__c;
                    }
                }
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
            }
        }
        // AccountIdなど参照項目の場合Nameなどをセット
        if (accIdList.size() > 0) {
            Map<Id, Account> accMap = new Map<Id, Account> ([select Id, Name, Management_Code__c from Account where Id IN: accIdList]);
            for (AssetHistory__c ah : ahList) {
                if (ah.Field_Api_Name__c == 'AccountId') {
                    if (ah.OldValue__c.length() >= 15 && accMap.get(ah.OldValue__c) != null) ah.OldValue__c = accMap.get(ah.OldValue__c).Management_Code__c;
                    if (ah.NewValue__c.length() >= 15 && accMap.get(ah.NewValue__c) != null) ah.NewValue__c = accMap.get(ah.NewValue__c).Management_Code__c;
                }
            }
        }
        OFSInsReportAssetHistoryController.upsertAssetHistory(ahList);
 
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
        // upsert用の場合、Userを逆引き
        if (userIdList.size() > 0) {
            Map<Id, User> userMap = new Map<Id, User> ([select Id, Alias, Province__c from User where Id IN: userIdList]);
            for (AssetHistory__c ah : ahUpsertList) {
                if (!String.isBlank(ah.Inspection_name__c)) {
                    ah.Inspection_Staff_State__c = userMap.get(ah.Inspection_name__c) == null ? '' : userMap.get(ah.Inspection_name__c).Province__c;
                    ah.Inspection_staff__c = userMap.get(ah.Inspection_name__c) == null ? '' : userMap.get(ah.Inspection_name__c).Alias;
                }
            }
        }
        OFSInsReportAssetHistoryController.upsertExtAssetHistory(ahUpsertList);
        
        // 最後削除する
        OFSInsReportAssetHistoryController.delAssetHistoryExtIdList(ahDelExtIdList);
        
        // newInspection_report_number_List の分の中1つだけ、1にする
        if (newInspection_report_number_List.size() > 0) {
            List<AssetHistory__c> ahListFlag = new List<AssetHistory__c>();
            String breakInspection = '';
            Boolean isUpdateFlag = true;
System.debug('newInspection_report_number_List=' + newInspection_report_number_List);
            List<AssetHistory__c> ahs = [Select Id, Inspection_report_number__c, Inspection_Times__c from AssetHistory__c where NewValue__c != null and NewValue__c IN :newInspection_report_number_List and Field_Api_Name__c = 'Inspection_report_number__c' order by NewValue__c];
            for(AssetHistory__c ah : ahs) {
System.debug('ah.Inspection_report_number__c=' + ah.Inspection_report_number__c);
                if (breakInspection != ah.Inspection_report_number__c) {
System.debug('breakInspection != ah.Inspection_report_number__c');
                    isUpdateFlag = true;
                    breakInspection = ah.Inspection_report_number__c;
                    if (ah.Inspection_Times__c != 1) {
                        ahListFlag.add(ah); // まずは処理対象
                    } else {
                        isUpdateFlag = false;
                    }
                }
                if (isUpdateFlag) {
                    if (ah.Inspection_Times__c == 1) {
                        isUpdateFlag = false;
System.debug('ah.Inspection_Times__c != 0');
                        ahListFlag.remove(ahListFlag.size() - 1);
                    }
                }
            }
            if (ahListFlag.size() > 0) {
System.debug('ahListFlag.size()=' + ahListFlag.size());
                for (AssetHistory__c ah : ahListFlag) {
                    ah.Inspection_Times__c = 1;
                }
                update ahListFlag;
            }
        }
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
    }
}