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
global class RepairResonCannotWarrantyHistoricalBatch implements Database.Batchable<sObject> {
    public String query;
 
    global RepairResonCannotWarrantyHistoricalBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query='Select Delivered_Product__r.id,Delivered_Product__r.Reson_Can_not_Warranty__c,CancellationReason__c '
        +' from Repair__c where Return_Without_Repair_IF__c = true and Return_Without_Repair_Reason__c = \'I/F理由、確認\' and( CancellationReason__c =\'报价单到期\' or CancellationReason__c =\'废弃\' or CancellationReason__c =\'购买新品\' or CancellationReason__c =\'零件停产\' or CancellationReason__c =\'零件已停止生产\' '
        +' or CancellationReason__c =\'到其他公司修理\'  or CancellationReason__c =\'其他\'  or CancellationReason__c =\'修理价格高\'  or CancellationReason__c =\'坚持继续使用\'  or CancellationReason__c =\'无法修理\'   or CancellationReason__c =\'修理价格太高\'  or CancellationReason__c =\'用户坚持继续使用\'  or CancellationReason__c =\'无法修理返品\')';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Repair__c> repList) {
        List<String> assids = new List<String>();
        for(Repair__c rep:repList){
            assids.add(rep.Delivered_Product__r.id);
        }
 
        List<repair__c> repairs2 = [SELECT Id,name,Repair_List_Price_formula__c,Delivered_Product__r.id ,CreatedDate 
                                             FROM repair__c
                                             WHERE Delivered_Product__c in :assids order by Delivered_Product__c ,CreatedDate desc];
        Map<String,repair__c> assNewRepMap = new Map<String,repair__c>();
        List<String> repairs3 = new List<String> ();
        for (repair__c rep : repairs2) {
            if (assNewRepMap.get(rep.Delivered_Product__r.id)==null){
                assNewRepMap.put(rep.Delivered_Product__r.id,rep);
                repairs3.add(rep.id);
            }
            if (assNewRepMap.get(rep.Delivered_Product__r.id)!=null) {
                if (assNewRepMap.get(rep.Delivered_Product__r.id).CreatedDate < rep.CreatedDate) {
                    assNewRepMap.remove(rep.Delivered_Product__r.id);
                    assNewRepMap.put(rep.Delivered_Product__r.id,rep);
                    repairs3.add(rep.id);
                }
            }
        }
        List<String> idsss = new List<String>();
        integer i =0;
        List<repair__c> repairs4 = [SELECT id,Return_Without_Repair_Reason__c,CancellationReason__c,Delivered_Product__r.id  
                                             FROM repair__c
                                             WHERE id in :repairs3 ];
                                             for (repair__c rep : repairs4) {
                                                
                                                if (rep.Return_Without_Repair_Reason__c == 'I/F理由、確認' &&(rep.CancellationReason__c == '报价单到期' 
                            || rep.CancellationReason__c == '废弃' || rep.CancellationReason__c == '购买新品' || rep.CancellationReason__c == '零件停产'
                            || rep.CancellationReason__c == '零件已停止生产' || rep.CancellationReason__c == '到其他公司修理' || rep.CancellationReason__c == '其他'
                            || rep.CancellationReason__c == '修理价格高' || rep.CancellationReason__c == '坚持继续使用' || rep.CancellationReason__c == '无法修理'
                            || rep.CancellationReason__c == '修理价格太高' || rep.CancellationReason__c == '用户坚持继续使用' || rep.CancellationReason__c == '无法修理返品')){
                                                        i++;
                                                    idsss.add(rep.Delivered_Product__r.id);
                                                }
                                             }
    List<Asset> assList = [select id ,Reson_Can_not_Warranty__c from asset where id in :idsss];
    List<Asset> updateList = new  List<Asset>();
                for(Asset ass:assList){
                    Asset a1 = new Asset();
                    a1.id = ass.id;
                    a1.Reson_Can_not_Warranty__c = ass.Reson_Can_not_Warranty__c;
                    if (a1.Reson_Can_not_Warranty__c != null) {
                                                    if (a1.Reson_Can_not_Warranty__c.contains('修理中')) {
                                                        a1.Reson_Can_not_Warranty__c = a1.Reson_Can_not_Warranty__c.replace('修理中', '');
                                                    }
                                                    if (!a1.Reson_Can_not_Warranty__c.contains('弃修')) {
                                                        a1.Reson_Can_not_Warranty__c = a1.Reson_Can_not_Warranty__c + '弃修';
                                                    }
                                                } else {
                                                   a1.Reson_Can_not_Warranty__c = '弃修';
                                                }
                    updateList.add(a1);
                }
        if(updateList.size()>0){
            update updateList;
            system.debug('执行成功');
        }
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}