@isTest
private class DealerInquiryModifyStateControllerTest {
  @TestSetup
  static void init() {
    List<RecordType> rectCo = [
      SELECT Id
      FROM RecordType
      WHERE IsActive = TRUE AND SobjectType = 'Account' AND Name = '病院'
    ];
    if (rectCo.size() == 0) {
      return;
    }
    List<RecordType> rectSct = [
      SELECT Id
      FROM RecordType
      WHERE
        IsActive = TRUE
        AND SobjectType = 'Account'
        AND Name = '戦略科室分類 呼吸科'
    ];
    if (rectSct.size() == 0) {
      return;
    }
    List<RecordType> rectDpt = [
      SELECT Id
      FROM RecordType
      WHERE
        IsActive = TRUE
        AND SobjectType = 'Account'
        AND Name = '診療科 消化科'
    ];
    if (rectDpt.size() == 0) {
      return;
    }

    // テストデータ

    Account company = new Account();
    company.RecordTypeId = rectCo[0].Id;
    company.Name = 'NFM007TestCompany';
    insert company;
    Account section = new Account();
    section.RecordTypeId = rectSct[0].Id;
    section.Name = '*';
    section.Department_Class_Label__c = '消化科';
    section.ParentId = company.Id;
    section.Hospital_Department_Class__c = company.Id;
    insert section;
    // Test.startTest();
    Account depart = new Account();
    depart.RecordTypeId = rectDpt[0].Id;
    depart.Name = '*';
    depart.Department_Name__c = 'NFM007TestDepart';
    depart.ParentId = section.Id;
    depart.Department_Class__c = section.Id;
    depart.Hospital__c = company.Id;
    insert depart;

    Opportunity opp = new Opportunity();
    opp.AccountId = depart.Id;
    opp.Department_Class__c = section.Id;
    opp.Hospital__c = company.Id;
    opp.SAP_Send_OK__c = false;
    opp.Name = 'GZ-SP-NFM007_1';
    opp.Trade__c = '内貿';
    opp.StageName = '引合';
    opp.CloseDate = date.newinstance(2022, 11, 30);
    opp.Stock_apply_status__c = '申请中';
    insert opp;
    Test.startTest();
    Account account2 = new Account();
    account2.Name = 'test1经销商';
    account2.RecordTypeId = '01210000000Qem1';
    insert account2;

    Contact contact1 = new Contact();
    contact1.AccountId = account2.Id;
    contact1.FirstName = '責任者';
    contact1.LastName = 'test1医院';
    contact1.Agency_User__c = true;
    insert contact1;
    List<Contact> contactlist = [
      SELECT Id, FirstName
      FROM Contact
    ];

    System.assertEquals('責任者', contactlist[0].FirstName);

    //
    User user = new User();
    Profile p = [SELECT Id FROM Profile WHERE Name = '901_经销商活动系统'];
    user.ProfileId = p.Id;
    user.ContactId = contact1.Id;
    user.FirstName = 'ユーザー';
    user.LastName = 'テスト';
    user.Email = 'test_user@example.com';
    user.emailencodingkey = 'UTF-8';
    user.languagelocalekey = 'zh_CN';
    user.localesidkey = 'ja_JP';
    user.timezonesidkey = 'Asia/Shanghai';
    user.Username = 'test_user@example.com';
    user.Alias = 'テユ';
    user.CommunityNickname = 'テストユーザー';
    user.SalesManager__c = UserInfo.getUserId();

    insert user;
    List<user> users = [
      SELECT Id, Name, LastName, FirstName
      FROM User
      WHERE LastName = 'テスト'
    ];
    //

    Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    agency_hospital_link.Name = 'test1代理店医院';
    agency_hospital_link.Hospital__c = company.Id;
    agency_hospital_link.Agency__c = account2.Id;
    agency_hospital_link.Agency_Campaign_Obj__c = true;
    agency_hospital_link.OwnerId = user.Id;
    insert agency_hospital_link;
    //

    //System.assertEquals('テスト',users[0].LastName);
    System.assertEquals('ユーザー', users[0].FirstName);

    Id rtId = [
      SELECT Id, DeveloperName
      FROM RecordType
      WHERE
        IsActive = TRUE
        AND SobjectType = 'Agency_Opportunity__c'
        AND DeveloperName = 'Opportunity'
    ]
    .Id;
    // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
    Agency_opportunity__c agency_opportunity = new Agency_opportunity__c();
    agency_opportunity.RecordTypeId = rtId;
    agency_opportunity.Name = '引合1';
    agency_opportunity.Hospital_Target__c = company.Id;
    agency_opportunity.Agency__c = depart.Id;
    agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
    agency_opportunity.StageName__c = '还没申请预算';
    agency_opportunity.OwnerId = user.Id;
    agency_opportunity.Change_To_Opportunity_T__c = '::CF-HQ290I';
    agency_opportunity.Sales_Manager__c = UserInfo.getUserId();

    insert agency_opportunity;
    Test.stopTest();
  }

