@isTest
private class ArriveGoodsMainControllerTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test

        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];
        
        List<RecordType> rectCo = [SELECT Id 
                                    FROM RecordType
                                    WHERE IsActive = true
                                    AND SobjectType = 'Account'
                                    AND Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        Account myAccount1 = new Account(name='testaccount001',RecordTypeId = rectCo[0].Id ,AgentCode_Ext__c = '9999900');
        insert myAccount1;
        Contact core = new Contact(email='jplumber@salesforce.com',
        firstname='Joe',
        lastname='Plumber',
        accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(
            ContactId = core.id,
            Alias = 'newUser',
            Email='newuser@testorg.com',
            EmailEncodingKey='UTF-8',
            LastName='testUser',
            LanguageLocaleKey='zh_CN',
            LocaleSidKey='zh_CN',
            ProfileId = prof.Id,
            TimeZoneSidKey='Asia/Shanghai',
            UserName='testUser@testorg.com',
            Work_Location__c='北京');
        insert myUser_test;
        system.runAs(myUser_test){
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効');
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効');
            insert new Product2[]{prod01,prod02};
            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //订单
            Consumable_order__c Order1 = new Consumable_order__c(
                Name='OCM_01_001',
                RecordTypeId = System.Label.RT_ConOrder_Delivery,
                Order_type__c ='订单',
                Dealer_info__c=myAccount1.Id,
                Order_ProType__c = 'ET');
            Consumable_order__c Order2 = new Consumable_order__c(
                Name='OCM_01_002',
                RecordTypeId = System.Label.RT_ConOrder_Delivery,
                Order_type__c ='订单',
                Dealer_info__c=myAccount1.Id,
                Order_ProType__c = 'ET');
            insert new Consumable_order__c[] {Order1, Order2};

            //明细1
            Consumable_orderdetails__c createDetail = new Consumable_orderdetails__c();
            createDetail.name = 'Too001';
            createDetail.Consumable_Product__c = pro1.id;
            createDetail.Dealer_Custom_Price__c = 999.00;
            createDetail.Consumable_count__c = 3;
            createDetail.Consumable_order__c =  Order1.id;
            createDetail.RecordTypeid = System.Label.RT_ConOrderDetail1_Order;
            insert createDetail;
            Consumable_orderdetails__c createDetail1 = new Consumable_orderdetails__c();
            createDetail1.name = 'Too002';
            createDetail1.Consumable_Product__c = pro2.id;
            createDetail1.Consumable_order__c =  Order2.id;
            createDetail1.Dealer_Custom_Price__c = 999.00;
            createDetail1.Consumable_count__c = 4;
            createDetail1.RecordTypeid = System.Label.RT_ConOrderDetail1_Order;
            insert createDetail1;

            //订单明细
            Consumable_order_details2__c Orderdet1 =  new Consumable_order_details2__c(
                Name='OCM_01_001001',
                Consumable_order_minor__c = Order1.Id,
                Arrive_date__c = Date.today(),
                Consumable_Product__c = pro1.id,
                RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery);
            Consumable_order_details2__c Orderdet2 =  new Consumable_order_details2__c(
                Name='OCM_01_001002',
                Consumable_order_minor__c = Order2.Id,
                Arrive_date__c = Date.today(),
                Consumable_Product__c = pro2.id,
                RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery);
            Consumable_order_details2__c Orderdet3 =  new Consumable_order_details2__c(
                Name='OCM_01_001003',
                Consumable_order_minor__c = Order1.Id,
                //Arrive_date__c = Date.today(),
                Consumable_Product__c = pro1.id,
                RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery);
            Consumable_order_details2__c Orderdet4 =  new Consumable_order_details2__c(
                Name='OCM_01_001004',
                Consumable_order_minor__c = Order2.Id,
                Arrive_date__c = Date.today(),
                Consumable_Product__c = pro2.id,
                RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery);
            insert new Consumable_order_details2__c[] {Orderdet1, Orderdet2, Orderdet3, Orderdet4};
            List<Consumable_order__c> orderList = [SELECT id,name,OrderNumber_notarrive__c,Delivery_detail_count__c FROM Consumable_order__c order by Name];
            System.assertEquals(2, orderList.size());
            System.assertEquals('OCM_01_001', orderList[0].Name);
            System.assertEquals(1, orderList[0].OrderNumber_notarrive__c);
            System.assertEquals(1, orderList[0].Delivery_detail_count__c);
            ArriveGoodsMainController AGMCTest = new ArriveGoodsMainController();
            AGMCTest.init1();
            System.assertEquals('OCM_01_001', AGMCTest.raesList[0].name);
            System.assertEquals('OCM_01_002', AGMCTest.raesList[1].name);
            AGMCTest.category1 = 'OCM_01_001';
            AGMCTest.searchConsumableorderdetails();
            AGMCTest.orderArrive();
            AGMCTest.orderReturn();
            AGMCTest.category1 = 'tst----6';
            AGMCTest.coc.Shipment_date__c = Date.today();
            AGMCTest.searchConsumableorderdetails();
            System.assertEquals(0, AGMCTest.raesList.size());
        }
    }
}