19626
2023-10-31 fd9df05c48c09b4d7f39a3b401882bda226dedea
force-app/main/default/classes/OpportunityLightingButtonController.cls
@@ -4,9 +4,37 @@
 * @Author: chen jing wu
 * @Date: 2023-04-12 11:16:07
 * @LastEditors: chen jing wu
 * @LastEditTime: 2023-06-29 16:53:17
 * @LastEditTime: 2023-10-26 20:30:06
 */
public with sharing class OpportunityLightingButtonController {
    @AuraEnabled
    public static Map<String,Object> queryForEditLostButton(String recordId){
        Map<String,Object> objs = new Map<String,Object>();
        Report1 rep = new Report1();
        try {
            List<Lost_cancel_report__c> re = [
                select id,
                Report_Status__c,
                RecordTypeId
                from Lost_cancel_report__c where RecordTypeId != '01210000000R4hHAAS' and Opportunity__c =: recordId];
            rep.lostReport = re;
            objs.put('report',rep);
            Schema.DescribeSObjectResult s = Lost_cancel_report__c.sObjectType.getDescribe();
         if( !s.isCreateable()) {
            objs.put('error',true);
         }else{
                objs.put('error',false);
            }
        } catch (Exception e) {
            objs.put('error',e.getMessage());
            return objs;
        }
        return objs;
    }
    public class Report1{
        @AuraEnabled
        public List<Lost_cancel_report__c> lostReport;
    }
    @AuraEnabled
    public static InitData initForPredictedDateChangeButton(String recordId){
        InitData res = new InitData();
@@ -19,7 +47,7 @@
            res.createdDay = opportunity.Created_Day__c;
            res.directLossFLG = opportunity.DirectLossFLG__c;
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -45,9 +73,21 @@
                IF_Submit__c ,
                Trade__c,
                Is_Corrosion__c,
                If_Authorizing_Lock__c
                If_Authorizing_Lock__c,
                AccDealerBlacklist__c,
                Agency1__c,
                Agency2__c,
                Hospital__c,
                OwnerId,
                Sales_assistant_name_text__c
                from Opportunity where Id =: recordId 
            ];
            res.AccDealerBlacklist = opportunity.AccDealerBlacklist__c;
            res.angency1 = opportunity.Agency1__c;
            res.angency2 = opportunity.Agency2__c;
            res.accname = opportunity.Hospital__c;
            res.ownerids = opportunity.OwnerId;
            res.ddid = opportunity.Sales_assistant_name_text__c;
            res.agency1Id = opportunity.Agency1_ID_18__c;
            res.stageName = opportunity.StageName;
            res.sapSendOK = opportunity.SAP_Send_OK__c;
@@ -72,7 +112,7 @@
            res.s62ProfileId = getProfileIdByName(lexLightingButtonConstant.S62_PROFILE_NAME);
            res.m4ProfileId = getProfileIdByName(lexLightingButtonConstant.M4_PROFILE_NAME);
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -87,7 +127,7 @@
            ];
            res.ifOpenBid = opportunity.ifOpenBid__c;
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -102,7 +142,7 @@
            ];
            res.ifOpenBid = opportunity.ifOpenBid__c;
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -116,7 +156,7 @@
                from Opportunity where Id =: recordId];
            res.accountId = opportunity.AccountId;
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -132,7 +172,7 @@
            res.accountId = opportunity.AccountId;
            res.userId = UserInfo.getUserId();
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -148,7 +188,7 @@
            res.accountId = opportunity.AccountId;
            res.userId = UserInfo.getUserId();
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -186,7 +226,7 @@
            res.s4ProfileId = getProfileIdByName(lexLightingButtonConstant.S4_PROFILE_NAME);
            res.recordTypeId = Schema.SObjectType.Request_tedner_doc__c.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_AUTHORIZATION_REQUEST).getRecordTypeId();
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return res;
    }
@@ -226,7 +266,7 @@
        res.salesOwnerBuchangID = opportunity.Sales_owner_buchangID__c;
        res.recordTypeId = Schema.SObjectType.Lost_cancel_report__c.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_OPPO_CANCEL_REPORT).getRecordTypeId();
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -238,6 +278,7 @@
                select
                StageName,
                SAP_Send_OK__c,
                Cnt_Lost_cancel_Draft__c,
                Cnt_Lost_cancel_report__c,
                Name,
                Sales_assistant_name__c,
