19626
2023-06-29 45e4876c811c861adc5744d06b5bba840fae397a
force-app/main/default/classes/lexPCLLostReportLwcController.cls
@@ -1,59 +1,111 @@
public with sharing class lexPCLLostReportLwcController {
    public static Product2 pro2;
   public static string oppId;
   public static string lostReportId;
   public static Product2 pro2 { get; set; }
   public static string oppId { get; set; }
   public static string lostReportId { get; set; }
   // 当前页面状态 Create,View and Edit, MarketEdit
   public static string pageStatus;
   public static string pageStatus { get; set; }
   // 失单,部分失单
   public static string lostType;
   public static Set<ID> deleteBrandIDSet = new Set<ID>();
   public static string submitFlag;
   public static LostReport LostReport;
   public static integer brandNo;
   public static integer RemoveBrandNo;
   public static integer setBrandNo;
   public static integer brandCount;
   public static string lostType {get; set;}
   public  static Set<ID> deleteBrandIDSet = new Set<ID>();
   public static string submitFlag {get; set;}
   public static LostReport LostReport {get; set;}
   public static integer brandNo {get; set;}
   public static integer RemoveBrandNo {get; set;}
   public static integer setBrandNo {get; set;}
   public static integer brandCount {get; set;}
   // add tcm 20211122 start
   public static integer topNum;
   public static integer secondNum;
   public static integer topNum {get; set;}
   public static integer secondNum {get; set;}
   // add tcm 20211122 end
   // 竞争对手对照关系表,key是品牌名字,value是ID
   public static map<string,id> CompetitionMap;
   // public PCLLostReportController(ApexPages.StandardController controller) {
   //    this();
   // }
   // public PCLLostReportController(){
   //    oppId =
   //       ApexPages.currentPage().getParameters().get('oppId') == null ? ''
   //       : ApexPages.currentPage().getParameters().get('oppId');
   //    lostReportId =
   //       ApexPages.currentPage().getParameters().get('Id') == null ? ''
   //       : ApexPages.currentPage().getParameters().get('Id');
   //    pageStatus =
   //       ApexPages.currentPage().getParameters().get('pageStatus') == null ? ''
   //       : ApexPages.currentPage().getParameters().get('pageStatus');
   //    lostType =
   //       ApexPages.currentPage().getParameters().get('lostType') == null ? ''
   //       : ApexPages.currentPage().getParameters().get('lostType');
   //    submitFlag = ApexPages.currentPage().getParameters().get('submitFlag') == null ? ''
   //        : ApexPages.currentPage().getParameters().get('submitFlag');
   // }
   @AuraEnabled
    public static String getPickList(String objectName, String fieldName) {
      List<Map<String, String>> lstPickvals = new List<Map<String, String>>();
        List<Schema.DescribeSobjectResult> results = Schema.describeSObjects(new List<String>{objectName});
        for(Schema.DescribeSobjectResult res : results) {
            for (Schema.PicklistEntry entry : res.fields.getMap().get(fieldName).getDescribe().getPicklistValues()) {
                if (entry.isActive()) {
                    lstPickvals.add(new Map<String, String>{'label' => entry.getValue(), 'value' => entry.getValue()});
                }
            }
        }
      Map<string,List<Map<String, String>>> pickList =  new Map<string,List<Map<String, String>>>();
      List<Map<String, String>> zhuji = new List<Map<String, String>>();
      List<Map<String, String>> jingzi = new List<Map<String, String>>();
      List<Map<String, String>> xiaojingzhong = new List<Map<String, String>>();
      List<Map<String, String>> nengliang = new List<Map<String, String>>();
      List<Map<String, String>> qita = new List<Map<String, String>>();
      Map<String, String> empty = new Map<String, String>{'label' => '--无--', 'value' => ''};
      zhuji.add(empty);
      jingzi.add(empty);
      xiaojingzhong.add(empty);
      nengliang.add(empty);
      qita.add(empty);
      for (Integer i = 0; i < lstPickvals.size(); i++) {
         if ((i >= 0 && i <= 6) || i == 36) {
            zhuji.add(lstPickvals[i]);
         }
         if ((i >= 7 && i <= 23) || i == 36) {
            jingzi.add(lstPickvals[i]);
         }
         if ((i >= 24 && i <= 29) || i == 35) {
            xiaojingzhong.add(lstPickvals[i]);
         }
         if ((i >= 30 && i <= 34) || i == 36) {
            nengliang.add(lstPickvals[i]);
         }
         if (i == 36) {
            qita.add(lstPickvals[i]);
         }
      }
      pickList.put('主机', zhuji);
      pickList.put('镜子', jingzi);
      pickList.put('小镜种', xiaojingzhong);
      pickList.put('能量', nengliang);
      pickList.put('其他', qita);
        return JSON.serialize(pickList);
    }
   @AuraEnabled
   public static Map<string,object> init (string oppId1,string lostReportId1,string pageStatus1,string lostType1,string submitFlag1){
      try {
         oppId = oppId1;
         lostReportId = lostReportId1;
         pageStatus = pageStatus1;
         lostType = lostType1;
         submitFlag = submitFlag1;
         return init1();
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
   // 初始化
    @AuraEnabled
   public static String init(){
   public static Map<string,object> init1(){
      Map<string,object> maps = new Map<string,object>();
      if(string.isblank(pageStatus)) {
         // 这里需要报错没有传参数
         return '页面出错,请刷新后重试!';
         maps.put('message', '页面出错,请刷新后重试!');
         return maps;
      }
      else if(pageStatus.equals('Create')) {
         if(string.isBlank(oppId)) {
            // 这里报错创建时候必须有询价
            return '创建失单报告时,请重询价页面创建';
            maps.put('message', '创建失单报告时,请重询价页面创建');
            return maps;
         }
         list<opportunity> opplist =
         [select id,Name,
@@ -64,11 +116,13 @@
                     limit 1];
         if( opplist.size() == 0) {
            pageStatus = null;
            return '您没有询价的查看权限,无法创建失单报告!';
            maps.put('message', '您没有询价的查看权限,无法创建失单报告!');
            return maps;
         }
         Schema.DescribeSObjectResult s = Lost_cancel_report__c.sObjectType.getDescribe();
         if( !s.isCreateable()) {
            return '您不能创建失单/部分失单报告!';
            maps.put('message', '您不能创建失单/部分失单报告!');
            return maps;
         }
         Lost_cancel_report__c tempLostReport =
            new Lost_cancel_report__c(LostType__c= lostType,
@@ -85,7 +139,8 @@
         system.debug('edit1');
         if(string.isBlank(lostReportId)) {
            pageStatus = null;
            return '您没有查看这个失单/部分失单报告的权限!';
            maps.put('message', '您没有查看这个失单/部分失单报告的权限!');
            return maps;
         }
         system.debug('edit2');
         list<Lost_cancel_report__c> lostReportList =
@@ -94,6 +149,7 @@
          Lost_Reason_Sub__c,Lost_By_Company__c,
          LostTotalAmount__c,LostType__c,
          Report_Status__c,Sales_assistant__c,
          InclusionUltrasound__c, //20230506 lt DB202304618804  包含超声
          Opportunity__c, Opportunity__r.Sales_assistant_ID__c,
          Opportunity__r.Sales_manager_departmentID__c,
          Manager_sales__c,recordtypeid,recordtype.DeveloperName
@@ -102,7 +158,8 @@
         if(lostReportList.size() == 0) {
            pageStatus = null;
            return '您没有查看这个失单/部分失单报告的权限!';
            maps.put('message', '您没有查看这个失单/部分失单报告的权限!');
            return maps;
         }
         lostReportList[0].Sales_assistant__c =
            lostReportList[0].Opportunity__r.Sales_assistant_ID__c;
@@ -176,231 +233,83 @@
      // 读取竞争对手品牌
      // BrandmapSet();
      if(string.isNotEmpty(submitFlag)) {
            return '提交成功!';
      }
        return null;
   }
   // 编辑 只有系统管理员或者草案中可以编辑
    @AuraEnabled
   public static String edit2(){
      if( userinfo.getProfileId() == (ID) '00e10000000Y3o5AAC' ||
          '草案'.equals(LostReport.LostReport.Report_Status__c)
          ) {
         pageStatus = 'Edit';
         init();
      }
      else{
            return '只有在草案中才能进行编辑!';
      }
      return null;
   }
   // 设置品牌
    @AuraEnabled
   public static void setBrand(){
      list<LostBrand> LostBrandlist =  LostReport.LostBrands;
      LostBrand tempBrand = LostBrandlist.get(setBrandNo);
      string brandName = tempBrand.lostBrand.Lost_By_Company__c;
      // fy SWAG-CCC6F6 start
      if(!'其他'.equals(tempBrand.lostBrand.Lost_By_Company_Mannual__c)){
         tempBrand.lostBrand.Lost_By_Company_Mannual__c = null;
      }
      system.debug('aaa5+++'+tempBrand.lostBrand.Lost_By_Company_Mannual__c);
      // fy SWAG-CCC6F6 end
      // ID compID = CompetitionMap.get(brandName);
      for(PCLLostProducts tempLostProduct : tempBrand.LostProducts ) {
         // test
         // tempLostProduct.LostProductss.Competitor__c = compID;
         tempLostProduct.LostProductss.LostBrandName__c = brandName;
         tempLostProduct.LostProductss.LostProduct__c = null;
         tempLostProduct.LostProductss.LostProductMannual__c = null;
         tempLostProduct.LostProductss.Quantity__c = null;
         tempLostProduct.LostProductss.ProductClass__c = null;
         tempLostProduct.LostProductss.ProductCategory__c = null;
         tempLostProduct.bool=false;
      }
   }
   // 设置品牌
    @AuraEnabled
   public static void setbrandmannual(){
      list<LostBrand> LostBrandlist =  LostReport.LostBrands;
      LostBrand tempBrand = LostBrandlist.get(setBrandNo);
      string brandName = tempBrand.lostBrand.Lost_By_Company__c;
      String brandNameMannual = tempBrand.lostBrand.Lost_By_Company_Mannual__c;
      // ID compID = CompetitionMap.get(brandName);
      for(PCLLostProducts tempLostProduct : tempBrand.LostProducts ) {
         // test
         // tempLostProduct.LostProductss.Competitor__c = compID;
         system.debug('aaaa3++'+brandNameMannual);
         if ('其他'.equals(brandName) && brandNameMannual != null && !''.equals(brandNameMannual)) {
            tempLostProduct.LostProductss.LostBrandName__c = brandNameMannual;
            // tempLostProduct.LostProductss.LostProduct__c = null;
            // tempLostProduct.LostProductss.LostProductMannual__c = null;
            // tempLostProduct.LostProductss.Quantity__c = null;
            // tempLostProduct.LostProductss.ProductClass__c = null;
            // tempLostProduct.LostProductss.ProductCategory__c = null;
            // tempLostProduct.bool=false;
         }
      }
   }
   // 保存
    @AuraEnabled
   public static String save(){
         maps.put('message', '提交成功!');
         maps.put('LostReport', LostReport);
         return maps;
      if(!DataCheck()) {
         return null;
      }
      if(!dataEntry()) {
         return null;
      }
      brandCount = LostReport.LostBrands.size();
      pageStatus = 'View';
      return '保存成功!';
      // List<Map<String,String>> col = new List<Map<String,String>>();
      // Map<String,String> ele = new Map<String,String>();
      // LostReport.LostBrands[0].columns.add();
      maps.put('LostReport', LostReport);
      return maps;
   }
   //读取并构建竞争对手品牌
   // public void BrandmapSet(){
   //    CompetitionMap = new map<string, id>();
   //    list <Competition_Company__c> competitionList
   //        =  [select id,name from Competition_Company__c];
   //    for(Competition_Company__c tempComp : competitionList ) {
   //       CompetitionMap.put(tempComp.name, tempComp.id);
   //    }
   // }
   // 数据检查
    @AuraEnabled
   public static boolean DataCheck(){
      boolean dataCheck = true;
      if(string.isBlank(LostReport.LostReport.LostType__c))
      {
         LostReport.LostReport.LostType__c.addError('必须填写失单类型!');
         dataCheck = false;
      }
      for(LostBrand tempLostBrand : LostReport.LostBrands ) {
         system.debug('aaaa1++'+tempLostBrand.lostBrand.Lost_By_Company_Mannual__c);
         if(string.isblank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
            tempLostBrand.lostBrand.Lost_By_Company__c.addError('请填写失单品牌!');
            dataCheck = false;
         }// fy SWAG-CCC6F6 start
         else if('其他'.equals(tempLostBrand.lostBrand.Lost_By_Company__c)&&string.isblank(tempLostBrand.lostBrand.Lost_By_Company_Mannual__c)){
            tempLostBrand.lostBrand.Lost_By_Company_Mannual__c.addError('请填写失单品牌(手动)!');
            dataCheck = false;
         }// fy SWAG-CCC6F6 end
         system.debug('aaaa2++'+tempLostBrand.lostBrand.Lost_By_Company_Mannual__c);
         if(tempLostBrand.lostBrand.LostPrice__c == null) {
            tempLostBrand.lostBrand.LostPrice__c.addError('失单金额必填!');
            dataCheck = false;
         }
         if(string.isblank(tempLostBrand.lostBrand.Lost_reason_main__c )) {
            tempLostBrand.lostBrand.Lost_reason_main__c.addError('失单理由(主)必填!');
            dataCheck = false;
         }
         if(string.isblank(tempLostBrand.lostBrand.Agency__c )) {
            tempLostBrand.lostBrand.Agency__c.addError('中标经销商必填!');
            dataCheck = false;
         }
         // 检查是否有超过1个有数的产品
         integer productCount = 0;
         for( PCLLostProducts temlostProduct : tempLostBrand.LostProducts) {
            if (temlostProduct.LostProductss.LostProduct__c != null || temlostProduct.LostProductss.LostProductMannual__c != null) {
               System.debug('失单型号' + temlostProduct.LostProductss.LostProduct__c);
               System.debug('失单型号手动' + temlostProduct.LostProductss.LostProductMannual__c);
               productCount ++;
            }
            // update tcm 20211123 start
            if((temlostProduct.LostProductss.LostProduct__c!=null || temlostProduct.LostProductss.LostProductMannual__c!=null)&&(temlostProduct.LostProductss.Quantity__c==null || temlostProduct.LostProductss.Quantity__c ==0)) {
               temlostProduct.LostProductss.Quantity__c.addError('请填写失单数量!');
               dataCheck = false;
            }
            if((temlostProduct.LostProductss.LostProduct__c!=null || temlostProduct.LostProductss.LostProductMannual__c!=null)&&temlostProduct.LostProductss.ProductCategory__c==null) {
               if (temlostProduct.LostProductss.ProductClass__c==null) {
                  temlostProduct.LostProductss.ProductClass__c.addError('失单产品类别必填!');
                  temlostProduct.LostProductss.ProductCategory__c.addError('失单产品必填!');
               }else {
                  temlostProduct.LostProductss.ProductCategory__c.addError('失单产品必填!');
               }
               dataCheck = false;
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
            if ((temlostProduct.LostProductss.LostProduct__c==null && temlostProduct.LostProductss.LostProductMannual__c==null)&&(temlostProduct.LostProductss.ProductCategory__c!=null||temlostProduct.LostProductss.Quantity__c!=null)) {
               if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
                  temlostProduct.LostProductss.LostProduct__c.addError('失单对手型号或失单对手型号(手动)必填!');
               } else{
                  temlostProduct.LostProductss.LostProductMannual__c.addError('失单对手型号或失单对手型号(手动)必填!');
               }
               dataCheck = false;
            }
            // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
            // update tcm 20211123 end
         }
         // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 start
         if (productCount == 0 && tempLostBrand.LostProducts != null && tempLostBrand.LostProducts.size() > 0) {
            if(tempLostBrand.lostBrand.Lost_By_Company__c != '其他'){
               tempLostBrand.LostProducts[0].LostProductss.LostProduct__c.addError('至少录入1条失单对手型号信息!');
            }else{
               tempLostBrand.LostProducts[0].LostProductss.LostProductMannual__c.addError('至少录入1条失单对手型号信息!');
            }
            dataCheck = false;
         }
         // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
      }
      return dataCheck;
   }
    @AuraEnabled
   public static list<LostBrand> brandcopy(){
      list<LostBrand> tempbrands = new list<LostBrand>();
      for(LostBrand tempbrand: LostReport.LostBrands) {
         LostBrand LostBrand =
            new LostBrand(tempbrand.lineNo,tempbrand.LostProducts );
         LostBrand.lostBrand = tempbrand.lostBrand.clone();
         LostBrand.lostBrand.id = tempbrand.lostBrand.id;
         tempbrands.add(LostBrand);
      }
      return tempbrands;
   @AuraEnabled
   public static String searchBrands(){
      String ObjectApi_name = 'PCLLostBrand__c';
      String Field_name = 'Lost_By_Company__c';
        //From the Object Api name retrieving the SObject
        Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name);
        Sobject Object_name = targetType.newSObject();
         //grab the sobject that was passed
        Schema.sObjectType sobject_type = Object_name.getSObjectType();
        //describe the sobject
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
         //get a map of fields for the passed sobject
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
        //grab the list of picklist values for the passed field on the sobject
        List<Schema.PicklistEntry> pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues();
        List<Map<String, Object>> lstPickvals = new List<Map<String, Object>>();
        for (Schema.PicklistEntry a : pick_list_values)
        { //for all values in the picklist list
         lstPickvals.add(new Map<String, Object>{'label' => a.getValue(), 'value' => a.getValue()});
        }
      String jsonStr = JSON.serialize(lstPickvals);
        return jsonStr;
   }
   // 数据录入
    @AuraEnabled
   public static boolean dataEntry(){
   @AuraEnabled
   public static Map<String,String> dataEntry(String report1,List<Id> deleteBrandIdList,List<Id> deleteProductIdList){
      LostReport report = (LostReport)JSON.deserialize(report1, LostReport.class);
      Map<String,String> messageMap = new Map <String,String>();
      system.debug('save---start:');
      Savepoint sp = Database.setSavepoint();
      Lost_cancel_report__c templostReport = LostReport.lostReport.clone();
      templostReport.id = LostReport.lostReport.id;
      list<LostBrand> tempBrands = brandcopy();
      try{
         // 待完成,这里需要把第一个品牌的信息赋值到这个失单报告上去,还有需要把各个品牌金额汇总加起来;
         //system.debug('LostReport.LostBrands:'+LostReport.LostBrands);
         // 设置记录类型
         LostReport.lostReport.recordTypeID  =  LostReport.lostReport.LostType__c== '失单'?Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_Lost_report').getRecordTypeId() : Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_PartLost_report').getRecordTypeId();
         LostReport.lostReport.LostTotalAmount__c    = 0;
         LostReport.lostReport.of_lost_system_processor__c   = 0;
         LostReport.lostReport.Lost_reason_main__c = null;
         LostReport.lostReport.Lost_Reason_Sub__c  = null;
         LostReport.lostReport.Lost_By_Company__c = null;
         for(LostBrand tempLostBrand : LostReport.LostBrands) {
         report.lostReport.recordTypeID  =  report.lostReport.LostType__c== '失单'?Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_Lost_report').getRecordTypeId() : Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByDeveloperName().get('PCL_PartLost_report').getRecordTypeId();
         report.lostReport.LostTotalAmount__c    = 0;
         report.lostReport.of_lost_system_processor__c   = 0;
         report.lostReport.Lost_reason_main__c = null;
         report.lostReport.Lost_Reason_Sub__c  = null;
         report.lostReport.Lost_By_Company__c = null;
         for(LostBrand tempLostBrand : report.LostBrands) {
            if(string.isNotBlank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
               LostReport.lostReport.LostTotalAmount__c += tempLostBrand.lostBrand.LostPrice__c;
               LostReport.lostReport.Lost_reason_main__c =
                  LostReport.lostReport.Lost_reason_main__c == null
               report.lostReport.LostTotalAmount__c += tempLostBrand.lostBrand.LostPrice__c;
               report.lostReport.Lost_reason_main__c =
               report.lostReport.Lost_reason_main__c == null
                  ? tempLostBrand.lostBrand.Lost_reason_main__c
                  : LostReport.lostReport.Lost_reason_main__c;
               LostReport.lostReport.Lost_Reason_Sub__c  =
                  LostReport.lostReport.Lost_Reason_Sub__c == null
                  : report.lostReport.Lost_reason_main__c;
                  report.lostReport.Lost_Reason_Sub__c  =
                  report.lostReport.Lost_Reason_Sub__c == null
                  ? tempLostBrand.lostBrand.Lost_Reason_Sub__c
                  : LostReport.lostReport.Lost_Reason_Sub__c;
               LostReport.lostReport.Lost_By_Company__c =
                  LostReport.lostReport.Lost_By_Company__c == null
                  : report.lostReport.Lost_Reason_Sub__c;
                  report.lostReport.Lost_By_Company__c =
                  report.lostReport.Lost_By_Company__c == null
                  ? tempLostBrand.lostBrand.Lost_By_Company__c
                  : LostReport.lostReport.Lost_By_Company__c;
                  : report.lostReport.Lost_By_Company__c;
               for( PCLLostProducts tempLostProduct : tempLostBrand.LostProducts) {
                  if(tempLostProduct.LostProductss.Quantity__c !=null && tempLostProduct.LostProductss.Quantity__c >0) {
                     LostReport.lostReport.of_lost_system_processor__c   += tempLostProduct.LostProductss.Quantity__c;
                     report.lostReport.of_lost_system_processor__c   += tempLostProduct.LostProductss.Quantity__c;
                  }
               }
            }
         }
         upsert LostReport.lostReport;
         upsert report.lostReport;
         String reportId = report.lostReport.Id;
         messageMap.put('reportId', reportId);
         // 需要插入更新的失单品牌
         map<integer,PCLLostBrand__c> upsertLostBrandMap = new map<integer,PCLLostBrand__c>();
         // 需要删掉的失单品牌
@@ -409,30 +318,32 @@
         map<string,PCLLostProduct__c> upsertLostProductMap = new map<string,PCLLostProduct__c>();
         // 需要删掉的失单品牌
         list<PCLLostProduct__c> deleteLostProductList = new list<PCLLostProduct__c>();
         for(LostBrand tempLostBrand : LostReport.LostBrands ) {
         Integer lineNo = 0;
         for(LostBrand tempLostBrand : report.LostBrands ) {
            // 如果有失单品牌,那么就要插入更新;
            system.debug('tempLostBrand.lostBrand.Lost_By_Company__c:'+tempLostBrand.lostBrand.Lost_By_Company__c);
            if(string.isNotBlank(tempLostBrand.lostBrand.Lost_By_Company__c)) {
               upsertLostBrandMap.put(tempLostBrand.lineNo,tempLostBrand.lostBrand);
               tempLostBrand.lostBrand.Lost_cancel_report__c = LostReport.lostReport.id;
               upsertLostBrandMap.put(lineNo,tempLostBrand.lostBrand);
               tempLostBrand.lostBrand.Lost_cancel_report__c = report.lostReport.Id;
               tempLostBrand.lostBrand.Name = tempLostBrand.lostBrand.Lost_By_Company__c;
               integer tempNo = 0;
               for( PCLLostProducts tempLostProduct : tempLostBrand.LostProducts) {
                  system.debug('Name:'+ tempLostProduct.LostProductss.Name );
                  if(tempLostProduct.LostProductss.Quantity__c !=null && tempLostProduct.LostProductss.Quantity__c >0) {
                     tempLostProduct.LostProductss.Name = tempLostBrand.lostBrand.Name + tempLostProduct.LostProductss.Quantity__c;
                     upsertLostProductMap.put(tempLostBrand.lineNo +':'+ tempNo,tempLostProduct.LostProductss);
                  }else if(string.isNotBlank(tempLostProduct.LostProductss.id)) {
                     upsertLostProductMap.put(lineNo +':'+ tempNo,tempLostProduct.LostProductss);
                  }else if(string.isNotBlank(tempLostProduct.LostProductss.Id)) {
                     deleteLostProductList.add(tempLostProduct.LostProductss);
                  }
                  tempNo++;
               }
            }
            // 如果没有品牌,但是有id, 这样数据需要删除
            else if(string.isNotBlank(tempLostBrand.lostBrand.id)) {
               deleteLostBrandMap.put(tempLostBrand.lineNo,tempLostBrand.lostBrand);
            else if(string.isNotBlank(tempLostBrand.lostBrand.Id )) {
               deleteLostBrandMap.put(lineNo,tempLostBrand.lostBrand);
               // 这里可能需要也判断一下失单型号有没有需要删掉
            }
            lineNo++;
         }
         if(upsertLostBrandMap.size()>0) {
            upsert upsertLostBrandMap.values();
@@ -440,7 +351,7 @@
         for( string productNo :upsertLostProductMap.keyset()) {
            PCLLostProduct__c tempLostProduct = upsertLostProductMap.get(productNo);
            integer brandNo = integer.valueof(productNo.split(':')[0]);
            tempLostProduct.PCLLostBrand__c = upsertLostBrandMap.get(brandNo).id;
            tempLostProduct.PCLLostBrand__c = upsertLostBrandMap.get(brandNo).Id;
         }
         if(upsertLostProductMap.size()>0) {
            upsert upsertLostProductMap.values();
@@ -448,13 +359,20 @@
         if(deleteLostBrandMap.size()>0) {
            delete deleteLostBrandMap.values();
            for(PCLLostBrand__c pcl: deleteLostBrandMap.values()) {
               pcl.id = null;
               pcl.Id = null;
            }
         }
         if (deleteProductIdList.size()>0) {
            database.delete(deleteProductIdList);
         }
         if (deleteBrandIdList.size()>0) {
            database.delete(deleteBrandIdList);
         }
         if(deleteLostProductList.size()>0) {
            delete deleteLostProductList;
            for(PCLLostProduct__c pclp: deleteLostProductList) {
               pclp.id = null;
               pclp.Id = null;
            }
         }
         if(deleteBrandIDSet.size() > 0) {
@@ -464,11 +382,11 @@
         }
         for(integer brandNumber =0; brandNumber< LostReport.LostBrands.size(); brandNumber++) {
         for(integer brandNumber =0; brandNumber< report.LostBrands.size(); brandNumber++) {
            LostBrand tempLostBrand = LostReport.LostBrands.get(brandNumber);
            LostBrand tempLostBrand = report.LostBrands.get(brandNumber);
            if(tempLostBrand.lostBrand.Id == null) {
               LostReport.LostBrands.remove(brandNumber);
               report.LostBrands.remove(brandNumber);
               brandNumber--;
               continue;
            }
@@ -481,121 +399,122 @@
               }
            }
         }
         lostReportId = LostReport.lostReport.id;
         // lostReportId = report.lostReport.id;
         system.debug('save---end:');
         messageMap.put('LostReport',JSON.serialize(report));
      }catch (Exception ex) {
         Database.rollback(sp);
         LostReport.lostReport = templostReport;
         LostReport.LostBrands = tempBrands;
         // 这里需要写一些报错信息
         return false;
         messageMap.put('error', ex.getMessage());
         return messageMap;
      }
      return true;
      return messageMap;
   }
   // 取消
   // public PageReference cancel(){
   //    PageReference page = new PageReference('/'+oppId);
   //    page.setRedirect(true);
   //    return page;
   // }
   // 提交
   // public PageReference submit(){
   //    if(!'草案'.equals(LostReport.LostReport.Report_Status__c) ) {
   //       return '只有在草案中才能提交申请!';
   //    }
   //    Savepoint sp = Database.setSavepoint();
   //    try{
   //       LostReport.lostReport.Report_Status__c = '提交';
   //       update LostReport.lostReport;
   //       Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
   //       psr.setObjectId(lostReportId);
   //       Approval.ProcessResult submitResult = Approval.process(psr);
   //       PageReference page = new PageReference('/apex/PCLLostReportPage');
   //       page.getParameters().put('Id', lostReportId);
   //       page.getParameters().put('pageStatus', 'View');
   //       page.getParameters().put('submitFlag', '1');
   //       page.setRedirect(true);
   //       return page;
   //    }catch(exception ex) {
   //       Database.rollback(sp);
   //       return ex.getMessage();
   //    }
   // }
   // 添加品牌
    @AuraEnabled
   public static String addBrand(){
      LostReport.LostBrands.add(new LostBrand(LostReport.LostBrands.size()));
      brandCount = LostReport.LostBrands.size();
      return null;
   }
   // 删除品牌 这个有参数brandNo,才知道是删除那个品牌
    @AuraEnabled
   public static String Remove(){
      system.debug('RemoveBrandNo:'+RemoveBrandNo);
      list<LostBrand> tempLostBrands = new List<lostBrand>();
      Integer i = 0;
      for(integer j = 0; j< LostReport.LostBrands.size(); j++ ) {
         LostBrand templostBrand  = LostReport.LostBrands.get(j);
         if(j != RemoveBrandNo) {
            templostBrand.lineNo = i;
            tempLostBrands.add(templostBrand);
            i++;
         }else if(!string.isBlank(templostBrand.lostBrand.id)) {
            deleteBrandIDSet.add(templostBrand.lostBrand.id);
         }
   @AuraEnabled
   public static Map<string,Object> submit(String report1,String reportId){
      LostReport report = (LostReport)JSON.deserialize(report1, LostReport.class);
      Map<string,string> messages = new Map<string,string>();
      if(!'草案'.equals(report.lostReport.Report_Status__c) ) {
         messages.put('error', '只有在草案中才能提交申请!');
         return messages;
      }
      LostReport.LostBrands =  tempLostBrands;
      brandCount = LostReport.LostBrands.size();
      return null;
      Map<string,Object> objs = new Map<string,Object>();
      Savepoint sp = Database.setSavepoint();
      try{
         report.lostReport.Id = reportId;
         report.lostReport.Report_Status__c = '提交';
         update report.lostReport;
         Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
         psr.setObjectId(reportId);
         Approval.ProcessResult submitResult = Approval.process(psr);
         objs.put('uri', '/apex/PCLLostReportPage');
         objs.put('Id', reportId);
         objs.put('pageStatus', 'View');
         objs.put('submitFlag', '1');
         ProcessInstance pro = [
            SELECT
            ProcessDefinition.Name,
             (
               SELECT StepStatus,
               Comments,
               CreatedDate,
               Actor.Name,
               OriginalActor.Name,
               ProcessNode.Name FROM StepsAndWorkitems order by IsPending DESC, CreatedDate DESC
            )
            FROM ProcessInstance where TargetObjectId =:reportId order by CreatedDate DESC
         ];
         List<ProcessInstanceHistory> items = new List<ProcessInstanceHistory>();
         items.addall(pro.StepsAndWorkitems);
         objs.put('approvalHistorys',items);
         return objs;
      }catch(exception ex) {
         Database.rollback(sp);
         messages.put('error', ex.getMessage());
         return messages;
      }
   }
   // 添加型号, 这个有参数brandNo,才知道是添加到那个品牌
   // update tcm 20211125 添加型号时自动带出品牌 start
    @AuraEnabled
   public static String addProduct(){
      system.debug('brandNo:'+brandNo);
      LostBrand tempLostBrand = LostReport.LostBrands.get(brandNo);
      // PCLLostProduct__c plp = new PCLLostProduct__c(Competitor__c=CompetitionMap.get(LostReport.LostBrands[brandNo].lostBrand.Lost_By_Company__c));
      string brandName = tempLostBrand.lostBrand.Lost_By_Company__c;
      PCLLostProduct__c plp = new PCLLostProduct__c();
      plp.LostBrandName__c = brandName;
      tempLostBrand.LostProducts.add(new PCLLostProducts(tempLostBrand.LostProducts.size(),plp));
      tempLostBrand.ProductSize = tempLostBrand.LostProducts.size();
      return null;
   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
   @AuraEnabled
   public static LostBrand getNewLostBrand(Integer lineNo){
      LostBrand lostBrand = null;
      try {
         //report.LostBrands.add(new LostBrand(report.LostBrands.size()));
         lostBrand = new LostBrand(lineNo);
      } catch (Exception e) {
         System.debug(e.getMessage());
      }
      return lostBrand;
   }
   @AuraEnabled
   public static PCLLostProducts getNewLostProduct(Integer lineNo2){
      PCLLostProducts pro = null;
      try {
         pro = new PCLLostProducts(lineNo2);
      } catch (Exception e) {
         System.debug(e.getMessage());
      }
      return pro;
   }
   // update tcm 20211125 添加型号时自动带出品牌 end
   // 页面的数据结构
   public class LostReport {
        @AuraEnabled
      @AuraEnabled
      public Lost_cancel_report__c lostReport { get; set; }
        @AuraEnabled
      @AuraEnabled
      public list<LostBrand> LostBrands { get; set; }
      @AuraEnabled
      public list<ProcessInstanceHistory> approvalHistorys { get; set; }
      public LostReport(){
         lostReport = new Lost_cancel_report__c();
         LostBrands = new list<LostBrand> {new LostBrand(0)};
         approvalHistorys = new list<ProcessInstanceHistory>();
      }
      public LostReport(Lost_cancel_report__c lostReport){
         this.lostReport = lostReport;
         LostBrands = new list<LostBrand> {new LostBrand(0)};
         approvalHistorys = new list<ProcessInstanceHistory>();
      }
      public LostReport(Lost_cancel_report__c lostReport,  list<LostBrand> LostBrands){
         this.lostReport = lostReport;
         this.LostBrands = LostBrands;
         approvalHistorys = new list<ProcessInstanceHistory>();
      }
   }
   public class LostBrand {
        @AuraEnabled
      @AuraEnabled
      public PCLLostBrand__c lostBrand;
        @AuraEnabled
      @AuraEnabled
      public Integer lineNo;
        @AuraEnabled
      @AuraEnabled
      public list<PCLLostProducts> LostProducts;
        @AuraEnabled
      @AuraEnabled
      public Integer ProductSize;
      public LostBrand( integer lineNo ){
         lostBrand = new PCLLostBrand__c();
@@ -619,13 +538,16 @@
   }
   // add tcm 20211119 start
   public class PCLLostProducts {
        @AuraEnabled
      @AuraEnabled
      public Integer lineNo2;
        @AuraEnabled
      @AuraEnabled
      public PCLLostProduct__c LostProductss;
        @AuraEnabled
      @AuraEnabled
      public Boolean bool;
      @AuraEnabled
      public String productName;
      @AuraEnabled
      public List<Map<String, String>> productOptions = new List<Map<String, String>>();
      public PCLLostProducts() {
         this.lineNo2 = 0;
         this.LostProductss=new PCLLostProduct__c();
@@ -647,7 +569,6 @@
   // add tcm 20211119 end
   // add tcm 20211118 start
    @AuraEnabled
   public static void search() {
      if (LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.LostProduct__c==null) {
         LostReport.LostBrands[topNum].LostProducts[secondNum].LostProductss.ProductClass__c=null;
@@ -668,5 +589,17 @@
         }
      }
   }
   @AuraEnabled
   public static string searchProduct(String lostProduct){
      Product2 prd = null;
      try {
         prd = [select Id,ProductClass__c, ProductCategory__c from Product2 where Id =:lostProduct];
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
      return JSON.serialize(prd);
   }
   // add tcm 20211118 end
}