buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
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
trigger OpponentBidInformation2Asset on Opponent_Bid_Information__c (after delete, after insert, after update) {
// 2018年7月12日 为'对手中标信息'添加trigger by 张玉山
    List<Opponent_Bid_Information__c> targetInsertObis = new List<Opponent_Bid_Information__c>();
    List<Opponent_Bid_Information__c> targetUpdateObis = new List<Opponent_Bid_Information__c>();
    List<Id> delAstIds = new List<Id>();
    List<String> OppoList = new List<String>();
    if (Trigger.isInsert) {
        for (Opponent_Bid_Information__c obi : Trigger.new) {
            if (obi.RecordType_DevName__c == 'OpponentBidInformation' && obi.HP_RecordType_DevName__c == 'HP') {
                targetInsertObis.add(obi);
            }
            OppoList.add(obi.Opportunity__c);
        }
        system.debug('1234' + targetInsertObis);
        //ControllerUtil.updateSIodcList(OppoList);//询价失单,SI需求表无效化
    }
    else if (Trigger.isUpdate) {
        for (Opponent_Bid_Information__c obi : Trigger.new) {
            if (obi.RecordType_DevName__c == 'OpponentBidInformation' && obi.HP_RecordType_DevName__c == 'HP'
                && (obi.CompetitorProduct1__c != Trigger.oldMap.get(obi.Id).get('CompetitorProduct1__c')
                    || obi.CompetitorProduct2__c != Trigger.oldMap.get(obi.Id).get('CompetitorProduct2__c')
                    || obi.CompetitorProduct3__c != Trigger.oldMap.get(obi.Id).get('CompetitorProduct3__c')
                    || obi.CompetitorProduct4__c != Trigger.oldMap.get(obi.Id).get('CompetitorProduct4__c')
                )
            ) {
                targetUpdateObis.add(obi);
            }
        }
    }
    else if (Trigger.isDelete) {
        for (Opponent_Bid_Information__c obi : Trigger.old) {
            if (obi.CompetitorAsset1__c != null) {
                delAstIds.add(obi.CompetitorAsset1__c);
            }
            if (obi.CompetitorAsset2__c != null) {
                delAstIds.add(obi.CompetitorAsset2__c);
            }
            if (obi.CompetitorAsset3__c != null) {
                delAstIds.add(obi.CompetitorAsset3__c);
            }
            if (obi.CompetitorAsset4__c != null) {
                delAstIds.add(obi.CompetitorAsset4__c);
            }
        }
    }
    
    if (targetInsertObis.size() > 0 || targetUpdateObis.size() > 0) {
        Map<String, Asset> upsertAstMap = new Map<String, Asset>();   // obi.id + 枝番 ==> Asset 
        for (Opponent_Bid_Information__c obi : [
                 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 Opponent_Bid_Information__c
                  Where (id IN :targetInsertObis or id IN :targetUpdateObis)
                ]
        ) {
            if (obi.CompetitorProduct1__c != null) {
                Asset upsertAssert = new Asset(
                    Id = obi.CompetitorAsset1__c,
                    Name = '*',
                    Product2Id = obi.CompetitorProduct1__c,
                    Opportunity__c = obi.Opportunity__c,
                    InstallDate = obi.Submit_Day__c,
                    Hospital__c = obi.Opportunity__r.Hospital__c,
                    Department_Class__c = obi.Opportunity__r.Department_Class__c,
                    AccountId = obi.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                if(obi.Lost_By_Product__c != null){
                    upsertAssert.Manual_competitor_product_code__c = obi.Lost_By_Product__c;
                }
                if(obi.Lost_By_Brand__c != null){
                    upsertAssert.Manual_competitor_product_company__c = obi.Lost_By_Brand__c;
                }
 
                upsertAstMap.put(obi.Id + '_1', upsertAssert);
            }
            if (obi.CompetitorProduct2__c != null) {
                Asset upsertAssert = new Asset(
                    Id = obi.CompetitorAsset2__c,
                    Name = '*',
                    Product2Id = obi.CompetitorProduct2__c,
                    Opportunity__c = obi.Opportunity__c,
                    InstallDate = obi.Submit_Day__c,
                    Hospital__c = obi.Opportunity__r.Hospital__c,
                    Department_Class__c = obi.Opportunity__r.Department_Class__c,
                    AccountId = obi.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(obi.Id + '_2', upsertAssert);
            }
            if (obi.CompetitorProduct3__c != null) {
                Asset upsertAssert = new Asset(
                    Id = obi.CompetitorAsset3__c,
                    Name = '*',
                    Product2Id = obi.CompetitorProduct3__c,
                    Opportunity__c = obi.Opportunity__c,
                    InstallDate = obi.Submit_Day__c,
                    Hospital__c = obi.Opportunity__r.Hospital__c,
                    Department_Class__c = obi.Opportunity__r.Department_Class__c,
                    AccountId = obi.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(obi.Id + '_3', upsertAssert);
            }
            if (obi.CompetitorProduct4__c != null) {
                Asset upsertAssert = new Asset(
                    Id = obi.CompetitorAsset4__c,
                    Name = '*',
                    Product2Id = obi.CompetitorProduct4__c,
                    Opportunity__c = obi.Opportunity__c,
                    InstallDate = obi.Submit_Day__c,
                    Hospital__c = obi.Opportunity__r.Hospital__c,
                    Department_Class__c = obi.Opportunity__r.Department_Class__c,
                    AccountId = obi.Opportunity__r.AccountId,
                    Status = '使用中',
                    Asset_Owner__c = '病院資産',
                    IsCompetitorProduct = True
                );
                upsertAstMap.put(obi.Id + '_4', upsertAssert);
            }
        }
        system.debug('233332'+upsertAstMap.values());
        ControllerUtil.upsertAsset(upsertAstMap.values());
        List<Opponent_Bid_Information__c> updateObis = new List<Opponent_Bid_Information__c>();
        for (Opponent_Bid_Information__c obi : targetInsertObis) {
            if (upsertAstMap.containsKey(obi.Id + '_1') || upsertAstMap.containsKey(obi.Id + '_2')
                    || upsertAstMap.containsKey(obi.Id + '_3') || upsertAstMap.containsKey(obi.Id + '_4')
            ) {
                Opponent_Bid_Information__c updObi = new Opponent_Bid_Information__c(Id = obi.Id);
                updateObis.add(updObi);
                if (upsertAstMap.containsKey(obi.Id + '_1')) {
                    updObi.CompetitorAsset1__c = upsertAstMap.get(obi.Id + '_1').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_2')) {
                    updObi.CompetitorAsset2__c = upsertAstMap.get(obi.Id + '_2').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_3')) {
                    updObi.CompetitorAsset3__c = upsertAstMap.get(obi.Id + '_3').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_4')) {
                    updObi.CompetitorAsset4__c = upsertAstMap.get(obi.Id + '_4').Id;
                }
            }
        }
        for (Opponent_Bid_Information__c obi : targetUpdateObis) {
            if (upsertAstMap.containsKey(obi.Id + '_1') || upsertAstMap.containsKey(obi.Id + '_2')
                    || upsertAstMap.containsKey(obi.Id + '_3') || upsertAstMap.containsKey(obi.Id + '_4')
            ) {
                Opponent_Bid_Information__c updObi = new Opponent_Bid_Information__c(Id = obi.Id);
                updateObis.add(updObi);
                if (upsertAstMap.containsKey(obi.Id + '_1')) {
                    updObi.CompetitorAsset1__c = upsertAstMap.get(obi.Id + '_1').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_2')) {
                    updObi.CompetitorAsset2__c = upsertAstMap.get(obi.Id + '_2').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_3')) {
                    updObi.CompetitorAsset3__c = upsertAstMap.get(obi.Id + '_3').Id;
                }
                if (upsertAstMap.containsKey(obi.Id + '_4')) {
                    updObi.CompetitorAsset4__c = upsertAstMap.get(obi.Id + '_4').Id;
                }
            }
        }
        update updateObis;
    }
    if (delAstIds.size() > 0) {
        ControllerUtil.deleteAsset(delAstIds);
    }
}