global without sharing class AssetTurnoverBatch implements Database.Batchable { public static Integer FIELDMAX = 62; private final List TEST_ID = null; private BatchIF_Log__c iflog; global AssetTurnoverBatch(List testId) { TEST_ID = testId; System.debug('TEST_ID=' + TEST_ID); iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Log__c = 'AssetTurnoverBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; } global AssetTurnoverBatch(){ iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Log__c = 'AssetTurnoverBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; } /** *查询样机借出明细的信息 */ global Database.QueryLocator start(Database.BatchableContext BC) { SS_Batch_Column_Mapping__c ladMapping = SS_Batch_Column_Mapping__c.getValues('loaner_application_detail__c'); Set apiTempSet = new Set(); apiTempSet.add('Id'); for (Integer i = 1; i <= FIELDMAX; i++) { String lpadI = ('00' + i).right(3); String fromColumn = 'From_Column_' + lpadI + '__c'; String apiStr = String.valueOf(ladMapping.get(fromColumn)); if (String.isBlank(apiStr) == false && apiTempSet.contains(apiStr) == false) { apiTempSet.add(apiStr); } } String soql = 'Select ' + String.join(new List(apiTempSet), ',') + ' from ' + ladMapping.Name; if (TEST_ID <> null) { soql += ' where Id IN: TEST_ID'; } System.debug('soql=' + soql); return Database.getQueryLocator(soql); } /** *样机周转表生成 */ global void execute(Database.BatchableContext BC, List ladList) { List lrList = new List(); SS_Batch_Column_Mapping__c ladMapping = SS_Batch_Column_Mapping__c.getValues('loaner_application_detail__c'); //将样机借用申请明细的字段的值复制到周转表中。 for(loaner_application_detail__c lad : ladList){ Boolean insflag = false; loaner_report__c lr = new loaner_report__c(); for(Integer i = 1; i <= FIELDMAX; i++){ String lpadI = ('00' + i).right(3); String fromColumn = 'From_Column_' + lpadI + '__c'; String apiStr = String.valueOf(ladMapping.get(fromColumn)); if (String.isBlank(apiStr) == false) { String ssColumn = 'SS_Column_' + lpadI + '__c'; String ssApiStr = String.valueOf(ladMapping.get(ssColumn)); try{ lr.put(ssApiStr, lad.get(apiStr)); }catch(Exception e){ iflog.ErrorLog__c = 'ERROR ' + String.valueOf(ladMapping.get('SS_TableName__c')) + ' [' + apiStr + '] => [' + ssApiStr + ']' + e.getMessage() + '\n'; } } } System.debug(lr); lrList.add(lr); } for(loaner_report__c lr : lrList){ Integer thisMonth = Date.today().month() == 1 ? 12 : Date.today().month() -1; Integer lronMonth = null; Integer lroutMonth = null; Integer thisYear = Date.today().month() == 1 ? Date.today().year() -1 : Date.today().year(); Integer lronYear = null; Integer lroutYear = null; if(lr.Detail_Received_loaner_Date__c != null){ lronMonth = lr.Detail_Received_loaner_Date__c.month(); lronYear = lr.Detail_Received_loaner_Date__c.year(); } if(lr.Detail_Shipment_Request_Date__c != null){ lroutMonth = lr.Detail_Shipment_Request_Date__c.month(); lroutYear = lr.Detail_Shipment_Request_Date__c.year(); } System.debug('thisYear : ' + thisYear + '\tlronYear : ' + lronYear + '\tlroutYear : ' + lroutYear); System.debug('thisMonth : ' + thisMonth + '\tlronMonth : ' + lronMonth + '\tlroutMonth : ' + lroutMonth); //获取本月天数,月份,年份 lr.DayOfTheMonth__c = Date.daysInMonth(thisYear, thisMonth); lr.Month__c = thisMonth; lr.Year__c = String.valueOf(thisYear); lr.StatisticsTime__c = Date.newInstance(thisYear, thisMonth, 1); System.debug('dayInMonth : ' + lr.DayOfTheMonth__c); //判断本月借出天数 if(thisMonth == lronMonth && thisYear == lronYear){ if(thisMonth != lroutMonth ){ System.debug('1'); lr.loan_days__c = Date.newInstance(thisYear, thisMonth, 1).daysBetween(lr.Detail_Received_loaner_Date__c) + 1; lr.OutTime__c = Datetime.newInstance(thisYear, thisMonth, 1, 0, 0, 0); }else{ System.debug('666'); lr.loan_days__c = lr.Detail_Shipment_Request_Date__c.daysBetween(lr.Detail_Received_loaner_Date__c) + 1; } }else if(lronMonth == null){ if(thisMonth == lroutMonth && thisYear == lroutYear){ lr.loan_days__c = lr.Detail_Shipment_Request_Date__c.daysBetween(Date.newInstance(thisYear, thisMonth, Date.daysInMonth(thisYear, thisMonth))) + 1; lr.inTime__c = Datetime.newInstance(thisYear,thisMonth+1,1,0,0,0); }else if(lroutMonth > thisMonth && thisYear == lroutYear){ lr.loan_days__c = 0; System.debug('2'); lr.inTime__c = null; lr.OutTime__c = null; }else if(lroutMonth == null){ lr.loan_days__c = 0; System.debug('3'); lr.inTime__c = null; lr.OutTime__c = null; }else{ lr.loan_days__c = lr.DayOfTheMonth__c; lr.OutTime__c = Datetime.newInstance(thisYear, thisMonth, 1, 0, 0, 0); lr.inTime__c = Datetime.newInstance(thisYear,thisMonth+1,1,0,0,0); } }else{ if(lroutMonth == thisMonth && lroutYear == thisYear && lronMonth > thisMonth){ lr.loan_days__c = lr.Detail_Shipment_Request_Date__c.daysBetween(Date.newInstance(thisYear, thisMonth, Date.daysInMonth(thisYear, thisMonth))) + 1; lr.inTime__c = Datetime.newInstance(thisYear, thisMonth+1, 1, 0, 0, 0); }else{ System.debug('4'); lr.loan_days__c = 0; lr.inTime__c = null; lr.OutTime__c = null; } } } System.debug(lrList); this.aa(); try{ insert lrList; }catch(Exception e){ System.debug(e); } } global void finish(Database.BatchableContext BC) { } public void aa(){ Integer i = 1; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; i++; } }