public without sharing class MaintenanceUpdateProductHandler extends Oly_TriggerHandler {
    private Map<Id, Maintenance_Product_Data__c> newMap;
    private Map<Id, Maintenance_Product_Data__c> oldMap;
    private List<Maintenance_Product_Data__c> newList;
    private List<Maintenance_Product_Data__c> oldList;
    public String query;
    public static Integer FIELDMAX = 200;
    public MaintenanceUpdateProductHandler() {
        this.newMap = (Map<Id, Maintenance_Product_Data__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Maintenance_Product_Data__c>) Trigger.oldMap;
        this.newList = (List<Maintenance_Product_Data__c>) Trigger.new;
        this.oldList = (List<Maintenance_Product_Data__c>) Trigger.old;
    }
    protected override void beforeUpdate() {
        beforeUpdateProduct(this.newList);
    }
    protected override void afterUpdate() {
        updateProduct(this.newList);
    }
    private void beforeUpdateProduct(List<Maintenance_Product_Data__c> nList){
        List<String> mpdIds = new List<String>();
        for (Maintenance_Product_Data__c newMpd : nList) {
            Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
            if(newMpd.Status__c=='审批中-服务' && newMpd.Status__c != old.Status__c){
                //newMpd.addError('请填写完整价格相关的信息！');
                mpdIds.add(newMpd.Id);
            }
        }
        if(mpdIds.size()>0){ 
            List<Maintenance_Product_Data_Details__c> mpddList = [select Id, Name ,ProductsID__c,Maintenance_Product_Data__c,GuranteeType__c,Intra_Trade_Service_RMB__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c,Maintenance_Price_Year__c 
             from Maintenance_Product_Data_Details__c where  Maintenance_Product_Data__c in :mpdIds ];
            List<Maintenance_Product_Data__c> mpdList = [select Id, Name ,Intra_Trade_Service_RMB_Sum__c,Intra_Trade_Gurantee_RMB_Sum__c,Maintenance_Price_Year_Sum__c  from Maintenance_Product_Data__c where  id in :mpdIds ];
            Boolean isWarning = false;
            Boolean attachmentSize =false;
            Boolean attachmentChange = false;
            String  contentWarning = '';
            for(Maintenance_Product_Data_Details__c mpdDetails : mpddList){
                //   1.2  多年保修类型为服务时，NoDiscount价格不能填写，多年保修计提价格\维修合同报价必须填写；
                //  1.1  多年保修类型为市场时，NoDiscount价格\多年保修计提价格\维修合同报价必须填写；
                //Intra_Trade_Service_RMB__c Intra_Trade_Gurantee_RMB__c Maintenance_Price_Year__c
                if(mpdDetails.GuranteeType__c==null||mpdDetails.GuranteeType__c.length()==0){
                    if((mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format()!='0')
                        ||(mpdDetails.Intra_Trade_Gurantee_RMB__c!=null&&mpdDetails.Intra_Trade_Gurantee_RMB__c.format()!='0')
                        ||(mpdDetails.Maintenance_Price_Year__c !=null&&mpdDetails.Maintenance_Price_Year__c.format() !='0')
                        ){
                        isWarning = true;
                        contentWarning = 'NoDiscount价格、多年保修计提价格、维修合同报价必须为空！';
                    }
                }else{
                    if(mpdDetails.GuranteeType__c.equals('市场')){
                        if(mpdDetails.Intra_Trade_Service_RMB__c==null
                            ||mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                            isWarning = true;
                            contentWarning = '必须填写NoDiscount价格、多年保修计提价格以及维修合同报价！';
                        }
                    }
                    if(mpdDetails.GuranteeType__c.equals('服务')){
                        if((mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format() !='0')
                            ||mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                            isWarning = true;
                            if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null
                            ||mpdDetails.Maintenance_Price_Year__c==null){
                                contentWarning = '必须填写多年保修计提价格、维修合同报价！';
                            }
                            if(mpdDetails.Intra_Trade_Service_RMB__c !=null&&mpdDetails.Intra_Trade_Service_RMB__c.format() !='0'){
                                contentWarning = 'NoDiscount价格必须为空！';
                            }
                        }
                    }

                }   
            }
            for(String id:mpdIds){
                List<Attachment> attachmentinfo = [SELECT Id, Name,OwnerId FROM Attachment WHERE  parentid  =:mpdIds ];
                if(attachmentinfo.size() ==0){
                    attachmentSize = true;
                }
            }
            for(Maintenance_Product_Data__c mpdChange : mpdList){
                if(mpdChange.Intra_Trade_Service_RMB_Sum__c>0 || mpdChange.Intra_Trade_Gurantee_RMB_Sum__c>0 ||mpdChange.Maintenance_Price_Year_Sum__c>0){
                    attachmentChange = true;
                }
            }
            for (Maintenance_Product_Data__c newMpd : nList) {
                Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
                if(newMpd.Status__c=='审批中-服务'&& newMpd.Status__c != old.Status__c && isWarning){
                    newMpd.addError(contentWarning);
                }
                
                if(newMpd.Status__c=='审批中-服务'&& newMpd.Status__c != old.Status__c && attachmentSize&&attachmentChange){
                    newMpd.addError('还没有上传附件，请上传附件！');
                }
            }
        }
    }
    private void updateProduct(List<Maintenance_Product_Data__c> nList){
        List<String> mpdId = new List<String>();
        List<Maintenance_Product_Data__c> EmailMPD = new List<Maintenance_Product_Data__c>();
        for (Maintenance_Product_Data__c newMpd : nList) {
            Maintenance_Product_Data__c old = oldMap.get(newMpd.Id);
            if(newMpd.Status__c=='完成'&&newMpd.Status__c != old.Status__c){
                mpdId.add(newMpd.Id);
                EmailMPD.add(newMpd);
            }
        }
        if(mpdId.size()>0){ 
            String sqlTail = '(\'';
            for(Integer i = 0 ; i< mpdId.size();i++){
                if(i<mpdId.size()-1){
                    sqlTail += mpdId[i]+'\',\'';
                }else{
                    sqlTail += mpdId[i]+'\')';
                }
            } 
            //Tier1_Category__c,
            query = 'select Id, Name ,ProductsID__c,Maintenance_Product_Data__c,RecordType.DeveloperName  ';
            query += ' ,ENG_DeaerProFlag__c,Dealer_special_Object__c,Key_product_147P__c,Key_product_147P_Old__c,Important_Endopartner_product__c ';
            // FY23修改 WLIG-CFV4AV 重点产品维护（新）start
            query += ' ,Key_product_155P__c,Key_product_155P_Old__c,Celon_Product__c,Celon_Product_Old__c ';
            // FY23修改 WLIG-CFV4AV 重点产品维护（新）end
            query += ' ,GuranteeType__c,Entend_gurantee_period__c,Gurantee_Start_Date__c,Gurantee_End_Date__c,CanNotCancelledGurantee__c  ';
            query += ' ,GuranteeType_Old__c,Entend_gurantee_period_Old__c,Gurantee_Start_Date_Old__c,Gurantee_End_Date_Old__c';
            query += ' ,Intra_Trade_Service_RMB__c,Intra_Trade_Service_RMB_DateFrom__c,Virtual_Contract__c,Intra_Trade_Gurantee_RMB__c, Intra_Trade_Gurantee_RMB_DateFrom__c,Maintenance_Price_Year__c ';
            query += ' ,Intra_Trade_Service_RMB_Old__c,Intra_Trade_Gurantee_RMB_Old__c';
            query += ' ,Intra_Trade_Service_RMB_Date1__c,Intra_Trade_Service_RMB_EndDate1__c  ';
            query += ' ,Intra_Trade_Gurantee_RMB_Date1__c,Intra_Trade_Gurantee_RMB_End_Date1__c ';
            query += ' ,RepairListPriceLevelA__c ,RepairListPriceLevelB__c ,RepairListPriceLevelC__c ,CanRepairAccsessary__c ,Repair_Product_Code__c ,PartSupplyFinishDate__c ';
            query += ' ,Asset_loaner_category__c , Special_Model__c ,Fixture_Model_No__c,Packing_list_Fixture__c,Default_Fixture_Arrival_Process__c, Default_Fixture_Arrival_Product__c   ';
            //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
            query += ' ,Can_Repair__c , Service_Category1__c ,Service_Category2__c,Service_Category3__c,Service_Category4__c, Service_Category5__c,Service_Category6__c,Service_Category7__c ';
            //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 End
            query += ' from Maintenance_Product_Data_Details__c where Maintenance_Product_Data__r.Status__c =\'完成\' ';
            query += ' and Maintenance_Product_Data__c in '+sqlTail;
            List<Maintenance_Product_Data_Details__c> mpdList = Database.query(query);
            List<Product2> product2Update = new List<Product2>();
            //Map<String,String> mpdId = new Map<String,String>();
            List<Maintenance_Product_Data_Details__c> mpdDetailsStatus = new List<Maintenance_Product_Data_Details__c>();
            for(Maintenance_Product_Data_Details__c mpdDetails : mpdList){
                Maintenance_Product_Data_Details__c pdd = new Maintenance_Product_Data_Details__c();
                Product2 product2 = new Product2();
                //mpdId.put(mpdDetails.Maintenance_Product_Data__c,mpdDetails.Maintenance_Product_Data__c);
                pdd.Id = mpdDetails.Id;
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')||mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')){
                    pdd.Intra_Trade_Service_RMB_DateFrom__c = Date.toDay();
                    pdd.Intra_Trade_Gurantee_RMB_DateFrom__c = Date.toDay();
                }
                pdd.Status__c = '已更新';
                mpdDetailsStatus.add(pdd);
                //五个记录类型
                if(mpdDetails.RecordType.DeveloperName.equals('ImportantProduct')){
                    product2.Id = mpdDetails.ProductsID__c;
                    //product2.Tier1_Category__c = mpdDetails.Tier1_Category__c;
                    product2.ENG_DeaerProFlag__c = mpdDetails.ENG_DeaerProFlag__c;
                    product2.Dealer_special_Object__c = mpdDetails.Dealer_special_Object__c;
                    product2.Important_Endopartner_product__c = mpdDetails.Important_Endopartner_product__c;
                    //需要做处理
                    //MaintenanceProductDataDetails
                    SS_Batch_Column_Mapping__c mpdMapping = SS_Batch_Column_Mapping__c.getValues('MaintenanceProductDataDetails');
                    Map<String,String> tempImportantRroduct = new Map<String,String>();
                    for (Integer i = 1; i <= FIELDMAX; i++) {
                        String lpadI = ('00' + i).right(3);
                        String fromColumn = 'From_Column_' + lpadI + '__c';
                        String apiStr = String.valueOf(mpdMapping.get(fromColumn));
                        if (String.isBlank(apiStr) == false) {
                            String ssColumn = 'SS_Column_' + lpadI + '__c';
                            String ssApiStr = String.valueOf(mpdMapping.get(ssColumn));
                            tempImportantRroduct.put(ssApiStr,apiStr); 
                        }
                    }
                    // FY23修改 WLIG-CFV4AV 重点产品维护（新）start 
                    String keyOld;
                    // String old_BF;
                    // String old_ENT;
                    // String old_ET;
                    String old_GI;
                    // String old_GS;
                    // String old_GYN;
                    // String old_OTH;
                    // String old_URO;

                    String keyNew;
                    // String new_BF;
                    // String new_ENT;
                    // String new_ET;
                    String new_GI;
                    // String new_GS;
                    // String new_GYN;
                    // String new_OTH;
                    // String new_URO;
                    if(String.isNotBlank(mpdDetails.Key_product_147P_Old__c)){
                        // keyOld = tempImportantRroduct.get(mpdDetails.Key_product_147P_Old__c);
                        keyOld = ControllerUtil.getKeyByProduct(mpdDetails.Key_product_147P_Old__c);
                        if(String.isNotBlank(keyOld)){
                            // old_BF = 'Important_Rroduct_'+keyOld+'BF__c';
                            // old_ENT = 'Important_Rroduct_'+keyOld+'ENT__c';
                            // old_ET = 'Important_Rroduct_'+keyOld+'ET__c';
                            old_GI = 'Important_Rroduct_'+keyOld+'GI__c';
                            // old_GS = 'Important_Rroduct_'+keyOld+'GS__c';
                            // old_GYN = 'Important_Rroduct_'+keyOld+'GYN__c';
                            // old_OTH = 'Important_Rroduct_'+keyOld+'OTH__c';
                            // old_URO = 'Important_Rroduct_'+keyOld+'URO__c';
                        }
                    }
                    if(String.isNotBlank(mpdDetails.Key_product_147P__c)){
                        // keyNew = tempImportantRroduct.get(mpdDetails.Key_product_147P__c);
                        keyNew = ControllerUtil.getKeyByProduct(mpdDetails.Key_product_147P__c);
                        System.debug('====keyNew：'+keyNew);
                        // new_BF = 'Important_Rroduct_'+keyNew+'BF__c';
                        // new_ENT = 'Important_Rroduct_'+keyNew+'ENT__c';
                        // new_ET = 'Important_Rroduct_'+keyNew+'ET__c';
                        new_GI = 'Important_Rroduct_'+keyNew+'GI__c';
                        // new_GS = 'Important_Rroduct_'+keyNew+'GS__c';
                        // new_GYN = 'Important_Rroduct_'+keyNew+'GYN__c';
                        // new_OTH = 'Important_Rroduct_'+keyNew+'OTH__c';
                        // new_URO = 'Important_Rroduct_'+keyNew+'URO__c';
                    }
                    if(String.isNotBlank(mpdDetails.Key_product_147P__c)){
                        if(mpdDetails.Key_product_147P_Old__c !=null&&!mpdDetails.Key_product_147P__c.equals(mpdDetails.Key_product_147P_Old__c)){
                            //old的false 新的true
                            if(String.isNotBlank(keyNew)){
                                // product2.put(new_BF,true);product2.put(new_ENT,true);
                                // product2.put(new_ET,true);
                                product2.put(new_GI,true);
                                // product2.put(new_GS,true);product2.put(new_GYN,true);
                                // product2.put(new_OTH,true);product2.put(new_URO,true);
                            }
                            if(String.isNotBlank(keyOld)){
                                // product2.put(old_BF,false);product2.put(old_ENT,false);
                                // product2.put(old_ET,false);
                                product2.put(old_GI,false);
                                // product2.put(old_GS,false);product2.put(old_GYN,false);
                                // product2.put(old_OTH,false);product2.put(old_URO,false);
                            }  
                        }else if(mpdDetails.Key_product_147P_Old__c == null){
                            //old的空只需要新的true
                            System.debug('====keyNew：'+keyNew);
                            if(String.isNotBlank(keyNew)){
                                // product2.put(new_BF,true);product2.put(new_ENT,true);
                                // product2.put(new_ET,true);
                                product2.put(new_GI,true);
                                // product2.put(new_GS,true);product2.put(new_GYN,true);
                                // product2.put(new_OTH,true);product2.put(new_URO,true);
                            }
                        }   
                    }
                    else if(String.isNotBlank(mpdDetails.Key_product_147P_Old__c)){
                        //old的false 新的true
                        if(String.isNotBlank(keyOld)){
                            // product2.put(old_BF,false);product2.put(old_ENT,false);
                            // product2.put(old_ET,false);
                            product2.put(old_GI,false);
                            // product2.put(old_GS,false);product2.put(old_GYN,false);
                            // product2.put(old_OTH,false);product2.put(old_URO,false);
                        }
                    }
                    // 新的重点产品
                    product2.Key_product_155P__c = mpdDetails.Key_product_155P__c;
                    // Celon产品区分
                    product2.Celon_Product__c = mpdDetails.Celon_Product__c;
                }
                // FY23修改 WLIG-CFV4AV 重点产品维护（新）end
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteeMain')){
                    product2.Id = mpdDetails.ProductsID__c;
                    if(mpdDetails.GuranteeType_Old__c !=null&&String.isNotBlank(mpdDetails.GuranteeType_Old__c)){
                        if(mpdDetails.GuranteeType__c !=null&&!mpdDetails.GuranteeType__c.equals(mpdDetails.GuranteeType_Old__c)){
                            if(mpdDetails.GuranteeType__c==null||mpdDetails.GuranteeType__c.equals('')){
                                product2.Extend_new_product_gurantee__c = false;//服务
                                product2.Extend_new_product_gurantee_MD__c = false;

                                product2.Entend_gurantee_period_MD__c = null;
                                product2.Extend_Gurantee_Start_MD__c = null;
                                product2.Extend_Gurantee_End_MD__c = null;

                                product2.Entend_gurantee_period__c = null;
                                product2.Extend_Gurantee_Start__c = null;
                                product2.Extend_Gurantee_End__c = null;
                            }else{
                                if(mpdDetails.GuranteeType__c.equals('市场')){
                                    product2.Extend_new_product_gurantee__c = false;//服务
                                    product2.Extend_new_product_gurantee_MD__c = true;
                                    product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;
                                    product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;
                                    product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c;    
                                }
                                if(mpdDetails.GuranteeType__c.equals('服务')){
                                    product2.Extend_new_product_gurantee_MD__c = false;//市场
                                    product2.Extend_new_product_gurantee__c = true;
                                    product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;
                                    product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;
                                    product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;
                                }
                            }   
                        }else{
                            if(mpdDetails.GuranteeType__c==null){
                                product2.Extend_new_product_gurantee__c = false;//服务
                                product2.Extend_new_product_gurantee_MD__c = false;

                                product2.Entend_gurantee_period_MD__c = null;
                                product2.Extend_Gurantee_Start_MD__c = null;
                                product2.Extend_Gurantee_End_MD__c = null;

                                product2.Entend_gurantee_period__c = null;
                                product2.Extend_Gurantee_Start__c = null;
                                product2.Extend_Gurantee_End__c = null;
                            }else{
                                if(mpdDetails.GuranteeType__c.equals('市场')){
                                    if(mpdDetails.Entend_gurantee_period__c != mpdDetails.Entend_gurantee_period_Old__c){product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;}
                                    if(mpdDetails.Gurantee_Start_Date__c !=mpdDetails.Gurantee_Start_Date_Old__c ){product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;}
                                    if(mpdDetails.Gurantee_End_Date__c != mpdDetails.Gurantee_End_Date_Old__c){product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c; } 
                                }
                                if(mpdDetails.GuranteeType__c.equals('服务')){
                                    if(mpdDetails.Entend_gurantee_period__c != mpdDetails.Entend_gurantee_period_Old__c){product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;}
                                    if(mpdDetails.Gurantee_Start_Date__c !=mpdDetails.Gurantee_Start_Date_Old__c ){product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;}
                                    if(mpdDetails.Gurantee_End_Date__c != mpdDetails.Gurantee_End_Date_Old__c){product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;}
                                }
                            }
                            
                        }
                    }else{
                        if(mpdDetails.GuranteeType__c!=null&&mpdDetails.GuranteeType__c.equals('市场')){
                            product2.Extend_new_product_gurantee__c = false;//服务
                            product2.Extend_new_product_gurantee_MD__c = true;
                            product2.Entend_gurantee_period_MD__c = mpdDetails.Entend_gurantee_period__c;
                            product2.Extend_Gurantee_Start_MD__c = mpdDetails.Gurantee_Start_Date__c;
                            product2.Extend_Gurantee_End_MD__c = mpdDetails.Gurantee_End_Date__c;  
                        }
                        if(mpdDetails.GuranteeType__c !=null&&mpdDetails.GuranteeType__c.equals('服务')){
                            product2.Extend_new_product_gurantee_MD__c = false;//市场
                            product2.Extend_new_product_gurantee__c = true;
                            product2.Entend_gurantee_period__c = mpdDetails.Entend_gurantee_period__c;
                            product2.Extend_Gurantee_Start__c = mpdDetails.Gurantee_Start_Date__c;
                            product2.Extend_Gurantee_End__c = mpdDetails.Gurantee_End_Date__c;
                        }
                    }
                    product2.CanNotCancelledGurantee__c = mpdDetails.CanNotCancelledGurantee__c;//20201012 ljh  CHAN-BTR67N add
                    //两个价格
                    //1.NoDiscount价格
                    //Date  dateFrom_Service = mpdDetails.Intra_Trade_Service_RMB_DateFrom__c;
                    Date  dateFrom_Service = Date.toDay();
                    Date  dateTo_Service =  Date.valueOf(System.Label.dateTo_Service);
                    if(mpdDetails.Intra_Trade_Service_RMB__c==null|| mpdDetails.Intra_Trade_Service_RMB__c.format()=='0'){
                        product2.Intra_Trade_Service_RMB_Date1__c =  null;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = null;
                    }else{
                        product2.Intra_Trade_Service_RMB_Date1__c =  dateFrom_Service;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = dateTo_Service;
                    }
                    product2.Intra_Trade_Service_RMB_1__c  = mpdDetails.Intra_Trade_Service_RMB__c;
                    //2.多年保修计提价格
                    //Date  dateFrom_Gurantee = mpdDetails.Intra_Trade_Gurantee_RMB_DateFrom__c;
                    Date  dateFrom_Gurantee = Date.toDay();
                    Date  dateTo_Gurantee =  Date.valueOf(System.Label.dateTo_Gurantee);
                    if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null || mpdDetails.Intra_Trade_Gurantee_RMB__c.format()=='0'){
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  null;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = null;
                    }else{
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  dateFrom_Gurantee;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = dateTo_Gurantee;
                    }
                    product2.Intra_Trade_Gurantee_RMB_1__c  = mpdDetails.Intra_Trade_Gurantee_RMB__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                    //2021/01/19 liying start
                    product2.Virtual_Contract__c  = mpdDetails.Virtual_Contract__c; 
                    //2021/01/19 liying end
                }
                if(mpdDetails.RecordType.DeveloperName.equals('GuranteePrice')){
                    product2.Id = mpdDetails.ProductsID__c;
                    //2021/01/19 liying start
                    product2.Virtual_Contract__c  = mpdDetails.Virtual_Contract__c; 
                    //2021/01/19 liying end
                    //两个价格
                    //1.NoDiscount价格
                    //Date  dateFrom_Service_G = mpdDetails.Intra_Trade_Service_RMB_DateFrom__c;
                    Date  dateFrom_Service_G = Date.toDay();
                    Date  dateTo_Service = Date.valueOf(System.Label.dateTo_Service);
                    if(mpdDetails.Intra_Trade_Service_RMB__c==null|| mpdDetails.Intra_Trade_Service_RMB__c.format()=='0'){
                        product2.Intra_Trade_Service_RMB_Date1__c =  null;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = null;
                    }else{
                        product2.Intra_Trade_Service_RMB_Date1__c =  dateFrom_Service_G;
                        product2.Intra_Trade_Service_RMB_EndDate1__c = dateTo_Service;
                    }
                    product2.Intra_Trade_Service_RMB_1__c  = mpdDetails.Intra_Trade_Service_RMB__c;  
                    //2.多年保修计提价格
                    //Date  dateFrom_Gurantee_G = mpdDetails.Intra_Trade_Gurantee_RMB_DateFrom__c;
                    Date  dateFrom_Gurantee_G = Date.toDay();
                    Date dateTo_Gurantee = Date.valueOf(System.Label.dateTo_Gurantee);
                    if(mpdDetails.Intra_Trade_Gurantee_RMB__c==null || mpdDetails.Intra_Trade_Gurantee_RMB__c.format()=='0'){
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  null;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = null;
                    }else{
                        product2.Intra_Trade_Gurantee_RMB_Date1__c =  dateFrom_Gurantee_G;
                        product2.Intra_Trade_Gurantee_RMB_End_Date1__c = dateTo_Gurantee;
                    }
                    product2.Intra_Trade_Gurantee_RMB_1__c  = mpdDetails.Intra_Trade_Gurantee_RMB__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                }
                if(mpdDetails.RecordType.DeveloperName.equals('RepairInfo')){
                    product2.Id = mpdDetails.ProductsID__c;
                    product2.RepairListPriceLevelA__c = mpdDetails.RepairListPriceLevelA__c;
                    product2.RepairListPriceLevelB__c = mpdDetails.RepairListPriceLevelB__c;
                    product2.RepairListPriceLevelC__c = mpdDetails.RepairListPriceLevelC__c;
                    product2.CanRepairAccsessary__c = mpdDetails.CanRepairAccsessary__c;
                    product2.Repair_Product_Code__c = mpdDetails.Repair_Product_Code__c;
                    product2.PartSupplyFinishDate__c = mpdDetails.PartSupplyFinishDate__c;
                    product2.Maintenance_Price_Year__c = mpdDetails.Maintenance_Price_Year__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star
                    product2.Can_Repair__c = mpdDetails.Can_Repair__c;
                    product2.Service_Category1__c = mpdDetails.Service_Category1__c;
                    product2.Service_Category2__c = mpdDetails.Service_Category2__c;
                    product2.Service_Category3__c = mpdDetails.Service_Category3__c;
                    product2.Service_Category4__c = mpdDetails.Service_Category4__c;
                    product2.Service_Category5__c = mpdDetails.Service_Category5__c;
                    product2.Service_Category6__c = mpdDetails.Service_Category6__c;
                    product2.Service_Category7__c = mpdDetails.Service_Category7__c;
                    //LJPH-C6A3DF 【委托】 【重要】产品主数据中增加服务用产品分类 liuyan 20210831 Star

                }
                if(mpdDetails.RecordType.DeveloperName.equals('EquipmentInfo')){
                    product2.Id = mpdDetails.ProductsID__c;
                    product2.Asset_loaner_category__c  = mpdDetails.Asset_loaner_category__c;
                    product2.Special_Model__c  = mpdDetails.Special_Model__c;
                    product2.Fixture_Model_No__c  = mpdDetails.Fixture_Model_No__c;
                    product2.Packing_list_Fixture__c = mpdDetails.Packing_list_Fixture__c;
                    product2.Default_Fixture_Arrival_Process__c = mpdDetails.Default_Fixture_Arrival_Process__c;
                    product2.Default_Fixture_Arrival_Product__c = mpdDetails.Default_Fixture_Arrival_Product__c;
                }
                //跟新标识
                product2.IsFromSAP__c = true;
                product2Update.add(product2); 
            }

            if(product2Update.size()>0 && product2Update !=null ){
                Database.SaveResult[] lsr = Database.update(product2Update, false);
                Boolean flag = true;
                for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                    Database.SaveResult sr = lsr[tIdx];
                    System.debug('sr.isSuccess:' + sr.isSuccess());
                    if (!sr.isSuccess()) {
                        Database.Error emsg = sr.getErrors()[0];
                        flag = false;
                    }
                }
                if(flag){
                    update mpdDetailsStatus;
                }   
            }
            //-------------------------
            //发送邮件
            sendMailtoComplete(EmailMPD);          
        }
    }

    private static List<String> getEmail(String label){
        List<String> toEmailList = new List<String>();
        String query = 'select id , Email from user where id in (';
        query += label.replaceall('\'','\\\'') +')';
        system.debug('===test:'+query);
        List<user> tempUserList = Database.Query(query);
        for (user toUser : tempUserList) {
            if (!String.isBlank(toUser.Email)) {
                toEmailList.add(toUser.Email);
            }
        }
        return toEmailList;
    }
    private static boolean sendMailtoComplete(list<Maintenance_Product_Data__c> EmailMPD){
        boolean rs = true;
        List<Messaging.SingleEmailMessage> sendMails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
        String ImportantProduct = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('ImportantProduct').getRecordTypeId();
        String GuranteeMain = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('GuranteeMain').getRecordTypeId();
        String GuranteePrice = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('GuranteePrice').getRecordTypeId();
        String RepairInfo = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('RepairInfo').getRecordTypeId();
        String EquipmentInfo = Schema.SObjectType.Maintenance_Product_Data__c.getRecordTypeInfosByDeveloperName().get('EquipmentInfo').getRecordTypeId();
        String  profileId = UserInfo.getProfileId();//18位

        for (Maintenance_Product_Data__c mpdEmail:EmailMPD) {
            String title = '';
            String body = '';
            List<String> toMailsList = new List<String>();
            String ApplyPersion = '\''+mpdEmail.ApplyPersion__c+'\'';//申请人
            String BuchangApprovalRecommend = '\''+mpdEmail.BuchangApprovalRecommend__c+'\'';//部长推广
            //重点产品  重点产品+基础2+基础3
            if(mpdEmail.RecordTypeId.equals(ImportantProduct)){
                String label01 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_ImportantProduct;
                label01 +=','+System.Label.Maintenance_base02+','+System.Label.Maintenance_base03;
                toMailsList.addall(getEmail(label01));
            }
            //多年保主数据 基础1+基础3 
            if(mpdEmail.RecordTypeId.equals(GuranteeMain)){
                String label02 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                //String label02_2 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                //if(UserInfo.getUserId().equals('00510000002ZJQTAA4')){
                    toMailsList.addall(getEmail(label02)); 
                //}
                //if(UserInfo.getUserId().equals('00510000007cXA9AAM')){
                    //toMailsList.addall(getEmail(label02_2)); 
                //}
            }
            //多年保价格  基础1+基础3+多年保价格
            if(mpdEmail.RecordTypeId.equals(GuranteePrice)){
                String label03 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_GuranteePrice;
                //label03 +=','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base02;
                label03 +=','+System.Label.Maintenance_base01+','+System.Label.Maintenance_base03;
                toMailsList.addall(getEmail(label03));
            }
            //修理 （申请人、部长）简档
            if(mpdEmail.RecordTypeId.equals(RepairInfo)){
                String label04_1 = System.Label.Maintenance_RepairInfo01;
                String label04_2 = System.Label.Maintenance_RepairInfo02;
                if(label04_1.contains(mpdEmail.ApplyPersion__c)){
                    label04_1 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_RepairInfo01;
                    toMailsList.addall(getEmail(label04_1));
                }
                if(label04_2.contains(mpdEmail.ApplyPersion__c)){
                    label04_2 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_RepairInfo02;
                    toMailsList.addall(getEmail(label04_2));
                }
            }
            //备品 （申请人、部长）
            if(mpdEmail.RecordTypeId.equals(EquipmentInfo)){
                String label05 = ApplyPersion+','+BuchangApprovalRecommend+','+System.Label.Maintenance_EquipmentInfo;
                toMailsList.addall(getEmail(label05));
            }
            title =  '产品主数据维护审批申请已批准';
            body += '以下产品主数据维护审批申请已批准，请了解。';
            body += '<br/>';
            body += '<br/>';
            body += '产品维护单链接：';
            body += '<a href="' + URL.getSalesforceBaseUrl().toExternalForm() + '/apex/MaintenanceProductData?id=' + mpdEmail.id + '">'
                    + URL.getSalesforceBaseUrl().toExternalForm() + '/apex/MaintenanceProductData?id=' + mpdEmail.id + '</a><br/>';
            body += '<br/>';
            body += '<br/>';
            body += '谢谢！';

            messageNEW = new Messaging.SingleEmailMessage();
            messageNEW.setSubject(title);
            messageNEW.setHtmlBody(body);
            messageNEW.setCharset('UTF-8');
            messageNEW.setToAddresses(toMailsList);

            List<String> toCcMailsList = new List<String>();
            toCcMailsList.add('olympus@prec-tech.com');
            messageNEW.setCcAddresses(toCcMailsList); 

            sendMails.add(messageNEW);
        }
        system.debug('sendMailtoComplete:' + sendMails);
        if (sendMails.size() > 0) {
            //
            Messaging.Email[] allMails = new Messaging.Email[]{};
            for(Integer j = 0; j < sendMails.size(); j++) {
                allMails.add(sendMails.get(j));
            }
            system.debug('allMails:'+allMails);
            //Messaging.sendEmail(allMails);
            Messaging.SendEmailResult[] results = Messaging.sendEmail(allMails);
            for (Integer i = 0; i < results.size(); i++) {
                if (results[i].success == false) {
                    system.debug('=====send mail error:' + results[i].errors[0].message);
                    rs = false;
                }
            }
        }
        return rs;
    }
}