@ isTest (SeeAllData = true)
private class OpportunityProductControllerTest {


    static testMethod void myUnitTest() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectOpp = [select Id from RecordType where IsActive = true and SobjectType = 'Opportunity' and Name = 'SSBD'];
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            CurrencyIsoCode = 'CNY'
        );
        insert pricebook;
        
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        product1.ProductCode = 'product1';
        product1.ProductStatus__c = '1';
        product1.NMPAStatus_one__c = 'Z1';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        
        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductStatus__c = '1';
        product2.IsActive = true;
        product2.ProductCode = 'product2';
        product2.NMPAStatus_one__c = 'Z1';
        product2.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product2.MaterialStatus_one_End__c = Date.today().addDays(22);
        
        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductStatus__c  = '1';
        product3.IsActive = true;
        product3.ProductCode = 'product3';
        product3.NMPAStatus_one__c = 'Z1';
        product3.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product3.MaterialStatus_one_End__c = Date.today().addDays(22);
        insert new Product2[] {product1,product2,product3};
        
        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        insert new PricebookEntry[] {standardPrice1,standardPrice2,standardPrice3};
        //List<Pricebook2> pris = [select Id from Pricebook2 where IsStandard  = true And isActive = true limit 1];
        PricebookEntry entry1 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product1.Id);
        entry1.UnitPrice = 0;
        entry1.IsActive = true;
        entry1.UseStandardPrice = false;
        entry1.CurrencyIsoCode = 'CNY';
            
        PricebookEntry entry2 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product2.Id);
        entry2.UnitPrice = 0;
        entry2.IsActive = true;
        entry2.UseStandardPrice = false;
        entry2.CurrencyIsoCode = 'CNY';
        
        PricebookEntry entry3 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product3.Id);
        entry3.UnitPrice = 0;
        entry3.IsActive = true;
        entry3.UseStandardPrice = false;
        entry3.CurrencyIsoCode = 'CNY';
        
        insert new PricebookEntry[] {entry1,entry2,entry3};
        
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            PostCode__c='000000'
        );
        insert acc1;
        
        Opportunity opp1 = new Opportunity(
            name = 'test opp1',
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            AccountId = acc1.Id,
            StageName = 'Phase3',
            ProductSegment__c = 'IE',
            CurrencyIsoCode = 'CNY',
            Pricebook2Id = pricebook.Id,
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine',
            //CloseDate = Date.valueof('1900-01-01')
            CloseDate = Date.today().addDays(22),
            ExpectedOrderDate__c = Date.today().addDays(2),
            IE_Custom_Price__c = 1000
        );
        insert opp1;
        
        // 询价产品新建test
        PageReference page = new PageReference('/apex/OpportunityProduct?oppid=' + opp1.Id);
        System.Test.setCurrentPage(page);
        OpportunityProductController controller = new OpportunityProductController();
        System.assertEquals(opp1.Id, controller.opp.id);
        
        controller.init();
        //controller.addLine();
        //controller.lineNo = 1;
        //controller.deleteLine();
        
        controller.OPInfoList[0].productName = product1.Name;
        controller.OPInfoList[0].productId = product1.Id;
        controller.OPInfoList[0].productCode = product1.ProductCode;
        controller.OPInfoList[0].oli.QuantityD__c = 2;
        controller.OPInfoList[0].oli.UnitPriceD__c = 111;
        controller.OPInfoList[0].oli.DescriptionD__c = 'test1';
        
        controller.OPInfoList[1].productName = product2.Name;
        controller.OPInfoList[1].productId = product2.Id;
        controller.OPInfoList[1].productCode = product2.ProductCode;
        controller.OPInfoList[1].oli.QuantityD__c = 1;
        controller.OPInfoList[1].oli.UnitPriceD__c = 200;
        controller.OPInfoList[1].oli.DescriptionD__c = 'test2';

        controller.opp.IE_Custom_Price__c = 1000;
        
        controller.saveLine();
        
        List<OpportunityLineItem> oppList = [select Id, Product2Id from OpportunityLineItem where OpportunityId = :opp1.Id order by DescriptionD__c];
        //System.assertEquals(2, oppList.size());
        //System.assertEquals(product1.Id, oppList[0].Product2Id);
        //System.assertEquals(product2.Id, oppList[1].Product2Id);
        
        //询价产品更新test
        controller = new OpportunityProductController(new ApexPages.StandardController(opp1));
        controller.init();
        controller.testI();
        controller.OPInfoList[0].productName = product3.Name;
        controller.OPInfoList[0].productId = product3.Id;
        controller.OPInfoList[0].productCode = product3.ProductCode;
        controller.OPInfoList[0].oli.QuantityD__c = 3;
        controller.OPInfoList[0].oli.UnitPriceD__c = 111;
        controller.OPInfoList[0].oli.DescriptionD__c = 'test3';

        controller.OPInfoList[1].oli.IsDelete__c = true;
        
        controller.saveLine();
        
        List<OpportunityLineItem> oppList2 = [select Id, Product2Id, DealerSelectProduct__c from OpportunityLineItem where OpportunityId = :opp1.Id];
        //System.assertEquals(1, oppList2.size());
        //System.assertEquals(product3.Id, oppList2[0].Product2Id);
    }

    @isTest static void test_init2() {
        Id pricebookId = Test.getStandardPricebookId();

        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true
        );
        insert pricebook;

        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.ProductCode = 'product1';
        product1.Product_ECCode__c = 'product1';
        product1.IsActive = true;
        product1.ProductStatus__c  = '1';

        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductCode = 'product2';
        product2.Product_ECCode__c = 'product2';
        product2.IsActive = true;
        product2.ProductStatus__c  = '1';

        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductCode = 'product3';
        product3.Product_ECCode__c = 'product3';
        product3.IsActive = true;
        product3.ProductStatus__c  = '1';

        insert new Product2[] {product1, product2, product3};

        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        insert new PricebookEntry[] {standardPrice1, standardPrice2, standardPrice3};

        PricebookEntry entry1 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );

        PricebookEntry entry2 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );

        PricebookEntry entry3 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );

        insert new PricebookEntry[] {entry1, entry2, entry3};

        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            RecordTypeId = rectIE[0].Id
        );
        insert user;

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine',
            Pricebook2Id = pricebook.Id
        );
        insert opp;

        PageReference page = new PageReference('/apex/OpportunityProduct?oppid=' + opp.Id);
        System.Test.setCurrentPage(page);
        OpportunityProductController controller = new OpportunityProductController();

        controller.init();

        String strblob = '"套装","产品code","产品名称","数量","折扣"\n"set02","product1","product1","2.00","0.00"\n';

        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();

        strblob = '套装,产品code,产品名称,数量,折扣\nset01,product1,product1,2,49\nset01,product2,product2,2,49';

        controller.contentFile = Blob.valueOf(strblob);

        controller.csvRead();
        controller.testI();

        controller.fileName = 'aaa';
        controller.csvExport();
    }
}