19626
2023-06-29 45e4876c811c861adc5744d06b5bba840fae397a
force-app/main/default/classes/lexPCLLostReportLwcController.cls
@@ -31,29 +31,6 @@
   public static integer secondNum {get; set;}
   // add tcm 20211122 end
   @AuraEnabled
   public static LostReport getLostReport(){
      try {
         return LostReport;
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
   @AuraEnabled( cacheable = true )
    public static List< Account > getAccounts() {
        return [ SELECT Id, Name, Industry FROM Account LIMIT 10 ];
    }
    @AuraEnabled( cacheable = true )
    public static void saveAccounts(List<Account> accList){
        Insert accList;
        /*if(accList.size()>0 && accList != null){
            insert accList;
        }*/
    }
   @AuraEnabled
    public static String getPickList(String objectName, String fieldName) {
@@ -103,27 +80,6 @@
      pickList.put('其他', qita);
        return JSON.serialize(pickList);
    }
   @AuraEnabled
   public static String getPickListByFilter(String objectName,String fieldName,String controlFieldName,String controlFieldValue){
      try {
         Schema.DescribeFieldResult fieldDescribe = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(fieldName).getDescribe();
         // 如果该字段为选项列表类型,则获取选项列表
         List<Schema.PicklistEntry> picklistValues = fieldDescribe.getPicklistValues();
         // 根据控制字段的值筛选出对应的选项
         List<Map<String, Object>> lstPickvals = new List<Map<String, Object>>();
         for (Schema.PicklistEntry entry : picklistValues) {
            if (entry.isActive() && entry.getValue().startsWith(controlFieldValue)) {
               lstPickvals.add(new Map<String, Object>{'label' => entry.getValue(), 'value' => entry.getValue()});
            }
         }
         String jsonStr = JSON.serialize(lstPickvals);
         return jsonStr;
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
   @AuraEnabled
   public static Map<string,object> init (string oppId1,string lostReportId1,string pageStatus1,string lostType1,string submitFlag1){
      try {
@@ -288,176 +244,6 @@
      maps.put('LostReport', LostReport);
      return maps;
   }
   //写到js
   // 编辑 只有系统管理员或者草案中可以编辑
   @AuraEnabled
   public static String edit2(){
      if( userinfo.getProfileId() == (ID) '00e10000000Y3o5AAC' ||
          '草案'.equals(LostReport.LostReport.Report_Status__c)
          ) {
         pageStatus = 'Edit';
         init1();
      }
      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(LostReport report){
   //    try {
   //       if(!dataEntry(report)) {
   //          return null;
   //       }
   //       // brandCount = LostReport.LostBrands.size();
   //       pageStatus = 'View';
   //       return '保存成功!';
   //    } catch (Exception e) {
   //       return e.getMessage();
   //    }
   // }
   //读取并构建竞争对手品牌
   // 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(LostReport report){
      LostReport = report;
      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 String searchBrands(){
      String ObjectApi_name = 'PCLLostBrand__c';
@@ -480,17 +266,6 @@
        }
      String jsonStr = JSON.serialize(lstPickvals);
        return jsonStr;
   }
   public static list<LostBrand> brandcopy(LostReport report){
      list<LostBrand> tempbrands = new list<LostBrand>();
      for(LostBrand tempbrand: report.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
@@ -659,6 +434,22 @@
         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);
@@ -679,51 +470,17 @@
      return lostBrand;
   }
   // 删除品牌 这个有参数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);
         }
      }
      LostReport.LostBrands =  tempLostBrands;
      brandCount = LostReport.LostBrands.size();
      return null;
   }
   // 添加型号, 这个有参数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 PCLLostProducts getLostProduct(){
   public static PCLLostProducts getNewLostProduct(Integer lineNo2){
      PCLLostProducts pro = null;
      try {
         PCLLostProduct__c plp = new PCLLostProduct__c();
         return new PCLLostProducts(0,plp);
         pro = new PCLLostProducts(lineNo2);
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
         System.debug(e.getMessage());
      }
      return pro;
   }
   // update tcm 20211125 添加型号时自动带出品牌 end
   // 页面的数据结构
   public class LostReport {
@@ -731,17 +488,22 @@
      public Lost_cancel_report__c lostReport { get; set; }
      @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>();
      }
   }