global class AssetUseInfoBatch implements Database.Batchable<sObject> {
  
  private Integer dt;
  private Integer mt;
  private List < String > assIDList;

  global AssetUseInfoBatch() {
    
  }
  // 测试用
  global AssetUseInfoBatch(Integer intDate) {
    dt = intDate;
  }
  // 测试用
  global AssetUseInfoBatch(Integer intDate, List < String > assetIDList) {
    mt = intDate;
    assIDList = assetIDList;
  }

  global Database.QueryLocator start(Database.BatchableContext BC) {
    if (assIDList == null || assIDList.size() <= 0) {
      return Database.getQueryLocator([
        SELECT Id, Use_Info__c,CurrentTarget_Asset__c,CurrentAge_Asset__c,CurrentSOM_Status_2H__c,CurrentSOM_Status_1H__c
        FROM Asset WHERE Category2__c ='本体' OR Product2.Can_Repair__c = '可'
        ]);
    }else {
      return Database.getQueryLocator([
        SELECT Id, Use_Info__c,CurrentTarget_Asset__c,CurrentAge_Asset__c,CurrentSOM_Status_2H__c,CurrentSOM_Status_1H__c
        FROM Asset WHERE id =: assIDList
        ]);
    }
    
  }

  global void execute(Database.BatchableContext BC, List<sObject> sList) {
    
    Oly_TriggerHandler.bypass('NFM010UpsertStatuAchievements');
    List<Asset> updateList = new List<Asset>();
    Integer year = 0;
    if (dt == null) {
      year = Date.today().year();
    } else {
      year = dt;
    }
    //DB202305350020 LY 20230707 start
    Integer month = 0;
    if (mt == null) {
      month = Date.today().month();
    } else {
      month = mt;
    }
    System.debug('输入的月份是'+month);
    //DB202305350020 LY 20230707 end
    for (sObject sobj : sList) {
      Asset ast = (Asset) sobj;
      if (year == 2018) {
        ast.Use_Info_2018__c = ast.Use_Info__c;
      } else if (year == 2019) {
        ast.Use_Info_2019__c = ast.Use_Info__c;
      } else if (year == 2020) {
        ast.Use_Info_2020__c = ast.Use_Info__c;
      } else if (year == 2021) {
        ast.Use_Info_2021__c = ast.Use_Info__c;
      } else if (year == 2022) {
        ast.Use_Info_2022__c = ast.Use_Info__c;
      } else if (year == 2023) {
        ast.Use_Info_2023__c = ast.Use_Info__c;
      } else if (year == 2024) {
        ast.Use_Info_2024__c = ast.Use_Info__c;
      } else if (year == 2025) {
        ast.Use_Info_2025__c = ast.Use_Info__c;
      } else {
        break;
      }
      //DB202305350020 LY 20230707 start
      if (month == 4){
        ast.LastPeriodTarget_Asset__c = ast.CurrentTarget_Asset__c;//在每一财年首日，复制【当期覆盖设备的可选对象】给【上期覆盖设备的可选对象】
        ast.LastPeriodAge_Asset__c = ast.CurrentAge_Asset__c;//在每一财年首日，复制【当期设备年龄】给【上期设备年龄】；
        ast.LastPeriodSOM_Status_2H__c = ast.CurrentSOM_Status_2H__c;//每半期财年的首日（即：4月1日），根据新财年的日期逻辑运行，并保存上一个半期财年的记录。【上期2H期末SOM状态】
        if ((ast.Status =='使用中' || ast.Status =='不明' || ast.Status =='未使用') && ast.HP_Name__c !='Olympus社内') {
          ast.Existing_Asset__c = '1.既有设备';
        }else{
          ast.Existing_Asset__c = '';
        }

      }
      if (month == 10 && ast.HP_Name__c !='Olympus社内'){
        ast.LastPeriodSOM_Status_1H__c = ast.CurrentSOM_Status_1H__c;//每半期财年的首日（即：10月1日），根据新财年的日期逻辑运行，并保存上一个半期财年的记录。【上期1H期末SOM状态】
      }
      System.debug('更新前的保有设备是'+ast);
      //DB202305350020 LY 20230707 start
      updateList.add(ast);
    }

    if (updateList.size() > 0) {
      update updateList;
    }
  }
  global void finish(Database.BatchableContext BC) {
    
  }
  
}