@isTest
private class OutboundorderImportControllerTest
{   
    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectHos = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHos.size() == 0) {
            return;
        }
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        
        Account myAccount1 = new Account(name='Testaccount001',Dealer_discount__c =20,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');
        insert MyUser_Test;
        system.runAs(myUser_Test){
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Packing_list_manual__c=12);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Packing_list_manual__c=12);
            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();
            Order1.Name = 'OCM_01_001';
            Order1.Order_status__c = '批准';
            Order1.Order_type__c = '订单';
            Order1.RecordTypeid = System.Label.RT_ConOrder_Order;
            Order1.Dealer_Info__c = myAccount1.Id;
            Order1.Order_ProType__c = 'ET';
            insert  Order1;
            Consumable_order_details2__c Orderdet4 = new Consumable_order_details2__c();
            Orderdet4.Name = 'OCM_01_001004';
            Orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery ;
            Orderdet4.Consumable_Arrived_order__c = Order1.Id;
            Orderdet4.Sterilization_limit__c = Date.today().addDays(-1);
            Orderdet4.Bar_Code__c = '123456789';
            Orderdet4.Consumable_Product__c = pro1.Id;
            Orderdet4.Arrive_date__c = Date.today().addDays(-1);
            Orderdet4.Box_Piece__c = '盒';

            Consumable_order_details2__c orderdet3 = new Consumable_order_details2__c();
            orderdet3.Name = 'OCM_01_001004';
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery ;
            orderdet3.Consumable_Arrived_order__c = Order1.Id;
            orderdet3.Sterilization_limit__c = Date.today().addDays(-1);
            orderdet3.Bar_Code__c = '123456788';
            orderdet3.Consumable_Product__c = pro1.Id;
            orderdet3.Arrive_date__c = Date.today().addDays(-1);
            orderdet3.Box_Piece__c = '盒';

            Consumable_order_details2__c orderdet2 = new Consumable_order_details2__c();
            orderdet2.Name = 'OCM_01_001004';
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery ;
            orderdet2.Consumable_Arrived_order__c = Order1.Id;
            orderdet2.Sterilization_limit__c = Date.today().addDays(-1);
            orderdet2.Bar_Code__c = '123456787';
            orderdet2.Consumable_Product__c = pro1.Id;
            orderdet2.Arrive_date__c = Date.today().addDays(-1);
            orderdet2.Box_Piece__c = '个';

            Consumable_order_details2__c orderdet1 = new Consumable_order_details2__c();
            Orderdet1.Name = 'OCM_01_001004';
            Orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery ;
            Orderdet1.Consumable_Arrived_order__c = Order1.Id;
            Orderdet1.Sterilization_limit__c = Date.today().addDays(-1);
            Orderdet1.Bar_Code__c = '123456786';
            Orderdet1.Consumable_Product__c = pro1.Id;
            Orderdet1.Arrive_date__c = Date.today().addDays(-1);
            Orderdet1.Box_Piece__c = '个';
            insert new Consumable_order_details2__c[] {orderdet1, orderdet2,orderdet3,orderdet4};
            Account hosp1 = new Account(name='Testhosp001',RecordTypeId = rectHos[0].Id);
            insert hosp1;
            Agency_Hospital_Link__c jxs = new Agency_Hospital_Link__c(name='jxs001',Agency__c = myAccount1.Id,Hospital__c=hosp1.Id);
            insert jxs;
            List<Agency_Hospital_Link__c> jxsList = [select Name,Hospital__r.Management_Code__c from Agency_Hospital_Link__c where Name = 'Testhosp001'];
            system.assertEquals(1, jxsList.size());
            PageReference page = new PageReference('/apex/OutboundorderImport');
            System.Test.setCurrentPage(page);
            OutboundorderImportController ctrl = new OutboundorderImportController();
            System.Test.startTest();
            String csvBody = '';
            //初始化测试
            ctrl.init();
            ctrl.csvFileBody=null;
            ctrl.importCSVFile();

            /*//exception
            csvBody = '出库单编码,出库目的,医院编码,医院名称,科室,二级经销商,Bar_Code__c,出货数量,出货单价,单位,二级经销商给客户单价,出库日期\n';
            csvBody +='CS-S0001,销售给二级经销商,,aaa,,测试二级经销商1,123456789,2,230,盒,230,2017/02/17\n';
            csvBody +='CS-S0001,销售给二级经销商,,aaa,,测试二级经销商1,123456788,2,230,盒,230,2017/02/17\n';
            csvBody +='CS-S0001,销售给二级经销商,,aaa,,测试二级经销商1,123456787,2,230,盒,230,2017/02/17\n';
            csvBody +='CS-S0001,销售给二级经销商,,aaa,,测试二级经销商1,123456786,2,230,盒,230,2017/02/17\n';
            ctrl.csvFileBody = Blob.valueOf(csvBody);
            ctrl.importCSVFile();
            //system.assertEquals(1, ctrl.orderRecords.size());
            ctrl.dataImport();*/

            //hospitalCode=null,hospitalName!=null
            csvBody = 'Name,SummonsForDirction__c,HospitalCode,Order_ForHospital__c,Order_ForCustomerText__c,Order_ForDealer__c,Bar_Code__c,Shipment_Count__c,Delivery_List_RMB__c,Box_Piece__c,Unitprice_To_agency__c,Outbound_Date__c\n';
            csvBody +='CS-S0001,销售给二级经销商,' + jxsList[0].Hospital__r.Management_Code__c + ',Testhosp001,test,测试二级经销商1,123456789,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,销售给二级经销商,' + jxsList[0].Hospital__r.Management_Code__c + ',Testhosp001,test,测试二级经销商1,123456788,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,销售给二级经销商,' + jxsList[0].Hospital__r.Management_Code__c + ',Testhosp001,test,测试二级经销商1,123456787,1,230,个,230,2017-02-17\n';
            csvBody +='CS-S0001,销售给二级经销商,' + jxsList[0].Hospital__r.Management_Code__c + ',Testhosp001,test,测试二级经销商1,123456786,1,230,个,230,2017-02-17\n';
            ctrl.csvFileBody = Blob.valueOf(csvBody);
            ctrl.importCSVFile();
            system.assertEquals(1, ctrl.orderRecords.size());
            system.assertEquals(false, ctrl.saveFLGbln);
            ctrl.dataImport();

            //医院试用 Delivery_List_RMB__c == null
            csvBody = 'Name,SummonsForDirction__c,HospitalCode,Order_ForHospital__c,Order_ForCustomerText__c,Order_ForDealer__c,Bar_Code__c,Shipment_Count__c,Delivery_List_RMB__c,Box_Piece__c,Unitprice_To_agency__c,product,Outbound_Date__c\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456789,1,,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456788,1,,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,测医院试用1,123456787,1,,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456786,1,,盒,230,2017-02-17\n';
            ctrl.csvFileBody = Blob.valueOf(csvBody);
            ctrl.importCSVFile();
            //system.assertEquals(csvBody,ctrl.csvAsString);
            //system.assertEquals(5,ctrl.csvFileLines.size());
            //system.assertEquals('CS-S0001,销售给二级经销商,,,,测试二级经销商1,123456789,2,230,盒,230,CD-6C,2017/2/17',ctrl.csvFileLines[1]);
            system.assertEquals(1, ctrl.orderRecords.size());
            ctrl.dataImport();

            //医院试用 barcode=null
            csvBody = 'Name,SummonsForDirction__c,HospitalCode,Order_ForHospital__c,Order_ForCustomerText__c,Order_ForDealer__c,Bar_Code__c,Shipment_Count__c,Delivery_List_RMB__c,Box_Piece__c,Unitprice_To_agency__c,Outbound_Date__c\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,测医院试用1,,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,,1,230,盒,230,2017-02-17\n';
            ctrl.csvFileBody = Blob.valueOf(csvBody);
            ctrl.importCSVFile();
            //system.assertEquals(csvBody,ctrl.csvAsString);
            //system.assertEquals(5,ctrl.csvFileLines.size());
            //system.assertEquals('CS-S0001,销售给二级经销商,,,,测试二级经销商1,123456789,2,230,盒,230,CD-6C,2017/2/17',ctrl.csvFileLines[1]);
            system.assertEquals(1, ctrl.orderRecords.size());
            ctrl.dataImport();

            //医院试用
            csvBody = 'Name,SummonsForDirction__c,HospitalCode,Order_ForHospital__c,Order_ForCustomerText__c,Order_ForDealer__c,Bar_Code__c,Shipment_Count__c,Delivery_List_RMB__c,Box_Piece__c,Unitprice_To_agency__c,Outbound_Date__c\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456789,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456788,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,测医院试用1,123456787,1,230,盒,230,2017-02-17\n';
            csvBody +='CS-S0001,医院试用,,,,医院试用1,123456786,12,230,盒,230,2017-02-17\n';
            ctrl.csvFileBody = Blob.valueOf(csvBody);
            ctrl.importCSVFile();
            //system.assertEquals(csvBody,ctrl.csvAsString);
            //system.assertEquals(5,ctrl.csvFileLines.size());
            //system.assertEquals('CS-S0001,销售给二级经销商,,,,测试二级经销商1,123456789,2,230,盒,230,CD-6C,2017/2/17',ctrl.csvFileLines[1]);
            system.assertEquals(1, ctrl.orderRecords.size());
            ctrl.dataImport();
            System.Test.stopTest();


        }
    }
}