张宇恒
2022-12-23 ec47f5ab5923a315bf22e0df5af7371e9668589e
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
trigger LostCancelReport2Asset on Lost_cancel_report__c (after delete, after insert, after update) {
/*
    List<Lost_cancel_report__c> targetInsertLcrs = new List<Lost_cancel_report__c>();
    List<Lost_cancel_report__c> targetUpdateLcrs = new List<Lost_cancel_report__c>();
    List<Id> delAstIds = new List<Id>();
    List<String> OppoList = new List<String>();
    if (Trigger.isInsert) {
        for (Lost_cancel_report__c lcr : Trigger.new) {
            if (lcr.RecordType_DevName__c == 'PCL_Lost_report' && lcr.HP_RecordType_DevName__c == 'HP') {
                targetInsertLcrs.add(lcr);
            }
            OppoList.add(lcr.Opportunity__c);
        }
        //ControllerUtil.updateSIodcList(OppoList);//询价失单,SI需求表无效化
    }
    else if (Trigger.isUpdate) {
        for (Lost_cancel_report__c lcr : Trigger.new) {
            if (lcr.RecordType_DevName__c == 'PCL_Lost_report' && lcr.HP_RecordType_DevName__c == 'HP'
                && (lcr.CompetitorProduct1__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct1__c')
                    || lcr.CompetitorProduct2__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct2__c')
                    || lcr.CompetitorProduct3__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct3__c')
                    || lcr.CompetitorProduct4__c != Trigger.oldMap.get(lcr.Id).get('CompetitorProduct4__c')
                )
            ) {
                targetUpdateLcrs.add(lcr);
            }
        }
    }
    else if (Trigger.isDelete) {
        for (Lost_cancel_report__c lcr : Trigger.old) {
            if (lcr.CompetitorAsset1__c != null) {
                delAstIds.add(lcr.CompetitorAsset1__c);
            }
            if (lcr.CompetitorAsset2__c != null) {
                delAstIds.add(lcr.CompetitorAsset2__c);
            }
            if (lcr.CompetitorAsset3__c != null) {
                delAstIds.add(lcr.CompetitorAsset3__c);
            }
            if (lcr.CompetitorAsset4__c != null) {
                delAstIds.add(lcr.CompetitorAsset4__c);
            }
        }
    }
    if (targetInsertLcrs.size() > 0 || targetUpdateLcrs.size() > 0) {
        Map<String, Asset> upsertAstMap = new Map<String, Asset>();   // lcr.id + 枝番 ==> Asset 
        for (Lost_cancel_report__c lcr : [
                 Select CompetitorAsset1__c, CompetitorProduct1__c,
                        CompetitorAsset2__c, CompetitorProduct2__c,
                        CompetitorAsset3__c, CompetitorProduct3__c,
                        CompetitorAsset4__c, CompetitorProduct4__c,
                        Opportunity__c, Submit_Day__c,
                        Lost_By_Product__c,
                        Lost_By_Brand__c,
                        Opportunity__r.Hospital__c, Opportunity__r.Department_Class__c, Opportunity__r.AccountId
                   From Lost_cancel_report__c
                  Where (id IN :targetInsertLcrs or id IN :targetUpdateLcrs)
                ]
        ) {
            if (lcr.CompetitorProduct1__c != null) {
                Asset upsertAssert = new Asset(
                    Id = lcr.CompetitorAsset1__c,
                    Name = '*',
                    Product2Id = lcr.CompetitorProduct1__c,
                    Opportunity__c = lcr.Opportunity__c,
                    InstallDate = lcr.Submit_Day__c,
                    Hospital__c = lcr.Opportunity__r.Hospital__c,
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c,
                    AccountId = lcr.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                if(lcr.Lost_By_Product__c != null){
                    upsertAssert.Manual_competitor_product_code__c = lcr.Lost_By_Product__c;
                }
                if(lcr.Lost_By_Brand__c != null){
                    upsertAssert.Manual_competitor_product_company__c = lcr.Lost_By_Brand__c;
                }
 
                upsertAstMap.put(lcr.Id + '_1', upsertAssert);
            }
            if (lcr.CompetitorProduct2__c != null) {
                Asset upsertAssert = new Asset(
                    Id = lcr.CompetitorAsset2__c,
                    Name = '*',
                    Product2Id = lcr.CompetitorProduct2__c,
                    Opportunity__c = lcr.Opportunity__c,
                    InstallDate = lcr.Submit_Day__c,
                    Hospital__c = lcr.Opportunity__r.Hospital__c,
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c,
                    AccountId = lcr.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(lcr.Id + '_2', upsertAssert);
            }
            if (lcr.CompetitorProduct3__c != null) {
                Asset upsertAssert = new Asset(
                    Id = lcr.CompetitorAsset3__c,
                    Name = '*',
                    Product2Id = lcr.CompetitorProduct3__c,
                    Opportunity__c = lcr.Opportunity__c,
                    InstallDate = lcr.Submit_Day__c,
                    Hospital__c = lcr.Opportunity__r.Hospital__c,
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c,
                    AccountId = lcr.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(lcr.Id + '_3', upsertAssert);
            }
            if (lcr.CompetitorProduct4__c != null) {
                Asset upsertAssert = new Asset(
                    Id = lcr.CompetitorAsset4__c,
                    Name = '*',
                    Product2Id = lcr.CompetitorProduct4__c,
                    Opportunity__c = lcr.Opportunity__c,
                    InstallDate = lcr.Submit_Day__c,
                    Hospital__c = lcr.Opportunity__r.Hospital__c,
                    Department_Class__c = lcr.Opportunity__r.Department_Class__c,
                    AccountId = lcr.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(lcr.Id + '_4', upsertAssert);
            }
        }
        system.debug('233332'+upsertAstMap.values());
        ControllerUtil.upsertAsset(upsertAstMap.values());
        List<Lost_cancel_report__c> updateLcrs = new List<Lost_cancel_report__c>();
        for (Lost_cancel_report__c lcr : targetInsertLcrs) {
            if (upsertAstMap.containsKey(lcr.Id + '_1') || upsertAstMap.containsKey(lcr.Id + '_2')
                    || upsertAstMap.containsKey(lcr.Id + '_3') || upsertAstMap.containsKey(lcr.Id + '_4')
            ) {
                Lost_cancel_report__c updLcr = new Lost_cancel_report__c(Id = lcr.Id);
                updateLcrs.add(updLcr);
                if (upsertAstMap.containsKey(lcr.Id + '_1')) {
                    updLcr.CompetitorAsset1__c = upsertAstMap.get(lcr.Id + '_1').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_2')) {
                    updLcr.CompetitorAsset2__c = upsertAstMap.get(lcr.Id + '_2').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_3')) {
                    updLcr.CompetitorAsset3__c = upsertAstMap.get(lcr.Id + '_3').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_4')) {
                    updLcr.CompetitorAsset4__c = upsertAstMap.get(lcr.Id + '_4').Id;
                }
            }
        }
        for (Lost_cancel_report__c lcr : targetUpdateLcrs) {
            if (upsertAstMap.containsKey(lcr.Id + '_1') || upsertAstMap.containsKey(lcr.Id + '_2')
                    || upsertAstMap.containsKey(lcr.Id + '_3') || upsertAstMap.containsKey(lcr.Id + '_4')
            ) {
                Lost_cancel_report__c updLcr = new Lost_cancel_report__c(Id = lcr.Id);
                updateLcrs.add(updLcr);
                if (upsertAstMap.containsKey(lcr.Id + '_1')) {
                    updLcr.CompetitorAsset1__c = upsertAstMap.get(lcr.Id + '_1').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_2')) {
                    updLcr.CompetitorAsset2__c = upsertAstMap.get(lcr.Id + '_2').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_3')) {
                    updLcr.CompetitorAsset3__c = upsertAstMap.get(lcr.Id + '_3').Id;
                }
                if (upsertAstMap.containsKey(lcr.Id + '_4')) {
                    updLcr.CompetitorAsset4__c = upsertAstMap.get(lcr.Id + '_4').Id;
                }
            }
        }
        update updateLcrs;
    }
    if (delAstIds.size() > 0) {
        ControllerUtil.deleteAsset(delAstIds);
    }*/
}