高章伟
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
/**
 * MakeAssetHistory.triggerを動かすために一部ロジックを追加
 * 填写完毕 後、明細のAssetを更新、MakeAssetHistory.triggerにて履歴を削除、更新する
 */
public without sharing class OFSInsReportAssetHistoryController {
    // MakeAssetHistory.trigger用、何回も呼び出すので、ここで項目が重複しないようにするため
    public static Map<Id, Map<String, AssetHistory__c>> astHistoryMap = new Map<Id, Map<String, AssetHistory__c>>();
    public static Map<Id, Map<String, AssetHistory__c>> astHistoryUpsertMap = new Map<Id, Map<String, AssetHistory__c>>();
 
    // MakeAssetHistory.trigger用のメソッド
    public static void upsertAssetHistory(List<AssetHistory__c> ahList) {
        if (ahList.size() > 0) {
            upsert ahList;
        }
    }
    
    // MakeAssetHistory.trigger用のメソッド
    public static void upsertExtAssetHistory(List<AssetHistory__c> ahUpsertList) {
        if (ahUpsertList.size() > 0) {
            upsert ahUpsertList Asset_Inspection_ExtID__c;
        }
    }
 
    public static void delAssetHistoryExtIdList(List<String> ahDelExtIdList) {
        if (ahDelExtIdList.size() > 0) {
            List<AssetHistory__c> ahList = [Select Id from AssetHistory__c where Asset_Inspection_ExtID__c IN: ahDelExtIdList];
            if (ahList.size() > 0) delete ahList;
        }
    }
 
    public static void delAssetHistory() {
        List<AssetHistory__c> ahList = [Select Id from AssetHistory__c where AssetId__c = null];
        if (ahList.size() > 0) delete ahList;
    }
    
    public static void upsertInspection_Report(Inspection_Report__c ir) {
        system.debug(ir.Id);
        upsert ir;
    }
 
    public static void deleteInspection_Item(Inspection_Report__c ir, List<Inspection_Item__c> irItems) {
        if (irItems.size() > 0) {
            delete irItems;
            if (ir.status__c != '草案中') {
                List<Id> astIdList = new List<Id>();
                List<Asset> ahDelList = new List<Asset>();
                for (Inspection_Item__c irItem : irItems) {
                    if (irItem.AssetId__c != null) {
                        astIdList.add(irItem.AssetId__c);
                    }
                }
                
                if (astIdList.size() > 0) {
                    for (Asset ast : [Select Id, Inspection_report_number__c from Asset where Id IN :astIdList]) {
                        // 同じ点検番号のもの再度提出時、AssetHistoryを作り直します
                        if (ast.Inspection_report_number__c == ir.Name) {
                            ast.Inspection_report_number__c = '';
                            ast.Inspection_Report__c = null;
                            ast.Inspection_Result__c = '';
                            ast.Last_inspection_staff__c = null;
                            ast.Final_Examination_Date__c = null;
                            ast.Inspection_Comment__c = '';
                            ahDelList.add(ast);
                        }
                    }
                    if (ahDelList.size() > 0) update ahDelList;
                }
            }
        }
    }
 
    public static void upsertInspection_Item(Inspection_Report__c ir, List<Inspection_Item__c> irItems) {
        if (irItems.size() > 0) {
            upsert irItems;
            //2021-05-12  mzy  update LJPH-BZPB7W SFDC点检报告更新逻辑修改 start 
            //现在想要的是通过batch来更新保有设备的信息,在点检报告书提交时只把标识 √勾上就行
            //因为报告书提交的时候状态会发生改变,触发器(InspectionReportTriggerHandler)里会自动给报告书进行 打勾√ 
           /* List<Asset> ahList = new List<Asset>();
            for (Inspection_Item__c irItem : irItems) {
                if (irItem.AssetId__c != null) {
                    if (ir.status__c != '草案中') {
                        ahList.add(new Asset(Id = irItem.AssetId__c,
                            Inspection_report_number__c = ir.Name,
                            Inspection_Report__c = ir.id,
                            Inspection_Result__c = irItem.Inspection_Result__c,
                            Last_Inspection_staff__c = ir.Reporter__c,
                            Final_Examination_Date__c = ir.Inspection_Date__c,
                            Inspection_Comment__c = irItem.Inspection_Comment__c
                        ));
                    } else {
                        // TODO 同じ点検番号のもの再度提出時、AssetHistoryを作り直しますので、ここは AssetHistoryを削除しなくでもいいでしょう
                    }
                }
            }
            if (ahList.size() > 0) {
                update ahList;
            }*/
            //2021-05-12  mzy  update LJPH-BZPB7W SFDC点检报告更新逻辑修改 end
        }
    }
}