@@ -258,11 +299,48 @@
            res.salesManagerDepartmentID = opportunity.Sales_manager_departmentID__c;
            res.salesOwnerBuchang = opportunity.Sales_owner_buchang__c;
            res.salesOwnerBuchangID = opportunity.Sales_owner_buchangID__c;
            res.cntLostCancelDraft = opportunity.Cnt_Lost_cancel_Draft__c;
            Schema.DescribeSObjectResult s = Lost_cancel_report__c.sObjectType.getDescribe();
         if( !s.isCreateable()) {
            res.isHavePower = false;
         }else{
                res.isHavePower = true;
            }
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
    @AuraEnabled
    public static InitData initForLexQuoteEntryNewbotton(String recordId){
        InitData res = new InitData();
        try {
            Opportunity opportunity = [
                select
                Agency_Is_Delete__c,
                Account_Is_Active__c,
                Hospital_Is_Active__c,
                RecordTypeId,
                Estimation_Decision__c
                from Opportunity where Id =: recordId
            ];
            res.agencyIsDeletec = opportunity.Agency_Is_Delete__c;
            res.accountIsActivec = opportunity.Account_Is_Active__c;
            res.hospitalIsActivec = opportunity.Hospital_Is_Active__c;
            res.recordTypeId = opportunity.RecordTypeId;
            res.estimationDecisionc = opportunity.Estimation_Decision__c;
        } catch (Exception e) {
        }
        return res;
    }
    @AuraEnabled
    public static Boolean describeSObjectCommon (String objname)
    {
        Schema.DescribeSobjectResult schemaMap = Schema.describeSObjects(new String[]{objname})[0];
        return schemaMap.isCreateable();
    }
    @AuraEnabled
    public static InitData initForQuotationRequestButton(String recordId){
        InitData res = new InitData();
@@ -276,7 +354,7 @@
            res.biddingProjectNameBidId = opportunity.Bidding_Project_Name_Bid__r.Id;
            res.estimationId = opportunity.Estimation_Id__c;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -293,7 +371,7 @@
            res.stockApplyStatus = opportunity.Stock_apply_status__c;
            res.lastOpportunityFileId = opportunity.Last_opportunity_file__r.Id;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -312,7 +390,7 @@
            res.reportForCNYId = cnyReport.Id;
            res.reportForUSDId = usdReport.Id;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -331,7 +409,7 @@
            res.sapSendOK = oppo.SAP_Send_OK__c;
            res.isAuthorized = oppo.IsAuthorized__c;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -369,7 +447,7 @@
            res.opportunityCategory = opp.Opportunity_Category__c;
            res.agency1Name = opp.Agency1__r.Name;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -384,7 +462,8 @@
                SI_Decide_ID__c,
                Name,
                Opportunity_No__c,
                IsAuthorized__c
                IsAuthorized__c,
                recordTypeId
                from Opportunity where Id =: recordId
            ];
            res.authorizedDBNo = oppo.Authorized_DB_No__c;
@@ -393,8 +472,9 @@
            res.name = oppo.Name;
            res.opportunityNo = oppo.Opportunity_No__c;
            res.isAuthorized = oppo.IsAuthorized__c;
            res.recordTypeId = oppo.recordTypeId;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -409,7 +489,7 @@
            ];
            res.accountId = opp.AccountId;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -431,9 +511,9 @@
            res.accountId = opp.AccountId;
            res.accountName = acc.Name;
            res.name = opp.Name;
            res.recordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_INQUIRY_FOLLOW_UP).getRecordTypeId();
            // res.recordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_INQUIRY_FOLLOW_UP).getRecordTypeId();
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -454,7 +534,7 @@
            res.sapSendOK = opp.SAP_Send_OK__c;
            res.ifAuthorizingLock = opp.If_Authorizing_Lock__c;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -471,7 +551,7 @@
            res.oppForecastStatus = opp.Forecast_this_month__c;
            res.recordTypeId = Schema.SObjectType.OpportunitySpecialApply__c.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_SPECIL_APPLY_CREATE).getRecordTypeId();
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -490,7 +570,7 @@
            res.oppForecastStatus = ach.Monthly_forecast_shipping__c;
            res.recordTypeId = Schema.SObjectType.OpportunitySpecialApply__c.getRecordTypeInfosByName().get(lexLightingButtonConstant.RECORD_TYPE_NAME_BY_SPECIL_APPLY_CREATE_ACH).getRecordTypeId();
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -505,7 +585,7 @@
            ];
            res.stockApplyStatus = opp.Stock_apply_status__c;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        return res;
    }
@@ -520,10 +600,7 @@
            update opp;
            return null;
        } catch (Exception e) {
            String eMessage = e.getMessage();
            Integer left  = eMessage.indexOf(',') + 1;
            Integer right = eMessage.length();
            return eMessage.substring(left,right);
            return errorMessage(e);
        }
    }
    @AuraEnabled
@@ -535,10 +612,7 @@
            update opp;
            return null;
        } catch (Exception e) {
            String eMessage = e.getMessage();
            Integer left = eMessage.indexOf(',') + 1;
            Integer right = eMessage.length();
            return eMessage.substring(left,right);
            return errorMessage(e);
        }
    }
    @AuraEnabled
