global class LogAutoSendBatch implements Database.Batchable<sObject> {
	/**
     * コンスタント
     */
    private String typeNFM;
    private String  messageGroupNumber;
    private Datetime times;

    global LogAutoSendBatch() {}	 
    global LogAutoSendBatch(String type, String message, Datetime times) {
        this.typeNFM = type;
        this.messageGroupNumber = message;
        this.times = times;
    }
    /**
     * startには、queryを実行、备品申请借出历史を検索
     */
    

    global Database.QueryLocator start(Database.BatchableContext BC) {
        Integer max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
        if (String.isNotBlank(typeNFM) && String.isNotBlank(messageGroupNumber) && times != null) {
            return Database.getQueryLocator(
                [SELECT Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
                        Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c,Type__c,
                        retry_cnt__c
                   FROM BatchIF_Log__c
                  WHERE RowDataFlg__c = true
                    AND Type__c = :typeNFM AND CreatedDate >= :times  AND MessageGroupNumber__c >= :messageGroupNumber
                    order by MessageGroupNumber__c asc  ]
            );
        } else if (new_profileId == System.Label.SystemAdmin || new_profileId == System.Label.SystemAdmin2) {
            return Database.getQueryLocator(
                [SELECT Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
                        Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c,Type__c,
                        retry_cnt__c
                   FROM BatchIF_Log__c
                  WHERE RowDataFlg__c = true
                    AND (Type__c = 'SBGITMRestNew'  OR Type__c = 'SBG010' OR Type__c = 'SBG017' OR Type__c = 'SBG027' OR
                        Type__c = 'SBG004'  OR Type__c = 'SBG005' OR Type__c = 'SBG203' OR Type__c = 'SBG204' OR Type__c = 'SBG018' OR Type__c = 'SBG001' OR Type__c = 'SBG007') 
                    AND retry_cnt__c != null AND retry_cnt__c > 0 AND retry_cnt__c < :max_cnt 
                ]
            );
        } else {
            return Database.getQueryLocator(
                [SELECT Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
                        Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c,Type__c,
                        retry_cnt__c
                   FROM BatchIF_Log__c
                  WHERE RowDataFlg__c = true
                    AND retry_cnt__c != null AND retry_cnt__c > 0 AND retry_cnt__c < :max_cnt 
                    AND (Type__c = 'SBGITMRestNew'  OR Type__c = 'SBG010' OR Type__c = 'SBG017' OR Type__c = 'SBG027' OR
                        Type__c = 'SBG004'  OR Type__c = 'SBG005' OR Type__c = 'SBG203' OR Type__c = 'SBG204' OR Type__c = 'SBG018' OR Type__c = 'SBG001' OR Type__c = 'SBG007')  AND ownerId = : UserInfo.getUserId()]
            );    
        }
        /*
        return Database.getQueryLocator(
            [SELECT Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
                    Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c,Type__c,
                    retry_cnt__c
               FROM BatchIF_Log__c
              WHERE RowDataFlg__c = true
                AND retry_cnt__c != null AND retry_cnt__c > 0 AND retry_cnt__c < :max_cnt 
                AND (Type__c = 'SBGITMRestNew'  OR Type__c = 'SBG010' OR Type__c = 'SBG017' OR Type__c = 'SBG027' OR
                    Type__c = 'SBG004'  OR Type__c = 'SBG005' OR Type__c = 'SBG203' OR Type__c = 'SBG204' OR Type__c = 'SBG018' OR Type__c = 'SBG001' OR Type__c = 'SBG007') ]
        );
        */
    }

    global void execute(Database.BatchableContext BC, List<BatchIF_Log__c> rowDataList) {
        String strType_c = null;
        for (BatchIF_Log__c rowData:rowDataList){
            strType_c= String.valueOf(rowData.get('Type__c'));
            if (strType_c == 'SBGITMRestNew'){
                // SBGITMWebService.execute(rowData.Id);
                SBGITMRestNew.main(rowData.Id);
            }
            else if(strType_c == 'SBG010'){
                SBG010Rest.main(rowData.Id);
            }
            else if(strType_c == 'SBG017'){
                SBG017Rest.main(rowData.Id);
            }else if(strType_c == 'SBG027'){
                SBG027Controller.execute2(rowData.Id);
            }else if(strType_c == 'SBG004'){
                SBG004Rest.main(rowData.Id);
            }else if(strType_c == 'SBG005'){
                SBG005Rest.main(rowData.Id);
            }else if(strType_c == 'SBG203'){
                SBG203Rest.main(rowData.Id);
            }else if(strType_c == 'SBG204'){
                SBG204Rest.main(rowData.Id);
            }else if(strType_c == 'SBG018'){
                SBG018Rest.main(rowData.Id);
            }else if(strType_c == 'SBG001'){
                SBG001TriggerHandler.execute2(rowData.Id);
            }

        }
    }

    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
}