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) {

    }
}