@isTest
private class NFM110dataBatchTest {
    static Account company;
    static Account section1;        // 呼吸科
    static Account section2;        // 消化科
    static Account olympus;
    static Account olympus1;        // 呼吸科
    static Account olympus2;        // 消化科
    static Account depart1;
    static Account depart2;

    static void initHpData() {
        ControllerUtil.EscapeNFM001Trigger = true;
        ID rectCoID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        ID rectDpt1ID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();


        ID rectDpt2ID = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();

        // テストデータ
        company = new Account(RecordTypeId = rectCoID, Name = 'NFM110TestCompany', FSE_SP_Main_Leader__c = UserInfo.getUserId(), FSE_GI_Main_Leader__c = UserInfo.getUserId());
        olympus = new Account(RecordTypeId = rectCoID, AgentCode_Ext__c = '9999999', Name = 'olympus');
        insert new Account[] {company, olympus};
        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // section1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 呼吸科'];
        // section2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 消化科'];
        // olympus1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 呼吸科'];
        // olympus2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 消化科'];
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        section1 = [select Management_Code__c,  Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 呼吸科'];
        section2 = [select Management_Code__c,  Name, Id, Department_Class_Label__c from Account where ParentId = :company.Id and RecordType.name = '戦略科室分類 消化科'];
        olympus1 = [select Management_Code__c,  Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 呼吸科'];
        olympus2 = [select Management_Code__c,  Name, Id, Department_Class_Label__c from Account where ParentId = :olympus.Id and RecordType.name = '戦略科室分類 消化科'];
        System.debug('section1.Department_Class_Label__c=' + section1.Department_Class_Label__c);
        System.debug('section2.Department_Class_Label__c=' + section2.Department_Class_Label__c);
        depart1 = new Account(RecordTypeId = rectDpt1Id, Name = '*', ParentId  = section1.Id, Department_Class__c = section1.Id, Hospital__c = company.Id,
                              Department_Name__c = 'NFM110TestDepart1');
        depart2 = new Account(RecordTypeId = rectDpt2Id, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                              Department_Name__c = 'NFM110TestDepart2');
        Account ocm = new Account(RecordTypeId = rectDpt2Id, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                                  AgentCode_Ext__c = '9999900', Department_Name__c = 'NFM110TestDepart3');
        Account olympus_return = new Account(RecordTypeId = rectDpt2Id, Name = '*', ParentId  = olympus2.Id, Department_Class__c = olympus2.Id, Hospital__c = olympus.Id,
                                             AgentCode_Ext__c = '9999901', Department_Name__c = 'olympus_return');
        insert new Account[] {depart1, depart2, ocm, olympus_return};
        // depart1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart1.Id];
        // depart2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart2.Id];
        depart1 = [select Management_Code__c,  Name, Id from Account where Id = :depart1.Id];
        depart2 = [select Management_Code__c,  Name, Id from Account where Id = :depart2.Id];
    }
    @isTest //销售渠道=经销商 包含 ET和非ET24小时
    static void test_agency() {
        initHpData();
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        //一般经销商信息
        Account myAccount2 = new Account(name = 'Testaccount002', Dealer_discount__c = 20, RecordTypeId = rectCo[0].Id, Sales_Shop_Class__c = '一級販売店');
        insert myAccount2;
        Product2 prd = new Product2(Virtual_Contract__c = true,Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false, MDM_Model_No__c = 'NFM110Ast1', Dealer_special_Object__c = true,ConsumCategory1__c = 'ET耗材');
        insert prd;
        Product2__c pro1 = new Product2__c(Name = 'Pro001', OT_CODE_Text__c = 'NFM110Prd1', Product2__c = prd.Id);
        insert pro1;
        // Asset ast = new Asset(Name='NFM110Ast1', AccountId=depart1.Id, Department_Class__c=section1.Id, Hospital__c=company.Id, Product2Id=prd.Id,
        //     SerialNumber='2577010001675', Guarantee_period_for_products__c=Date.today()
        // );
        // insert ast;
        Opportunity opp1 = new Opportunity(Name = 'aiueo1', StageName = '出荷', CurrencyIsoCode = 'CNY', CloseDate = Date.today(), Purchase_Type__c = '一般询价',
                                           Sales_Root__c = '販売店',Agency1__c = myAccount2.Id, AccountId = depart1.Id, Department_Class__c = section1.Id, Hospital__c = company.Id, Trade__c = '内貿');
        insert opp1;
        opp1 = [select Id, Opportunity_No__c, Trade__c from Opportunity where Id = :opp1.Id];
        System.assertEquals('内貿', opp1.Trade__c);
        // Statu_Achievements__c sta11 = new Statu_Achievements__c(Name='SO0000001', Opportunity__c=opp1.Id, ContractNO__c='sta11', DeliveryStatus__c = '未交付',ContractAmount__c = 0);
        // insert sta11;

        NFM110Rest.GeneralData generalData1 = new NFM110Rest.GeneralData();
        NFM110Rest.ProductsDelivery ProductsDelivery = new NFM110Rest.ProductsDelivery();

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110Rest.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110Rest.DnInformation dnInfo1 = new NFM110Rest.DnInformation();
        NFM110Rest.DnInformation dnInfo2 = new NFM110Rest.DnInformation();
        generalData1.DnInformation = new NFM110Rest.DnInformation[] { dnInfo1, dnInfo2 };
        generalData1.EndUserNo     = company.Management_Code__c;
        generalData1.DepartmentNo  = depart1.Management_Code__c;
        generalData1.SoNo = '11111111111';
        generalData1.InquiryNo = opp1.Opportunity_No__c + ',';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.Barcode          = 'testbarcode001';
        dnInfo1.SerialNoorLotNo = '2577010001675';
        dnInfo1.VALUE           = '123';
        dnInfo1.ProvistonPeriod = '03';
        dnInfo1.GuaranteePeriod = '05';
        dnInfo1.GuaranteePrice = '1';
        dnInfo1.MaintenancePrice = '1';
        dnInfo1.ServicePrice = '1';
        dnInfo2.SorLMark        = 'S';
        dnInfo2.OTCode          = prd.ProductCode_Ext__c;
        dnInfo2.Barcode          = 'testbarcode001';
        dnInfo2.SerialNoorLotNo = '2577010001675';
        dnInfo2.VALUE           = '123';
        dnInfo2.ProvistonPeriod = '03';
        dnInfo2.GuaranteePeriod = '05';
        dnInfo2.GuaranteeType   = '';
        dnInfo2.GuaranteePrice = '1';
        dnInfo2.MaintenancePrice = '1';
        dnInfo2.ServicePrice = '1';

        System.Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(ProductsDelivery.Monitoring, 'NFM110', ProductsDelivery.GeneralData);
        if (String.isBlank(rowData.Log__c) == false)
            Database.executeBatch(new NFM110dataBatch(rowData.Id), 1);
            // NFM110Rest.main(rowData.Id);
        System.Test.stopTest();
    }
}