@isTest
public class MergeAgencyActivityBatchTest {
    public static Account hospital;
    public static Account agency1;
    public static Account agency2;
    public static Account agency3;
    public static String oldAgencyCode;
    public static String newAgencyCode;
    public static String oldAgencyId15;
    public static String newAgencyId15;

    public static Contact contact1;
    public static Contact contact2;
    public static User user1;
    public static User user2;
    public static User user3;

    public static Agency_Hospital_Link__c a1HPLink;
    public static Agency_Hospital_Link__c a2HPLink;
    public static Agency_Hospital_Link__c a3HPLink;

    public static Date monday;
    public static Id Contact_Agency_RT = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();

    // Normal Data
    public static void setupNormalData() {
        makeNormalData(true);
    }
    public static void makeNormalData(boolean withAgencyDetail) {
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // create 病院
            hospital = new Account();
            hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            hospital.Name = 'test hospital_1';
            // create 经销商
            agency1 = new Account();
            agency1.Name = 'test1经销商';
            agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency2 = new Account();
            agency2.Name = 'test2经销商';
            agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency3 = new Account();
            agency3.Name = 'test3经销商';
            agency3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            insert new List<Account> {hospital, agency1, agency2,agency3};
            List<Account> agencyList = [SELECT Name, Management_Code__c
                    FROM Account WHERE (Id = :agency1.Id OR Id = :agency2.Id)
                    ORDER BY ID];
            oldAgencyCode = agencyList[0].Management_Code__c;
            oldAgencyId15 = ('' + agencyList[0].Id).mid(0, 15);
            newAgencyCode = agencyList[1].Management_Code__c;
            newAgencyId15 = ('' + agencyList[1].Id).mid(0, 15);

            // 取引先責任者
            contact1 = new Contact();
            contact1.AccountId = agency1.Id;
            contact1.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact1.FirstName = '責任者';
            contact1.LastName = 'test1经销商';
            contact1.Agency_User__c = true;
            Contact contact12 = new Contact();
            contact12.AccountId = agency1.Id;
            contact12.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact12.FirstName = '社員2';
            contact12.LastName = 'test1经销商';
            contact12.Agency_User__c = true;
            contact2 = new Contact();
            contact2.AccountId = agency2.Id;
            contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact2.FirstName = '責任者';
            contact2.LastName = 'test2经销商';
            contact2.Agency_User__c = true;
            Contact contact22 = new Contact();
            contact22.AccountId = agency2.Id;
            contact22.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact22.FirstName = '社員2';
            contact22.LastName = 'test1经销商';
            contact22.Agency_User__c = true;
            Contact contact23 = new Contact();
            contact23.AccountId = agency2.Id;
            contact23.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact23.FirstName = '社員3';
            contact23.LastName = 'test2经销商';
            contact23.Agency_User__c = true;

            Contact contact3 = new Contact();
            contact3.AccountId = agency3.Id;
            contact3.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact3.FirstName = '社員3';
            contact3.LastName = 'test2经销商';
            contact3.Agency_User__c = true;
            insert new List<Contact> {contact1, contact12, contact2, contact22, contact23,contact3};
            // User (link to ContactId)
            Profile p = [SELECT Id FROM Profile WHERE Name = '901_经销商活动系统'];
            user1 = new User();
            user1.ProfileId = p.Id;
            user1.ContactId = contact1.Id;
            user1.FirstName = 'ユーザー';
            user1.LastName = 'テスト';
            user1.Email = 'test_user1@example.com';
            user1.Username = 'test_user1@sb1.example.com' + System.now().millisecond();
            user1.Alias = 'テユa1';
            user1.CommunityNickname = 'tu1' + System.now().millisecond();
            user1.PortalRole = 'Worker';
            user1.emailencodingkey='UTF-8';
            user1.languagelocalekey='zh_CN';
            user1.localesidkey='ja_JP';
            user1.timezonesidkey='Asia/Shanghai';
            user2 = new User();
            user2.ProfileId = p.Id;
            user2.ContactId = contact2.Id;
            user2.FirstName = 'ユーザー';
            user2.LastName = 'テスト';
            user2.Email = 'test_user2@example.com';
            user2.Username = 'test_user2@sb1.example.com' + System.now().millisecond();
            user2.Alias = 'テユa2';
            user2.CommunityNickname = 'tu2' + System.now().millisecond();
            user2.PortalRole = 'Worker';
            user2.emailencodingkey='UTF-8';
            user2.languagelocalekey='zh_CN';
            user2.localesidkey='ja_JP';
            user2.timezonesidkey='Asia/Shanghai';

            user3 = new User();
            user3.ProfileId = p.Id;
            user3.ContactId = contact3.Id;
            user3.FirstName = 'ユーザー';
            user3.LastName = 'テスト';
            user3.Email = 'test_user3@example.com';
            user3.Username = 'test_user3@sb1.example.com' + System.now().millisecond();
            user3.Alias = 'テユa3';
            user3.CommunityNickname = 'tu3' + System.now().millisecond();
            user3.PortalRole = 'Worker';
            user3.emailencodingkey='UTF-8';
            user3.languagelocalekey='zh_CN';
            user3.localesidkey='ja_JP';
            user3.timezonesidkey='Asia/Shanghai';
            insert new List<User> {user1, user2,user3};
            user2 = [SELECT FirstName, LastName, ContactId, Contact.AccountId, UserRoleId FROM User WHERE Id = :user2.Id];
            System.assertEquals(agency2.Id, user2.Contact.AccountId);
            UserRole rolesAcc = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE PortalAccountId = :agency2.Id
                    AND PortalRole = 'Worker'];
            UserRole rolesUser = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE Id = :user2.UserRoleId];
            System.assertEquals(rolesAcc.PortalAccountId, rolesUser.PortalAccountId);
            System.assertEquals(rolesAcc.PortalRole, rolesUser.PortalRole);
            System.assertEquals(rolesAcc.Name, rolesUser.Name);
            System.assertEquals(rolesAcc.Id, rolesUser.Id);
        }
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // UserにPermissionSetを設定
            PermissionSet ps = [SELECT ID FROM PermissionSet WHERE Name = 'P002_Partner'];
            insert new List<PermissionSetAssignment> {
                new PermissionSetAssignment(AssigneeId = user1.id, PermissionSetId = ps.Id),
                new PermissionSetAssignment(AssigneeId = user2.id, PermissionSetId = ps.Id)
            };
        }

        System.runAs(new User(Id = UserInfo.getUserId())) {
            // 营销关系
            a1HPLink = new Agency_Hospital_Link__c();
            a1HPLink.Name = 'test1经销商医院';
            a1HPLink.Hospital__c = hospital.Id;
            a1HPLink.Agency__c = agency1.Id;
            a1HPLink.Agency_Campaign_Obj__c = true;
            insert a1HPLink;
            a2HPLink = new Agency_Hospital_Link__c();
            a2HPLink.Name = 'test2经销商医院';
            a2HPLink.Hospital__c = hospital.Id;
            a2HPLink.Agency__c = agency2.Id;
            a2HPLink.Agency_Campaign_Obj__c = true;
            insert a2HPLink;

            a3HPLink = new Agency_Hospital_Link__c();
            a3HPLink.Name = 'test2经销商医院01';
            a3HPLink.Hospital__c = hospital.Id;
            a3HPLink.Agency__c = agency3.Id;
            a3HPLink.Agency_Campaign_Obj__c = true;
            insert a3HPLink;

            a3HPLink.Agency_Campaign_Obj__c = false;
            update a3HPLink; 

            if (withAgencyDetail) {
                //.客户人员 (agency1)
                Agency_Contact__c aContact1 = new Agency_Contact__c();
                aContact1.Agency_Hospital__c = a1HPLink.Id;
                aContact1.Type__c = '医生';
                aContact1.Name = 'testAgency医生';
                aContact1.OwnerId = user1.Id;
                Agency_Contact__c aContact2 = new Agency_Contact__c();
                aContact2.Agency_Hospital__c = a1HPLink.Id;
                aContact2.Type__c = '护士';
                aContact2.Name = 'testAgency护士';
                aContact2.OwnerId = user1.Id;
                insert new List<Agency_Contact__c> {aContact1, aContact2};

                // 经销商询价 (agency1)
                Agency_Opportunity__c agency_opportunity1 = new Agency_Opportunity__c();
                agency_opportunity1.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity1.Name = '引合1';
                agency_opportunity1.Hospital_Target__c = hospital.Id;
                agency_opportunity1.Agency__c = agency1.Id;
                agency_opportunity1.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity1.StageName__c = '还没申请预算';
                agency_opportunity1.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity1.OwnerId = user1.Id;
                Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
                agency_opportunity2.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity2.Name = '引合2';
                agency_opportunity2.Hospital_Target__c = hospital.Id;
                agency_opportunity2.Agency__c = agency1.Id;
                agency_opportunity2.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity2.StageName__c = '还没申请预算';
                agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity2.OwnerId = user1.Id;
                insert new List<Agency_Opportunity__c> {agency_opportunity1, agency_opportunity2};

                // .周报一览 (agency1)
                monday = System.today().toStartofWeek().addDays(1);
                if (Test.isRunningTest() == true) {
                    OlympusCalendar__c cal1 = new OlympusCalendar__c(Date__c = monday, ChangeToHoliday__c=false, ChangeToWorkday__c=true);
                    OlympusCalendar__c cal2 = new OlympusCalendar__c(Date__c = monday.addDays(-7), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
                    insert new List<OlympusCalendar__c> {cal1, cal2};
                }

                String dtGTM = String.valueOf(monday);
                String lastWkGTM = String.valueOf(monday.addDays(-7));
                Agency_Report_Header__c agency_report_header1 = WeeklyReportCmp.makeReportHeader(user1.LastName
                        , dtGTM, contact1.Id, contact1.Id + ':' + dtGTM.replaceAll('-', ''));
                agency_report_header1.OwnerId = user1.Id;
                Agency_Report_Header__c agency_report_header2 = WeeklyReportCmp.makeReportHeader(user1.LastName
                        , lastWkGTM, contact1.Id, contact1.Id + ':' + lastWkGTM.replaceAll('-', ''));
                agency_report_header2.OwnerId = user1.Id;
                insert new List<Agency_Report_Header__c> {agency_report_header1, agency_report_header2};
                agency_report_header1 = [SELECT Id, Agency_ID__c FROM Agency_Report_Header__c WHERE Id = :agency_report_header1.Id];
                System.assertEquals(agency1.Id, agency_report_header1.Agency_ID__c);

                // .周报m明细 (agency1)
                ProductTypes__c proType;
                if (Test.isRunningTest() == true) {
                    proType = new ProductTypes__c(Department_Cateogy__c = 'GI;BF;GS;OTH', Name = '260单镜');
                    insert proType;
                } else {
                    proType = [SELECT Id, Department_Cateogy__c, Name FROM ProductTypes__c WHERE Department_Cateogy__c = 'GI;BF;GS;OTH' AND Name = '260单镜'];
                }
                Agency_Report__c agency_report1 = WeeklyReportCmp.makeAgencyReport('GI', '信息收集-日常拜访', agency_report_header1.Id
                        , a1HPLink.Id, contact1.Id, '', dtGTM
                        , proType.Id, '', ''
                        , 'Result', '', '', '', '', '', dtGTM,'');          //zhj 2022-12-21 因为WeeklyReportCmp.makeAgencyReport增加了一个参数SupportNeedsc
                Agency_Report__c agency_report2 = WeeklyReportCmp.makeAgencyReport('GI', '信息收集-日常拜访', agency_report_header2.Id
                        , a1HPLink.Id, contact1.Id, '', lastWkGTM
                        , proType.Id, '', ''
                        , 'Result', '', '', '', '', '', lastWkGTM,'');      //zhj 2022-12-21 因为WeeklyReportCmp.makeAgencyReport增加了一个参数SupportNeedsc
                insert new List<Agency_Report__c> {agency_report1, agency_report2};
            }
        }
    }

    /**
     * @description 
     * 没有AgencyReport时不会报错
     */
    @isTest static void test_emptyAgencyReport() {
        makeNormalData(false);

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
    }

    /**
     * @description 
     * OCSM内部人员所有经销商询价不会修改所有人
     */
    @isTest static void test_OCSMAgencyOpportunity() {
        setupNormalData();

        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合3';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency1.Id;
        aOpp.Agency_Hospital__c = a1HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = UserInfo.getUserId();
        insert aOpp;

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        List<Agency_Opportunity__c> aoppList = [select Id, OwnerId from Agency_Opportunity__c where Name = '::引合3'];
        System.assertEquals(UserInfo.getUserId(), aoppList[0].OwnerId);
    }

    /**
     * @description start error test, forceFlg = false
     * 没有找到 或 有多条 旧经销商 [' + this.oldAgencyCode + '] 的数据
     */
    @isTest static void test_oldAgencyCode() {
        setupNormalData();
        //报错的数据
        Account agency1 = new Account(AgentCode_Ext__c = oldAgencyCode);
        agency1.Name = 'test1经销商';
        agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        insert agency1;

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('没有找到 或 有多条 旧经销商 [' + oldAgencyCode + '] 的数据, size=2', MergeAgencyActivityBatch.messagesForTest[0]);
    }

    /**
     * @description start error test, forceFlg = false
     * 没有找到 或 有多条 新经销商 [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyCode() {
        setupNormalData();
        //报错的数据
        Account agency2 = new Account(AgentCode_Ext__c = newAgencyCode);
        agency2.Name = 'test1经销商';
        agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        insert agency2;

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('没有找到 或 有多条 新经销商 [' + newAgencyCode + '] 的数据, size=2', MergeAgencyActivityBatch.messagesForTest[0]);
    }

    /**
     * @description start error test, forceFlg = false
     * .客户人员 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyContact() {
        setupNormalData();
        //报错的数据
        Agency_Contact__c aContact = new Agency_Contact__c();
        aContact.Agency_Hospital__c = a2HPLink.Id;
        aContact.Type__c = '医生';
        aContact.Name = 'testAgency医生';
        aContact.OwnerId = user2.Id;
        insert aContact;

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('.客户人员 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }

    /**
     * @description start error test, forceFlg = false
     * 经销商询价 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void testStart_newAgencyOpportunity() {
        setupNormalData();
        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合1';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency2.Id;
        aOpp.Agency_Hospital__c = a2HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = user2.Id;
        insert aOpp;

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('经销商询价 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }

    /**
     * @description start error test, forceFlg = false
     * .周报一览 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyReportHeader() {
        setupNormalData();
        //报错的数据
        String dtGTM = String.valueOf(monday);
        Agency_Report_Header__c arh = WeeklyReportCmp.makeReportHeader(user2.LastName
                , dtGTM, contact2.Id, contact2.Id + ':' + dtGTM.replaceAll('-', ''));
        arh.OwnerId = user2.Id;
        insert new List<Agency_Report_Header__c> {arh};

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();

        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('.周报一览 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }

    /**
     * @description start test, forceFlg = true
     * // 5. New Contact 件数, 补了一个Contact1
     * // 7. Agency_Report_Header の結果を確認
     * // 3. Agency_Contact の結果を確認
     * // 4. Agency_Opportunity の結果を確認
     * // 8. Agency_Report の結果を確認
     */
    @isTest static void test_newAgencyOpportunity_Force() {
        setupNormalData();
        //报错的数据, 但是 forceFlg = true, 不报错
        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合1';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency2.Id;
        aOpp.Agency_Hospital__c = a2HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = user2.Id;
        insert aOpp;

        String dtGTM = String.valueOf(monday);
        Agency_Report_Header__c arh = WeeklyReportCmp.makeReportHeader(user2.LastName
                , dtGTM, contact2.Id, contact2.Id + ':' + dtGTM.replaceAll('-', ''));
        arh.OwnerId = user2.Id;
        insert new List<Agency_Report_Header__c> {arh};

        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, true);
        Database.executeBatch(mBatch);
        Test.stopTest();

        // バッチエラーなし
        System.assertEquals(0, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        // 5. New Contact 件数, 补了一个Contact1
        List<Contact> a1ContactList = [SELECT Id, Name, LastName, FirstName, Agency_User__c, AccountId, RecordTypeId FROM Contact WHERE AccountId = :agency1.Id ORDER BY Name];
        List<Contact> a2ContactList = [SELECT Id, Name, LastName, FirstName, Agency_User__c, AccountId, RecordTypeId FROM Contact WHERE AccountId = :agency2.Id ORDER BY Name];
        System.assertEquals(2, a1ContactList.size());
        System.assertEquals(4, a2ContactList.size());
        Contact newContact1;            // batchで作った新Contact
        for (Contact nObj : a2ContactList) {
            if (nObj.LastName == contact1.LastName && nObj.FirstName == contact1.FirstName) {
                newContact1 = nObj;
            }
            System.assertEquals(agency2.Id, nObj.AccountId);
            System.assertEquals(true, nObj.Agency_User__c);
            System.assertEquals(Contact_Agency_RT, nObj.RecordTypeId);
        }
        System.assertNotEquals(null, newContact1, a2ContactList);

        // 7. Agency_Report_Header の結果を確認
        List<Agency_Report_Header__c> arhList = [SELECT Id, Name, OwnerId
                    , Agency__c, Old_Agency__c, Agency_Person2__c, Old_Agency_Person2__c, Old_OwnerId__c
                FROM Agency_Report_Header__c
                WHERE Agency__r.Management_Code__c = :newAgencyCode];
        System.assertEquals(3, arhList.size());
        Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(agency2.Id);
        for (Agency_Report_Header__c nObj : arhList) {
            if (nObj.Id == arh.Id) {
                System.assertEquals(agency2.Id, nObj.Agency__c);
                System.assertEquals(null, nObj.Old_Agency__c);
                System.assertEquals(contact2.Id, nObj.Agency_Person2__c);
                System.assertEquals(null, nObj.Old_Agency_Person2__c);
                System.assertEquals(null, nObj.Old_OwnerId__c);
                System.assertEquals(user2.Id, nObj.OwnerId);
            } else {
                System.assertEquals(agency2.Id, nObj.Agency__c);
                System.assertEquals(agency1.Id, nObj.Old_Agency__c);
                System.assertEquals(newContact1.Id, nObj.Agency_Person2__c);
                System.assertEquals(contact1.Id, nObj.Old_Agency_Person2__c);
                System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
                System.assertEquals(user2.Id, nObj.OwnerId);
            }
            // List<Agency_Report_Header__share> tsList = [SELECT Id, UserOrGroupId
            //         FROM Agency_Report_Header__share WHERE ParentId = :nObj.Id AND RowCause = 'Manual'];
            // System.assertEquals(1, tsList.size());
            // System.assertEquals(grpId, tsList[0].UserOrGroupId);
        }

        // 3. Agency_Contact の結果を確認
        // List<Agency_Contact__c> acList = [SELECT Id, Name, OwnerId
        //             , Old_Agency_Hospital__c, Old_Agency_ID__c, Agency_ID__c, Agency_Hospital__c, Old_OwnerId__c
        //         FROM Agency_Contact__c
        //         WHERE Agency_Hospital__r.Agency__r.Management_Code__c = :newAgencyCode];
        // System.assertEquals(2, acList.size());
        // for (Agency_Contact__c nObj : acList) {
        //     System.assertEquals(agency2.Id, nObj.Agency_ID__c);
        //     System.assertEquals(agency1.Id, nObj.Old_Agency_ID__c);
        //     System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital__c);
        //     System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital__c);
        //     System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
        //     System.assertEquals(user2.Id, nObj.OwnerId);
        // }

        // 4. Agency_Opportunity の結果を確認
        // List<Agency_Opportunity__c> aoppList = [SELECT Id, Name
        //             , Old_OwnerId__c, Old_Agency__c, Old_Agency_Hospital__c, Old_Agency_Hospital_All__c, Old_Agency_Hospital_Target__c, Old_TargetInputKey__c
        //             , OwnerId, Agency__c, Agency_ID__c, Agency_Hospital__c, Agency_Hospital_All__c, Agency_Hospital_Target__c, TargetInputKey__c
        //         FROM Agency_Opportunity__c
        //         WHERE Agency_Hospital_All__r.Agency__r.Management_Code__c = :newAgencyCode];
        // System.assertEquals(3, aoppList.size());
        // for (Agency_Opportunity__c nObj : aoppList) {
        //     if (nObj.Id == aOpp.Id) {
        //         System.assertEquals(agency2.Id, nObj.Agency__c);
        //         System.assertEquals(null, nObj.Old_Agency__c);
        //         System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital_All__c);
        //         System.assertEquals(null, nObj.Old_Agency_Hospital_All__c);
        //         System.assertEquals(null, nObj.Old_OwnerId__c);
        //         System.assertEquals(user2.Id, nObj.OwnerId);
        //    } else {
        //         System.assertEquals(agency2.Id, nObj.Agency__c);
        //         System.assertEquals(agency1.Id, nObj.Old_Agency__c);
        //         System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital_All__c);
        //         System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital_All__c);
        //         System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
        //         System.assertEquals(user2.Id, nObj.OwnerId);
        //     }
        // }

        // 8. Agency_Report の結果を確認
        // List<Agency_Report__c> arList = [SELECT Id, Name
        //             , Old_Agency__c, Old_Agency_Hospital__c, Old_Person_In_Charge2__c
        //             , Agency__c, Agency_ID__c, Agency_Hospital__c, Person_In_Charge2__c
        //         FROM Agency_Report__c WHERE Agency__c = :agency2.Id];
        // System.assertEquals(2, acList.size());
        // for (Agency_Report__c nObj : arList) {
        //     System.assertEquals(agency1.Id, nObj.Old_Agency__c);
        //     System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital__c);
        //     System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital__c);
        //     System.assertEquals(newContact1.Id, nObj.Person_In_Charge2__c);
        //     System.assertEquals(contact1.Id, nObj.Old_Person_In_Charge2__c);
        // }
    }
}