global class AssetWhereabouts implements Database.Batchable<sObject>,Database.Stateful {
    public String query;
    public BatchIF_Log__c iflog ;
    public Date toDay = Date.today().addYears(-1);
    global AssetWhereabouts() {
        this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        iflog  = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Log__c  = 'AssetWhereabouts start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        query = 'select id,Asset__c,Maintenance_Contract__c,Asset__r.CurrentContract__r.RecordType_Name__c,Asset__r.CurrentContract__r.Status__c'
                +' from Maintenance_Contract_Asset__c where Asset__r.AssetWhereabouts__c = null ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> mcaList) {
        List<Asset> Asslist = new List<Asset>();
        List<String> mcList = new List<String>();
        List<Repair__c> reList  = [select id,Maintenance_Contract__c,Status1__c,DateReceiptQuestions__c  
                                    from Repair__c 
                                    where DateReceiptQuestions__c >= :toDay and Status1__c != '0.取消' and Status1__c != '0.删除' and Maintenance_Contract__c != null];
        System.debug('reList=='+reList);                                           
        for (Repair__c re: reList) {
            mcList.add(re.Maintenance_Contract__c);
        }
        System.debug('mcaList=='+mcaList );
        for (Maintenance_Contract_Asset__c  mca: mcaList) {
                Asset ass =  new Asset();
                // System.debug('mca.Maintenance_Contract__r.RecordType_Name__c=='+mca.Maintenance_Contract__r.RecordType_Name__c);
                // System.debug('mca.Maintenance_Contract__r.Status__c=='+mca.Maintenance_Contract__r.Status__c);
            if (mca.Asset__r.CurrentContract__r.RecordType_Name__c == '服务合同' && mca.Asset__r.CurrentContract__r.Status__c == '契約') {
                ass.id = mca.Asset__c;
                ass.AssetWhereabouts__c = '服务合同';
                if (!Asslist.contains(ass)) {
                         Asslist.add(ass);
                }
            }
             else if(mca.Asset__r.CurrentContract__r.Status__c != '契約' && mcList.contains(mca.Maintenance_Contract__c)){
                    ass.id = mca.Asset__c;
                    ass.AssetWhereabouts__c = '一般维修';
                    if (!Asslist.contains(ass)) {
                         Asslist.add(ass);
                    }
            } else{
                    ass.id = mca.Asset__c;
                    ass.AssetWhereabouts__c = '其他';
                    if (!Asslist.contains(ass)) {
                         Asslist.add(ass);
                    }
            }
        } 
        if (Asslist!=null && Asslist.size()!= 0) {
            update Asslist;
        }  
    }

    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\nAssetWhereabouts end';
        String tmp = iflog.ErrorLog__c;
        integer i=0;
        i++;
        if (tmp.length() >= 5000) {
            tmp = tmp.substring(0, 5000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        i++;
        i++;
        update iflog;
    }
}