@isTest
private class NFM115ControllerTest {
    static Account Agency;
    static Product2 product;

    public class Samples {
        public NFMUtil.Monitoring Monitoring;
        public Sample[] Sample;
    }
    //样本订货单
    public class Sample {
        public String AgentCode;
        public String DeliveryAddress;
        public String DeliveryContact;
        public String DeliveryPhone;
        public Detail[] Detail;
    }
    //样本订货单明细
    public class Detail {
        public String ItemCode;
        public String ItemQuantity;
    }

    static testMethod void testMethod1(){
        Account account2 = new Account();
		account2.Name = 'test1经销商';
        account2.RecordTypeId = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'office'][0].ID;
        insert account2;
        Schema.RecordTypeInfo rti = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Office');
        if(rti == null){
            rti = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('office');
        }
        ID AgencyID = rti.getRecordTypeId();
        Agency = new Account(ParentId = account2.Id, RecordTypeId = AgencyID, Name = 'NFM115TestAgency', FSE_SP_Main_Leader__c = UserInfo.getUserId(), FSE_GI_Main_Leader__c = UserInfo.getUserId());
        insert Agency;
        // Agency = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :Agency.Id];
        Agency = [select Management_Code__c,  Name, Id from Account where Id = :Agency.Id];

        Sample_order_list__c samOrder = new Sample_order_list__c();
        samOrder.Status__c          = '订单已发送'; 
        samOrder.DeliveryAddress__c = '12312';
        samOrder.DeliveryPhone__c   = '12312312';
        samOrder.DeliveryContact__c = 'thh';
        samOrder.ReceivePart__c     = '办事处';
        samOrder.Account__c         = Agency.Id;
        insert samOrder;
        List<String> samIdList = new List<String>();
        samIdList.add(samOrder.Id);
        if (samIdList.size() > 0) {
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM115';
            iflog.Log__c  = 'callout start\n';
            insert iflog;           
            NFM115Controller.callout(iflog.Id, samIdList);
        }
    }

    static testMethod void testMethod2(){
        Account account2 = new Account();
		account2.Name = 'test1经销商';
        account2.RecordTypeId = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'office'][0].ID;
        insert account2;
        Schema.RecordTypeInfo rti = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Office');
        if(rti == null){
            rti = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('office');
        }
        ID AgencyID = rti.getRecordTypeId();
        Agency = new Account(ParentId = account2.Id, RecordTypeId = AgencyID, Name = 'NFM115TestAgency', FSE_SP_Main_Leader__c = UserInfo.getUserId(), FSE_GI_Main_Leader__c = UserInfo.getUserId());
        insert Agency;
        // Agency = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :Agency.Id];
        Agency = [select Management_Code__c,  Name, Id from Account where Id = :Agency.Id];

        product = new Product2(Name='name05',IsActive=true,Family='SP',Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                                Fixture_Model_No_T__c = 'n05', ProductCode = 'A06000008601',ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert product;

        Sample_order_list__c samOrder = new Sample_order_list__c();
        samOrder.Status__c          = '订单已发送';
        samOrder.DeliveryAddress__c = '12312';
        samOrder.DeliveryPhone__c   = '12312312';
        samOrder.DeliveryContact__c = 'thh';
        samOrder.ReceivePart__c     = '办事处';
        samOrder.Account__c         = Agency.Id;
        insert samOrder;

        Sample_order_list_detail__c samDetail = new Sample_order_list_detail__c();
        samDetail.Product__c           = product.Id;
        samDetail.ItemQuantity__c      = 100;
        samDetail.Sample_order_list__c = samOrder.Id;
        insert samDetail;

        Samples samples = new Samples();
        samples.Monitoring = new NFMUtil.Monitoring();
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        samples.Monitoring.TransmissionDateTime = nowStr;
        samples.Monitoring.Text                 = ''; 
        samples.Monitoring.Tag                  = 'MSGH';
        samples.Monitoring.Sender               = 'SFDC';
        samples.Monitoring.Receiver             = 'SAP';
        samples.Monitoring.MessageType          = 'NFM115';

        samples.Sample = new List<Sample>();
        Sample sample  = new Sample();
        sample.AgentCode       = samOrder.AgentCode__c;
        sample.DeliveryAddress = samOrder.DeliveryAddress__c;
        sample.DeliveryContact = samOrder.DeliveryContact__c;
        sample.DeliveryPhone   = samOrder.DeliveryPhone__c;
        sample.Detail          = new List<Detail>();
        Detail Detail = new Detail();
        Detail.ItemCode     = samDetail.ItemCode__c;
        Detail.ItemQuantity = String.valueOf(samDetail.ItemQuantity__c);
        sample.Detail.add(Detail);
        samples.Sample.add(sample);

        NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
        Monitoring.Tag                  = samples.Monitoring.Tag;
        Monitoring.Sender               = samples.Monitoring.Sender;
        Monitoring.Receiver             = samples.Monitoring.Receiver;
        Monitoring.MessageType          = samples.Monitoring.MessageType;
        Monitoring.MessageGroupNumber   = samples.Monitoring.MessageGroupNumber;
        Monitoring.NumberOfRecord       = samples.Monitoring.NumberOfRecord;
        Monitoring.TransmissionDateTime = samples.Monitoring.TransmissionDateTime;
        Monitoring.Text = '';
        BatchIF_Log__c rowData = NFMUtil.makeRowData(Monitoring, 'NFM115', samples);

        List<String> samIdList = new List<String>();
        samIdList.add(samOrder.Id);
        if (samIdList.size() > 0) {
            BatchIF_Log__c iflog = null;           
            NFM115Controller.execute(rowData, null);
        }
    }
}