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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//  预测消费率计算对应型号的过去3年期间有合同的的全国合同修理金额合计
global class ComputeLTYRepairBatch2 implements Database.Batchable<sObject> {
    public String query;
    public String year;
    public Date timeOne ;
    public Date timeTwo;
    public Date timeThr;
    public Date timeFour;
    public List<String> targetList= new List<String>();
    public List<String> asslist= new List<String>();
 
    global ComputeLTYRepairBatch2() {
    }
 
    global ComputeLTYRepairBatch2(List<String> targetList) {
 
        this.query = query;
        this.targetList = targetList;
        this.timeTwo = date.newInstance(
                                        2023,
                                        4,
                                        31
                                        );
        this.timeThr = date.newInstance(
                                        2020,
                                        4,
                                        1
                                        );
        this.timeFour = this.timeTwo.addDays(1);
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
 
        system.debug('targetList=='+targetList);
        //查询符合条件的所有设备
        query = 'select id,product2.Asset_Model_No__c,Category4__c ';
        query +=' from asset where IsCompetitorProduct = false AND ( AssetMark__c != \'耗材\' OR Product2.Family != \'ET\') and RecordTypeId !=\'01210000000kOPRAA2\' and  Return_Flag__c = false   ';
        if (targetList.size()>0 && targetList !=null) {
            query += ' and id in :targetList';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<asset> assetList) {
            // 分类计算,分别赋值,第四分类和产品型号
            List<Asset> assList1 = ComputeLRepair(1,assetList);
            List<Asset> assList2 = ComputeLRepair(2,assetList);             
        try {
 
            if (assList1.size()>0) {
                update assList1;
            }
            if (assList2.size()>0) {
                update assList2;
            }
            
        } catch (Exception e) {
            system.debug('err:::::' + e.getMessage());
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
 
    private  List<Asset> ComputeLRepair(Integer type,list<asset> assetList) {
            List<String> uniqueAssetModelNo = new List<String>();
            List<String> uniqueCategories = new List<String>();
            for (Asset ass: assetList) {
                // 统计第四分类和 产品分类的集合
                if (!uniqueCategories.contains(ass.Category4__c) && ass.Category4__c !=null && type==1) {
                    uniqueCategories.add(ass.Category4__c);
                }
                if (!uniqueAssetModelNo.contains(ass.product2.Asset_Model_No__c) &&ass.product2.Asset_Model_No__c !=null && type==2) {
                    uniqueAssetModelNo.add(ass.product2.Asset_Model_No__c);
                }
            }
            // 产品型号
            if (type==2) {   
                List<Maintenance_Contract_Asset__c>  mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.product2.Asset_Model_No__c from Maintenance_Contract_Asset__c 
                                                            where ((Maintenance_Contract_StarDate__c>= :this.timeThr  and  Maintenance_Contract_StarDate__c < :this.timeFour) 
                                                                or (Contract_End_Date__c>= :this.timeThr and  Contract_End_Date__c < :this.timeFour)) 
                                                            and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.product2.Asset_Model_No__c in :uniqueAssetModelNo];
 
                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                // 过去3年
 
                for (Maintenance_Contract_Asset__c mca:mcaList) {
                    if (NotoAssSize.containsKey(mca.Asset__r.product2.Asset_Model_No__c)) {
                        Decimal ms = NotoAssSize.get(mca.Asset__r.product2.Asset_Model_No__c);
                        ms += mca.Maintenance_Contract__r.MonthSize__c;
                        NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,ms);
                    }else{
                        NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,mca.Maintenance_Contract__r.MonthSize__c);
                    }
                }
               
                for (Asset ass : assetList) {
                    // 按照产品型号,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
 
                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            if (NotoAssSize.get(ass.product2.Asset_Model_No__c) !=null) {
                                 assOne.MonthSizeAssetModelNo__c = NotoAssSize.get(ass.product2.Asset_Model_No__c);
                            }
                            updateAss.add(assOne);
                }
 
                return updateAss;
                //第四分类
            }else if (type==1) {
                List<Maintenance_Contract_Asset__c>  mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.Category4__c   from Maintenance_Contract_Asset__c 
                                                            where ((Maintenance_Contract_StarDate__c>= :this.timeThr  and  Maintenance_Contract_StarDate__c < :this.timeFour) 
                                                                or (Contract_End_Date__c>= :this.timeThr and  Contract_End_Date__c < :this.timeFour)) 
                                                            and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.Category4__c in:uniqueCategories];
 
 
                List<Asset>   updateAss = new List<Asset>();
                Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
                
                    for (Maintenance_Contract_Asset__c mca:mcaList) {
                        if (NotoAssSize.containsKey(mca.Asset__r.Category4__c)) {
                            Decimal ms = NotoAssSize.get(mca.Asset__r.Category4__c);
                            ms += mca.Maintenance_Contract__r.MonthSize__c;
                            NotoAssSize.put(mca.Asset__r.Category4__c,ms);
                        }else{
                            NotoAssSize.put(mca.Asset__r.Category4__c,mca.Maintenance_Contract__r.MonthSize__c);
                        }
                    }
                    for (Asset ass : assetList) {
                    // 按照第四分类,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
                            Asset assOne = new Asset();
                            assOne.id = ass.id;
                            if (NotoAssSize.get(ass.Category4__c) !=null) {
                                 assOne.MonthSizeCategory4__c = NotoAssSize.get(ass.Category4__c);
                            }
                            updateAss.add(assOne);
 
                    }
                return updateAss;
            }else{
                return null;
            }
    }
 
}