/**
 * 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
public class SyncMBObjectTest {

    private static Address_Level__c al { get; set; }
    private static Address_Level2__c al2 { get; set; }

    private static void init() {
        al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;

        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;
    }

    // MB_修理
    static testMethod void SyncMBRepairTest() {
        init();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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);
        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;

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'Prd';
        prd.ProductCode            = 'Prd';
        prd.Repair_Product_Code__c = 'Prd_RP';
        prd.Name                   = 'Prd';
        prd.Manual_Entry__c        = false;
        insert prd;

        Asset ast = new Asset();
        ast.Name                   = '保有設備';
        ast.Hospital__c            = hp.Id;
        ast.Department_Class__c    = dcs[0].Id;
        ast.AccountId              = depart.Id;
        ast.Product2Id             = prd.Id;
        ast.SerialNumber           = 'SerialNumber';
        insert ast;

        List<MB_Repair__c> mbrepairs = [select Id, Repair__c from MB_Repair__c];
        //System.assertEquals(0, mbrepairs.size());

        Repair__c repair = new Repair__c();
        repair.Service_Repair_No__c = 'repair';
        repair.Hospital__c          = hp.Id;
        repair.Department_Class__c  = dcs[0].Id;
        repair.Account__c           = depart.Id;
        repair.Delivered_Product__c = ast.Id;
        insert repair;

        List<MB_Repair__c> mbrepairs2 = [select Id, Repair__c from MB_Repair__c];
        //System.assertEquals(1, mbrepairs2.size());
        //System.assertEquals(repair.Id, mbrepairs2[0].Repair__c);

        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        NFM001Controller.isRunning = false;
        update hp;

        mbrepairs2 = [select Id, State_Text__c from MB_Repair__c];
        //System.assertEquals('東京', mbrepairs2[0].State_Text__c);

        delete repair;

        List<MB_Repair__c> mbrepairs3 = [select Id, Repair__c from MB_Repair__c];
        //System.assertEquals(0, mbrepairs3.size());
    }

    // MB_客户
    public static testMethod void SyncMBAccountTest() {
        init();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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<MB_Account__c> mbaccounts = [select Id, Account__c from MB_Account__c];
        //System.assertEquals(0, mbaccounts.size());

        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id);
        insert hp;

        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        NFM001Controller.isRunning = false;
        update hp;

        MB_Account__c mbaccount1 = [select Id, Account__c, State_Text__c from MB_Account__c where Account__c = :hp.Id];
        //System.assertEquals(hp.Id, mbaccount1.Account__c);
        //System.assertEquals('東京', mbaccount1.State_Text__c);

        List<MB_Account__c> mbaccounts2 = [select Id, Account__c, State_Text__c from MB_Account__c order by Account__c];
        //System.assertEquals(9, mbaccounts2.size());
        //System.assertEquals(hp.Id, mbaccounts2[0].Account__c);

        //delete hp;

        List<MB_Account__c> mbaccounts3 = [select Id, Account__c from MB_Account__c];
        //System.assertEquals(8, mbaccounts3.size());
    }

    // MB_报告一览
    static testMethod void SyncMBEventTest() {

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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;

        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = date.today();
        dr.Status__c = '申請中';
        dr.Reporter__c = hpOwner.id;
        insert dr;

        List<MB_Event__c> mbevents = [select Id, Event__c from MB_Event__c];
        //System.assertEquals(0, mbevents.size());

        Event__c event = new Event__c();
        event.Daily_Report__c = dr.Id;
        event.StartDateTime__c = Datetime.now().addHours(-1);
        event.EndDateTime__c = Datetime.now();
        insert event;

        List<MB_Event__c> mbevents2 = [select Id, Event__c from MB_Event__c];
        //System.assertEquals(1, mbevents2.size());
        //System.assertEquals(event.Id, mbevents2[0].Event__c);

        hpOwner.Province__c = '東京';
        update hpOwner;

        mbevents2 = [select Id, State_Text__c from MB_Event__c];
        //System.assertEquals('東京', mbevents2[0].State_Text__c);

        delete event;

        List<MB_Event__c> mbevents3 = [select Id, Event__c from MB_Event__c];
        //System.assertEquals(0, mbevents3.size());
    }

    // MB_报告书
    static testMethod void SyncMBReportTest() {
        init();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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);
        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;

        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = date.today();
        dr.Status__c = '申請中';
        dr.Reporter__c = hpOwner.id;
        insert dr;

        List<MB_Report__c> mbreports = [select Id, Report__c from MB_Report__c];
        //System.assertEquals(0, mbreports.size());

        Report__c rep = new Report__c();
        rep.Daily_Report__c = dr.Id;
        rep.RecordTypeId = '01210000000QekoAAC';
        rep.Manual_Name__c = 'test hospital';
        rep.Status__c = '申請中';
        rep.Hospital_Reference__c   = hp.Id;
        rep.Department_Class_Ref__c = dcs[0].Id;
        rep.Hospital_Department__c  = depart.Id;
        rep.Date__c = System.today().addDays(-1);
        rep.From__c = System.today();
        rep.To__c = System.today().addDays(1);
        rep.Caller_phone_c__c = '123456789';
        rep.Content__c = '①使用方法';
        rep.Detailed_Report__c = 'test';
        rep.Location__c = 'test';
        rep.NTC_TTC_Select__c = 'NTC';
        rep.Product_Category_for_NTC__c = 'test';
        rep.Purpose_Of_Implementation__c = 'test';
        rep.Reason__c = 'test';
        rep.Responsible_Person_HP_c__c = 'test';
        rep.Use_Materials__c = 'test';
        insert rep;

        List<MB_Report__c> mbreports2 = [select Id, Report__c from MB_Report__c];
        //System.assertEquals(1, mbreports2.size());
        //System.assertEquals(rep.Id, mbreports2[0].Report__c);

        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        NFM001Controller.isRunning = false;
        update hp;

        mbreports2 = [select Id, State_text__c from MB_Report__c];
        //System.assertEquals('東京', mbreports2[0].State_text__c);

        delete rep;

        List<MB_Report__c> mbreports3 = [select Id, Report__c from MB_Report__c];
        //System.assertEquals(0, mbreports3.size());
    }

    // MB_日报一览
    static testMethod void SyncMBDailyReportTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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<MB_Daily_Report__c> mbdrs = [select Id, Daily_Report__c from MB_Daily_Report__c];
        //System.assertEquals(0, mbdrs.size());

        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = date.today();
        dr.Status__c = '申請中';
        dr.Reporter__c = hpOwner.id;
        insert dr;

        List<MB_Daily_Report__c> mbdrs2 = [select Id, Daily_Report__c from MB_Daily_Report__c];
        //System.assertEquals(1, mbdrs2.size());
        //System.assertEquals(dr.Id, mbdrs2[0].Daily_Report__c);

        hpOwner.Province__c = '東京';
        update hpOwner;

        mbdrs2 = [select Id, State_Text__c from MB_Daily_Report__c];
        //System.assertEquals('東京', mbdrs2[0].State_Text__c);

        delete dr;

        List<MB_Daily_Report__c> mbdrs3 = [select Id, Daily_Report__c from MB_Daily_Report__c];
        //System.assertEquals(0, mbdrs3.size());
    }

    // MB_维修合同
    static testMethod void SyncMBMaintenanceContractTest() {
        init();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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);
        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;

        List<MB_Maintenance_Contract__c> mbmcs = [select Id, Maintenance_Contract__c from MB_Maintenance_Contract__c];
        //System.assertEquals(0, mbmcs.size());

        Maintenance_Contract__c mc = new Maintenance_Contract__c();
        mc.Name = 'mc';
        mc.Past_update_contract__c = true;
        mc.RecordTypeId = '01210000000QjeFAAS';
        mc.CurrencyIsoCode = 'CNY';
        mc.Status__c = '契約';
        mc.Hospital__c = hp.Id;
        mc.Department_Class__c = dcs[0].Id;
        mc.Department__c = depart.Id;
        mc.Maintenance_Contract_No__c = 'mc';
        mc.Contract_Conclusion_Date__c = Date.today();
        mc.Contract_End_Date__c = Date.today().addDays(1);
        mc.SalesOfficeCode_selection__c = '北京RC';
        insert mc;

        List<MB_Maintenance_Contract__c> mbmcs2 = [select Id, Maintenance_Contract__c from MB_Maintenance_Contract__c];
        //System.assertEquals(1, mbmcs2.size());
        //System.assertEquals(mc.Id, mbmcs2[0].Maintenance_Contract__c);

        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        NFM001Controller.isRunning = false;
        update hp;

        mbmcs2 = [select Id, State_text__c from MB_Maintenance_Contract__c];
        //System.assertEquals('東京', mbmcs2[0].State_text__c);

        delete mc;

        List<MB_Maintenance_Contract__c> mbmcs3 = [select Id, Maintenance_Contract__c from MB_Maintenance_Contract__c];
        //System.assertEquals(0, mbmcs3.size());
    }

    // MB_资产
    static testMethod void SyncMBAssetTest() {
        init();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', 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);
        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;

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'Prd';
        prd.ProductCode            = 'Prd';
        prd.Repair_Product_Code__c = 'Prd_RP';
        prd.Name                   = 'Prd';
        prd.Manual_Entry__c        = false;
        insert prd;

        List<MB_Asset__c> mbassets = [select Id, Asset__c from MB_Asset__c];
        //System.assertEquals(0, mbassets.size());

        Asset ast = new Asset();
        ast.Name                   = '保有設備';
        ast.Hospital__c            = hp.Id;
        ast.Department_Class__c    = dcs[0].Id;
        ast.AccountId              = depart.Id;
        ast.Product2Id             = prd.Id;
        ast.SerialNumber           = 'SerialNumber';
        insert ast;

        List<MB_Asset__c> mbassets2 = [select Id, Asset__c from MB_Asset__c];
        //System.assertEquals(1, mbassets2.size());
        //System.assertEquals(ast.Id, mbassets2[0].Asset__c);

        hp.State_Master__c = al.id;
        hp.City_Master__c = al2.id;
        NFM001Controller.isRunning = false;
        update hp;

        mbassets2 = [select Id, State_text__c from MB_Asset__c];
        //System.assertEquals('東京', mbassets2[0].State_text__c);

        delete ast;

        List<MB_Asset__c> mbassets3 = [select Id, Asset__c from MB_Asset__c];
        //System.assertEquals(0, mbassets3.size());
    }

}