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

    global UpdateURFContactBatch() {
        this.query = query;
    }
    global UpdateURFContactBatch(String conId) {
        this.query = query;
        this.conId = conId;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        // query = 'select Id,Maintenance_Contract__c,Series_AllCount__c,URF_Series_F__c from Maintenance_Contract_Asset__c where ';
        // query += ' Maintenance_Contract__r.Status__c = \'契約\' and  Maintenance_Contract__r.URF_Contract__c = true ';
        // if (conId != null && conId != '') {
        //     query += ' and Maintenance_Contract__c = :conId ';
        // }
        query = 'select Id,URF_Contract__c,Status__c from Maintenance_Contract__c where ';
        query += ' Status__c = \'契約\' and  URF_Contract__c = true ';
        if (conId != null && conId != '') {
            query += ' and Id = :conId ';
        }
        return Database.getQueryLocator(query);
    }

    //global void execute(Database.BatchableContext BC, list<Maintenance_Contract_Asset__c> scope) {
    global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
        Map<String,Maintenance_Contract__c> MCMap = new Map<String,Maintenance_Contract__c>();
        //URF限次合同2期 LY 20220908 start
        //限次合同内的所有限次产品的大修次数全部使用完时，合同状态变为契約満了
        Map<ID,List<String>> urfAMap1 = new Map<ID,List<String>>();
        Map<ID,List<String>> urfAMap2 = new Map<ID,List<String>>();
        List<ID> urfList = new List<ID>();
        for (Maintenance_Contract__c mcp : scope) {
            List<Maintenance_Contract_Asset__c> mcList=[select Id,Maintenance_Contract__c,Series_AllCount__c,URF_Series_F__c 
                        from Maintenance_Contract_Asset__c 
                        where Maintenance_Contract__c =: mcp.Id];
            System.debug('1111111111111111111111111111mcList'+mcList);
            for (Maintenance_Contract_Asset__c mca : mcList) {
                // if (MCMap.containsKey(mca.Maintenance_Contract__c) && mca.Series_AllCount__c) {
                //     MCMap.remove(mca.Maintenance_Contract__c);
                // }else if (!MCMap.containsKey(mca.Maintenance_Contract__c) && mca.Series_AllCount__c == false) {
                //     Maintenance_Contract__c mc = new Maintenance_Contract__c();
                //     mc.Id = mca.Maintenance_Contract__c;
                //     mc.Status__c = '契約満了';
                //     mc.URFContact_EndDate__c = Date.today();
                //     MCMap.put(mca.Maintenance_Contract__c, mc);
                // }

                List<String> list1 = urfAMap1.get(mca.Maintenance_Contract__c);
                if (list1 == null) {
                    list1 = new List<String>();
                }
                list1.add(mca.URF_Series_F__c);
                urfAMap1.put(mca.Maintenance_Contract__c, list1);

                if (mca.Series_AllCount__c == false ){
                    List<String> list2 = urfAMap2.get(mca.Maintenance_Contract__c);
                    if (list2 == null) {
                        list2 = new List<String>();
                    }
                    list2.add(mca.URF_Series_F__c);
                    urfAMap2.put(mca.Maintenance_Contract__c,list2);
                }
                
                if (!urfList.contains(mca.Maintenance_Contract__c)){
                    urfList.add(mca.Maintenance_Contract__c);
                }
                
            }
            
            for(ID urf: urfList){
                
                if (urfAMap1.size() >0 && urfAMap2.size() >0){
                    System.debug('111111111111111111111111111111111'+urfAMap1);
                    System.debug('111111111111111111111111111111111'+urfAMap2);
                    if (urfAMap1.get(urf).size() >0 && urfAMap2.get(urf).size() >0 && urfAMap1.get(urf).size() == urfAMap2.get(urf).size()) {
                        Maintenance_Contract__c mc = new Maintenance_Contract__c();
                        mc.Id = urf;
                        mc.Status__c = '契約満了';
                        mc.URFContact_EndDate__c = Date.today();
                        MCMap.put(urf, mc);
                    }
                }
            }
            //URF限次合同2期 LY 20220908 end
            if (MCMap.size() > 0) {
                update MCMap.values();
            }
        } 
            
    }

    global void finish(Database.BatchableContext BC) {

    }
}