liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**************************************************************************************************
@Author: Denny陈帮才
@Name: SummaryContractFMBatch
@CreateDate: 2023/01/15
@Description: 市场多年保续签至合同的价格开发
@Version 1.0
*****************************************************************************************************/
global class SummaryContractFMBatch implements Database.Batchable<sObject>,Database.Stateful {
    public String query;
    public List < String > contactLists;
    private BatchIF_Log__c iflog;
    global SummaryContractFMBatch() {
        this.query = query;
    }
    global SummaryContractFMBatch(List < String > contactLists) {
        this.contactLists = contactLists;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
 
        system.debug('执行start');
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Log__c  = 'SummaryContractFMBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
 
        // query ='select Id, Hospital__c from Maintenance_Contract__c where ((Contract_End_Date__c>=:sTime and Contract_End_Date__c<=:eTime) or (Contract_Start_Date__c >=:sTime and Contract_Start_Date__c <=:eTime) or(Contract_Start_Date__c <:sTime and Contract_End_Date__c>:eTime)) and RecordType.Name!=\'多年保修合同\' ';
        // query ='select Id,FM__c from Maintenance_Contract__c where RecordType.Name = \'多年保修合同\'';
        query ='SELECT Asset__c,Asset__r.Guarantee_period_for_products__c,Asset__r.Posting_Date__c,Asset__r.InstallDate FROM Maintenance_Contract_Asset__c  where Maintenance_Contract__r.RecordType_DeveloperName__c =\'VM_Contract\' and Asset__r.RecordType.Name!=\'备品\' and (Maintenance_Contract__r.Status__c=\'契約\' or Maintenance_Contract__r.Status__c=\'契約満了\')';
        if (contactLists != null && contactLists.size() > 0) {
            query += ' AND Id IN :contactLists ';
        }
        System.debug(LoggingLevel.INFO, '*** query: ' + query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> scope) {
        Date today  = Date.today();
 
        List<String> assetIds = new List<String>();
        for (Maintenance_Contract_Asset__c mc : scope) {
            assetIds.add(mc.Asset__c);
        }
 
        // List<Maintenance_Contract_Asset__c> updateContractAssets = [SELECT Asset__r.Intra_Trade_Gurantee_RMB__c,Maintenance_Contract__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c = :mcIds]
        //汇总分子:多年保修期间的所有【修理原价】累计的金额,请注意多年保修期间内,需要计算经历月数内的如上修理金额;如果是多年保修期已经结束,需要计算3年多年保修期间的如上修理金额
        Map<String,Decimal>mcMoney = new Map<String,Decimal>();
        for (Repair__c  rc :[SELECT Id,Repair_Quotation_Id__r.ListPrice__c,Repair_Quotation_Id__r.New_QIS_free__c,Repair_Quotation_Id__r.Contract_free__c,
                            
                            Delivered_Product__c,ListPrice__c,Delivered_Product__r.Guarantee_period_for_products__c,Delivered_Product__r.InstallDate__c,Failure_Occurrence_Date__c
                            // (SELECT ListPrice__c FROM RepairRelation__r WHERE ListPrice__c >0) 
                            FROM Repair__c
                            WHERE Delivered_Product__c IN:assetIds 
                            AND ListPrice__c > 0
                            AND Status1__c!='0.删除' AND Status1__c!='0.取消' 
                            AND Status2__c!='00.删除' AND Status2__c!='00.取消']){
                             // AND Failure_Occurrence_Date__c < Delivered_Product__r.Guarantee_period_for_products__c
                             // AND((Failure_Occurrence_Date__c > Delivered_Product__r.Posting_Date__c) OR (Failure_Occurrence_Date__c > Delivered_Product__r.InstallDate))] ) {
            // if((rc.Failure_Occurrence_Date__c < rc.Delivered_Product__r.Guarantee_period_for_products__c) && ((rc.Failure_Occurrence_Date__c > rc.Delivered_Product__r.Posting_Date__c) || (rc.Failure_Occurrence_Date__c > rc.Delivered_Product__r.InstallDate))
            //保修期限内
            // if(today< rc.Delivered_Product__r.Guarantee_period_for_products__c){
            //     Date today=td.toStartOfMonth(); //如 2022.10.1
 
 
            // }else{
            //汇总 故障发生日处于保有设备 保修开始日 及 保修期限内 的所有修理原价
            if((rc.Failure_Occurrence_Date__c <= rc.Delivered_Product__r.Guarantee_period_for_products__c) && (rc.Failure_Occurrence_Date__c >= rc.Delivered_Product__r.InstallDate__c)){
                // Date purDate = rc.Delivered_Product__r.Guarantee_period_for_products__c;
                Decimal  rcPrice = 0;
                // if(today < purDate){
                //     Decimal bDay = purDate.daysBetween(today);  
                //     rcPrice = mcMoney.get(mca.Asset__c) * (bDay /30) /12;
                // }
                
                if(rc.Repair_Quotation_Id__r.ListPrice__c != null){
                    rcPrice += rc.Repair_Quotation_Id__r.ListPrice__c;
                }
                if(rc.Repair_Quotation_Id__r.New_QIS_free__c != null){
                    rcPrice += rc.Repair_Quotation_Id__r.New_QIS_free__c;
                }
                if(rc.Repair_Quotation_Id__r.Contract_free__c != null){
                    rcPrice += rc.Repair_Quotation_Id__r.Contract_free__c;
                }
 
                if(!mcMoney.containsKey(rc.Delivered_Product__c)){
                    mcMoney.put(rc.Delivered_Product__c,rcPrice);
                }else{
                    mcMoney.put(rc.Delivered_Product__c,mcMoney.get(rc.Delivered_Product__c) + rcPrice);
                }
            }
            
        }
 
        for (Maintenance_Contract_Asset__c mca : scope) {
            if(mcMoney.containsKey(mca.Asset__c)){
                // Date purDate = mca.Asset__r.Guarantee_period_for_products__c;
                // if(today > purDate){
                //     Decimal bDay = purDate.daysBetween(today);  
                //     mca.Summary_FZ__c = mcMoney.get(mca.Asset__c) * (bDay /30) /12;
                // }else{
                //     mca.Summary_FZ__c = mcMoney.get(mca.Asset__c);
                // }
                mca.Summary_FZ__c = mcMoney.get(mca.Asset__c);
            }
            
        }
 
        StaticParameter.MaintenanceContractAssetTrigger=false;
        StaticParameter.SetContractEstimatePriceTrigger=false;
 
        update scope;
 
    }
 
    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\nEquipmentRepairBatch end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;
        
        StaticParameter.MaintenanceContractAssetTrigger=true;
        StaticParameter.SetContractEstimatePriceTrigger=true;
 
    }
}