@isTest
private class LastbuyProductHandlerTest {
    @isTest 
	static void TestMethod1() {

        Product2 pro = new Product2();
        pro.Name = 'Testname0215';
        pro.LastbuyProductFLG__c = false;
        insert pro;

        Product2 pro1 = new Product2();
        pro1.Name = 'Testname0216';
        pro1.LastbuyProductFLG__c = false;
        insert pro1;

        Address_Level__c al = new Address_Level__c();
        al.Name = '北京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '北京';
        al2.Name = '海淀区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        Account company = new Account();
        company.Name = 'aiueo病院';
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.State_Master__c = al.Id;
        company.City_Master__c = al2.Id;
        insert company;
        
        Account sct = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company.Id and RecordTypeId = :Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId() Limit 1];
        
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        depart.Name = '*';
        depart.Department_Name__c = 'aiueo診療科';
        depart.ParentId = sct.Id;
        depart.Department_Class__c = sct.Id;
        depart.Hospital__c = company.Id;
        insert depart;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        Opportunity opp1 = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), SAP_Send_OK__c = false, AccountId = depart.Id);
        insert opp1;
        
        LastbuyProduct__c lbp = new LastbuyProduct__c();
        lbp.Name = 'Test0215';
        lbp.ProductName__c = pro.Id;
        lbp.InquiryCode__c = opp1.Id;
        lbp.effectiveFLG__c = true;
        insert lbp;

        lbp.effectiveFLG__c = false;
        update lbp;

        lbp.effectiveFLG__c = true;
        update lbp;

        delete lbp;
    }

    @isTest 
    static void TestMethod2() {

        Product2 pro = new Product2();
        pro.Name = 'Testname0215';
        pro.LastbuyProductFLG__c = false;
        insert pro;

        Address_Level__c al = new Address_Level__c();
        al.Name = '北京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '北京';
        al2.Name = '海淀区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        Account company = new Account();
        company.Name = 'aiueo病院';
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.State_Master__c = al.Id;
        company.City_Master__c = al2.Id;
        insert company;
        
        Account sct = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company.Id and RecordTypeId = :Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId() Limit 1];
        
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        depart.Name = '*';
        depart.Department_Name__c = 'aiueo診療科';
        depart.ParentId = sct.Id;
        depart.Department_Class__c = sct.Id;
        depart.Hospital__c = company.Id;
        insert depart;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        Opportunity opp1 = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), SAP_Send_OK__c = true, AccountId = depart.Id);
        insert opp1;

        Opportunity opp2 = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), SAP_Send_OK__c = false, AccountId = depart.Id);
        insert opp2;

        LastbuyProduct__c lbp = new LastbuyProduct__c();
        lbp.Name = 'Test0215';
        lbp.ProductName__c = pro.Id;
        lbp.InquiryCode__c = opp1.Id;
        lbp.effectiveFLG__c = true;
        insert lbp;

        try{
            lbp.InquiryCode__c = opp2.Id;
            update lbp;
        } catch (Exception e) {
            
        }
    }

    @isTest 
    static void TestMethod3() {

        Product2 pro = new Product2();
        pro.Name = 'Testname0215';
        pro.LastbuyProductFLG__c = false;
        insert pro;

        Address_Level__c al = new Address_Level__c();
        al.Name = '北京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '北京';
        al2.Name = '海淀区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        Account company = new Account();
        company.Name = 'aiueo病院';
        company.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company.State_Master__c = al.Id;
        company.City_Master__c = al2.Id;
        insert company;
        
        Account sct = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company.Id and RecordTypeId = :Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId() Limit 1];
        
        Account depart = new Account();
        depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        depart.Name = '*';
        depart.Department_Name__c = 'aiueo診療科';
        depart.ParentId = sct.Id;
        depart.Department_Class__c = sct.Id;
        depart.Hospital__c = company.Id;
        insert depart;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        Opportunity opp1 = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), SAP_Send_OK__c = true, AccountId = depart.Id);
        insert opp1;

        LastbuyProduct__c lbp = new LastbuyProduct__c();
        lbp.Name = 'Test0215';
        lbp.ProductName__c = pro.Id;
        lbp.InquiryCode__c = opp1.Id;
        lbp.effectiveFLG__c = true;
        insert lbp;

        try{
            delete lbp;
        } catch (Exception e) {
            
        }
    }
}