@isTest
private class ESignFormHandlerTest {
    static testMethod void testMethod1() {
    	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, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
         List<RecordType> rectAs = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '备品'];
        if (rectAs.size() == 0) {
            return;
        }

     
        // insert hospital
        Account company1 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        company1.OCM_man_province_txt__c = '江西';
        List<Account> hps = new Account[]{company1};
        insert hps;
          
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        depart1.OCM_man_province_txt__c = '江西';
        
        insert new Account[] {depart1};

        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'HistoryPrd1';
        prd1.ProductCode            = 'HistoryPrd1';
        prd1.Repair_Product_Code__c = 'HistoryPrd1_RP';
        prd1.Name                   = 'HistoryPrd1';
        prd1.Manual_Entry__c        = false;
        prd1.Extend_new_product_gurantee__c = true;
        prd1.Extend_Gurantee_Start__c = Date.today().addDays(-60);
        prd1.Extend_Gurantee_End__c = Date.today().addDays(60);
        prdList.add(prd1);
        insert prdList;
    
        Asset ast = new Asset();

        ast.RecordTypeId = rectAs[0].Id;
        ast.SerialNumber = 'abcdefg';
        ast.Name = '测试资产1';
        ast.AccountId              = depart1.Id;
        ast.Department_Class__c    = dc1s[0].Id;
        ast.Hospital__c            = company1.Id;
        ast.Product2Id             = prd1.Id;
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        ast.Manage_type__c = '个体管理';
        ast.FirstApproveDate_old__c = Date.today().addDays(-30);

        Oly_TriggerHandler.bypass('AssetHandler');

        insert ast;
        
        Asset ast1 = [select RecordTypeID__c from Asset where Id =: ast.Id];
        
        String RecordTypeID = ast1.RecordTypeID__c;
           
        System.assertEquals(RecordTypeID , ast.RecordTypeId);

        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=depart1.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 eSignForm = new eSignForm__c();
    	eSignForm.Statu_Achievements__c = Sac.Id;
    	eSignForm.Name = '::测试电子签收单';

    	insert eSignForm;

        eSignFormEntry__c eSignFormEntry1 = new eSignFormEntry__c();
        eSignFormEntry1.Name = '::电子签收单录入表1';
        eSignFormEntry1.eSignForm__c = eSignForm.Id;
        eSignFormEntry1.entryType__c = '经销商收货';
        
        insert eSignFormEntry1;

    	eSignFormEntry__c eSignFormEntry2 = new eSignFormEntry__c();
    	eSignFormEntry2.Name = '::电子签收单录入表2';
    	eSignFormEntry2.eSignForm__c = eSignForm.Id;
    	eSignFormEntry2.entryType__c = '经销商收货';
        eSignFormEntry2.IsSubmit__c = true;
    	// eSignFormEntry2.createdDate = Date.today().addDays(-2);
    	insert eSignFormEntry2;


        //新建电子签收单明细
        eSignFormLineItem__c eSignFormLineItem = new eSignFormLineItem__c();
        eSignFormLineItem.Name = '::电子签收单明细';
        eSignFormLineItem.eSignForm__c = eSignForm.Id;

        insert eSignFormLineItem;

        //新建电子签收单明细录入表

        eSignFormLineItemEntry__c eSignFormLineItemEntry1 = new eSignFormLineItemEntry__c();
        eSignFormLineItemEntry1.Name = '::电子签收单明细录入1';
        eSignFormLineItemEntry1.eSignFormEntry__c =eSignFormEntry1.Id;
        eSignFormLineItemEntry1.eSignFormLineItem__c = eSignFormLineItem.Id;

        insert eSignFormLineItemEntry1;

        eSignFormLineItemEntry__c eSignFormLineItemEntry2 = new eSignFormLineItemEntry__c();
        eSignFormLineItemEntry2.Name = '::电子签收单明细录入2';
        eSignFormLineItemEntry2.eSignFormEntry__c =eSignFormEntry2.Id;
        eSignFormLineItemEntry2.eSignFormLineItem__c = eSignFormLineItem.Id;

        insert eSignFormLineItemEntry2;

