@isTest
private class ContentDocumentTriggerTest {
    @TestSetup
    static void setup(){
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Agency_Contact__c','Contact'});
    } 
    
	
	@isTest 
	static void test_method_one() {

		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;
        }
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert 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;
        upsert section;
        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;
        upsert 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(2023, 12, 30);
        opp.Stock_apply_status__c = '申请中';
        insert opp;

        Opportunity_File__c ofile = new Opportunity_File__c();
        ofile.Name = 'test file';
        ofile.Opportunity__c = opp.Id;
        ofile.Oppor_File_Stage__c = 'G';
        insert ofile;
        System.Test.startTest();
        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
        );
        insert version;
        version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
        ContentDocumentLink link = new ContentDocumentLink();
        link.ContentDocumentId = version.ContentDocumentId;
        link.LinkedEntityId = ofile.Id;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        insert link;
        ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
        delete doc;
        // Attachment att1 = new Attachment(
        //     Name = 'test',
        //     ParentId = ofile.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );
        // System.Test.startTest();
        // insert att1;
        // ContentDocumentLink link1 = new ContentDocumentLink();
        // link1.ContentDocumentId = version.ContentDocumentId;
        // link1.LinkedEntityId = ofile.Id;
        // link1.ShareType = 'V';
        // link1.Visibility = 'AllUsers';
        // insert link1;
        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = ofile.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );
        // insert att;
        
        //delete att1;
        System.Test.stopTest();
	}
	
	//@isTest 
	static void test_method_two() {
		ControllerUtil.EscapeNFM001Trigger = true;
		        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;

        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();
        raObj.Phone_number__c = '1234567890';
        raObj.Loaner_medical_Staff__c = contact2.Id;
        insert raObj;
		
	}


	@isTest 
	static void test_method_3() {
		ControllerUtil.EscapeNFM001Trigger = true;
		        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '草案中';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        Test.setMock(HttpCalloutMock.class, new HttpMock());

            List<Agency_Contact__c> lra = new List<Agency_Contact__c>();
            lra.add(new Agency_Contact__c(
                Aws_Data_Id__c = '123456'
            ));

        System.Test.startTest();
        AWSServiceTool2.EncryptPushCore(Json.serialize(lra),'Agency_Contact__c');
        AWSServiceTool2.EncryptPushFuture(null,null);
        //insert att1;
        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
        );
        insert version;
        version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
        ContentDocumentLink link = new ContentDocumentLink();
        link.ContentDocumentId = version.ContentDocumentId;
        link.LinkedEntityId = hospital.Id;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        insert link;
        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = hospital.Id,
        //     Body = EncodingUtil.base64Decode('test')
            
        // );
        // insert att;
        ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
        delete doc;
        System.Test.stopTest();
		
	}

	@isTest 
	static void test_method_4() {

		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;
        }
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert 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;
        upsert section;
        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;
        upsert 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(2023, 11, 30);
        opp.Stock_apply_status__c = '申请中';
        insert opp;

        OpportunitySpecialApply__c osa = new OpportunitySpecialApply__c();
        osa.Opportunity__c = opp.Id;
        System.Test.startTest();
        insert osa;

        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
        );
        insert version;
        version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
        ContentDocumentLink link = new ContentDocumentLink();
        link.ContentDocumentId = version.ContentDocumentId;
        link.LinkedEntityId = osa.Id;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        insert link;

        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = osa.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );
        // insert att;
        ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
        delete doc;
        System.Test.stopTest();
		
	}

    //  MZY SWAG-BXXBKA  2021-02-19 start
    @isTest 
    static void test_method_5() {
        ControllerUtil.EscapeNFM001Trigger = true;
        OPDPlan__c opd = new OPDPlan__c();
        opd.Status__c = '草案中';
        opd.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        opd.NoOpp_Reason__c = 'HCP对应';
        opd.OPDType__c = '科室';

        insert opd;
        System.Test.startTest();

        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
        );
        insert version;
        version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
        ContentDocumentLink link = new ContentDocumentLink();
        link.ContentDocumentId = version.ContentDocumentId;
        link.LinkedEntityId = opd.Id;
        link.ShareType = 'V';
        link.Visibility = 'AllUsers';
        insert link;

        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = opd.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );

        // insert att;
        System.Test.stopTest();

    }
    @isTest 
    static void test_method_6() {
        ControllerUtil.EscapeNFM001Trigger = true;
        User thisUser = [select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ){
            List<RecordType> rectC = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
            if (rectC.size() == 0) {
                return;
            }
            Account act1 = new Account();
            act1.Name =  'TestAccount01';
            act1.RecordTypeId = rectC[0].Id;

            upsert act1;

            List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
            if (rectCo.size() == 0) {
                return;
            }
            Account act2 = new Account();
            act2.Name =  'TestAccount02';
            act2.Business_Assistant__c = getUser().id;
            act2.RecordTypeId = rectCo[0].Id;
            act2.ParentId = act1.Id;

            upsert act2;

            Consumable_order__c co = new Consumable_order__c();
            co.Name = 'TestCo';
            co.Order_status__c = '草案中';
            co.Order_effective_contact__c = act2.id;

            insert co;
            System.Test.startTest();

            ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.ContentDocumentId = version.ContentDocumentId;
            link.LinkedEntityId = co.Id;
            link.ShareType = 'V';
            link.Visibility = 'AllUsers';
            insert link;

            // Attachment att = new Attachment(
            //     Name = 'test',
            //     ParentId = co.Id,
            //     Body = EncodingUtil.base64Decode('test')
            // );

            // insert att;
            ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
            delete doc;

            System.Test.stopTest();
        }
    }

    @isTest 
    static void test_method_7() {
        ControllerUtil.EscapeNFM001Trigger = true;
        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         = 'TestCompany';
        upsert 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;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;


        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        Opportunity opp = new Opportunity(
               Name='testOpp1',
               StageName='引合',
               CloseDate=Date.today(),
               AccountId=depart.Id,
               Sales_Root__c = '販売店',
               Competitor__c ='A',
               Click_Close_Date__c = null,
               RecordType = rectOpp
        );
        insert opp;

        //注残
        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            DeliveryDate__c = Date.today(),
            ContractNO__c = 'ContractNO1',
            ContractAmount__c = 1000
        );
        insert Sac;

        eSignForm__c es = new eSignForm__c();
        es.Statu_Achievements__c = Sac.id;

        insert es;
        System.Test.startTest();


        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.ContentDocumentId = version.ContentDocumentId;
            link.LinkedEntityId = es.Id;
            link.ShareType = 'V';
            link.Visibility = 'AllUsers';
            insert link;

        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = es.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );

        // insert att;
        System.Test.stopTest();

    }
    /*@isTest 
    static void test_method_8() {
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
        Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());

        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
        // 病院を作る
        Account hospital = new Account();
        String rtId = [SELECT ID FROM RecordType WHERE DeveloperName = 'HP'].Id;
        hospital.recordtypeId = rtId;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;   
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        String rtId1 = [SELECT ID FROM RecordType WHERE DeveloperName = 'Department_OTH'].Id;
        dep.recordtypeId = rtId1;
        dep.Name = 'test dep1';
        dep.AgentCode_Ext__c = '9999999';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        OPDPlan__c opd = new OPDPlan__c();
        opd.Status__c = '计划中';
        opd.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        opd.NoOpp_Reason__c = 'HCP对应';
        opd.OPDType__c = '科室';
        insert opd;

        Rental_Apply__c  rap = new Rental_Apply__c();
        rap.Name = 'testra';
        rap.Product_category__c = 'GI';
        rap.Hospital__c = hospital.Id;
        rap.Strategic_dept__c = strategicDep[0].Id;
        rap.Account__c = dep.Id;
        rap.demo_purpose2__c = '试用（无询价）';
        rap.Request_shipping_day__c = Date.toDay();
        rap.Request_return_day__c = Date.toDay();
        rap.OPDPlan__c = opd.id;
        rap.Demo_purpose1__c ='产品试用';
        rap.direct_send__c = '医疗机构';
        rap.Hope_Lonaer_date_Num__c = 16;
        rap.OwnerId = getUser().Id;
        rap.Loaner_received_staff__c = '王五';
        rap.Loaner_received_staff_phone__c = '110';
        rap.direct_shippment_address__c = '北京市';
        rap.Phone_number__c = '1234567890';

        insert rap;

        System.Test.startTest();

        Attachment att = new Attachment(
            Name = 'test',
            ParentId = rap.Id,
            Body = EncodingUtil.base64Decode('test')
        );

        insert att;
        System.Test.stopTest();

    }*/

    // 20230206  lt  start 
    @isTest 
    static void test_method_9() {
        ControllerUtil.EscapeNFM001Trigger = true;
        //新建省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '草案中';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        // hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
        
        Account acc = new Account();
        acc.RecordTypeId = rectCo.Id;
        acc.Name = 'HP test1';
        acc.Is_Active__c = '有効';
        insert acc;

        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = acc.Id;
        ada.ChangeReason__c = '地址错误';
        ada.State_Master__c = al.Id;
        insert ada;

        System.Test.startTest();

        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.ContentDocumentId = version.ContentDocumentId;
            link.LinkedEntityId = hospital.Id;
            link.ShareType = 'V';
            link.Visibility = 'AllUsers';
            insert link;

            ContentDocument doc = new ContentDocument();
            doc.Id = version.ContentDocumentId;
            doc.Description = '123';
            update doc;

        // Attachment att = new Attachment(
        //     Name = 'test',
        //     ParentId = hospital.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );
        // insert att;
        // att.Name = 'test01';
        // update att;

        ContentVersion version1 = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version1;
            version1 = [select ContentDocumentId from ContentVersion where Id =: version1.Id];
            ContentDocumentLink link1 = new ContentDocumentLink();
            link1.ContentDocumentId = version1.ContentDocumentId;
            link1.LinkedEntityId = ada.Id;
            link1.ShareType = 'V';
            link1.Visibility = 'AllUsers';
            insert link1;

            ContentDocument doc1 = new ContentDocument();
            doc1.Id = version1.ContentDocumentId;
            doc1.Description = '123';
            update doc1;

        // Attachment att1 = new Attachment(
        //     Name = '2test',
        //     ParentId = ada.Id,
        //     Body = EncodingUtil.base64Decode('test')
        // );
        // insert att1;
        // att1.Name = '2test01';
        // update att1;
        System.Test.stopTest();

    }

    @isTest 
    static void test_method_10() {
        ControllerUtil.EscapeNFM001Trigger = true;
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        // 病院を作る
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'test hospital';
            insert hospital;
        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
            // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=dep.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
            );
        insert opp1;
        
        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '草案中';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;
        // TestDataUtility.CreatePIPolicyConfiguration('Rental_Apply__c');
        TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Rental_Apply__c','Rental_Apply__cV2'});
        Rental_Apply__c raObj1 = new Rental_Apply__c();
        raObj1.Rental_Start_Date__c = Date.today().addDays(1);
        raObj1.Request_return_day__c=Date.today().addDays(5);
        raObj1.DataMigration_Flag__c = true;
        raObj1.demo_purpose2__c = 'ceshi';
        raObj1.Follow_UP_Opp__c = opp1.Id;
        insert raObj1;
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Rental_Start_Date__c = Date.today().addDays(1);
        raObj.Request_return_day__c=Date.today().addDays(5);
        raObj.DataMigration_Flag__c = true;
        raObj.demo_purpose2__c = '试用（有询价）';
        raObj.Old_Rental_Apply__c = raObj1.id;
        raObj.Follow_UP_Opp__c = opp1.Id;
        raObj.Status__c = '草案中';
        raObj.OPDPlan__c = oPDPlan0.id;
        insert raObj;
        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        raesObj.Rental_Apply__c = raObj.Id;
        raesObj.Cancel_Select__c = false;
        raesObj.Shipment_request_time__c = Date.toDay();
        raesObj.Rental_Start_Date__c = Date.toDay();
        raesObj.Rental_End_Date__c = Date.toDay();
        raesObj.IndexFromUniqueKey__c = 1;
        raesObj.UniqueKey__c = '1::1';
        raesObj.DataMigration_Flag__c =true;
        insert raesObj;
         // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj1.Rental_Apply__c = raObj.Id;
       // raesdObj1.Rental_Num__c = 1;
        raesdObj1.Queue_Number__c = null;
        raesdObj1.Is_Body__c = true;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.UniqueKey__c = '1:'+ raesObj.Id +  ':1';
        raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObj1.Product_category_text__c = 'GI';
        raesdObj1.Equipment_Type_text__c = '产品试用';
        raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj1.Fixture_Model_No_text__c ='n05';
        insert raesdObj1;

        raesObj.First_RAESD__c = raesdObj1.Id;
        update raesObj;
        Rental_Apply_Fault__c ra = new Rental_Apply_Fault__c();
        ra.Name = '123';
        ra.FaultDesc__c = '456';
        ra.Reply_message__c = '789';
        ra.Rental_Apply_Equipment_Set_Detail__c = raesdObj1.Id;
        insert ra;
        System.Test.startTest();

        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.ContentDocumentId = version.ContentDocumentId;
            link.LinkedEntityId = ra.Id;
            link.ShareType = 'V';
            link.Visibility = 'AllUsers';
            insert link;
            ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
            delete link;
            delete doc;
        System.Test.stopTest();

    }

    @isTest 
    static void test_method_11() {
        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;
        }
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert 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;
        upsert section;
        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;
        upsert 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(2023, 11, 30);
        opp.Stock_apply_status__c = '申请中';
        insert opp;


        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            DeliveryDate__c = Date.today(),
            ContractNO__c = 'ContractNO1',
            ContractAmount__c = 0
        );
        insert Sac;

        OpportunityFileOrder__c opportunityFileOrder = new OpportunityFileOrder__c();
        opportunityFileOrder.Name  = 'test';
        opportunityFileOrder.StatuAchievements__c = Sac.Id;
        opportunityFileOrder.OpporFileStage__c = 'G';

        insert opportunityFileOrder;
        System.Test.startTest();

        ContentVersion version = new ContentVersion(
            Title = 'test',
            VersionData = EncodingUtil.base64Decode('test'),
            ContentLocation = 's',
            PathOnClient = 'test.txt'
            );
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.ContentDocumentId = version.ContentDocumentId;
            link.LinkedEntityId = opportunityFileOrder.Id;
            link.ShareType = 'V';
            link.Visibility = 'AllUsers';
            insert link;
            ContentDocument doc = [select Id from ContentDocument where Id =: version.ContentDocumentId limit 1];
        delete doc;
        System.Test.stopTest();

    }



    // 20230206  lt  end

    private static User getUser() {
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User usr = new User(Test_staff__c = true, LastName = 'TestZY', FirstName = 'TestZY',
                                Alias = 'zy', CommunityNickname = 'TestMiao', Email = 'TestZY@test.com',
                                Username = 'Test' + timenow + '@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP',
                                TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = System.Label.ProfileId_SystemAdmin,
                                Province__c = '北京市');
        insert usr;
        return usr;
    }

    // MZY SWAG-BXXBKA  2021-02-19 end

     class HttpMock implements HttpCalloutMock{   
            public HTTPResponse respond(HTTPRequest request) {
            // 创建一个假的回应
            System.debug('------------------------------------------------------');
            HttpResponse response = new HttpResponse();
            string body = '';
            system.debug(request.getEndpoint());
            if(request.getEndpoint().contains('token')){
                system.debug('url=token');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": "freqfewqfewewfewfew", "status": "", "success": true, "timestamp": 0, "txId": "" }';          
            } else if(request.getEndpoint().contains('insert')){
                system.debug('url=Insert');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": [ { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" } ], "status": "", "success": true, "timestamp": 0, "txId": "" }';          
            } else if(request.getEndpoint().contains('update')){
                system.debug('url=update');
                response.setHeader('Content-Type', 'application/json');
          body='{ "message": "", "object": [ { "dataId": "123456", "directShippmentAddress": "", "directShippmentAddressEncrypt": "", "isDelete": 0, "phoneNumber": "", "phoneNumberEncrypt": "", "sfRecordId": "a2R1m0000007BPD" } ], "status": "", "success": true, "timestamp": 0, "txId": "" }';          
            } else{
              
            }
            
            response.setBody(body);
            response.setStatus('OK');
            response.setStatusCode(200);
            return response;
            // }
        }
    }
}