111
沙世明
2022-11-22 928399eceec50e3d37ea08669a12789a9410a9d2
force-app/main/default/classes/SumAnnualRepairAmountBatch.cls
@@ -35,13 +35,13 @@
        //判断过去一年时间
        if(nowDt.month() >= 4){
            sTime = Date.newInstance(nowDt.year()-1,4,1);
            eTime = Date.newInstance(nowDt.year(),3,31);
            eTime = Date.newInstance(nowDt.year(),4,1);
        }else{
            sTime = Date.newInstance(nowDt.year()-2,4,1);
            eTime = Date.newInstance(nowDt.year()-1,3,31);
            eTime = Date.newInstance(nowDt.year()-1,4,1);
        }
        //修理表查询 修理单的金额
        query = 'select Id,Hospital__c from Repair__c where Agreed_Date__c >=:sTime and Agreed_Date__c <=:eTime and Discount_Price_formula__c !=null and Discount_Price_formula__c!=0 ';
        query = 'select Id,Hospital__c,PurchaseOrInstallationDate__c,Repair_List_Price_formula__c from Repair__c where Agreed_Date__c >=:sTime and Agreed_Date__c <:eTime and Repair_List_Price_formula__c !=null and Repair_List_Price_formula__c!=0 and Status1__c!=\'0.删除\' and Status1__c!=\'0.取消\' and Status2__c!=\'00.删除\' and Status2__c!=\'00.取消\'  ';
        if (accountIdList != null && accountIdList.size() > 0) {
            query += ' AND Hospital__c IN :accountIdList ';
@@ -58,33 +58,55 @@
        }
        System.debug(LoggingLevel.INFO, '*** the OCSM_Period_half__c: ' + OCSM_Period_half);
        List<Id>scopeId = new List<Id>();
        // List<Id>scopeId = new List<Id>();
        List<Id>hosId = new List<Id>();
        //汇总 修理原价 安装日期小于12个月的 算平均月 再*12
        Map<Id,Decimal> HosMoneys = new Map<Id,Decimal>();
        for (Repair__c mcc : scope) {
            scopeId.add(mcc.Id);
            // scopeId.add(mcc.Id);
            hosId.add(mcc.Hospital__c);
            Date purDate = mcc.PurchaseOrInstallationDate__c;
            if(purDate!=null){
                Decimal bMonth = purDate.monthsBetween(eTime);
                Decimal hmoney = 0;
                if(bMonth < 0){
                    bMonth *=-1;
                }
                //设备 安装日期至 Etime 不足三年的 按平均月计算再乘12
                if((bMonth < 12 * 3 ) && bMonth > 0){
                hmoney = (mcc.Repair_List_Price_formula__c / bMonth) *12;
                }else{
                    hmoney = mcc.Repair_List_Price_formula__c;
                }
                if(HosMoneys.containsKey(mcc.Hospital__c)){
                    HosMoneys.put(mcc.Hospital__c, HosMoneys.get(mcc.Hospital__c)+hmoney);
                }else{
                HosMoneys.put(mcc.Hospital__c, hmoney);
                }
            }
        }
        //汇总修理表中医院 修理金额
        List<AggregateResult> LastyearList = [
            select
            sum(Discount_Price_formula__c) SumPrice,
            Hospital__c
            from
            Repair__c
            where
            Id in:scopeId
            group by Hospital__c
        ];
        //存医院 以及医院年修理金额
        Map<Id,Decimal> LastYearPriceSumMap = new Map<Id,Decimal>();
        for (AggregateResult Rpc : LastyearList) {
            Id idf        = String.valueOf(Rpc.get('Hospital__c'));
            Decimal Defir = (Decimal)Rpc.get('SumPrice');
        // List<AggregateResult> LastyearList = [
        //     select
        //     sum(Repair_List_Price_formula__c) SumPrice,
        //     Hospital__c
        //     from
        //     Repair__c
        //     where
        //     Id in:scopeId
        //     group by Hospital__c
        // ];
        // //存医院 以及医院年修理金额
        // Map<Id,Decimal> LastYearPriceSumMap = new Map<Id,Decimal>();
        // for (AggregateResult Rpc : LastyearList) {
        //     Id idf        = String.valueOf(Rpc.get('Hospital__c'));
        //     Decimal Defir = (Decimal)Rpc.get('SumPrice');
            LastYearPriceSumMap.put(idf, Defir);
        }
        //     LastYearPriceSumMap.put(idf, Defir);
        // }
        
        //服务客户目标对象里 医院在scope里以及年份等于查询年份的
        List<Account_Service_Of_Target__c> asotList = [select Id,Account_HP__c 
@@ -106,17 +128,23 @@
            // }else{
            //     hospitalName.put(mc.Hospital__c,mc.Request_quotation_AmountF__c);
            // }
            Account_Service_Of_Target__c ast = new Account_Service_Of_Target__c();
            ast.Account_HP__c = mc.Hospital__c;
            ast.Annual_repair_amount__c = LastYearPriceSumMap.get(mc.Hospital__c);
            ast.OCSM_Period_half__c = OCSM_Period_half;
            ast.OCSM_Period__c = OCSM_Period;
            if(oldMap.containsKey(mc.Hospital__c)){
                ast.Id = oldMap.get(mc.Hospital__c).Id;
            try{
                Account_Service_Of_Target__c ast = new Account_Service_Of_Target__c();
                ast.Account_HP__c = mc.Hospital__c;
                ast.Annual_repair_amount__c = HosMoneys.get(mc.Hospital__c);
                ast.OCSM_Period_half__c = OCSM_Period_half;
                ast.OCSM_Period__c = OCSM_Period;
                if(oldMap.containsKey(mc.Hospital__c)){
                    ast.Id = oldMap.get(mc.Hospital__c).Id;
                }
                if(!hospital.contains(ast)){
                    hospital.add(ast);
                }
            }catch(Exception e){
                System.debug(LoggingLevel.INFO, '*** mc.Id: ' + mc.Id);
                System.debug(LoggingLevel.INFO, '*** mc: ' + mc);
            }
            if(!hospital.contains(ast)){
                hospital.add(ast);
            }
        }
        upsert hospital;