@isTest
private class SyncMBOpportunityLineItemTest {

    private static Product2 product = null;
    private static Id pricebookId = null;

    private static  Opportunity opprtunity = null;

    static final String RC_HOSPITAL = '病院';
    static final String RC_OPP = '引合';
    static final String RC_SENRYAKUKASHITSUBUNRUI = '戦略科室分類 消化科';
    static final String RC_BYOUIN = '病院';
    static final String RC_SHINRYOUKA = '診療科 その他';
    static final String RC_HANBAOITEN = '販売店';
    static final String RC_KEIYAKU = '契約';

    static {
        product = new Product2( Name='テスト商品');
        insert product;
        system.debug('##product.Id=[' + product.Id + ']' );

        pricebookId = ControllerUtil.getStandardPricebook().Id; 
    }

    public static String trade = '内貿';
    public static String CurrencyIso = 'CNY';

    private static PricebookEntry buildPB( String input) {
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);

        entry.UnitPrice = 10;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = input;
        insert entry;
        return entry;
    }

    private static Opportunity buildOppInstance( String inputTrade, String CurrencyIsoCode, Id recordTypeId) {
        Opportunity target = new Opportunity( Name='aiueo', StageName='contact', CloseDate=Date.today());
        target.recordTypeId = recordTypeId;
        target.Trade__c = inputTrade;
        target.CurrencyIsoCode = CurrencyIsoCode;
        target.Wholesale_Price__c = 9000;
        target.SAP_Province__c = '青海省';
        target.Opportunity_Category__c = 'GIGI';
        target.Distributor_InCharge_opp__c = true;
        return target;
    }

    // MB_询价产品
    static testMethod void SyncMBOpportunityListTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
                FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id,
                OCM_Category__c = 'LLL'
        );
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];

        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        // PricebookEntry
        PricebookEntry pbEntry = buildPB(CurrencyIso);

        // 引合作成
        Opportunity opp = buildOppInstance(trade, CurrencyIso, '01210000000QekK');
        opp.AccountId = depart.Id;
        opp.Hospital__c = hp.Id;
        opp.Trade__c = trade;
        opp.Sales_Root__c = 'AAA';
        opp.Pricebook2Id = pricebookId;
        insert opp;

        List<MB_OpportunityLineItem__c> mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        System.assertEquals(0, mbopplis1.size());

        // MB_询价产品
        OpportunityLineItem oppli = New OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 1,
            TotalPrice = 10
        );
        insert oppli;

        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode, 
                            State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c
                       from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals(oppli.Id, mbopplis1[0].OpportunityLineItemId__c);
        //System.assertEquals(18, mbopplis1[0].OpportunityLineItemId__c.length());
        //System.assertEquals(opp.Id, mbopplis1[0].Opportunity__c);
        //System.assertEquals(product.Id, mbopplis1[0].Product2__c);
        //System.assertEquals(CurrencyIso, mbopplis1[0].CurrencyIsoCode);
        //System.assertEquals('青海省', mbopplis1[0].State_Text__c);
        //System.assertEquals('LLL', mbopplis1[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbopplis1[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbopplis1[0].Distributor_InCharge_opp__c);

        opp.SAP_Province__c = '北京市';
        update opp;

        mbopplis1 = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_OpportunityLineItem__c];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals('北京市', mbopplis1[0].State_Text__c);

        //delete oppli;

        List<MB_OpportunityLineItem__c> mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());
    }

    // MB_询价产品
    static testMethod void SyncMBTargetListTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
                FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        User oppOwner = new User(Test_staff__c = true, LastName = 'opp', Province__c = '青海省',
                FirstName = 'owner', Alias = 'opp', CommunityNickname = 'oppOwner', Email = 'olympus_oppowner@sunbridge.com', Username = 'olympus_oppowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        insert new User[] {hpOwner, oppOwner};
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id,
                OCM_Category__c = 'LLL'
        );
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];

        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        // PricebookEntry
        PricebookEntry pbEntry = buildPB(CurrencyIso);

        // 引合作成
        Opportunity opp = buildOppInstance(trade, CurrencyIso, '01210000000QekP');      // 目標
        opp.Target_category__c = '担当目标';
        opp.OwnerId = oppOwner.Id;
        opp.AccountId = depart.Id;
        opp.Hospital__c = hp.Id;
        opp.Trade__c = trade;
        opp.Sales_Root__c = 'AAA';
        opp.Pricebook2Id = pricebookId;
        insert opp;

        List<MB_TargetLineItem__c> mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis1.size());
        List<MB_OpportunityLineItem__c> mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());

        // MB_询价产品
        OpportunityLineItem oppli = New OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 1,
            TotalPrice = 10
        );
        insert oppli;

        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode, 
                            State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c
                       from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals(oppli.Id, mbopplis1[0].OpportunityLineItemId__c);
        //System.assertEquals(18, mbopplis1[0].OpportunityLineItemId__c.length());
        //System.assertEquals(opp.Id, mbopplis1[0].Opportunity__c);
        //System.assertEquals(product.Id, mbopplis1[0].Product2__c);
        //System.assertEquals(CurrencyIso, mbopplis1[0].CurrencyIsoCode);
        //System.assertEquals('青海省', mbopplis1[0].State_Text__c);
        //System.assertEquals('LLL', mbopplis1[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbopplis1[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbopplis1[0].Distributor_InCharge_opp__c);
        mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis3.size());

        //delete oppli;

        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis1.size());
        mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());
   }
}