@@ -552,10 +626,7 @@
            update opp;
            return null;
        } catch (Exception e) {
            String eMessage = e.getMessage();
            Integer left = eMessage.indexOf(',') + 1;
            Integer right = eMessage.length();
            return eMessage.substring(left,right);
            return errorMessage(e);
        }
    }
    @AuraEnabled
@@ -571,10 +642,7 @@
            update opp;
            return null;
        } catch (Exception e) {
            String eMessage = e.getMessage();
            Integer left = eMessage.indexOf(',') + 1;
            Integer right = eMessage.length();
            return eMessage.substring(left,right);
            return errorMessage(e);
        }
    }
    @AuraEnabled
@@ -586,7 +654,7 @@
                id 
                from InquiryPredictsDateChange__c where Predicted_Date_Status__c = :LightingButtonConstant.PREDICTED_DATE_STATUS and Opportunity__c =:recordId];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return rac;
    }
@@ -600,7 +668,7 @@
                taskStatus__c
                from task__c where OpportunityId__c =: recordId and name like :obscureName];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return tasks;
    }
@@ -614,7 +682,7 @@
                taskStatus__c
                from task__c where OpportunityId__c =: recordId and name like :obscureName];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return tasks;
    }
@@ -629,7 +697,7 @@
                where (Tender_information__r.subInfoType__c =:lexLightingButtonConstant.SUB_INFO_TYPE_SCRAPPED_LABEL OR Tender_information__r.subInfoType__c =:lexLightingButtonConstant.SUB_INFO_TYPE_FLOW_LABEL OR Tender_information__r.TerminateApprovalStatus__c= :lexLightingButtonConstant.SUB_INFO_TYPE_APPROVE) and Opportunity__c =: recordId
            ];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return links;
    }
@@ -644,7 +712,7 @@
                from QuoteIrai__c where Note__c like :opportunityId and QuoteIrai_Status__c =:lexLightingButtonConstant.QUOTEIRAI_STATUS_DRAFT
            ];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return quotes;
    }
@@ -660,7 +728,7 @@
                FROM Agency_Hospital_Link__c WHERE Agency__c =: agency1Id AND Hospital__c =: hospitalId
            ];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return links;
    }
@@ -675,7 +743,7 @@
                Name FROM RecordType WHERE SobjectType =: sobjectType And developerName=: developerName And IsActive =: isActive 
            ];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return types;
    }
@@ -691,7 +759,7 @@
                DurableId From FieldDefinition WHERE EntityDefinition.QualifiedApiName =: qualifiedApiName
            ];
        } catch (Exception e) {
            System.debug('e:****' + e);
        }
        return fields;
    }
@@ -704,10 +772,7 @@
            processResults.add(Approval.process(request));
            return processResults[0].errors;
        } catch (Exception e) {
            String eMessage = e.getMessage();
            Integer left = eMessage.indexOf(',') + 1;
            Integer right = eMessage.length();
            return eMessage.substring(left,right);
            return errorMessage(e);
        }
    }
    @AuraEnabled
@@ -721,7 +786,18 @@
            ];
            return user.Id;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
            if (e.getMessage().contains(',')) {
                System.debug(LoggingLevel.INFO, '*** e: ' + e);
                String exc = '' + e.getMessage();
                Integer left = exc.indexOf(':') + 1;
                Integer right = exc.lastIndexOf(':');
                String str = exc.substring(left,right);
                left = str.indexOf(',') +  1;
                String newStr = str.substring(left);
                return newStr;
            }else {
                return e.getMessage();
            }
        }
    }
    @AuraEnabled
