/**
 * *
    ODescription:
    GAuthor: sun xia
    @Date: 2023-07-11 15:31:56
    GIastEditors: sun xia
    @IastEditTime: 2023-07-11 15:31:56
 * */
public with sharing class LexAddSubmitApprovalProcessController {
     @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new InitData();

        try {
            Rental_Apply__c ra = [SELECT Id, Request_shipping_day__c, Add_Approval_Status__c, Repair__c, 
                        Demo_purpose1__c, demo_purpose2__c, Repair__r.Repair_Estimated_date_formula__c,
                        Repair__r.Agreed_Date__c, Repair__r.NewProductGuaranteeObject__c,
                        Repair__r.Repair_Final_Inspection_Date__c, Repair__r.Repair_Shipped_Date__c,
                        Repair__r.Status1__c, Repair__r.ReRepairObject_F__c, Repair__r.Number_of_EffectiveContract__c,
                        Repair__r.Delivered_Product__r.Product2.Asset_Model_No__c, Campaign__c,
                        Campaign__r.Status, Campaign__r.Rental_Apply_Flag__c, QIS_number__c, QIS_number__r.next_action__c,
                        applyUser__r.Id, Owner.Id, Status__c,RecordTypeId from Rental_Apply__c WHERE Id = :recordId];
            
            if(ra==null){
                return null;
            }

            System.debug(LoggingLevel.INFO, '****LexAddSubmitApprovalProcessController****' + ra); 
            res.rentalApplyId = ra.Id;
            res.recordTypeId = ra.RecordTypeId;
            res.addApprovalStatus = ra.Add_Approval_Status__c;
            res.requestShippingDay = ra.Request_shipping_day__c;
            res.repairId = ra.Repair__c;
            res.demoPurpose1 = ra.Demo_purpose1__c;
            res.demoPurpose2 = ra.demo_purpose2__c;
            res.repairEstimatedDateFormula = ra.Repair__r.Repair_Estimated_date_formula__c;
            res.agreedDate = ra.Repair__r.Agreed_Date__c;
            res.newProductGuaranteeObject = ra.Repair__r.NewProductGuaranteeObject__c;
            res.repairFinalInspectionDate = ra.Repair__r.Repair_Final_Inspection_Date__c;
            res.repairShippedDate = ra.Repair__r.Repair_Shipped_Date__c;
            res.status1 = ra.Repair__r.Status1__c;
            res.reRepairObjectF = ra.Repair__r.ReRepairObject_F__c;
            res.numberOfEffectiveContract = ra.Repair__r.Number_of_EffectiveContract__c;
            res.assetModelNo = ra.Repair__r.Delivered_Product__r.Product2.Asset_Model_No__c;
            res.campaignId = ra.Campaign__c;
            res.campaignStatus = ra.Campaign__r.Status;
            res.rentalApplyFlag = Integer.valueOf(ra.Campaign__r.Rental_Apply_Flag__c);
            res.qISNumber = ra.QIS_number__c;
            res.nextAction = ra.QIS_number__r.next_action__c;
            res.applyUserId = ra.applyUser__r.Id;
            res.ownerId = ra.OwnerId;
            res.rentalApplyStatus = ra.Status__c;
            res.demoRequestPastDataId = Schema.SObjectType.Rental_Apply__c.getRecordTypeInfosByDeveloperName().get('Demo_request_past_data').getRecordTypeId();
            res.profileId = UserInfo.getProfileId();

            List<Rental_Apply_Equipment_Set_Detail__c> raeSet = [SELECT Id from Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply__c = :recordId AND Draft_Appended__c=1];
            res.detailSize = raeSet.size();
        }
        catch (Exception e) {
            System.debug(LoggingLevel.INFO, '****e:' + e); 
        }   
        return res;
    }


    //提交审批
    @AuraEnabled
    public static String submitApproval(String recordId){
        Savepoint sp = Database.setSavepoint();
        try {
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(recordId);
            Approval.ProcessResult submitResult = Approval.process(req1);
            return 'OK';
        }
        catch (Exception e) {
            Database.rollback(sp);
            return e.getMessage();
        }
    }


    //更新备品借出申请的提交追加待审批状态
    @AuraEnabled
    public static String changeAddApprovalStatus(String recordId, String status){
        Savepoint sp = Database.setSavepoint();
        try {
            Rental_Apply__c ra = new Rental_Apply__c();
            ra.Id = recordId;
            ra.Add_Approval_Status__c = status;
            Update ra;
            return '更新成功';
        }
        catch (Exception e) {
            Database.rollback(sp);
            return e.getMessage();
        }
        
    }


    public  class InitData{
        @AuraEnabled
        public String rentalApplyId;        //备品借出申请Id
        @AuraEnabled
        public String recordTypeId;     
        @AuraEnabled
        public String addApprovalStatus;    //备品借出申请.追加备品审批状态
        @AuraEnabled
        public Integer detailSize;          //明细Size
        @AuraEnabled
        public Date requestShippingDay;     //备品借出申请.希望到货日
        @AuraEnabled
        public String repairId;             //修理Id
        @AuraEnabled
        public String demoPurpose1;         //备品借出申请.使用目的1
        @AuraEnabled
        public String demoPurpose2;         //备品借出申请.使用目的2
        @AuraEnabled
        public Date repairEstimatedDateFormula; //修理.6.报价日
        @AuraEnabled
        public Date agreedDate;             //修理.7.用户同意日
        @AuraEnabled
        public String newProductGuaranteeObject;    //修理.无偿区别标志
        @AuraEnabled
        public Date repairFinalInspectionDate;      //修理.10.最终检测日
        @AuraEnabled
        public Date repairShippedDate;          //修理.11.RC修理品返送日
        @AuraEnabled
        public String status1;                  //修理.状态1
        @AuraEnabled
        public Boolean reRepairObjectF;         //修理.再受理对象品参考
        @AuraEnabled
        public String numberOfEffectiveContract;    //修理.有无维修合同对象(SFDC)
        @AuraEnabled
        public String assetModelNo;                 //修理.资产.产品.产品型号（MDM）
        @AuraEnabled
        public String campaignId;                   //备品借出申请.学会
        @AuraEnabled
        public String campaignStatus;               //学会.状态
        @AuraEnabled
        public Integer rentalApplyFlag;              //学会.提交申请FLG
        @AuraEnabled
        public String qISNumber;                    //备品借出申请.QIS
        @AuraEnabled
        public String nextAction;                   //QIS.对应方法
        @AuraEnabled
        public String applyUserId;                  //备品借出申请.操作者Id
        @AuraEnabled
        public String ownerId;                      //备品借出申请.所有人Id
        @AuraEnabled
        public String rentalApplyStatus;            //备品借出申请.状态
        @AuraEnabled
        public String demoRequestPastDataId;        //备品申请 过去历史转移Id
        @AuraEnabled
        public String profileId;                    //简档Id
    }
}