global class OpdAmountBatch implements Database.Batchable<sObject> {
  String hpId;  
  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 start
  /*正常执行 病历数 Database.executeBatch(new OpdAmountBatch(), 20);
   *手动执行 指定医院上一年度病例数 Database.executeBatch(new OpdAmountBatch('医院id',''), 20);
   *手动执行 指定年度所有医院病例数 Database.executeBatch(new OpdAmountBatch('','指定年度'), 20);
   *手动执行 指定医院的某一年度的病例数 Database.executeBatch(new OpdAmountBatch('医院id','指定年度'), 20);
   */
  //下财年手动更新日期(传年度)(不传或者传你指定的年度,像'2021年度'这种格式)
  String PerformDate;
  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 end
  //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 start
  Boolean AllAcounts =false;
  //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 end

  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 start
  //下财年手动更新标识
  Boolean ManuallyUpdateFlag =false;
  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 end
  /*
  ①正常执行 Database.executeBatch(new OpdAmountBatch(), 20); 更新或创建本年度目标用户 1.15~1.31创建下年度目标客户
  ②手动按医院执行 Database.executeBatch(new OpdAmountBatch(''), 20); 输入1家医院 一般8个科室 逻辑同上
  ③强行创建下年度所有科室目标客户 Database.executeBatch(new OpdAmountBatch(true), 20);有下年度目标客户则不创建 其他逻辑同上
  ④强行创建某医院下年度所有科室目标客户 Database.executeBatch(new OpdAmountBatch('',true), 20);有下年度目标客户则不创建 其他逻辑同上
  */
  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 start
   global OpdAmountBatch(String hpId,String PerformDate) {
    this.hpId = hpId;
    this.PerformDate = PerformDate;
  }
  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 end
  global OpdAmountBatch() {
  }
  global OpdAmountBatch(String hpId) {
    this.hpId = hpId;
  }
  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 start
  //手动更新批量创建空 目标客户
  global OpdAmountBatch(Boolean ManuallyUpdateFlag) {
      this.ManuallyUpdateFlag = ManuallyUpdateFlag;
  }
  //手动更新单个医院创建空 目标客户
  global OpdAmountBatch(String hpId,Boolean ManuallyUpdateFlag) {
    this.hpId = hpId;
    this.ManuallyUpdateFlag = ManuallyUpdateFlag;
  }
  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 end
  //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 start
  //生成所有医院病例数 (生成所有,强制执行)
  global OpdAmountBatch(Boolean AllAcounts, Boolean ManuallyUpdateFlag) {
    this.AllAcounts = AllAcounts;
    this.ManuallyUpdateFlag =ManuallyUpdateFlag;
  }
  //单个医院 非H层客户 生成病例数
  global OpdAmountBatch(String hpId,Boolean AllAcounts, Boolean ManuallyUpdateFlag) {
    this.hpId = hpId;
    this.AllAcounts = AllAcounts;
    this.ManuallyUpdateFlag =ManuallyUpdateFlag;
  }
  //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 end
  global Database.QueryLocator start(Database.BatchableContext BC) {
    //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 start
    //增加检索 parentid
    if (String.isBlank(this.hpId)) {
      return Database.getQueryLocator(
               [select Id,ParentId,
               //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
               Account2__r.AllHostsNumber__c,
               Account2__r.RivalHostsNumber__c
               //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end
                from Account
                where Parent.RecordType.DeveloperName = 'HP'
                    and Parent.Is_Active__c <> '無効'
                    order by Id]
             );
    } else {
      return Database.getQueryLocator(
               [select Id,ParentId,
               //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
               Account2__r.AllHostsNumber__c,
               Account2__r.RivalHostsNumber__c
               //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end
                from Account
                where Parent.RecordType.DeveloperName = 'HP'
                    and Parent.Is_Active__c <> '無効'
                    and ParentId = :this.hpId
                                   order by Id]
             );
    }
    //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 end
  }

  global void execute(Database.BatchableContext BC, List<sObject> dcList) {
    List<Account_Number_of_target__c> upsertList = new List<Account_Number_of_target__c>();
    //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 start
    List<Account_Number_of_case__c> insertNextList = new List<Account_Number_of_case__c>();
    //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 end

    //默认执行时，false 执行main方法
    //手动执行下财年时，不执行main方法
    if(!this.ManuallyUpdateFlag){
      upsertList.addAll(main(dcList));
    }
    

    //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 start
    //①进来此方法就说明该战略科室没有下一年度的目标客户
    //②(每年1月15~2月1号 暂作为自动生成时间段) 从下一个财年(155p)起，安排在1月的第一个周六日运行
    //方法前后都有时间判断 都写的原因是：
    //①不在时间段里的没必要进入（前面的时间判断）②可以让nextMain 方法能够独立运行（方法里的时间判断）
    //获取当前时间
    Date today = date.today();
    //获取当前年
    Integer year = today.year();
    //写死 每年的1月15日
    Date startDate = date.newInstance(year, 1, 15);
    //写死 每年的2月1日
    Date endDate  = date.newInstance(year, 2, 1);

    //转成string 用于比较
    String todayStr = NFMUtil.formatDate2Str(today);
    String startDateStr = NFMUtil.formatDate2Str(startDate);
    String endDateStr = NFMUtil.formatDate2Str(endDate);

    //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 start
    //每自然年的1月14日
    Date fourDate = date.newInstance(year, 1, 4);
    //转成string
    String fourDateStr = NFMUtil.formatDate2Str(fourDate);
    if((fourDateStr==todayStr) || String.isNotBlank(PerformDate) || String.isNotBlank(this.hpId)||(this.ManuallyUpdateFlag)){
        insertNextList.addAll(nextCaseMain(dcList,this.PerformDate,this.hpId,this.AllAcounts,this.ManuallyUpdateFlag));
    }
    //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 end

    //每年1.15~1.31 或者 强行执行 才会进入此方法
    if((startDateStr<=todayStr&&todayStr<endDateStr)|| (this.ManuallyUpdateFlag) ){
      System.debug(insertNextList);
        upsertList.addAll(nextMain(dcList,this.ManuallyUpdateFlag));
    }
    //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 end

    System.debug('upsertList:'+upsertList);
    if (upsertList.size() > 0) upsert upsertList;
    if (insertNextList.size() > 0) upsert insertNextList;
  }

  public static List<Account_Number_of_target__c> main(List<sObject> dcList) {
    List<Account2__c> acc2List = [select id,
                                  Account_Org__c,
                                  Account_Org__r.Department_Class_Label__c,
                                  Account_Org__r.ParentId,
                                  OPD_290Miro_BF__c,
                                  OPD_290Miro_GI__c,
                                  OPD_3D__c,
                                  OPD_CV_170__c,
                                  OPD_CV_190__c,
                                  OPD_CV_290__c,
                                  OPD_EU_ME2__c,
                                  OPD_OTV_S190__c,
                                  OPD_STMS__c,
                                  OPD_GW__c,
                                  OPD_OER_AW__c,
                                  OPD_OTV_S400__c,
                                  OPD_CH_S400_XZ__c,
                                  OPD_UCES_3__c,
                                  //  SWAG-BC832V start
                                  OPD_CYF__c,
                                  Order_cnt_CYF__c,
                                  Opp_cnt_CYF__c,
                                  // SWAG-BC832V end
                                  // LHJ Start
                                  OPD_USG400__c,
                                  OPD_290Miro_GICF__c,
                                  OPD_290Miro_GIPlus__c,
                                  OPD_GFUCT260__c,
                                  OPD_OTV_S300__c,
                                  OPD_ESG400__c,
                                  OPD_TB__c,
                                  // LHJ End
                                  //20220602 WLIG-CEWDWS you start
                                  OPD_TB_0520__c,
                                  OPD_TB_0535__c,
                                  //20220602 WLIG-CEWDWS you end
                                  Opp_Actual_ThousandY__c,
                                  BO_Forecast_ThousandY__c,
                                  OP_ThousandY__c,
                                  Opp_Forecast_ThousandY__c,
                                  Opportunity_ThousandY__c,
                                  Amount_Without_Tax_Thousand__c,
                                  //20220111 SWAG-C8MBB6 加7.询价 lt  start
                                  AllOP_ThousandY__c,
                                  //20220111 SWAG-C8MBB6 加7.询价 lt  end
                                  ThisYear10__c,
                                  ThisYear11__c,
                                  ThisYear12__c,
                                  ThisYear1__c,
                                  ThisYear2__c,
                                  ThisYear3__c,
                                  ThisYear4__c,
                                  ThisYear5__c,
                                  ThisYear6__c,
                                  ThisYear7__c,
                                  ThisYear8__c,
                                  ThisYear9__c,
                                  // SWAG-BBBAKG start
                                  ThisYear1_Eng__c,
                                  ThisYear2_Eng__c,
                                  ThisYear3_Eng__c,
                                  ThisYear4_Eng__c,
                                  ThisYear5_Eng__c,
                                  ThisYear6_Eng__c,
                                  ThisYear7_Eng__c,
                                  ThisYear8_Eng__c,
                                  ThisYear9_Eng__c,
                                  ThisYear10_Eng__c,
                                  ThisYear11_Eng__c,
                                  ThisYear12_Eng__c,
                                  // SWAG-BBBAKG end
                                  // 2018年7月31日 HWAG-B37AKX      start
                                  ThisYear1_FSE__c,
                                  ThisYear2_FSE__c,
                                  ThisYear3_FSE__c,
                                  ThisYear4_FSE__c,
                                  ThisYear5_FSE__c,
                                  ThisYear6_FSE__c,
                                  ThisYear7_FSE__c,
                                  ThisYear8_FSE__c,
                                  ThisYear9_FSE__c,
                                  ThisYear10_FSE__c,
                                  ThisYear11_FSE__c,
                                  ThisYear12_FSE__c,
                                  // 2018年7月31日 HWAG-B37AKX    end
                                  Order_cnt_290Miro__c,
                                  Order_cnt_3D__c,
                                  Order_cnt_CHS400XZ__c,
                                  Order_cnt_CV170__c,
                                  Order_cnt_CV190__c,
                                  Order_cnt_CV290__c,
                                  Order_cnt_EUME2__c,
                                  Order_cnt_OERAW__c,
                                  Order_cnt_OTVS190__c,
                                  Order_cnt_OTVS400__c,
                                  Order_cnt_UCES3__c,
                                  Order_cnt_USG400__c,
                                  Order_cnt_GW__c,
                                  //  SWAG-BD24SU start
                                  Delivery_cnt_3D__c,
                                  Delivery_cnt_CV290__c,
                                  Delivery_cnt_CYF__c,
                                  Delivery_cnt_EUME2__c,
                                  Delivery_cnt_OERAW__c,
                                  Delivery_cnt_OTVS400__c,
                                  Delivery_cnt_USG400__c,
                                  Delivery_cnt_CV170__c,
                                  Delivery_cnt_CV190__c,
                                  Delivery_cnt_OTVS190__c,

                                  //  SWAG-BD24SU end
                                  Opp_cnt_290Miro__c,
                                  Opp_cnt_3D__c,
                                  Opp_cnt_CHS400XZ__c,
                                  Opp_cnt_CV170__c,
                                  Opp_cnt_CV190__c,
                                  Opp_cnt_CV290__c,
                                  Opp_cnt_EUME2__c,
                                  Opp_cnt_OERAW__c,
                                  Opp_cnt_OTVS190__c,
                                  Opp_cnt_OTVS400__c,
                                  Opp_cnt_UCES3__c,
                                  Opp_cnt_USG400__c,
                                  Opp_cnt_GW__c,
                                  // LHJ Start
                                  Delivery_cnt_290Miro__c,
                                  Delivery_cnt_290MiroBF__c,
                                  Delivery_cnt_290MiroGI__c,
                                  Delivery_cnt_290MiroGI_CF__c,
                                  Delivery_cnt_290MiroGI_Plus__c,
                                  Delivery_cnt_GFUCT260__c,
                                  Delivery_cnt_OTVS300__c,
                                  Delivery_cnt_ESG400__c,
                                  Delivery_cnt_TB__c,
                                  //20220602 WLIG-CEWDWS you start
                                  Delivery_cnt_TB_0520__c,
                                  Delivery_cnt_TB_0535__c,
                                  //20220602 WLIG-CEWDWS you end
                                  Opp_cnt_290MiroBF__c,
                                  Opp_cnt_290MiroGI__c,
                                  Opp_cnt_290MiroGI_CF__c,
                                  Opp_cnt_290MiroGI_Plus__c,
                                  Opp_cnt_GFUCT260__c,
                                  Opp_cnt_OTVS300__c,
                                  Opp_cnt_ESG400__c,
                                  Opp_cnt_TB__c,
                                  //20220602 WLIG-CEWDWS you start
                                  Opp_cnt_TB_0520__c,
                                  Opp_cnt_TB_0535__c,
                                  //20220602 WLIG-CEWDWS you end
                                  Order_cnt_290MiroBF__c,
                                  Order_cnt_290MiroGI__c,
                                  Order_cnt_290MiroGI_CF__c,
                                  Order_cnt_290MiroGI_Plus__c,
                                  Order_cnt_GFUCT260__c,
                                  Order_cnt_OTVS300__c,
                                  Order_cnt_ESG400__c,
                                  Order_cnt_TB__c,
                                  //20220602 WLIG-CEWDWS you start
                                  Order_cnt_TB_0520__c,
                                  Order_cnt_TB_0535__c,
                                  //20220602 WLIG-CEWDWS you end
                                  Rental_Cnt_290MiroBF__c,
                                  Rental_Cnt_290MiroGI__c,
                                  Rental_Cnt_290MiroGI_CF__c,
                                  Rental_Cnt_290MiroGI_Plus__c,
                                  Rental_Cnt_CV170__c,
                                  Rental_Cnt_CV190__c,
                                  Rental_Cnt_CV290__c,
                                  Rental_Cnt_ESG400__c,
                                  Rental_Cnt_EUME2__c,
                                  Rental_Cnt_GFUCT260__c,
                                  Rental_Cnt_OTVS190__c,
                                  Rental_Cnt_OTVS300__c,
                                  Rental_Cnt_OTVS400__c,
                                  Rental_Cnt_TB__c,
                                  Rental_Cnt_USG400__c,
                                  // LHJ End
                                  //SWAG-BSC5WP you-20201021 start
                                  Asset_Cnt_290MiroBF__c,
                                  Asset_Cnt_290MiroGI__c ,
                                  Asset_Cnt_290MiroGI_CF__c,
                                  Asset_Cnt_290MiroGI_Plus__c,
                                  Asset_Cnt_CV170__c,
                                  Asset_Cnt_CV190__c,
                                  Asset_Cnt_CV290__c,
                                  Asset_Cnt_ESG400__c,
                                  Asset_Cnt_EUME2__c,
                                  Asset_Cnt_GFUCT260__c,
                                  Asset_Cnt_OTVS190__c,
                                  Asset_Cnt_OTVS300__c,
                                  Asset_Cnt_OTVS400__c,
                                  Asset_Cnt_TB__c,
                                  //20220602 WLIG-CEWDWS you start
                                  Asset_Cnt_TB_0520__c,
                                  Asset_Cnt_TB_0535__c,
                                  //20220602 WLIG-CEWDWS you end
                                  Asset_Cnt_USG400__c,
                                  //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
                                  AllHostsNumber__c,
                                  RivalHostsNumber__c
                                  //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end
                                  //SWAG-BSC5WP you-20201021 start
                                  from Account2__c
                                  where Account_Org__c = :dcList];
    // 2018年7月12日 SWAG-B2D4QK 从Account中获取 提案目标客户等5个字段 by 张玉山
    list<Account> accs = [select Id, Proposal_target_customer__c, Submit_proposal_document_SD__c,
                          Submit_proposal_document__c, Proposal_target_customer_number__c,
                          Proposal_Submit_Status_Using__c, Proposal_Submit_Status_Dept__c
                          from Account
                          where Id = :dcList];
    Map<string, Account> accsMap = new Map<string, Account>();
    for (Account acc : accs) {
      accsMap.put(acc.id, acc);
    }

    Date dateToday = Date.today();
    Date yesterday = dateToday.addDays(-1);
    Integer year = yesterday.year();
    Integer month = yesterday.month();
    if (month < 4) {
      year -= 1;
    }
    String ocmYear = year + '年度';
    String ocmPeriod = year - 1867 + 'P';

    Map<String, Account_Number_of_target__c> accTarMap = new Map<String, Account_Number_of_target__c>();
    List<Account_Number_of_target__c> accTarList = [select id, Account__c,OCM_Year__c 
        from Account_Number_of_target__c
        where Account__c = :dcList
                           and OCM_Year__c = :ocmYear];
    
    for (Account_Number_of_target__c accTar : accTarList) {
      //本年度
      if(accTar.OCM_Year__c.equals(ocmYear)){
          accTarMap.put(accTar.Account__c, accTar);// 目标客户（战略科室，目标客户）
      }
    }
    List<Account_Number_of_target__c> upsertList = new List<Account_Number_of_target__c>();

    for (Account2__c acc2 : acc2List) {
      String dclabel = acc2.Account_Org__r.Department_Class_Label__c;//战略科室分类
      Account_Number_of_target__c accTar = new Account_Number_of_target__c();

        if (accTarMap.containsKey(acc2.Account_Org__c) == true) {
          accTar = accTarMap.get(acc2.Account_Org__c);
        } else {
          accTar.OCM_Year__c = ocmYear;
          accTar.OCM_Period__c = ocmPeriod;
          accTar.Account_HP__c = acc2.Account_Org__r.ParentId;
          accTar.Account__c = acc2.Account_Org__c;
        }
      
      //  202020604 LHJ SWAG-BQ75WE Start
      // if (dclabel == '呼吸科') {
      //   accTar.OPD_amount_290Miro__c = acc2.OPD_290Miro_BF__c;
      //   accTar.OPD_amount_OERAW__c = acc2.OPD_OER_AW__c;
      //   accTar.OPD_amount_OTVS400__c = 0;
      //   accTar.OPD_amount_CHS400XZ__c = 0;
      //   accTar.OPD_amount_UCES3__c = 0;
      // } else if (dclabel == '消化科') {
      //   accTar.OPD_amount_290Miro__c = acc2.OPD_290Miro_GI__c;
      //   accTar.OPD_amount_OERAW__c = acc2.OPD_OER_AW__c;
      //   accTar.OPD_amount_OTVS400__c = 0;
      //   accTar.OPD_amount_CHS400XZ__c = 0;
      //   accTar.OPD_amount_UCES3__c = 0;
      // } else {
      //   accTar.OPD_amount_290Miro__c = 0;
      //   accTar.OPD_amount_OERAW__c = 0;

      //   accTar.OPD_amount_OTVS400__c = acc2.OPD_OTV_S400__c;
      //   accTar.OPD_amount_CHS400XZ__c = acc2.OPD_CH_S400_XZ__c;
      //   accTar.OPD_amount_UCES3__c = acc2.OPD_UCES_3__c;
      // }
      //  202020604 LHJ SWAG-BQ75WE End


      // 2018年7月12日 SWAG-B2D4QK 将Account中获取的提案目标客户等5个字段存入目标客户中 start
      if (accsMap.containsKey(accTar.Account__c)) {
        Account acc = accsMap.get(accTar.Account__c);
        accTar.Proposal_target_customer__c          = acc.Proposal_target_customer__c;
        accTar.Submit_proposal_document_SD__c       = acc.Submit_proposal_document_SD__c;
        accTar.Submit_proposal_document__c          = acc.Submit_proposal_document__c;
        accTar.Proposal_target_customer_number__c   = acc.Proposal_target_customer_number__c;
        accTar.Proposal_Submit_Status__c            = acc.Proposal_Submit_Status_Dept__c;
      }
      // 2018年7月12日 SWAG-B2D4QK 将Account中获取的提案目标客户等5个字段存入目标客户中 end
      accTar.OPD_amount_3D__c      = acc2.OPD_3D__c;
      accTar.OPD_amount_GW__c      = acc2.OPD_GW__c;
      // accTar.OPD_amount_USG400__c  = acc2.OPD_STMS__c;

      accTar.OPD_amount_CV290__c   = acc2.OPD_CV_290__c;
      accTar.OPD_amount_EUME2__c   = acc2.OPD_EU_ME2__c;
      accTar.OPD_amount_CV170__c   = acc2.OPD_CV_170__c;
      accTar.OPD_amount_CV190__c   = acc2.OPD_CV_190__c;
      accTar.OPD_amount_OTVS190__c = acc2.OPD_OTV_S190__c;
      //  202020604 LHJ SWAG-BQ75WE Start
      accTar.OPD_amount_USG400__c  = acc2.OPD_USG400__c;
      accTar.OPD_amount_OTVS400__c = acc2.OPD_OTV_S400__c;
      accTar.OPD_amount_290MiroBF__c = acc2.OPD_290Miro_BF__c;
      accTar.OPD_amount_290MiroGI__c = acc2.OPD_290Miro_GI__c;
      accTar.OPD_amount_290MiroGI_CF__c = acc2.OPD_290Miro_GICF__c;
      accTar.OPD_amount_290MiroGI_Plus__c = acc2.OPD_290Miro_GIPlus__c;

      accTar.OPD_amount_GFUCT260__c  = acc2.OPD_GFUCT260__c;
      accTar.OPD_amount_OTVS300__c = acc2.OPD_OTV_S300__c;
      accTar.OPD_amount_ESG400__c    = acc2.OPD_ESG400__c;
      //20220602 WLIG-CEWDWS you start
      //accTar.OPD_amount_TB__c        = acc2.OPD_TB__c;
      accTar.OPD_amount_TB_0520__c = acc2.OPD_TB_0520__c;
      accTar.OPD_amount_TB_0535__c = acc2.OPD_TB_0535__c;
      //20220602 WLIG-CEWDWS you end
      //  202020604 LHJ SWAG-BQ75WE End
      
      // SWAG-BC832V  从客户2 保存至目标客户：CYF start
      accTar.Opp_cnt_CYF__c      = acc2.Opp_cnt_CYF__c  ;
      accTar.Order_cnt_CYF__c     = acc2.Order_cnt_CYF__c;
      accTar.OPD_amount_CYF__c     = acc2.OPD_CYF__c;
      // SWAG-BC832V  从客户2 保存至目标客户：CYF end

      accTar.Opp_Actual_ThousandY__c        = acc2.Opp_Actual_ThousandY__c;
      accTar.BO_Forecast_ThousandY__c       = acc2.BO_Forecast_ThousandY__c;
      accTar.OP_ThousandY__c                = acc2.OP_ThousandY__c;
      accTar.Opp_Forecast_ThousandY__c      = acc2.Opp_Forecast_ThousandY__c;
      accTar.Opportunity_ThousandY__c       = acc2.Opportunity_ThousandY__c;
      accTar.Amount_Without_Tax_Thousand__c = acc2.Amount_Without_Tax_Thousand__c;

      //20220111 SWAG-C8MBB6 加7.询价 lt  start
      accTar.AllOP_ThousandY__c = acc2.AllOP_ThousandY__c;
      //20220111 SWAG-C8MBB6 加7.询价 lt  end

      // SWAG-BD24SU 发货数备份到目标客户 start
      accTar.Delivery_cnt_3D__c             = acc2.Delivery_cnt_3D__c;
      accTar.Delivery_cnt_CYF__c            = acc2.Delivery_cnt_CYF__c;
      accTar.Delivery_cnt_OERAW__c          = acc2.Delivery_cnt_OERAW__c;

      accTar.Delivery_cnt_CV290__c          = acc2.Delivery_cnt_CV290__c;
      accTar.Delivery_cnt_EUME2__c          = acc2.Delivery_cnt_EUME2__c;
      accTar.Delivery_cnt_OTVS400__c        = acc2.Delivery_cnt_OTVS400__c;
      accTar.Delivery_cnt_CV170__c          = acc2.Delivery_cnt_CV170__c;
      accTar.Delivery_cnt_CV190__c          = acc2.Delivery_cnt_CV190__c;
      accTar.Delivery_cnt_OTVS190__c        = acc2.Delivery_cnt_OTVS190__c;
      accTar.Delivery_cnt_USG400__c       = acc2.Delivery_cnt_USG400__c;
      // SWAG-BD24SU 发货数备份到目标客户 end
      //  202020604 LHJ SWAG-BQ75WE Start
      accTar.Delivery_cnt_290Miro__c  = acc2.Delivery_cnt_290Miro__c;
      accTar.Delivery_cnt_290MiroBF__c = acc2.Delivery_cnt_290MiroBF__c;
      accTar.Delivery_cnt_290MiroGI__c = acc2.Delivery_cnt_290MiroGI__c;
      accTar.Delivery_cnt_290MiroGI_CF__c = acc2.Delivery_cnt_290MiroGI_CF__c;
      accTar.Delivery_cnt_290MiroGI_Plus__c = acc2.Delivery_cnt_290MiroGI_Plus__c;
      accTar.Delivery_cnt_GFUCT260__c  = acc2.Delivery_cnt_GFUCT260__c;
      accTar.Delivery_cnt_OTVS300__c = acc2.Delivery_cnt_OTVS300__c;
      accTar.Delivery_cnt_ESG400__c    = acc2.Delivery_cnt_ESG400__c;
      //20220602 WLIG-CEWDWS you start
      //accTar.Delivery_cnt_TB__c        = acc2.Delivery_cnt_TB__c;
      accTar.Delivery_cnt_TB_0520__c  = acc2.Delivery_cnt_TB_0520__c;
      accTar.Delivery_cnt_TB_0535__c  = acc2.Delivery_cnt_TB_0535__c;
      //20220602 WLIG-CEWDWS you end
      //  202020604 LHJ SWAG-BQ75WE End

      accTar.Order_cnt_290Miro__c  = acc2.Order_cnt_290Miro__c;
      accTar.Order_cnt_3D__c       = acc2.Order_cnt_3D__c;
      accTar.Order_cnt_CHS400XZ__c = acc2.Order_cnt_CHS400XZ__c;
      accTar.Order_cnt_OERAW__c    = acc2.Order_cnt_OERAW__c;
      accTar.Order_cnt_UCES3__c    = acc2.Order_cnt_UCES3__c;
      accTar.Order_cnt_GW__c       = acc2.Order_cnt_GW__c;

      accTar.Order_cnt_CV290__c    = acc2.Order_cnt_CV290__c;
      accTar.Order_cnt_EUME2__c    = acc2.Order_cnt_EUME2__c;
      accTar.Order_cnt_OTVS400__c  = acc2.Order_cnt_OTVS400__c;
      accTar.Order_cnt_CV170__c    = acc2.Order_cnt_CV170__c;
      accTar.Order_cnt_CV190__c    = acc2.Order_cnt_CV190__c;
      accTar.Order_cnt_OTVS190__c  = acc2.Order_cnt_OTVS190__c;
      accTar.Order_cnt_USG400__c   = acc2.Order_cnt_USG400__c;
      //  202020604 LHJ SWAG-BQ75WE Start
      accTar.Order_cnt_290MiroBF__c = acc2.Order_cnt_290MiroBF__c;
      accTar.Order_cnt_290MiroGI__c = acc2.Order_cnt_290MiroGI__c;
      accTar.Order_cnt_290MiroGI_CF__c = acc2.Order_cnt_290MiroGI_CF__c;
      accTar.Order_cnt_290MiroGI_Plus__c = acc2.Order_cnt_290MiroGI_Plus__c;
      accTar.Order_cnt_GFUCT260__c  = acc2.Order_cnt_GFUCT260__c;
      accTar.Order_cnt_OTVS300__c = acc2.Order_cnt_OTVS300__c;
      accTar.Order_cnt_ESG400__c    = acc2.Order_cnt_ESG400__c;
      //20220602 WLIG-CEWDWS you start
      //accTar.Order_cnt_TB__c        = acc2.Order_cnt_TB__c;
      accTar.Order_cnt_TB_0520__c        = acc2.Order_cnt_TB_0520__c;
      accTar.Order_cnt_TB_0535__c        = acc2.Order_cnt_TB_0535__c;
      //20220602 WLIG-CEWDWS you end
      //  202020604 LHJ SWAG-BQ75WE End
      accTar.Opp_cnt_3D__c       = acc2.Opp_cnt_3D__c;
      accTar.Opp_cnt_CHS400XZ__c = acc2.Opp_cnt_CHS400XZ__c;
      accTar.Opp_cnt_OERAW__c    = acc2.Opp_cnt_OERAW__c;
      accTar.Opp_cnt_UCES3__c    = acc2.Opp_cnt_UCES3__c;
      accTar.Opp_cnt_GW__c       = acc2.Opp_cnt_GW__c;

      // accTar.Opp_cnt_290Miro__c  = acc2.Opp_cnt_290Miro__c;
      accTar.Opp_cnt_CV290__c    = acc2.Opp_cnt_CV290__c;
      accTar.Opp_cnt_EUME2__c    = acc2.Opp_cnt_EUME2__c;
      accTar.Opp_cnt_OTVS400__c  = acc2.Opp_cnt_OTVS400__c;
      accTar.Opp_cnt_CV170__c    = acc2.Opp_cnt_CV170__c;
      accTar.Opp_cnt_CV190__c    = acc2.Opp_cnt_CV190__c;
      accTar.Opp_cnt_OTVS190__c  = acc2.Opp_cnt_OTVS190__c;
      accTar.Opp_cnt_USG400__c   = acc2.Opp_cnt_USG400__c;
      //  202020604 LHJ SWAG-BQ75WE Start
      accTar.Opp_cnt_290MiroBF__c = acc2.Opp_cnt_290MiroBF__c;
      accTar.Opp_cnt_290MiroGI__c = acc2.Opp_cnt_290MiroGI__c;
      accTar.Opp_cnt_290MiroGI_CF__c = acc2.Opp_cnt_290MiroGI_CF__c;
      accTar.Opp_cnt_290MiroGI_Plus__c = acc2.Opp_cnt_290MiroGI_Plus__c;
      accTar.Opp_cnt_GFUCT260__c  = acc2.Opp_cnt_GFUCT260__c;
      accTar.Opp_cnt_OTVS300__c = acc2.Opp_cnt_OTVS300__c;
      accTar.Opp_cnt_ESG400__c    = acc2.Opp_cnt_ESG400__c;
      //20220602 WLIG-CEWDWS you start
      //accTar.Opp_cnt_TB__c        = acc2.Opp_cnt_TB__c;
      accTar.Opp_cnt_TB_0520__c        = acc2.Opp_cnt_TB_0520__c;
      accTar.Opp_cnt_TB_0535__c        = acc2.Opp_cnt_TB_0535__c;
      //20220602 WLIG-CEWDWS you end
      //  202020604 LHJ SWAG-BQ75WE End

      //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
      accTar.AllHostsNumber__c   = acc2.AllHostsNumber__c;
      accTar.RivalHostsNumber__c = acc2.RivalHostsNumber__c;
      //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end

      //  20200828 LHJ Start
      accTar.Rental_Cnt_290MiroBF__c        = acc2.Rental_Cnt_290MiroBF__c==0?null:acc2.Rental_Cnt_290MiroBF__c;
      accTar.Rental_Cnt_290MiroGI__c        = acc2.Rental_Cnt_290MiroGI__c==0?null:acc2.Rental_Cnt_290MiroGI__c;
      accTar.Rental_Cnt_290MiroGI_CF__c     = acc2.Rental_Cnt_290MiroGI_CF__c==0?null:acc2.Rental_Cnt_290MiroGI_CF__c;
      accTar.Rental_Cnt_290MiroGI_Plus__c   = acc2.Rental_Cnt_290MiroGI_Plus__c==0?null:acc2.Rental_Cnt_290MiroGI_Plus__c;
      accTar.Rental_Cnt_CV170__c            = acc2.Rental_Cnt_CV170__c==0?null:acc2.Rental_Cnt_CV170__c;
      accTar.Rental_Cnt_CV190__c            = acc2.Rental_Cnt_CV190__c==0?null:acc2.Rental_Cnt_CV190__c;
      accTar.Rental_Cnt_CV290__c            = acc2.Rental_Cnt_CV290__c==0?null:acc2.Rental_Cnt_CV290__c;
      accTar.Rental_Cnt_ESG400__c           = acc2.Rental_Cnt_ESG400__c==0?null:acc2.Rental_Cnt_ESG400__c;
      accTar.Rental_Cnt_EUME2__c            = acc2.Rental_Cnt_EUME2__c==0?null:acc2.Rental_Cnt_EUME2__c;
      accTar.Rental_Cnt_GFUCT260__c         = acc2.Rental_Cnt_GFUCT260__c==0?null:acc2.Rental_Cnt_GFUCT260__c;
      accTar.Rental_Cnt_OTVS190__c          = acc2.Rental_Cnt_OTVS190__c==0?null:acc2.Rental_Cnt_OTVS190__c;
      accTar.Rental_Cnt_OTVS300__c          = acc2.Rental_Cnt_OTVS300__c==0?null:acc2.Rental_Cnt_OTVS300__c;
      accTar.Rental_Cnt_OTVS400__c          = acc2.Rental_Cnt_OTVS400__c==0?null:acc2.Rental_Cnt_OTVS400__c;
      accTar.Rental_Cnt_TB__c               = acc2.Rental_Cnt_TB__c==0?null:acc2.Rental_Cnt_TB__c;
      accTar.Rental_Cnt_USG400__c           = acc2.Rental_Cnt_USG400__c==0?null:acc2.Rental_Cnt_USG400__c;
      //  20200828 LHJ End

      //SWAG-BSC5WP you-20201021 start
      accTar.Asset_Cnt_290MiroBF__c        = acc2.Asset_Cnt_290MiroBF__c==0?null:acc2.Asset_Cnt_290MiroBF__c;
      accTar.Asset_Cnt_290MiroGI__c        = acc2.Asset_Cnt_290MiroGI__c==0?null:acc2.Asset_Cnt_290MiroGI__c;
      accTar.Asset_Cnt_290MiroGI_CF__c     = acc2.Asset_Cnt_290MiroGI_CF__c==0?null:acc2.Asset_Cnt_290MiroGI_CF__c;
      accTar.Asset_Cnt_290MiroGI_Plus__c   = acc2.Asset_Cnt_290MiroGI_Plus__c==0?null:acc2.Asset_Cnt_290MiroGI_Plus__c;
      accTar.Asset_Cnt_CV170__c            = acc2.Asset_Cnt_CV170__c==0?null:acc2.Asset_Cnt_CV170__c;
      accTar.Asset_Cnt_CV190__c            = acc2.Asset_Cnt_CV190__c==0?null:acc2.Asset_Cnt_CV190__c;
      accTar.Asset_Cnt_CV290__c            = acc2.Asset_Cnt_CV290__c==0?null:acc2.Asset_Cnt_CV290__c;
      accTar.Asset_Cnt_ESG400__c           = acc2.Asset_Cnt_ESG400__c==0?null:acc2.Asset_Cnt_ESG400__c;
      accTar.Asset_Cnt_EUME2__c            = acc2.Asset_Cnt_EUME2__c==0?null:acc2.Asset_Cnt_EUME2__c;
      accTar.Asset_Cnt_GFUCT260__c         = acc2.Asset_Cnt_GFUCT260__c==0?null:acc2.Asset_Cnt_GFUCT260__c;
      accTar.Asset_Cnt_OTVS190__c          = acc2.Asset_Cnt_OTVS190__c==0?null:acc2.Asset_Cnt_OTVS190__c;
      accTar.Asset_Cnt_OTVS300__c          = acc2.Asset_Cnt_OTVS300__c==0?null:acc2.Asset_Cnt_OTVS300__c;
      accTar.Asset_Cnt_OTVS400__c          = acc2.Asset_Cnt_OTVS400__c==0?null:acc2.Asset_Cnt_OTVS400__c;
      //20220602 WLIG-CEWDWS you start
      //accTar.Asset_Cnt_TB__c               = acc2.Asset_Cnt_TB__c==0?null:acc2.Asset_Cnt_TB__c;
      accTar.Asset_Cnt_TB_0520__c               = acc2.Asset_Cnt_TB_0520__c==0?null:acc2.Asset_Cnt_TB_0520__c;
      accTar.Asset_Cnt_TB_0535__c               = acc2.Asset_Cnt_TB_0535__c==0?null:acc2.Asset_Cnt_TB_0535__c;
      //20220602 WLIG-CEWDWS you end
      accTar.Asset_Cnt_USG400__c           = acc2.Asset_Cnt_USG400__c==0?null:acc2.Asset_Cnt_USG400__c;
      //SWAG-BSC5WP you-20201021 end
      Date nowDate = Date.today();
      // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 Start
      //Integer iMonth = nowDate.month();
      Date yesterdate = nowDate.addDays(-1);
      Integer iMonth = yesterdate.month();

      //if (iMonth <= 4 || System.Test.isRunningTest()) {
      if (iMonth <= 3 || System.Test.isRunningTest()) {
        // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 End
        accTar.EventCount_M4__c = acc2.ThisYear4__c;
        accTar.EventCount_M5__c = acc2.ThisYear5__c;
        accTar.EventCount_M6__c = acc2.ThisYear6__c;
        accTar.EventCount_M7__c = acc2.ThisYear7__c;
        accTar.EventCount_M8__c = acc2.ThisYear8__c;
        accTar.EventCount_M9__c = acc2.ThisYear9__c;
        accTar.EventCount_M10__c = acc2.ThisYear10__c;
        accTar.EventCount_M11__c = acc2.ThisYear11__c;
        accTar.EventCount_M12__c = acc2.ThisYear12__c;
        // 2018年7月31日 HWAG-B37AKX  by 张玉山   start
        accTar.EventCount_M4_FSE__c = acc2.ThisYear4_FSE__c;
        accTar.EventCount_M5_FSE__c = acc2.ThisYear5_FSE__c;
        accTar.EventCount_M6_FSE__c = acc2.ThisYear6_FSE__c;
        accTar.EventCount_M7_FSE__c = acc2.ThisYear7_FSE__c;
        accTar.EventCount_M8_FSE__c = acc2.ThisYear8_FSE__c;
        accTar.EventCount_M9_FSE__c = acc2.ThisYear9_FSE__c;
        accTar.EventCount_M10_FSE__c = acc2.ThisYear10_FSE__c;
        accTar.EventCount_M11_FSE__c = acc2.ThisYear11_FSE__c;
        accTar.EventCount_M12_FSE__c = acc2.ThisYear12_FSE__c;
        // 2018年7月31日 HWAG-B37AKX  by 张玉山   end
        //   SWAG-BBBAKG     start
        accTar.EventCount_M4_Eng__c = acc2.ThisYear4_Eng__c;
        accTar.EventCount_M5_Eng__c = acc2.ThisYear5_Eng__c;
        accTar.EventCount_M6_Eng__c = acc2.ThisYear6_Eng__c;
        accTar.EventCount_M7_Eng__c = acc2.ThisYear7_Eng__c;
        accTar.EventCount_M8_Eng__c = acc2.ThisYear8_Eng__c;
        accTar.EventCount_M9_Eng__c = acc2.ThisYear9_Eng__c;
        accTar.EventCount_M10_Eng__c = acc2.ThisYear10_Eng__c;
        accTar.EventCount_M11_Eng__c = acc2.ThisYear11_Eng__c;
        accTar.EventCount_M12_Eng__c = acc2.ThisYear12_Eng__c;
        //   HWAG-B37AKX  end
        // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 Start
        // for (Integer i = 1; i < iMonth; i++) {
        for (Integer i = 1; i <= iMonth; i++) {
          // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 End
          accTar.put('EventCount_M' + i + '__c', (Decimal) acc2.get('ThisYear' + i + '__c'));
          // 2018年7月31日 HWAG-B37AKX     start
          accTar.put('EventCount_M' + i + '_FSE__c', (Decimal) acc2.get('ThisYear' + i + '_FSE__c'));
          // 2018年7月31日 HWAG-B37AKX      end
          //   SWAG-BBBAKG     start
          accTar.put('EventCount_M' + i + '_Eng__c', (Decimal) acc2.get('ThisYear' + i + '_Eng__c'));
          //   SWAG-BBBAKG     end
        }
      } else {
        // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 Start
        // for (Integer i = 4; i < iMonth; i++) {
        for (Integer i = 4; i <= iMonth; i++) {
          // LHJ 20180820 CHAN-B3M3GD 由每月初运行改为每周日运行 End
          accTar.put('EventCount_M' + i + '__c', (Decimal) acc2.get('ThisYear' + i + '__c'));
          // 2018年7月31日 HWAG-B37AKX     start
          accTar.put('EventCount_M' + i + '_FSE__c', (Decimal) acc2.get('ThisYear' + i + '_FSE__c'));
          // 2018年7月31日 HWAG-B37AKX  by 张玉山   end
          //   SWAG-BBBAKG     start
          accTar.put('EventCount_M' + i + '_Eng__c', (Decimal) acc2.get('ThisYear' + i + '_Eng__c'));
          //   SWAG-BBBAKG     end
        }
      }



      upsertList.add(accTar);
      
    }

    return upsertList;
  }

  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 start
  //OpdAmountBatch.nextMain('',false); 默认正常执行 1、15~1.31号可执行 每年1月15~1月31号 暂作为自动生成时间段
  //OpdAmountBatch.nextMain('',true);  跳过时间判断 强行执行
  public static List<Account_Number_of_target__c> nextMain(List<sObject> dcList,boolean ManuallyUpdateFlag){

    //下一年度 的生成目标客户 list
    List<Account_Number_of_target__c> upsertNextList = new List<Account_Number_of_target__c>();
    //①进来此方法就说明该战略科室没有下一年度的目标客户
    //②(每年1月15~1月31号 暂作为自动生成时间段) 从下一个财年(155p)起，安排在1月的第一个周六日运行
    //获取当前时间
    Date today = date.today();
    //获取当前年
    Integer year = today.year();
    //写死 每年的1月15日
    Date startDate = date.newInstance(year, 1, 15);
    //写死 每年的2月1日
    Date endDate  = date.newInstance(year, 2, 1);
    
    //转成string 用于比较
    String todayStr = NFMUtil.formatDate2Str(today);
    String startDateStr = NFMUtil.formatDate2Str(startDate);
    String endDateStr = NFMUtil.formatDate2Str(endDate);
    
    //获取昨天时间
    Date yesterday = today.addDays(-1);
    //获取昨天的年份
    Integer year2 = yesterday.year();
    //获取昨天的月份
    Integer month = yesterday.month();
    //判断当前财年
    if (month < 4) {
      year2 -= 1;
    }
    //计算下一年度 
    String ocmNextYear = year2+1 + '年度';
    String ocmNextPeriod = year2+1 - 1867 + 'P';
    
    System.debug('是否有值：' + dcList);

    //每年1.15~1.31 或者 强行执行 才会进入此方法
    if((startDateStr<=todayStr&&todayStr<endDateStr)|| (ManuallyUpdateFlag) ){
      
      //检索下一财年目标客户
      List<Account_Number_of_target__c> accTarList = [select id, Account__c,OCM_Year__c 
        from Account_Number_of_target__c
        where Account__c = :dcList
                           and OCM_Year__c = :ocmNextYear];
      //存放下一年度的目标客户
      Map<Id,Id> accTarMap = new Map<Id,Id>();

      if(accTarList.size()>0){
        for(Account_Number_of_target__c accTar :accTarList){
            accTarMap.put(accTar.Account__c,accTar.Id); //(战略科室id,目标客户id)
        }
        
      }
      //dcList 所有战略科室id
      for (sObject s : dcList) {
        //没有下一年度的目标客户才会生成
        if(accTarMap.containsKey((Id)s.get('Id'))==false){
            Account_Number_of_target__c accNextTar = new Account_Number_of_target__c();
            accNextTar.OCM_Year__c = ocmNextYear;
            accNextTar.OCM_Period__c = ocmNextPeriod;
            accNextTar.Account_HP__c = (Id)s.get('ParentId');
            accNextTar.Account__c = (Id)s.get('Id');
            
            //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 start
            Account acc = (Account) s; 
            accNextTar.AllHostsNumber_first__c = acc.Account2__r.AllHostsNumber__c;
            accNextTar.RivalHostsNumber_first__c = acc.Account2__r.RivalHostsNumber__c;
            //SWAG-C9WCE5 战略科室和目标客户设定页面增加字段  lt 20211228 end

            upsertNextList.add(accNextTar);
          }   
        }
           
    }

    return upsertNextList;
     
  }
  //WLIG-BXQB7J 【委托】【重要】财年目标客户页面自动生成 精琢技术 wql 2021/02/01 end

  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 start
  public static List<Account_Number_of_case__c> nextCaseMain(List<sObject> dcList,String PerformDate,String hpId,Boolean AllAcounts,Boolean ManuallyUpdateFlag){
    //下一年度的 病历数
    List<Account_Number_of_case__c> insertNextList = new List<Account_Number_of_case__c>();
    //每年1月4~1月31号 作为自动生成时间段 直到财年(155p)
    //获取当前时间
    Date today = date.today();
    //获取当前年
    Integer year = today.year();
    //每自然年的1月4日
    Date startDate = date.newInstance(year, 1, 4);
    
    //转成string 用于比较
    String todayStr     = NFMUtil.formatDate2Str(today);
    String startDateStr = NFMUtil.formatDate2Str(startDate);

    //获取昨天时间
    Date yesterday = today.addDays(-1);
    //获取昨天的年份
    Integer year2  = yesterday.year();
    //获取昨天的月份
    Integer month  = yesterday.month();
    //判断当前财年
    // 2022/1/10 ssm 不以财年为维度生成数据
    // if (month < 4) {
    //   year2 -= 1;
    // }
    //计算上一年度
    String ocmNextYear = year2-1 + '年度';
    String ocmNextPeriod = year2-1 - 1867 + 'P';
    System.debug('ocmNextPeriod:'+ocmNextPeriod);
    System.debug('ocmNextYear:'+ocmNextYear);
    String NextPeriod2;
    //判断传进来的年度,有年度就用，没有默认使用上一年度
    if(String.isBlank(PerformDate)){
      PerformDate = ocmNextYear;
      NextPeriod2 = ocmNextPeriod;
    }else{
      String hehe = '年度';
      String NextPeriod1 = PerformDate.removeEndIgnoreCase(hehe);
      Integer a = Integer.valueOf(NextPeriod1);
      NextPeriod2 = a-1867+'P';
    }
    //(医院的 OCSM分类(医院) 为H0或H1,战略科室里面没有病历数) 或者 手动控制台执行
    //查询符合要求的医院下的战略科室并存放于accseMap
    //每一自然年的 1月4号 或者 手动执行 进入判断
    if((startDateStr==todayStr) || String.isNotBlank(PerformDate) || String.isNotBlank(hpId) || (ManuallyUpdateFlag)){
      //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 start
        String query = 'select Id,OCM_Category__c,Name,ParentId,RecordType.DeveloperName from Account where Id = :dcList and Is_Active_Formula__c=\'有效\' ';
        if(!AllAcounts){
          query += ' and (OCM_Category__c=\'H0\' or OCM_Category__c=\'H1\') ';
        }
        List<Account> accseList = Database.Query(query);
      //生成所有医院 SWAG-C66C3P 【委托】 【优先重要】病例数报表调查及需求  wql 2021-09-06 end
        System.debug('accseList:'+accseList);
        Map<string,Account> accseMap = new Map<string, Account>();
        for(Account accse : accseList) {
          accseMap.put(accse.id, accse);
        }
        System.debug('accseMap:'+accseMap);
        
        //检索病历数并存放于anocMap
        List<Account_Number_of_case__c> anocList = [select Id,OCM_Year__c,Account__c
                              from Account_Number_of_case__c
                              where   Account__c = :accseList
                              and    OCM_Year__c = :PerformDate];
        System.debug('anocList:'+anocList);
        Map<string,Account_Number_of_case__c> anocMap = new Map<string, Account_Number_of_case__c>();
        if(anocList.size()>0){
            for(Account_Number_of_case__c anoc : anocList) {
                anocMap.put(anoc.Account__c, anoc);
            }
        }
        System.debug('anocMap:'+anocMap);

        Map<string,Id> RecordTypeMap = new Map<String,Id>();
        RecordTypeMap.put('Department_Class_GI','01210000000QmhoAAC');//戦略科室分類 消化科
        RecordTypeMap.put('Department_Class_ET','01210000000QmhoAAC');//戦略科室分類ET
        RecordTypeMap.put('Department_Class_BF','01210000000QmhyAAC');//戦略科室分類 呼吸科
        RecordTypeMap.put('Department_Class_GS','01210000000QmvDAAS');//戦略科室分類 普外科
        RecordTypeMap.put('Department_Class_URO','01210000000QmutAAC');//戦略科室分類 泌尿科
        RecordTypeMap.put('Department_Class_GYN','01210000000QmuyAAC');//戦略科室分類 婦人科
        RecordTypeMap.put('Department_Class_ENT','01210000000Qmv3AAC');//戦略科室分類 耳鼻喉科
        RecordTypeMap.put('Department_Class_OTH','01210000000Qmv8AAC');//戦略科室分類 その他
        for (Account c : accseList) {
        //判断有无要创建的战略科室下的病历数
            if(anocMap.containsKey(c.Id)==false){
                Account_Number_of_case__c accCaseTar = new Account_Number_of_case__c();
                accCaseTar.OCM_Year__c   = PerformDate;
                accCaseTar.OCM_Period__c = NextPeriod2;
                accCaseTar.Account_HP__c = c.ParentId;
                accCaseTar.Account__c    = c.Id;
                accCaseTar.RecordTypeid  = RecordTypeMap.get(c.RecordType.DeveloperName);
                accCaseTar.Case_name__c  = '科室累计病人数';
                insertNextList.add(accCaseTar);
                System.debug('insertNextList:'+insertNextList);
            }
        }
    }
   return insertNextList;
  }


  //WLIG-BZD684 【委托】H层客户 战略科室 病例数自运行 gwy 2021-03-25 end
  global void finish(Database.BatchableContext BC) {
    // 2020/11/24 songxiaoqi start   把EquipmentSetDailyBatch 放到 opdamountbatch的 finish
    // Database.executeBatch(new EquipmentSetDailyBatch(null), 100);
    //  2020/11/24 songxiaoqi end 
    
    //wangweipeng  2021/07/13 10:07    SWAG-C4P8CM       start
    //每天更新 opd计划表的 if_Newest_HaveOpportunity__c 字段值  
    Id execBTId = Database.executeBatch(new OPDIfNewestHaveOpportunityBatch(), 200);
    //wangweipeng  2021/07/13 10:07    SWAG-C4P8CM     end
  }

}