@@ -735,8 +811,128 @@
            ];
            return pro.Id;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
            if (e.getMessage().contains(',')) {
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            String exc = '' + e.getMessage();
            Integer left = exc.indexOf(':') + 1;
            Integer right = exc.lastIndexOf(':');
            String str = exc.substring(left,right);
            left = str.indexOf(',') +  1;
            String newStr = str.substring(left);
            return newStr;
         }else {
            return e.getMessage();
         }
        }
    }
    @AuraEnabled
    public static string queryForProfileId(){
        return UserInfo.getProfileId();
    }
    @AuraEnabled
    public static Boolean queryQuote(String estimationId){
        Boolean bo;
        try {
            Quote qu = [select Have_Virtual__c from Quote where Id =: estimationId];
            bo = qu.Have_Virtual__c;
        } catch (Exception e) {
        }
        return bo;
    }
    @AuraEnabled
    public static String initCopy(String recordId){
        String s='';
        try {
            String objectName = 'Opportunity'; // 要获取字段的对象名
            Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
            Schema.SObjectType objType = globalDescribe.get(objectName);
            if (objType != null) {
                Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
                s+='SELECT ';
                // 现在,fieldMap中包含了对象的所有字段信息
                for (String fieldName : fieldMap.keySet()) {
                    if(!fieldName.equals('id')
                        &&!fieldName.equals('Id') &&!fieldName.equals('Opportunity_No__c'))
                        s+=fieldName+',';
                }
                s=s.removeEnd(',');
                s+=' FROM Opportunity where id=\''+recordId+'\'';
                system.debug('SQL:'+s);
                List<Opportunity> opportunitys = Database.query(s);
                s='';
                if(opportunitys.size()>0){
                    system.debug('in!');
                    for (String fieldName : fieldMap.keySet()) {
                        String formaF=fieldMap.get(fieldName).getDescribe().getName();
                        if(opportunitys.get(0).get(fieldName)!=null&&!opportunitys.get(0).get(fieldName).equals('null')){
                            if(formaF.equals('Id')
                                ||formaF.equals('OwnerId')
                                ||formaF.equals('CreatedDate')
                                ||formaF.equals('CreatedById')
                                )
                            {
                                continue;
                            }
                            Object val=opportunitys.get(0).get(fieldName);
                            if(val instanceof Date ){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else if (val instanceof DateTime){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else{
                                s+=formaF+'='+opportunitys.get(0).get(fieldName)+',';
                            }
                        }
                    }
                    s=s.removeEnd(',');
                    return s;
                }
            }
         return s;
        } catch (Exception e) {
        }
        return s;
    }
    public static String forma(String str){
        String res='';
        String stra='a';
        Integer a=stra.charAt(0);
        String strz='z';
        Integer z=strz.charAt(0);
        String strAa='A';
        Integer bA=strAa.charAt(0);
        String strZz='Z';
        Integer bZ=strZz.charAt(0);
        String strx='_';
        Integer x=strx.charAt(0);
        List<Integer> charArr = new List<Integer>();
        Integer change=bA-a;
        Integer st=(str.charAt(0)+change);
        charArr.add(st);
        for(Integer i=0;i<str.length()-2;i++){
            Integer c=str.charAt(i);
            Integer nextC=str.charAt(i+1);
            if(c==x&&nextC>=a&&nextC<z&&i!=str.length()-2){
                nextC+=change;
            }
            charArr.add(nextC);
        }
        res=String.fromCharArray(charArr);
        res+=str.substring(str.length()-1,str.length());
        return res;
    }
    @AuraEnabled
    public static String changeTrade(String oppId){
@@ -771,9 +967,44 @@
            return '1';     
        } catch (Exception ex) {
            Database.rollback(sp);
            return  ex.getMessage() + ' | Line:' + ex.getLineNumber();
            return errorMessage(ex);
        }
    }
    @AuraEnabled
    public static Boolean queryLostReport (String recordId){
        RecordType recordType1 = [select Id from RecordType where Name = '询价取消报告'];
        List<Lost_cancel_report__c> repList = [select Id from Lost_cancel_report__c where Opportunity__c =: recordId and RecordTypeId !=: recordType1.Id];
        if(repList.size() > 0){
            return true;
        }
        return false;
    }
    @AuraEnabled
    public static Boolean queryCancelReport (String recordId){
        RecordType recordType1 = [select Id from RecordType where Name = '询价取消报告'];
        List<Lost_cancel_report__c> repList = [select Id from Lost_cancel_report__c where Opportunity__c =: recordId and RecordTypeId =: recordType1.Id];
        if(repList.size() > 0){
            return true;
        }
        return false;
    }
    public static string errorMessage(Exception e){
        if (e.getMessage().contains(',')) {
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
            String exc = '' + e.getMessage();
            Integer left = exc.indexOf(':') + 1;
            Integer right = exc.lastIndexOf(':');
            String str = exc.substring(left,right);
            left = str.indexOf(',') +  1;
            String newStr = str.substring(left);
            return newStr;
        }else {
            return e.getMessage();
        }
    }
    public class InitData{
        @AuraEnabled
        public Boolean directLossFLG;
@@ -901,5 +1132,31 @@
        public String oppForecastStatus;
        @AuraEnabled
        public String oppId;
        @AuraEnabled
        public String AccDealerBlacklist;
        @AuraEnabled
        public String angency1;
        @AuraEnabled
        public String angency2;
        @AuraEnabled
        public String accname;
        @AuraEnabled
        public String ownerids;
        @AuraEnabled
        public String ddid;
        @AuraEnabled
        public String agencyIsDeletec;
        @AuraEnabled
        public String accountIsActivec;
        @AuraEnabled
        public String hospitalIsActivec;
        @AuraEnabled
        public Boolean estimationDecisionc;
        @AuraEnabled
        public String reportStatus;
        @AuraEnabled
        public String reportId;
        @AuraEnabled
        public Boolean isHavePower;
    }
}