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

    
}