  static testMethod void myUnitTest_SortKey() {
    Test.startTest();
    Test.setMock(WebServiceMock.class, new NFMTest_Mock());
    Test.stopTest();
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);

    // 初始化测试
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();

    controller.init();
    //System.assertEquals(1, controller.oppRecords.size());验证预期值和结果值正确性
    // 排序测试
    // Test.startTest();
    // Test.stopTest();
    controller.sortKey = '1';
    controller.sortKey = '1';
    controller.sortTable();
    controller.sortKey = '1';
    controller.sortKey = '0';
    controller.sortTable();
    // Test.stopTest();
  }
  static testMethod void myUnitTest_AccSearchAndAoosearch() {
    Test.startTest();
    Test.setMock(WebServiceMock.class, new NFMTest_Mock());
    Test.stopTest();
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    // Test.startTest();

    controller.init();
    //文本输入框检索
    //1:经销商医院
    controller.accSearch = '洪洞县中医院';
    controller.chick();
    //2:担当人
    controller.aooSearch = '王契琪';
    controller.chick();
    // Test.stopTest();
  }
  static testMethod void myUnitTest_numtext_Equals() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    // 检索数据字段测试
    //1:检索经销商编码 = 151P-00001327
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '=';
    controller.numtext = '151P-00001327';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '<>';
    controller.numtext = '151P-00001327';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_numtext_Starts_with() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '<';
    controller.numtext = '151P-00001327';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'starts with';
    controller.numtext = '151P-00001327';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_contains() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //2:检索经销商编码 包含 151P
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'contains';
    controller.numtext = '151P';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'notcontains';
    controller.numtext = '151P';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_null_contains() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //3:检索条件含空格
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'contains';
    controller.numtext = '151P 150P';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_null_notcontains() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'notcontains';
    controller.numtext = '151P 150P';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_AllNull_contains() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'contains';
    controller.numtext = ' ';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_AllNull_notcontains() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = 'notcontains';
    controller.numtext = ' ';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '=';
    controller.numtext = ' ';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_tarts_with() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    Test.startTest();
    controller.init();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '<>';
    controller.numtext = ' ';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = ' starts with ';
    controller.numtext = ' ';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTest_Equals_D() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //4:检索条件含逗号
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '=';
    controller.numtext = '151P-00001327,151P-00001343';
    controller.chick();
    controller.numtext1 = 'Agency_Opportunity_No__c';
    controller.numtext2 = '<>';
    controller.numtext = '151P-00001327,151P-00001343';
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTestTime01() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //检索日期测试
    controller.tmpAO = new Agency_Opportunity__c();
    controller.tmpBO = new Agency_Opportunity__c();
    controller.timetext1 = 'Created_Day__c';
    controller.timetext2 = '=';
    controller.tmpAO.MaxActivityDate__c = date.valueOf('2018-12-12');
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-12');
    controller.chick();

    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-09');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-19');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-09');
    controller.chick();
    // Test.startTest();
    controller.tmpAO.MaxActivityDate__c = date.valueOf('2018-12-02');
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-12');
    controller.chick();

    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-09');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-19');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-09');
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTestTime02() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    controller.tmpAO = new Agency_Opportunity__c();
    controller.tmpBO = new Agency_Opportunity__c();
    controller.timetext1 = 'Created_Day__c';
    controller.timetext2 = '=';
    controller.tmpAO.MaxActivityDate__c = date.valueOf('2018-02-12');
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-12');
    controller.chick();

    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-09');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-19');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-09');
    controller.chick();
    // Test.startTest();
    controller.tmpAO.MaxActivityDate__c = date.valueOf('2018-02-02');
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-12');
    controller.chick();

    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-12-09');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-19');
    controller.chick();
    controller.tmpBO.MaxActivityDate__c = date.valueOf('2018-02-09');
    controller.chick();
    Test.stopTest();
  }
  static testMethod void myUnitTestTimeSave() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    Test.startTest();
    controller.init();
    controller.oppRecords[0].changeFlg = '1';
    controller.save();
    Test.stopTest();
  }
  static testMethod void myUnitTestTimeSave_1() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //保存时状态changeFlgRt为1
    controller.oppRecords[0].changeFlgRt = '1';
    controller.save();
    Test.stopTest();
  }
  static testMethod void myUnitTestTime_LimitsAndOppCount() {
    PageReference page = new PageReference('/apex/DealerInquiryModifyState');
    System.Test.setCurrentPage(page);
    DealerInquiryModifyStateController controller = new DealerInquiryModifyStateController();
    // 初始化测试
    Test.startTest();
    controller.init();
    //数据显示条数
    controller.limits = '10';
    controller.searchOppInner();
    //oppLimit and oppCount
    controller.oppCount = 1000;
    controller.init();
    Test.stopTest();
  }
}
