global class FirstContractHistoricalBatch implements Database.Batchable<sObject> {
    public String query;

    global FirstContractHistoricalBatch() {
        this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select Id,Maintenance_Contract__r.RecordType_Name__c,Maintenance_Contract__r.Contract_Start_Date__c,'
                                                            +' Maintenance_Contract__r.Contract_End_Date__c,Maintenance_Contract__r.Maintenance_Contract_No__c, Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,asset__r.First_Service_Start_Day__c,'
                                                            +' asset__r.First_Service_End_Day__c,CreatedDate from Maintenance_Contract_Asset__c where Maintenance_Contract__r.Status__c = \'契約\' or Maintenance_Contract__r.Status__c = \'契約満了\'  order by CreatedDate ';
        
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> mcaList) {
        List<Asset> assList = new List<Asset>();
        List<String> assSqlList = new List<String>();
        List<String> macheckList = new List<String>();
        List<Maintenance_Contract_Asset__c> mcaList2 = new List<Maintenance_Contract_Asset__c>();
        Map<String,Maintenance_Contract_Asset__c> assetMap = new Map<String,Maintenance_Contract_Asset__c>();
        for ( Maintenance_Contract_Asset__c mca :mcaList) {
            assSqlList.add(mca.Asset__r.id);
            // 首次出现服务合同
            if (!assetMap.containsKey(mca.Asset__r.id)&&mca.Maintenance_Contract__r.RecordType_Name__c == '服务合同') {
                assetMap.put(mca.Asset__r.id,mca);
            }
        }
        List<Asset> assList1 = [select id,First_Service_number__c from Asset where id in :assSqlList];
        for (Asset ass:assList1) {
            if (ass.First_Service_number__c!=null) {
                macheckList.add(ass.First_Service_number__c);
            }
        }
        if (macheckList.size()!=0 && macheckList!=null) {
             mcaList2 = [select id,Asset__r.id,CreatedDate,Maintenance_Contract__r.Contract_Start_Date__c,
                        Maintenance_Contract__r.Contract_End_Date__c,Maintenance_Contract__r.Maintenance_Contract_No__c,
                        Asset__r.name,asset__r.First_Service_number__c,asset__r.First_Service_Start_Day__c,
                        asset__r.First_Service_End_Day__c 
                        from Maintenance_Contract_Asset__c 
                        where Maintenance_Contract__r.Maintenance_Contract_No__c in :macheckList];
        }
        
        for ( Maintenance_Contract_Asset__c mca:mcaList2) {
            if (assetMap.get(mca.Asset__r.id)!=null && (assetMap.get(mca.Asset__r.id).CreatedDate > mca.CreatedDate)) {
                assetMap.remove(mca.Asset__r.id);
                assetMap.put(mca.Asset__r.id,mca);
            }
        }

        for (Maintenance_Contract_Asset__c mca:assetMap.values()) {
                  Asset ass = new Asset();
                  ass.id = mca.asset__r.id;
               
                   ass.First_Service_number__c = mca.Maintenance_Contract__r.Maintenance_Contract_No__c;
                   ass.First_Service_End_Day__c = mca.Maintenance_Contract__r.Contract_End_Date__c;
                   ass.First_Service_Start_Day__c = mca.Maintenance_Contract__r.Contract_Start_Date__c;
                assList.add(ass);
             }
        
      if (assList.size()>0) {
        update assList;
      }
        
    }

    global void finish(Database.BatchableContext BC) {

    }
}