19626
2023-05-24 e6a0a7f9ffb463c5ce78d6770b09eac739f88b15
force-app/main/default/classes/lexPCLLostReportLwcController.cls
@@ -1,46 +1,144 @@
public with sharing class lexPCLLostReportLwcController {
   @AuraEnabled
   public static Product2 pro2 { get; set; }
   @AuraEnabled
   public static string oppId { get; set; }
   @AuraEnabled
   public static string lostReportId { get; set; }
   // 当前页面状态 Create,View and Edit, MarketEdit
   @AuraEnabled
   public static string pageStatus { get; set; }
   // 失单,部分失单
   @AuraEnabled
   public static string lostType {get; set;}
   public  static Set<ID> deleteBrandIDSet = new Set<ID>();
   @AuraEnabled
   public static string submitFlag {get; set;}
   @AuraEnabled
   public static LostReport LostReport {get; set;}
   @AuraEnabled
   public static integer brandNo {get; set;}
   @AuraEnabled
   public static integer RemoveBrandNo {get; set;}
   @AuraEnabled
   public static integer setBrandNo {get; set;}
   @AuraEnabled
   public static integer brandCount {get; set;}
   // add tcm 20211122 start
   @AuraEnabled
   public static integer topNum {get; set;}
   @AuraEnabled
   public static integer secondNum {get; set;}
   // add tcm 20211122 end
   // 初始化
   @AuraEnabled
   public static void initForApex(string oppId1,string lostReportId1,string pageStatus1,string lostType1,string submitFlag1){
      oppId = oppId1;
      lostReportId = lostReportId;
      pageStatus = pageStatus1;
      lostType = lostType1;
      submitFlag = submitFlag1;
   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 Map<string,object> init(){
    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 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 {
         oppId = oppId1;
         lostReportId = lostReportId1;
         pageStatus = pageStatus1;
         lostType = lostType1;
         submitFlag = submitFlag1;
         return init1();
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
   // 初始化
   public static Map<string,object> init1(){
      Map<string,object> maps = new Map<string,object>();
      if(string.isblank(pageStatus)) {
         // 这里需要报错没有传参数
@@ -95,6 +193,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
@@ -183,8 +282,13 @@
         return maps;
      }
      return null;
      // 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;
   }
   //写到js
   // 编辑 只有系统管理员或者草案中可以编辑
   @AuraEnabled
   public static String edit2(){
@@ -192,7 +296,7 @@
          '草案'.equals(LostReport.LostReport.Report_Status__c)
          ) {
         pageStatus = 'Edit';
         init();
         init1();
      }
      else{
         return '只有在草案中才能进行编辑!';
@@ -248,20 +352,20 @@
      }
   }
   // 保存
   @AuraEnabled
   public static String save(){
      if(!DataCheck()) {
         return null;
      }
      if(!dataEntry()) {
         return null;
      }
      brandCount = LostReport.LostBrands.size();
      pageStatus = 'View';
      return '保存成功!';
   }
   // @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>();
@@ -274,7 +378,8 @@
   // }
   // 数据检查
   @AuraEnabled
   public static boolean DataCheck(){
   public static boolean DataCheck(LostReport report){
      LostReport = report;
      boolean dataCheck = true;
      if(string.isBlank(LostReport.LostReport.LostType__c))
      {
@@ -349,6 +454,7 @@
         }
         // 当失单品牌名为其他时,报错字段为失单对手型号(手动) thh 2022-01-17 end
      }
      return dataCheck;
   }
@@ -375,10 +481,9 @@
      String jsonStr = JSON.serialize(lstPickvals);
        return jsonStr;
   }
   @AuraEnabled
   public static list<LostBrand> brandcopy(){
   public static list<LostBrand> brandcopy(LostReport report){
      list<LostBrand> tempbrands = new list<LostBrand>();
      for(LostBrand tempbrand: LostReport.LostBrands) {
      for(LostBrand tempbrand: report.LostBrands) {
         LostBrand LostBrand =
            new LostBrand(tempbrand.lineNo,tempbrand.LostProducts );
         LostBrand.lostBrand = tempbrand.lostBrand.clone();
@@ -389,46 +494,47 @@
   }
   // 数据录入
   @AuraEnabled
   public static boolean dataEntry(){
   public static Map<String,String> dataEntry(String report1){
      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>();
         // 需要删掉的失单品牌
@@ -437,12 +543,12 @@
         map<string,PCLLostProduct__c> upsertLostProductMap = new map<string,PCLLostProduct__c>();
         // 需要删掉的失单品牌
         list<PCLLostProduct__c> deleteLostProductList = new list<PCLLostProduct__c>();
         for(LostBrand tempLostBrand : LostReport.LostBrands ) {
         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;
               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) {
@@ -492,11 +598,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;
            }
@@ -509,38 +615,38 @@
               }
            }
         }
         lostReportId = LostReport.lostReport.id;
         // lostReportId = report.lostReport.id;
         system.debug('save---end:');
      }catch (Exception ex) {
         Database.rollback(sp);
         LostReport.lostReport = templostReport;
         LostReport.LostBrands = tempBrands;
         // 这里需要写一些报错信息
         ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,ex.getMessage()));
         return false;
         messageMap.put('error', ex.getMessage());
         return messageMap;
      }
      return true;
      return messageMap;
   }
   
   // 提交
   @AuraEnabled
   public static Map<string,Object> submit(){
   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(LostReport.LostReport.Report_Status__c) ) {
      if(!'草案'.equals(report.lostReport.Report_Status__c) ) {
         messages.put('error', '只有在草案中才能提交申请!');
         return messages;
      }
      Map<string,Object> objs = new Map<string,Object>();
      Savepoint sp = Database.setSavepoint();
      try{
         LostReport.lostReport.Report_Status__c = '提交';
         update LostReport.lostReport;
         report.lostReport.Id = reportId;
         report.lostReport.Report_Status__c = '提交';
         update report.lostReport;
         Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
         psr.setObjectId(lostReportId);
         psr.setObjectId(reportId);
         Approval.ProcessResult submitResult = Approval.process(psr);
         objs.put('uri', '/apex/PCLLostReportPage');
         objs.put('Id', lostReportId);
         objs.put('Id', reportId);
         objs.put('pageStatus', 'View');
         objs.put('submitFlag', '1');
         return objs;
@@ -550,13 +656,19 @@
         return messages;
      }
   }
   // 添加品牌
   // +++++++++++++++++++++++++++++++++++++++++++++++++++++++
   @AuraEnabled
   public static String addBrand(){
      LostReport.LostBrands.add(new LostBrand(LostReport.LostBrands.size()));
      brandCount = LostReport.LostBrands.size();
      return null;
   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;
   }
   // 删除品牌 这个有参数brandNo,才知道是删除那个品牌
   @AuraEnabled
   public static String Remove(){
@@ -593,6 +705,15 @@
      tempLostBrand.ProductSize = tempLostBrand.LostProducts.size();
      return null;
   }
   @AuraEnabled
   public static PCLLostProducts getLostProduct(){
      try {
         PCLLostProduct__c plp = new PCLLostProduct__c();
         return new PCLLostProducts(0,plp);
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
   // update tcm 20211125 添加型号时自动带出品牌 end
   // 页面的数据结构
   public class LostReport {
@@ -616,13 +737,13 @@
   public class LostBrand {
      @AuraEnabled
      public PCLLostBrand__c lostBrand { get; set; }
      public PCLLostBrand__c lostBrand;
      @AuraEnabled
      public Integer lineNo { get; set; }
      public Integer lineNo;
      @AuraEnabled
      public list<PCLLostProducts> LostProducts { get; set; }
      public list<PCLLostProducts> LostProducts;
      @AuraEnabled
      public Integer ProductSize {get; set;}
      public Integer ProductSize;
      public LostBrand( integer lineNo ){
         lostBrand = new PCLLostBrand__c();
         this.lineNo = lineNo;
@@ -646,12 +767,13 @@
   // add tcm 20211119 start
   public class PCLLostProducts {
      @AuraEnabled
      public Integer lineNo2 { get; set; }
      public Integer lineNo2;
      @AuraEnabled
      public PCLLostProduct__c LostProductss { get; set; }
      public PCLLostProduct__c LostProductss;
      @AuraEnabled
      public Boolean bool { get; set; }
      public Boolean bool;
      @AuraEnabled
      public List<Map<String, String>> productOptions = new List<Map<String, String>>();
      public PCLLostProducts() {
         this.lineNo2 = 0;
         this.LostProductss=new PCLLostProduct__c();
@@ -694,4 +816,19 @@
      }
   }
   // add tcm 20211118 end
   @AuraEnabled
   public static List<Product2> searchProduct(){
      try {
         List<Product2> products = [
            select
            Id,name
            from Product2
            where Brand_Name__c = 'STORZ' limit 10
         ];
         return products;
      } catch (Exception e) {
         throw new AuraHandledException(e.getMessage());
      }
   }
}