/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 *
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SyncMBOpportunityTest {
    // MB_询价
    static testMethod void testSyncMBOpportunity() {
        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@sb.com', Username = 'olympus_hpowner@sb.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
                FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sb.com', Username = 'olympus_hpowner@sb.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        list<User> users = new list<User>{hpOwner,hpOwner2};
        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;

        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];

        List<MB_Opportunity__c> mbopps = [select Id, Opportunity__c from MB_Opportunity__c];
        //System.assertEquals(0, mbopps.size());
        Test.startTest();
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            OwnerId = hpOwner.Id,
            SAP_Province__c = '青海省',
            Opportunity_Category__c = 'GIGI',
            Distributor_InCharge_opp__c = true,
            StageName = '引合',
            CurrencyIsoCode = 'CNY',
             Opportunity_sub_owner__c =  hpOwner.id,
            Opportunity_stms_owner__c = hpOwner.id,
            CloseDate = Date.today(),
            AccountId = depart.Id,
            RecordTypeId = rectOpp.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hp.Id,
            Competitor__c = 'A'
        );
        insert opp;

        Opportunity opp2 = new Opportunity(
            Name = 'test opp',
            OwnerId = hpOwner.Id,
            SAP_Province__c = '青海省',
            Opportunity_Category__c = 'GIGI',
            Distributor_InCharge_opp__c = true,
            StageName = '引合',
           
            CurrencyIsoCode = 'CNY',
            CloseDate = Date.today(),
            AccountId = depart.Id,
            RecordTypeId = rectOpp.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hp.Id,
            Competitor__c = 'A',
            Old_Opportunity_ID__c = opp.id
        );
        insert opp2;
         
        mbopps = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Opportunity__c];
        //System.assertEquals(1, mbopps.size());
        //System.assertEquals(opp.Id, mbopps[0].Opportunity__c);
        //System.assertEquals('青海省', mbopps[0].State_Text__c);
        //System.assertEquals('LLL', mbopps[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbopps[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbopps[0].Distributor_InCharge_opp__c);
        List<MB_Target__c> mbTargets = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Target__c];
        //System.assertEquals(0, mbTargets.size());

        opp.SAP_Province__c = '北京市';
        opp.Opportunity_sub_owner__c =  hpOwner2.id;
        opp.Opportunity_stms_owner__c = hpOwner2.id;
        update opp;

        opp.Opportunity_sub_owner__c =  null;
        opp.Opportunity_stms_owner__c = null;
        update opp;
        Test.stopTest();
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        opp.SAP_Province__c = '上海市';
        update opp;

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

        //delete opp;

        mbopps = [select Id, Opportunity__c from MB_Opportunity__c];
        //System.assertEquals(0, mbopps.size());
    }

    // MB_目标
    static testMethod void testSyncMBTarget() {
        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@sb.com', Username = 'olympus_hpowner@sb.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;

        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Target'];

        List<MB_Target__c> mbTargets = [select Id, Opportunity__c from MB_Target__c];
        //System.assertEquals(0, mbTargets.size());

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            OwnerId = hpOwner.Id,
            SAP_Province__c = '青海省',
            Target_category__c = '担当目标',
            Opportunity_Category__c = 'GIGI',
            Distributor_InCharge_opp__c = true,
            StageName = '引合',
            CurrencyIsoCode = 'CNY',
            CloseDate = Date.today(),
            AccountId = depart.Id,
            RecordTypeId = rectOpp.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hp.Id,
            Competitor__c = 'A'
        );
        insert opp;

        mbTargets = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Target__c];
        //System.assertEquals(1, mbTargets.size());
        //System.assertEquals(opp.Id, mbTargets[0].Opportunity__c);
        //System.assertEquals('新疆维吾尔自治区', mbTargets[0].State_Text__c);   // User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
        //System.assertEquals('LLL', mbTargets[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbTargets[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbTargets[0].Distributor_InCharge_opp__c);
        List<MB_Opportunity__c> mbopps = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Opportunity__c];
        //System.assertEquals(1, mbopps.size());

        //delete opp;

        mbTargets = [select Id, Opportunity__c from MB_Target__c];
        //System.assertEquals(0, mbTargets.size());
        mbopps = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Opportunity__c];
        //System.assertEquals(0, mbopps.size());
    }
}