        eSignForm.finalUpadteFrom__c = eSignFormEntry2.Id;
        update eSignForm;

        
    }
    // 20230302 ljh add 进口单证数据 测试类
    // 关联进口单证，首先触发 DNDetailsHandler 不生成生成数据 按钮生成 大部分代码可以覆盖到
    static testMethod void testMethod3() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());

        Oly_TriggerHandler.bypass(DNDetailsHandler.class.getName());// 首先触发 DNDetailsHandler 不生成生成数据 按钮生成 大部分代码可以覆盖到

        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;
        }
        //签收单最终用户记录类型
        List < RecordType > recordTypeList = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '戦略科室分類 呼吸科'
        ];
        if (recordTypeList.size() == 0) {
            return;
        }
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        upsert 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;
        upsert al2;
        // 产品
        Product2 prd = new Product2();
        prd.ProductCode_Ext__c = 'N2656630';
        prd.ProductCode = 'N2656630';
        prd.Name = 'N2656630';
        prd.Manual_Entry__c = false;
        upsert prd;
        //客户医院
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'NFM008TestCompany';
        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 = 'NFM008TestDepart';
        depart.ParentId = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c = company.Id;
        upsert depart;
        //签收单最终用户记录类型
        Account recordType = new Account();
        recordType.RecordTypeId = recordTypeList[0].Id;
        recordType.Name = '*';
        recordType.Department_Class_Label__c = '消化科';
        recordType.ParentId = company.Id;
        recordType.Hospital_Department_Class__c = company.Id;
        upsert recordType;
        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);
        upsert 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 = 100);
        insert Sac;
        //保有设备
        Asset assnew = new Asset(Asset_Owner__c = 'Olympus');
        assnew.Backorder__c = Sac.Id;
        // assnew.Order_number__c = Sac.Id;
        assnew.RecordTypeId = System.Label.Asset_RecordType;
        assnew.SerialNumber = '22K';
        assnew.Name = 'NA-201SX-4021:一次性使用吸引活检针';
        assnew.Product2Id = prd.Id;
        assnew.Hospital__c = company.Id;
        assnew.Department_Class__c = section.Id;
        assnew.AccountId = depart.Id;
        assnew.Asset_Owner__c = '医院资产';
        assnew.Quantity = 1;
        assnew.Status = '有库存';
        assnew.Manage_type__c = '个体管理';
        assnew.Internal_asset_location__c = '北京 备品中心';
        assnew.Loaner_accsessary__c = true;
        assnew.Delete_Flag__c = false;
        assnew.Freeze_sign__c = false;
        assnew.Out_of_wh__c = 0;
        assnew.CIC_call_back_check__c = true;
        assnew.Product_Serial_No__c = 'N2656630:22K(KVWMX)';
        
        upsert assnew;
        //发货DN
        Statu_Achievements_DN__c statuAchievementsDN = new Statu_Achievements_DN__c();
        statuAchievementsDN.Statu_Achievements__c = Sac.Id;
        statuAchievementsDN.Name = '2021082049';
        statuAchievementsDN.endUser__c = recordType.Id;
        upsert statuAchievementsDN;
        //发货DN明细
        Statu_Achievements_DN_details__c statuAchievementsDNDetails = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails.Statu_Achievements_DN__c = statuAchievementsDN.Id;
        statuAchievementsDNDetails.Name = 'z1c200000292wse';
        statuAchievementsDNDetails.asset__c = assnew.Id;
        statuAchievementsDNDetails.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails.TracingCode_Raw__c = 'KVWMX'; // ※
        upsert statuAchievementsDNDetails;
      
        // 签收单
        //新建电子签收单
        eSignForm__c eSignForm = new eSignForm__c();
        eSignForm.Statu_Achievements__c = Sac.Id;
        eSignForm.Statu_Achievements_DN__c = statuAchievementsDN.Id;
        eSignForm.Name = '::测试电子签收单';
        eSignForm.DNName__c = '2021082049';
        eSignForm.AccessoriesQualified__c = true;
        eSignForm.handleOpinionAgency__c = '哇哈哈哈哈哈哈哈哈';
        eSignForm.agencyScanDay__c = Date.today();
        eSignForm.agencySignUpDate__c =Date.today();
        eSignForm.agencyConfirmDate__c = Date.today();

        insert eSignForm;

        SetFrameNumManageWebService.insertFrameNumManag(statuAchievementsDN.Id,eSignForm.Id);// 大部分主要代码 

        delete statuAchievementsDNDetails;
        //发货DN明细
        Statu_Achievements_DN_details__c statuAchievementsDNDetails1 = new Statu_Achievements_DN_details__c();
        statuAchievementsDNDetails1.Statu_Achievements_DN__c = statuAchievementsDN.Id;
        statuAchievementsDNDetails1.Name = 'z1c200000292wse';
        statuAchievementsDNDetails1.asset__c = assnew.Id;
        statuAchievementsDNDetails1.SerialNoorLotNo_Raw__c = '22K'; // ※
        statuAchievementsDNDetails1.TracingCode_Raw__c = 'KVWMX'; // ※
        upsert statuAchievementsDNDetails1;

    }



}