/** 备品智能化
 * 2023-10-15 Add by dzk 
 * 询价上新建OPD计划时将勾选的行项目转换为OPD计划的计划备品数据
 * 学会状态变更为公开中,将学会下计划备品数据,转换到OPD计划的计划备品数据
 * 状态变更为提交时,报价内产品数量、报价外产品数据更新
 */
public with sharing class OpdPlan_CreatePlanRentalEquipmentHandler {
    public static void createPlanRentalEquipment(List<OPDPlan__c> newList) {
        Set<Id> oppIdSet = new Set<Id>();
        Set<Id> quoteIdSet = new Set<Id>();
        Set<Id> campaignIdSet = new Set<Id>();
        String planFromDetai = '';
        String planNotFromDetai = '';
        String planSysDetai = '';
        List<OPDPlan__c> OPDList = new List<OPDPlan__c>();
        Map<String,OPDPlan__c> opdPlanMap = new Map<String,OPDPlan__c>();
        List<Plan_Rental_Equipment__c> planRentalEquipmentList = new List<Plan_Rental_Equipment__c>();
        Id planRERecordTypeOther = Schema.SObjectType.Plan_Rental_Equipment__c.getRecordTypeInfosByDeveloperName().get('Other').getRecordTypeId();
        Id planRERecordTypeCampaign = Schema.SObjectType.Plan_Rental_Equipment__c.getRecordTypeInfosByDeveloperName().get('campaign').getRecordTypeId();
        // 取得询价Id
        for(OPDPlan__c opdPlanNew : newList){
            if(opdPlanNew.OPDType__c == '询价' || opdPlanNew.OPDType__c == '日报'){
                oppIdSet.add(opdPlanNew.Related_Opportunity1_ID__c);   
            }
            if(opdPlanNew.OPDType__c == '学会'){
                campaignIdSet.add(opdPlanNew.Campaign__c);   
            }
        }
        // 取得最新报价Id
        List<Opportunity> oppList = new List<Opportunity>();
        if(oppIdSet.size() > 0){
            oppList = [SELECT Id,Estimation_Id__c FROM Opportunity WHERE Id =: oppIdSet];
        }
        for(Opportunity opp : oppList){
            quoteIdSet.add(opp.Estimation_Id__c);
        }
        // 取得符合备品借出条件的产品信息
        List<QuoteLineItem> quoteLineItemList = new List<QuoteLineItem>();
        List<Plan_Rental_Equipment__c> planREList = new List<Plan_Rental_Equipment__c>();
        if(quoteIdSet.size() > 0){
            quoteLineItemList = [SELECT Id, Quantity, //报价行项目数量
                                            Product2.Name, //产品名称
                                            Product2.ProductCode, //产品代码 
                                            Product2.Key_product_147P__c, //产品型号
                                            Product2.Fixture_Model_No_T__c, //产品型号
                                            OppIsLendMark__c, //是否出借标记
                                            QuoteId,//报价Id
                                            Quote.OpportunityId //询价Id
                                            FROM QuoteLineItem 
                                            WHERE QuoteId =: quoteIdSet
                                            AND OppIsLendMark__c = TRUE];
        }
        // 获取学会上的计划借出备品信息
        System.debug('campaignIdSet:'+campaignIdSet);
        if(campaignIdSet.size() > 0){
            planREList = [SELECT Id, Name, OPD_Plan__c,Rental_Quantity__c, 
                                    MDM_Model_No__c,FixtureModel__c, Campaign__c,
                                    Campaign__r.RecordTypeId,Campaign__r.Is_LendProduct__c
                                    FROM Plan_Rental_Equipment__c
                                    WHERE Campaign__c =: campaignIdSet
                                    AND RecordTypeId =: planRERecordTypeCampaign
                                    AND OPD_Plan__c = NULL];
        }
        System.debug('planREList:'+planREList);                                  
        
        if(quoteLineItemList.size() > 0){
            for(OPDPlan__c opdPlan : newList){
                OPDPlan__c opdPlanRecord = new OPDPlan__c();
                for(QuoteLineItem quoteItem : quoteLineItemList){
                    if(opdPlan.Related_Opportunity1_ID__c == quoteItem.Quote.OpportunityId){
                        // 新建计划借出备品数据
                        Plan_Rental_Equipment__c planRentalEquipment = new Plan_Rental_Equipment__c();
                        planRentalEquipment.RecordTypeId = planRERecordTypeOther;
                        planRentalEquipment.Rental_Quantity__c = '1';
                        planRentalEquipment.OPD_Plan__c = opdPlan.Id;
                        planRentalEquipment.Name = quoteItem.Product2.Name;
                        planRentalEquipment.Rental_Equipment__c = quoteItem.Product2.Name;
                        planRentalEquipment.Opportunity__c = opdPlan.Related_Opportunity1_ID__c;
                        planRentalEquipment.ProductCode__c = quoteItem.Product2.ProductCode;
                        planRentalEquipment.MDM_Model_No__c = quoteItem.Product2.Fixture_Model_No_T__c;
                        planRentalEquipment.Quote__c = quoteItem.QuoteId;
                        if(String.isNotBlank(quoteItem.Product2.Key_product_147P__c)){
                            planRentalEquipment.FixtureModel__c 
                                = quoteItem.Product2.Key_product_147P__c.substring(3);
                             planSysDetai += planRentalEquipment.FixtureModel__c + '*' + planRentalEquipment.Rental_Quantity__c + '; ';
                        }
                        planRentalEquipmentList.add(planRentalEquipment);
                       
                        if(quoteItem.QuoteId == null){
                            planNotFromDetai += planRentalEquipment.MDM_Model_No__c + '*' + planRentalEquipment.Rental_Quantity__c + '; ';
                        }else{
                            planFromDetai += planRentalEquipment.MDM_Model_No__c + '*' + planRentalEquipment.Rental_Quantity__c + '; ';
                        }
                    }
                }
                opdPlanRecord.Id = opdPlan.Id;
                opdPlanRecord.PlanProdDetail__c = planNotFromDetai + planFromDetai; // 计划出借备品信息
                opdPlanRecord.EquipmentFromOpp__c = planFromDetai; // 报价外产品
                opdPlanRecord.EquipmentNotFromOpp__c = planNotFromDetai; // 报价内产品
                opdPlanRecord.PlanProdDetailSys__c = planSysDetai; // 计划出借战略产品
                opdPlanRecord.PlanProdDetailSysT__c = planSysDetai.length() > 255 ? planSysDetai.substring(0, 255):planSysDetai; // 计划出借战略产品文本 add by ljh 20240302
                // 备品智能化 20240110 zyh start
                System.debug('OPDtestzyh==='+opdPlan.OriginalOpdPlanRental__c);
                if (String.isBlank( opdPlan.OriginalOpdPlanRental__c)) {
                    OPDList.add(opdPlanRecord);
                }
                // 备品智能化 20240110 zyh end
            }
            
            insert planRentalEquipmentList;     
            // 备品智能化 20240110 zyh start
            if (OPDList.size() > 0) {
                update OPDList;
            }
            // 备品智能化 20240110 zyh end
        }  

        // 学会状态变更为公开中时,自动创建OPD计划,将学会中的备品转到OPD计划中。
        if(planREList.size() > 0){
            List<Id> recordTypeIds = new List<Id>();
            recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId()); //学会/会议
            recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Training_event').getRecordTypeId()); //社外培训
            recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Service_trainig').getRecordTypeId()); //服务培训/学会会议
            recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('BusinessTraining').getRecordTypeId()); //营业部专用会议
            System.debug('recordTypeIds:'+recordTypeIds);
            String camquantity = '';
            List<Plan_Rental_Equipment__c> planREUpdateList = new List<Plan_Rental_Equipment__c>();
            for(OPDPlan__c opdPlan : newList){
                OPDPlan__c opdPlanRecord = new OPDPlan__c();
                //20240304  学会过来的OPD 产品报价内/外赋值逻辑添加  sx add
                Integer fromNotOppNum = 0;
                for(Plan_Rental_Equipment__c planRE : planREList){
                    System.debug('planRE:'+planRE);
                    if(opdPlan.Campaign__c == planRE.Campaign__c 
                        && recordTypeIds.contains(planRE.Campaign__r.RecordTypeId)
                            && String.isNotBlank(planRE.Campaign__r.Is_LendProduct__c)
                                && String.valueOf(planRE.Campaign__r.Is_LendProduct__c).equals('是')){
                        Plan_Rental_Equipment__c planREUpdate = new Plan_Rental_Equipment__c();
                        planREUpdate.Id = planRE.Id;
                        planREUpdate.OPD_Plan__c = opdPlan.Id;
                        planREUpdateList.add(planREUpdate);
                        planNotFromDetai += planRE.MDM_Model_No__c + '*' + planRE.Rental_Quantity__c + '; ';
                        if(String.isNotBlank(planRE.FixtureModel__c)){
                            planSysDetai += planRE.FixtureModel__c + '*' + planRE.Rental_Quantity__c + '; ';
                        }
                        fromNotOppNum ++ ;      //20240304  学会产品 报价内/外没有值  sx add
                    }
                }
                opdPlanRecord.Id = opdPlan.Id;
                opdPlanRecord.PlanProdDetail__c = planNotFromDetai;
                opdPlanRecord.EquipmentNotFromOpp__c = planNotFromDetai;
                opdPlanRecord.EquipmentFromOpp__c = '';
                opdPlanRecord.PlanProdDetailSys__c = planSysDetai;
                opdPlanRecord.PlanProdDetailSysT__c = planSysDetai.length() > 255 ? planSysDetai.substring(0, 255):planSysDetai; // 计划出借战略产品文本 add by ljh 20240302
                //20240304  学会过来的OPD 产品报价内/外赋值逻辑添加  sx start
                opdPlanRecord.EquipmentFromOppNum__c = 0;
                opdPlanRecord.EquipmentNotFromOppNum__c = fromNotOppNum;
                System.debug('opdPlanRecord.EquipmentNotFromOppNum__c===' + opdPlanRecord.EquipmentNotFromOppNum__c);
                //20240304  学会过来的OPD 产品报价内/外赋值逻辑添加  sx end
                OPDList.add(opdPlanRecord);
            }
            update planREUpdateList;
            update OPDList;
        }                                                                                     
    }

    // OPD计划状态更改为'提交'时,获取备品源于询价数量、备品手动补充数量
    public static void updateOPDPlan(List<OPDPlan__c> newList){
        Set<Id> opdIdSet = new Set<Id>();
        List<OPDPlan__c> opdPlanList = new List<OPDPlan__c>();
        
        for(OPDPlan__c opd : newList){
            opdIdSet.add(opd.Id);
        }
        Integer fromOppNum = 0;
        Integer fromNotOppNum = 0;
        List<Plan_Rental_Equipment__c> EquipmentFromOppList = new List<Plan_Rental_Equipment__c>();
        List<Plan_Rental_Equipment__c> EquipmentNotFromOppList = new List<Plan_Rental_Equipment__c>();
        if(opdIdSet.size() > 0){
            EquipmentFromOppList = [SELECT Id, Rental_Quantity__c, MDM_Model_No__c
                                        FROM Plan_Rental_Equipment__c
                                        WHERE Quote__c != null
                                        AND OPD_Plan__c =: opdIdSet];

            EquipmentNotFromOppList = [SELECT Id, Rental_Quantity__c, MDM_Model_No__c
                                            FROM Plan_Rental_Equipment__c
                                            WHERE Quote__c = null
                                            AND OPD_Plan__c =: opdIdSet];
        }
        for(Plan_Rental_Equipment__c fromOpp : EquipmentFromOppList){
            fromOppNum += Integer.valueOf(fromOpp.Rental_Quantity__c);
        }

        for(Plan_Rental_Equipment__c fromNotOpp : EquipmentNotFromOppList){
            fromNotOppNum += Integer.valueOf(fromNotOpp.Rental_Quantity__c);
        }
        for(OPDPlan__c opdUpdate : newList){
            OPDPlan__c opdRecord = new OPDPlan__c();
            opdRecord.Id = opdUpdate.Id;
            opdRecord.EquipmentFromOppNum__c = fromOppNum;
            opdRecord.EquipmentNotFromOppNum__c = fromNotOppNum;
            opdPlanList.add(opdRecord);
        }
        update opdPlanList;
    }

}