李彤
2023-03-16 cb4bae31a02af72fea30e1a57c1d498eabef5d01
force-app/main/default/classes/LostCancelReportOppBatch.cls
@@ -31,7 +31,8 @@
      //    query += ' where Id in :IdList';
      // }
      // 2022-01-18 修改 从失单报告取值
      String query = 'SELECT Id FROM Lost_cancel_report__c WHERE Report_Status__c = \'批准\' AND LostType__c in (\'失单\', \'部分失单\') ';
      String query = 'SELECT Id, Opportunity__c FROM Lost_cancel_report__c WHERE Report_Status__c = \'批准\' AND LostType__c in (\'失单\', \'部分失单\') ';
      //20230215 lt DB202302247719 add , Opportunity__c
      if (start_date != null) {
         query += ' AND Submit_Day__c >= :start_date';
      }
@@ -138,148 +139,207 @@
   // 2022-01-18 修改 从失单报告取值
   global void execute(Database.BatchableContext BC, List<Lost_cancel_report__c> scope) {
      // List<String> reportIdList=new List<String>();//20230215 lt DB202302247719 注释
      Set<String> oppIdList = new Set<String>();//20230215 lt DB202302247719
      List<String> reportIdList=new List<String>();
      for (Lost_cancel_report__c report : scope) {
         reportIdList.add(report.Id);
         // reportIdList.add(report.Id);   //20230215 lt DB202302247719 注释
         oppIdList.add(report.Opportunity__c);      //20230215 lt DB202302247719
      }
      List<PCLLostProduct__c> pclLpList=[select
                                 id,                                                                  // 失单型号id
                                 Quantity__c,                                                         // 失单数量
                                 LostProductName__c,                                                  // 失单对手型号
                                 LostProductMannual__c,                                               // 失单对手型号(手动)
                                 PCLLostBrand__r.Lost_By_Company__c,                                  // 失单品牌
                                 PCLLostBrand__r.Lost_reason_main__c,                                 // 失单原因(主)
                                 PCLLostBrand__r.Lost_Reason_Sub__c,                                  // 失单原因(次)
                                 PCLLostBrand__r.Lost_By_Company_Mannual__c,                          // 失单品牌(手动)
                                 PCLLostBrand__r.Agency__r.Name,                                      // 中标经销商
                                 PCLLostBrand__r.AgencyMannual__c,                                    // 中标经销商(手动)
                                 PCLLostBrand__r.Lost_cancel_report__r.LostType__c,                   // 失单类型
                                 PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c,            // 失单总金额
                                 PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c,           // 失单总金额(不含税)  //20230215 lt DB202302247719
                                 PCLLostBrand__r.Lost_cancel_report__r.of_lost_system_processor__c,   // 失单主机台数
                                 PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c
                              from
                                 PCLLostProduct__c
                              where
                              PCLLostBrand__r.Lost_cancel_report__c in :reportIdList];
      // 初始化保存参数
      Map<String, Opportunity> opp_map = new Map<String, Opportunity>();
      Map<String, String> prod_map = new Map<String, String>();
      Map<String, String> agency_map = new Map<String, String>();
      //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
      // List<PCLLostProduct__c> pclLpList=[select
      //                            id,                                                                  // 失单型号id
      //                            Quantity__c,                                                         // 失单数量
      //                            LostProductName__c,                                                  // 失单对手型号
      //                            LostProductMannual__c,                                               // 失单对手型号(手动)
      //                            PCLLostBrand__r.Lost_By_Company__c,                                  // 失单品牌
      //                            PCLLostBrand__r.Lost_reason_main__c,                                 // 失单原因(主)
      //                            PCLLostBrand__r.Lost_Reason_Sub__c,                                  // 失单原因(次)
      //                            PCLLostBrand__r.Lost_By_Company_Mannual__c,                          // 失单品牌(手动)
      //                            PCLLostBrand__r.Agency__r.Name,                                      // 中标经销商
      //                            PCLLostBrand__r.AgencyMannual__c,                                    // 中标经销商(手动)
      //                            PCLLostBrand__r.Lost_cancel_report__r.LostType__c,                   // 失单类型
      //                            PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c,            // 失单总金额
      //                            PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c,           // 失单总金额(不含税)  //20230215 lt DB202302247719
      //                            PCLLostBrand__r.Lost_cancel_report__r.of_lost_system_processor__c,   // 失单主机台数
      //                            PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c
      //                         from
      //                            PCLLostProduct__c
      //                         where
      //                         PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c in :reportIdList];
                              //20230215 lt DB202302247719   PCLLostBrand__r.Lost_cancel_report__c  ---update---  PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c
      // 循环整理数据
      for (PCLLostProduct__c lost : pclLpList) {
         // 判断是否可用数据
         // if (String.isBlank(lost.LostProductName__c) && String.isBlank(lost.LostProductMannual__c)) {
         //    continue;
         // }
      List<Lost_cancel_report__c> lcrList = [SELECT Id, Opportunity__c, TotalAmountLost__c
                                    FROM Lost_cancel_report__c
                                    WHERE Opportunity__c in :oppIdList];
      //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
      //DB202302247719 lt start
      Map<String, Opportunity> opp_map = new Map<String, Opportunity>();
      for (Lost_cancel_report__c lcr : lcrList){
         // 询价id
         String opp_id = lost.PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c;
         String opp_id = lcr.Opportunity__c;
         // 询价
         Opportunity opp = opp_map.get(opp_id);
         if (opp == null) {
            opp = new Opportunity();
            opp.Id = opp_id;
            opp.of_lost_system_processor__c = 0;
            opp_map.put(opp_id, opp);
         }
         // 赋值
         opp.LostTypeText__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostType__c;       // 失单类型
         //opp.LostPrices__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c;  // 失单金额 //20230215 lt DB202302247719 注释
         opp.LostPrices__c = lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c;  // 失单金额  //20230215 lt DB202302247719
         opp.Lost_reason_main__c = lost.PCLLostBrand__r.Lost_reason_main__c;                 // 失单原因(主)
         opp.Lost_Reason_Sub__c = lost.PCLLostBrand__r.Lost_Reason_Sub__c;                   // 失单理由(次)
         opp.of_lost_system_processor__c += lost.Quantity__c;                                // 失单主机台数(累加)
         // 失单产品123 start
         if (opp.CompetitorProduct1__c == null) {
            opp.CompetitorProduct1__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
         }else if (opp.CompetitorProduct2__c == null) {
            opp.CompetitorProduct2__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
         }else if (opp.CompetitorProduct3__c == null) {
            opp.CompetitorProduct3__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
         System.debug('---lt123---询价失单金额---'+opp.LostPrices__c);
         System.debug('---lt123---失单总金额,千元不含税---'+lcr.TotalAmountLost__c);
         if(lcr.TotalAmountLost__c != null){
            if(opp.LostPrices__c == null){
               opp.LostPrices__c = 0;
            }
            opp.LostPrices__c += lcr.TotalAmountLost__c;  // 失单金额
            System.debug('---lt123---累加后询价失单金额---'+opp.LostPrices__c);
         }
         // 失单产品123 end
         // 失单品牌
         opp.PCLLostBrands__c = lost_brand_name(opp.PCLLostBrands__c, lost.PCLLostBrand__r.Lost_By_Company__c, lost.PCLLostBrand__r.Lost_By_Company_Mannual__c);
         // 中标经销商
         opp.Agencies__c = lost_agency_name(opp.Agencies__c, lost.PCLLostBrand__r.Agency__r.Name, lost.PCLLostBrand__r.AgencyMannual__c);
      }
      //DB202302247719 lt end
      //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
      // // 初始化保存参数
      // Map<String, Opportunity> opp_map = new Map<String, Opportunity>();
      // Map<String, String> prod_map = new Map<String, String>();
      // Map<String, String> agency_map = new Map<String, String>();
      // // 循环整理数据
      // for (PCLLostProduct__c lost : pclLpList) {
      //    // 判断是否可用数据
      //    // if (String.isBlank(lost.LostProductName__c) && String.isBlank(lost.LostProductMannual__c)) {
      //    //    continue;
      //    // }
      //    // 询价id
      //    String opp_id = lost.PCLLostBrand__r.Lost_cancel_report__r.Opportunity__c;
      //    // 询价
      //    Opportunity opp = opp_map.get(opp_id);
      //    if (opp == null) {
      //       opp = new Opportunity();
      //       opp.Id = opp_id;
      //       opp.of_lost_system_processor__c = 0;
      //       opp_map.put(opp_id, opp);
      //    }
      //    //20230215 lt DB202302247719 start
      //    System.debug('---lt123---询价失单金额---'+opp.LostPrices__c);
      //    System.debug('---lt123---失单总金额,千元不含税---'+lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c);
      //    if(lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c != null){
      //       if(opp.LostPrices__c == null){
      //          opp.LostPrices__c = 0;
      //       }
      //       opp.LostPrices__c += lost.PCLLostBrand__r.Lost_cancel_report__r.TotalAmountLost__c;  // 失单金额
      //    }
      //    //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
      //    // 赋值
      //    // opp.LostTypeText__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostType__c;       // 失单类型
      //    // //opp.LostPrices__c = lost.PCLLostBrand__r.Lost_cancel_report__r.LostTotalAmount__c;  // 失单金额
      //    // opp.Lost_reason_main__c = lost.PCLLostBrand__r.Lost_reason_main__c;                 // 失单原因(主)
      //    // opp.Lost_Reason_Sub__c = lost.PCLLostBrand__r.Lost_Reason_Sub__c;                   // 失单理由(次)
      //    // opp.of_lost_system_processor__c += lost.Quantity__c;                                // 失单主机台数(累加)
      //    // 失单产品123 start
      //    // if (opp.CompetitorProduct1__c == null) {
      //    //    opp.CompetitorProduct1__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
      //    // }else if (opp.CompetitorProduct2__c == null) {
      //    //    opp.CompetitorProduct2__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
      //    // }else if (opp.CompetitorProduct3__c == null) {
      //    //    opp.CompetitorProduct3__c = lost_product_name(lost.LostProductName__c, lost.LostProductMannual__c);
      //    // }
      //    // 失单产品123 end
      //    // 失单品牌
      //    // opp.PCLLostBrands__c = lost_brand_name(opp.PCLLostBrands__c, lost.PCLLostBrand__r.Lost_By_Company__c, lost.PCLLostBrand__r.Lost_By_Company_Mannual__c);
      //    // 中标经销商
      //    // opp.Agencies__c = lost_agency_name(opp.Agencies__c, lost.PCLLostBrand__r.Agency__r.Name, lost.PCLLostBrand__r.AgencyMannual__c);
      //    //DB202302247719 注释  lt
      //    //20230215 lt DB202302247719 end
      // }
      //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
      System.debug(opp_map);
      StaticParameter.EscapeOppandStaTrigger = true; //20230215 lt DB202302247719
      update opp_map.values();
      StaticParameter.EscapeOppandStaTrigger = true; //20230215 lt DB202302247719
   }
   //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
   // 失单对手型号
   private String lost_product_name(String lost_product, String lost_product_mannual) {
      if (String.isBlank(lost_product) && String.isNotBlank(lost_product_mannual)) {
         return lost_product_mannual;
      }
      return lost_product;
   }
   // private String lost_product_name(String lost_product, String lost_product_mannual) {
   //    if (String.isBlank(lost_product) && String.isNotBlank(lost_product_mannual)) {
   //       return lost_product_mannual;
   //    }
   //    return lost_product;
   // }
   // 失单品牌
   private String lost_brand_name(String lost_brand_in_opp, String lost_brand, String lost_brand_mannual) {
      // 初始化参数
      String plus = '';
      // 判断
      // SWAG-CCC6F6 2022-04-22 ssm start
      // 询价上都只显示选项列表的值,不需要手动的值
      // if ('其他'.equals(lost_brand) && String.isNotBlank(lost_brand_mannual)) {
      //    plus = lost_brand_mannual;
      // } else {
      //    plus = lost_brand;
      // }
      plus = lost_brand;
      // SWAG-CCC6F6 2022-04-22 ssm end
      // 初始化返回值
      if (String.isBlank(lost_brand_in_opp)) {
         lost_brand_in_opp = '';
      }
      // 拼接
      if (String.isNotBlank(plus) && !lost_brand_in_opp.contains(plus)) {
         // 增加分隔标识
         if (String.isNotBlank(lost_brand_in_opp)) {
            lost_brand_in_opp += ',';
         }
         lost_brand_in_opp += plus;
      }
      return lost_brand_in_opp;
   }
   // // 失单品牌
   // private String lost_brand_name(String lost_brand_in_opp, String lost_brand, String lost_brand_mannual) {
   //    // 初始化参数
   //    String plus = '';
   //    // 判断
   //    // SWAG-CCC6F6 2022-04-22 ssm start
   //    // 询价上都只显示选项列表的值,不需要手动的值
   //    // if ('其他'.equals(lost_brand) && String.isNotBlank(lost_brand_mannual)) {
   //    //    plus = lost_brand_mannual;
   //    // } else {
   //    //    plus = lost_brand;
   //    // }
   //    plus = lost_brand;
   //    // SWAG-CCC6F6 2022-04-22 ssm end
   //    // 初始化返回值
   //    if (String.isBlank(lost_brand_in_opp)) {
   //       lost_brand_in_opp = '';
   //    }
   //    // 拼接
   //    if (String.isNotBlank(plus) && !lost_brand_in_opp.contains(plus)) {
   //       // 增加分隔标识
   //       if (String.isNotBlank(lost_brand_in_opp)) {
   //          lost_brand_in_opp += ',';
   //       }
   //       lost_brand_in_opp += plus;
   //    }
   //    return lost_brand_in_opp;
   // }
   // 中标经销商
   private String lost_agency_name(String lost_agency_in_opp, String lost_agency, String lost_agency_mannual) {
      // 初始化参数
      String plus = '';
      // 判断
      // SWAG-CCC6F6 2022-04-22 ssm start
      // 询价上都只显示选项列表的值,不需要手动的值
      // if ('对手经销商'.equals(lost_agency) && String.isNotBlank(lost_agency_mannual)) {
      //    plus = lost_agency_mannual;
      // } else {
      //    plus = lost_agency;
      // }
      plus = lost_agency;
      // SWAG-CCC6F6 2022-04-22 ssm end
      // 初始化返回值
      if (String.isBlank(lost_agency_in_opp)) {
         lost_agency_in_opp = '';
      }
      // 拼接
      if (String.isNotBlank(plus) && !lost_agency_in_opp.contains(plus)) {
         // 增加分隔标识
         if (String.isNotBlank(lost_agency_in_opp)) {
            lost_agency_in_opp += ',';
         }
         lost_agency_in_opp += plus;
      }
      return lost_agency_in_opp;
   }
   // // 中标经销商
   // private String lost_agency_name(String lost_agency_in_opp, String lost_agency, String lost_agency_mannual) {
   //    // 初始化参数
   //    String plus = '';
   //    // 判断
   //    // SWAG-CCC6F6 2022-04-22 ssm start
   //    // 询价上都只显示选项列表的值,不需要手动的值
   //    // if ('对手经销商'.equals(lost_agency) && String.isNotBlank(lost_agency_mannual)) {
   //    //    plus = lost_agency_mannual;
   //    // } else {
   //    //    plus = lost_agency;
   //    // }
   //    plus = lost_agency;
   //    // SWAG-CCC6F6 2022-04-22 ssm end
   //    // 初始化返回值
   //    if (String.isBlank(lost_agency_in_opp)) {
   //       lost_agency_in_opp = '';
   //    }
   //    // 拼接
   //    if (String.isNotBlank(plus) && !lost_agency_in_opp.contains(plus)) {
   //       // 增加分隔标识
   //       if (String.isNotBlank(lost_agency_in_opp)) {
   //          lost_agency_in_opp += ',';
   //       }
   //       lost_agency_in_opp += plus;
   //    }
   //    return lost_agency_in_opp;
   // }
   //DB202302247719 注释  lt  注释原因:DB202302247719课题刷失单报告数据只刷失单金额
   global void finish(Database.BatchableContext BC) {