@isTest
private class NFM009ControllerTest {

    private static OpportunityTeamMember oppMemTestCase(String oppId, String userId, String roleName) {
        OpportunityTeamMember target = new OpportunityTeamMember(
                OpportunityId = oppId, UserId = userId, TeamMemberRole = roleName);
        return target;
    }

    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    
    static testMethod void test_method_NFM009CT() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }

        Account myAccount2 = new Account(Name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|2|4,Test02|3|5');
        insert myAccount2;

        Product2 product = new Product2( Name='テスト商品',
                            ProductCode='Test01',
                            Asset_Model_No__c = 'Test01',
                            SFDA_Status__c = '有効',
                            Product_Status__c = '正常销售',
                            SFDA_Approbated_Status__c = '有效',
                            Category3__c = 'CDS',
                            Category4__c = 'OER',
                            Category5__c = '其它',
                            Dealer_special_Object__c = true,
                            SFDA_Approbation_No__c = 'Test001',
                            Packing_list_manual__c = 1,
                            SFDA_Expiration_Date__c= Date.today(),
                            Intra_Trade_List_RMB_1__c = 1000,
                            Intra_Trade_List_RMB_Date1__c = Date.today().addDays(-1),
                            Intra_Trade_List_RMB_Date2__c = Date.today().addDays(-1),
                            Intra_Trade_List_RMB_End_Date1__c = Date.today().addDays(1)
                            );

        insert product;

        /* 注册证相关信息 */
        //医疗器械经营许可证
        License_Information__c linc = new License_Information__c( name='Test20181204',
                                                LicenseType__c = '医疗器械经营许可证',
                                                BusinessLicense__c = '20180522',
                                                ValidFrom__c = date.newinstance(2018, 05, 22),
                                                ValidTo__c = date.newinstance(2088, 05, 22),
                                                Scope3__c = '6815;6822;6823;6825',
                                                LicenseAndAccount__c = myAccount2.Id
                                                );
        insert linc;
        //System.assertEquals('123',linc.ScopeKey__c);
        //产品注册证
        Product_Register__c prc = new Product_Register__c(Name = '国械注进20162220210',
                                                MedPrdClass__c = '3',
                                                ValidFrom__c = date.newinstance(2018, 07, 22),
                                                ValidTo__c = date.newinstance(2028, 07, 22),
                                                RegisterNoClass_Old__c = '6815',
                                                RegisterNoClass_New__c = '6815',
                                                RegisterNoStatus__c = '有效'
                                                );
        insert prc;
        //新旧关系对照表
        Product_Register_contrast__c prcc = new Product_Register_contrast__c(
                                                Name = 'Test20181204',
                                                Register_new__c = '314',
                                                Register_old__c = '36815'
                                                );
        insert prcc;
        //产品-产品注册证关系  prod01
        Product_Register_Link__c  prlc = new Product_Register_Link__c(
                                                Product2__c = product.Id,
                                                Product_Register__c = prc.Id
                                                );
        insert prlc;

        product = [select id,SFDA_Approbated_Status__c,SFDA_Status__c from Product2 where id = : product.Id];
        System.assertEquals('有効', product.SFDA_Status__c);

        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        String userId = UserInfo.getUserId();

        Opportunity opp = new Opportunity();
        opp.Name='aiueo';
        opp.StageName='引合';
        opp.Trade__c = '内貿';
        opp.CloseDate=Date.today();
        opp.CurrencyIsoCode = 'CNY';
        opp.Estimation_List_Price__c = 100;
        opp.Wholesale_Price__c = 101;
        opp.Dealer_Final_Price__c = 102;
        opp.OCM_Agent1_Price__c = 103;
        opp.Stocking_Price__c = 104;
        opp.Estimation_No__c = '105';
        opp.Estimation_Name__c = '106';
        opp.Estimation_Id__c = '107';
        opp.Opportunity_No__c = 'BJ-GI-SH0653328';
        opp.Close_Forecasted_Date__c =Date.today().addMonths(1);
        insert opp;

        

        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.Id__c = '107';
        oli.SFDA_Status__c = '有效';
        oli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oli.ListPrice__c = 112;
        oli.Quantity = 113;
        oli.UnitPrice = 114;
        oli.UnitPrice__c = 115;
        oli.Qty_Unit__c = '116';
        oli.Cost__c = 117;
        oli.BSS_Category__c = 'G&R';
        oli.Item_Order__c = 1;
        oli.PricebookEntryId = entry.Id;
        insert oli;


        
        
        OpportunityTeamMember oppMem4 = oppMemTestCase(opp.id, userId, 'STMS担当');
        OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.put(opp.id, oppMem4);
        insert oppMem4;
        List<OpportunityTeamMember> otmList = new  List<OpportunityTeamMember>();
        otmList.add(oppMem4);
        System.assertEquals(1, otmList.size());
        
        List<Opportunity> opps = [select Opportunity_No__c from Opportunity where Close_Forecasted_Date__c >= :Date.today() and Close_Forecasted_Date__c <= :Date.today().addMonths(2)];
        System.assertEquals(1, opps.size());

        List<String> oppIds = new List<String>();
        for(Opportunity oppo : opps){
            oppIds.add(oppo.Id);
        }

        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'NFM009';
        iflog.Log__c  = 'callout start\n';
        iflog.ErrorLog__c='Error! QuotationCode[' +opp.Opportunity_No__c+ '] contain inactive product. This DN is skipped.\n'; 
        insert iflog;
        BatchIF_Log__c iflogt = [Select Id, Name,Log__c,ErrorLog__c from BatchIF_Log__c where Id = :iflog.Id];
        List<BatchIF_Log__c> biflList = new List<BatchIF_Log__c>();
        biflList.add(iflogt);
        System.assertEquals(1, biflList.size());

        System.debug(Logginglevel.DEBUG, 'NFM009_' + iflogt.Name + ' start');
        System.assertEquals(1, oppIds.size());
        
        System.Test.StartTest();
        NFM009Controller.callout(iflogt,oppIds);
        System.assertEquals(1, oppIds.size());
        System.Test.StopTest();
        

        //NFM009Controller.callout(iflogt,null);

        //NFM009Controller.callout(null,oppIds);
        //System.Test.StopTest();
    }
     
    static testMethod void test_method_NFM009TZ() {

        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有效';
        product.SFDA_Approbated_Status__c= '有效';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        insert product;
        
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        String userId = UserInfo.getUserId();
        
        Opportunity opp = new Opportunity();
        opp.Name='aiueo';
        opp.StageName='引合';
        opp.Trade__c = '内貿';
        opp.CloseDate=Date.today();
        opp.CurrencyIsoCode = 'CNY';
        opp.Estimation_List_Price__c = 100;
        opp.Wholesale_Price__c = 101;
        opp.Dealer_Final_Price__c = 102;
        opp.OCM_Agent1_Price__c = 103;
        opp.Stocking_Price__c = 104;
        opp.Estimation_No__c = '105';
        opp.Estimation_Name__c = '106';
        opp.Estimation_Id__c = '107';
        opp.Opportunity_No__c = 'BJ-GI-SH0653328';
        opp.Close_Forecasted_Date__c =Date.today().addMonths(1);
        insert opp;

        

        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.Id__c = '107';
        oli.SFDA_Status__c = '有效';
        oli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oli.ListPrice__c = 112;
        oli.Quantity = 113;
        oli.UnitPrice = 114;
        oli.UnitPrice__c = 115;
        oli.Qty_Unit__c = '116';
        oli.Cost__c = 117;
        oli.BSS_Category__c = 'G&R';
        oli.Item_Order__c = 1;
        oli.PricebookEntryId = entry.Id;
        insert oli;

        OpportunityLineItem oll = new OpportunityLineItem();
        oll.OpportunityId = opp.Id;
        oll.Id__c = '104';
        oll.SFDA_Status__c = '準備中';
        oll.Name__c = '儿二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oll.ListPrice__c = 115;
        oll.Quantity = 113;
        oll.UnitPrice = 114;
        oll.UnitPrice__c = 115;
        oll.Qty_Unit__c = '116';
        oll.Cost__c = 117;
        oll.BSS_Category__c = 'G&R';
        oll.Item_Order__c = 1;
        oll.PricebookEntryId = entry.Id;
        insert oll;
        
        OpportunityTeamMember oppMem4 = oppMemTestCase(opp.id, userId, 'STMS担当');
        OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.put(opp.id, oppMem4);
        insert oppMem4;
        List<OpportunityTeamMember> otmList = new  List<OpportunityTeamMember>();
        otmList.add(oppMem4);
        System.assertEquals(1, otmList.size());
        
        List<Opportunity> opps = [select Opportunity_No__c from Opportunity where Close_Forecasted_Date__c >= :Date.today() and Close_Forecasted_Date__c <= :Date.today().addMonths(2)];
        System.assertEquals(1, opps.size());

        List<String> oppIds = new List<String>();
        for(Opportunity oppo : opps){
            oppIds.add(oppo.Id);
        }

        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'NFM009';
        iflog.Log__c  = 'callout start\n';
        iflog.ErrorLog__c='Error! QuotationCode[' +opp.Opportunity_No__c+ '] contain inactive product. This DN is skipped.\n'; 
        insert iflog;
        BatchIF_Log__c iflogt = [Select Id, Name,Log__c,ErrorLog__c from BatchIF_Log__c where Id = :iflog.Id];
        List<BatchIF_Log__c> biflList = new List<BatchIF_Log__c>();
        biflList.add(iflogt);
        System.assertEquals(1, biflList.size());

        System.debug(Logginglevel.DEBUG, 'NFM009_' + iflogt.Name + ' start');
        System.assertEquals(1, oppIds.size());
        
        System.Test.StartTest();
        NFM009Controller.callout(iflogt,oppIds);
        System.assertEquals(1, oppIds.size());
        System.Test.StopTest();
        

        //NFM009Controller.callout(iflogt,null);

        //NFM009Controller.callout(null,oppIds);
        //System.Test.StopTest();
    }
    
}