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
public with sharing class LexNewOPDButtonOpportunityController {
    //新建OPD计划(询价)  询价上的按钮
    @AuraEnabled
    public static oppLine  initNewOPDButton2(String recordId){
        Opportunity res = new Opportunity();
         //备品智能化 2023-10-12 Update By dzk Start 获取询价下报价数量
        oppLine oppLine = new oppLine();
        try{
            res = [SELECT Id,AccountId,Name ,StageName
                FROM Opportunity 
                    WHERE Id=:recordId];
 
            List<Quote> quoteList = [SELECT Id
                FROM Quote 
                    WHERE OpportunityId=:recordId];
            oppLine.opp = res;        
            oppLine.quoteNum = quoteList.size();
            return oppLine;
        }
        //备品智能化 2023-10-12 Update By dzk End 获取询价下报价数量
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }
    }
    //取消按钮
    @AuraEnabled
    public static OPDPlan__c  initCancleSumbit(String recordId){
        OPDPlan__c res = new OPDPlan__c();
        try{
            res = [SELECT Id,Status__c,Name 
                FROM OPDPlan__c 
                    WHERE Id=:recordId];
            return res;
        }
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }        
    }
    //OPD报告
    @AuraEnabled
    public static OPDPlan__c  initReportSubmit(String recordId){
        OPDPlan__c res = new OPDPlan__c();
        try{
            res = [SELECT Id,Status__c,Name,Rental_Apply2_Status_New__c,Rental_Apply1_Status_New__c,
            Rental_Apply3_Status_New__c,Rental_Apply4_Status_New__c,Rental_Apply5_Status_New__c,
            Related_Opportunity1_ID__c,Related_Opportunity2_ID__c,Activity_Type2__c,
            Account_Laboratory__c,OCM_category_ID__c,OCM_category_Name__c,HospitalID__c,
            HospitalName__c,Rental_Apply2__c
                FROM OPDPlan__c 
                    WHERE Id=:recordId];
            return res;
        }
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }        
    }
 
    //备品智能化 ADD By dzk Start 2023-10-12 新建OPD计划(询价)获取最新报价的报价行项目
    @AuraEnabled(cacheable=true)
    public static List<ProLine>  initGetQuote(String recordId){
        try{
            List<ProLine> proLineList = new List<ProLine>();
            Opportunity opp = [SELECT Id,Estimation_Id__c FROM Opportunity WHERE Id =: recordId];
            Id productOlympusProductsRecordTypeId = Schema.SObjectType.Product2.getRecordTypeInfosByDeveloperName().get('OlympusProducts').getRecordTypeId();
            
            String query = 'SELECT Id,Quantity,Product2.Fixture_Model_No_T__c, Product2.Name, Product2.ProductCode, Product2.Key_product_147P__c, OppIsLendMark__c, Product2.RentalSubject__c, QuoteId,Quote.Quote_No__c FROM QuoteLineItem';
            query += ' WHERE Product2.RecordTypeId = \'' + productOlympusProductsRecordTypeId + '\'';
            query += ' AND Product2.Fixture_Model_No_F__c != null';
            query += ' AND QuoteId = \'' + opp.Estimation_Id__c + '\'';
            query += ' AND Product2.Category5__c Not IN (\'虚拟\') ';
            query += ' AND Product2.IsActive = true';
            query += ' AND Product2.Loaner_categoryII__c NOT IN (\'模型\') ';
            query += ' AND Product2.RentalSubject__c = true';
            query += ' AND ((Product2.SFDA_Status__c Not IN (\'停止\') OR Product2.Manual_Entry__c = true) ';
            query += ' OR (Product2.Manual_Entry__c = false AND Product2.SFDA_Status__c IN (\'停止\') AND Product2.Loaner_categoryII__c IN (\'台车\',\'监视器\',\'录像设备\')))';
            
            System.debug('query--------' + query);
            List<QuoteLineItem> quoteLineItemList = Database.query(query);
 
            for(QuoteLineItem quoteItem : quoteLineItemList){
                ProLine pro = new ProLine();
                pro.Quantity = Integer.valueOf(quoteItem.Quantity);
                pro.ProductName = quoteItem.Product2.Name;
                pro.ProductCode = quoteItem.Product2.ProductCode;
                pro.ProductModel = quoteItem.Product2.Fixture_Model_No_T__c;
                pro.QuoteNo = quoteItem.Quote.Quote_No__c;
                pro.QuoteId = quoteItem.QuoteId;
                if(String.isNotBlank(quoteItem.Product2.Key_product_147P__c)){
                    pro.FixtureModel = quoteItem.Product2.Key_product_147P__c.substring(3);
                }       
                proLineList.add(pro);      
            }
            return proLineList;
        }
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }
    }
    //备品智能化 ADD By dzk End 2023-10-12 新建OPD计划(询价)获取最新报价的报价行项目
    //备品智能化 ADD By dzk Start 2023-10-12 新建OPD计划(询价)保存,对应报价行项目进行标记
    @AuraEnabled
    public static String  createProductTags(String records, String recordId){
        try{
            Map<String,Boolean> quoteLineMap = new Map<String,Boolean>();
            Set<Id> quoteIdSet = new Set<Id>();
            List<ProLine> ProLineList = 
            (List<ProLine>)System.JSON.deserialize(records, List<ProLine>.class);
        
            for(ProLine pro : ProLineList){
                quoteLineMap.put(pro.ProductCode,TRUE);
            }
            List<Quote> quoteList = [SELECT Id FROM Quote WHERE OpportunityId =: recordId];
            for(Quote quote : quoteList){
                quoteIdSet.add(quote.Id);
            }
 
            //取得符合备品借出条件的产品信息
            List<QuoteLineItem> quoteLineItemList = [SELECT Id, 
                                                        Product2.ProductCode,
                                                        OppIsLendMark__c //是否借出标记
                                                        FROM QuoteLineItem 
                                                        WHERE QuoteId =: quoteIdSet];
            System.debug('quoteLineItemList--------' + quoteLineItemList);
            // 更新报价行项目上,是否借出标记
            for(QuoteLineItem quoteItem : quoteLineItemList){
                if(ProLineList.size() == 0){
                    quoteItem.OppIsLendMark__c = false;
                }else if(quoteLineMap.containsKey(quoteItem.Product2.ProductCode)){
                    quoteItem.OppIsLendMark__c = true;
                }else{
                    quoteItem.OppIsLendMark__c = false;
                }
            }
            update quoteLineItemList;
            return 'Success';
        }
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }
    }
    //zzm 20240204 只有询价中的才可以 新建opd计划 start
 
    @AuraEnabled(cacheable=true)
    public static String  geOppStatus(String recordId){
        try{
            List<Opportunity> opp = [SELECT Id,StageName__c FROM Opportunity WHERE Id =: recordId];
            if(opp.size()>0) {
                return opp[0].StageName__c;
            }
            return '';
        }
        catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            return null;
        }
    }
    //zzm 20240204 只有询价中的才可以 新建opd计划 END
 
 
    //备品智能化 ADD By dzk Start 2023-10-12 新建OPD计划(询价)保存,对应报价行项目进行标记
    public class ProLine {
        @AuraEnabled
        public Integer Quantity { get; set; } 
        @AuraEnabled
        public String ProductName { get; set; } 
        @AuraEnabled
        public String ProductCode { get; set; }  
        @AuraEnabled
        public String ProductModel { get; set; } 
        @AuraEnabled
        public String QuoteNo { get; set; } 
        @AuraEnabled
        public String QuoteId { get; set; } 
        @AuraEnabled
        public String FixtureModel { get; set; } 
    } 
 
    public class oppLine {
        @AuraEnabled
        public Opportunity opp { get; set; } 
        @AuraEnabled
        public Integer quoteNum { get; set; } 
    } 
    //备品智能化 ADD By dzk End 2023-10-12
 
    
}