@isTest
private class NFM106ControllerTest {
    // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
    private static User getUser() {
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User user1 = new User(Test_staff__c = true, LastName = 'TestMao', FirstName = 'TestMaoF',
                Alias = 'hp', CommunityNickname = 'TestMao', Email = 'Test@sunbridge.com',
                Username = 'Test' + timenow + '@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP',
                TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja',
                ProfileId = System.Label.ProfileId_SystemAdmin,
                Dept__c = '医疗华北营业本部', Job_Category__c = '销售服务', Province__c = '北京');

        List<Profile> p = [Select Id From Profile Where Name = '2S1_销售医院担当'];
        // System.assertEquals(p.size(), 1);

        // User user2 = new User(Test_staff__c = true, LastName = 'TestMao1', FirstName = 'TestMaoF1',
        //         Alias = 'hp', CommunityNickname = 'TestMao1', Email = 'Test1@sunbridge.com',
        //         Username = 'Test1' + timenow + '@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP',
        //         TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja',
        //         ProfileId = p[0].Id,
        //         Dept__c = '医疗华北营业本部', Province__c = '北京');
        // List<User> us = new List<User>();
        // us.add(user1);
        // us.add(user2);
        System.runAs(new User(Id = Userinfo.getUserId())) {
            insert user1;
        }
        return user1;
    }
    // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
    @isTest
    static void testInsert() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        RecordType recordtype = [Select Id,DeveloperName FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'Maintenance_Contract'][0];
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM106TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM106TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        
        // 再取得
        List<Account> accList = new List<Account>();
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        accList.add(depart);
        
        // RecordType recordtype = new RecordType();
        // recordtype.DeveloperName = 'Maintenance_Contract';
        // recordtype.SobjectType = 'Maintenance_Contract__c';
        // insert recordtype;
        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = company.Id;
        contract.Department_Class__c = section.Id;
        contract.Department__c = depart.Id;
        contract.Contract_Start_Date__c = Date.today() - 10;
        contract.Contract_End_Date__c = Date.today() + 10;
        contract.Status__c = '契約';
        contract.Maintenance_Contract_No__c = '10001';
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.Contract_Conclusion_Date__c = Date.today();

        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        contract.recordtypeId = recordtype.id;
        // contract.recordtype.DeveloperName = recordtype.DeveloperName;
        // contract.agree_Upper_limit__c = true;
        contract.Service_Contract_Staff__c = getUser().Id;
        contract.Payment_Plan_Date_First__c = Date.today();
        contract.Payment_Plan_Sum_First__c = 1;
        contract.Payment_Plan_Date_Second__c = Date.today();
        contract.Payment_Plan_Sum_Second__c = 2;
        contract.Payment_Plan_Date_Third__c = Date.today();
        contract.Payment_Plan_Sum_Third__c = 3;
        contract.Payment_Plan_Date_Forth__c = Date.today();
        contract.Payment_Plan_Sum_Forth__c = 4;
        contract.Payment_Plan_Date_Fifth__c = Date.today();
        contract.Payment_Plan_Sum_Fifth__c = 5;
        contract.Payment_Plan_Date_Sixth__c = Date.today();
        contract.Payment_Plan_Sum_Sixth__c = 6;
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end


        // System.Test.startTest();
        insert contract;
        List<Maintenance_Contract__c> contractlist = new List<Maintenance_Contract__c>();
        contractlist.add(contract);
        // NFM106Controller.NFM106Trigger(contractlist, null, null, null);
        contract = [Select Id, Name,RecordType.DeveloperName, Maintenance_Contract_No__c from Maintenance_Contract__c where Id = :contract.Id];
        // System.Test.stopTest();
        // System.assertEquals(contract.RecordType.DeveloperName,NFM106Controller.debug_msg);
        
        // System.assertEquals('NFM106_callout_insert_' + contract.Name, NFM106Controller.debug_msg);
        // System.assertEquals('NFM106_callout_insert_tect contract', NFM106Controller.debug_msg);
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c, Type__c, Log__c, ErrorLog__c, retry_cnt__c from BatchIF_Log__c where Type__c = 'NFM106' order by CreatedDate desc];
        // System.assertEquals(1, bl.size());
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        // System.assertEquals(2, bl.size());
        // System.assertEquals(true, bl[0].Log__c.indexOf(contract.Maintenance_Contract_No__c) >= 0);
        // System.assertEquals(true, bl[0].Log__c.indexOf(company.Management_Code__c) >= 0);
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
    }
    
    @isTest
    static void testUpdate() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM106TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM106TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        
        // 再取得
        List<Account> accList = new List<Account>();
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

		// 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = company.Id;
        contract.Department_Class__c = section.Id;
        contract.Department__c = depart.Id;
        contract.Contract_Start_Date__c = Date.today() - 10;
        contract.Contract_End_Date__c = Date.today() + 10;
        // contract.Status__c = '草案中';
        contract.Status__c = '契約';
        contract.Maintenance_Contract_No__c = '10001';
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.Contract_Conclusion_Date__c = Date.today();
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        contract.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'Maintenance_Contract'].id;
        // contract.agree_Upper_limit__c = true;
        contract.Service_Contract_Staff__c = getUser().Id;
        contract.Payment_Plan_Date_First__c = Date.today();
        contract.Payment_Plan_Sum_First__c = 1;
        contract.Payment_Plan_Date_Second__c = Date.today();
        contract.Payment_Plan_Sum_Second__c = 2;
        contract.Payment_Plan_Date_Third__c = Date.today();
        contract.Payment_Plan_Sum_Third__c = 3;
        contract.Payment_Plan_Date_Forth__c = Date.today();
        contract.Payment_Plan_Sum_Forth__c = 4;
        contract.Payment_Plan_Date_Fifth__c = Date.today();
        contract.Payment_Plan_Sum_Fifth__c = 5;
        contract.Payment_Plan_Date_Sixth__c = Date.today();
        contract.Payment_Plan_Sum_Sixth__c = 6;
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
        insert contract;
        //System.assertEquals('', NFM106Controller.debug_msg);

        contract.Status__c = '契約';
        // System.Test.startTest();
        update contract;
        contract = [Select Id, Name, Maintenance_Contract_No__c from Maintenance_Contract__c where Id = :contract.Id];
        // System.Test.stopTest();

        // System.assertEquals('NFM106_callout_update_' + contract.Name, NFM106Controller.debug_msg);
        // System.assertEquals('NFM106_callout_update_::tect contract, execute()', NFM106Controller.debug_msg);
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c, Type__c, Log__c, ErrorLog__c,retry_cnt__c from BatchIF_Log__c where Type__c = 'NFM106' order by CreatedDate desc];
        // System.assertEquals(1, bl.size());
        // System.assertEquals(2, bl.size());
        // System.assertEquals(true, bl[0].Log__c.indexOf(contract.Maintenance_Contract_No__c) >= 0);
        // System.assertEquals(true, bl[0].Log__c.indexOf(company.Management_Code__c) >= 0);
    }
    @isTest
    static void resend_test() {

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM106TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM106TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        
        // 再取得
        List<Account> accList = new List<Account>();
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        accList.add(company);
        section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
        accList.add(section);
        depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        accList.add(depart);

        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = company.Id;
        contract.Department_Class__c = section.Id;
        contract.Department__c = depart.Id;
        contract.Contract_Start_Date__c = Date.today() - 10;
        contract.Contract_End_Date__c = Date.today() + 10;
        // contract.Status__c = '草案中';
        contract.Status__c = '契約';
        contract.Maintenance_Contract_No__c = '10001';
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.Contract_Conclusion_Date__c = Date.today();
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        contract.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'Maintenance_Contract'].id;
        // contract.agree_Upper_limit__c = true;
        contract.Service_Contract_Staff__c = getUser().Id;
        contract.Payment_Plan_Date_First__c = Date.today();
        contract.Payment_Plan_Sum_First__c = 1;
        contract.Payment_Plan_Date_Second__c = Date.today();
        contract.Payment_Plan_Sum_Second__c = 2;
        contract.Payment_Plan_Date_Third__c = Date.today();
        contract.Payment_Plan_Sum_Third__c = 3;
        contract.Payment_Plan_Date_Forth__c = Date.today();
        contract.Payment_Plan_Sum_Forth__c = 4;
        contract.Payment_Plan_Date_Fifth__c = Date.today();
        contract.Payment_Plan_Sum_Fifth__c = 5;
        contract.Payment_Plan_Date_Sixth__c = Date.today();
        contract.Payment_Plan_Sum_Sixth__c = 6;
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
        insert contract;
        //System.assertEquals('', NFM106Controller.debug_msg);

        // contract.Status__c = '契約';
        // System.Test.startTest();
        // update contract;
        // contract = [Select Id, Name, Maintenance_Contract_No__c from Maintenance_Contract__c where Id = :contract.Id];
        // System.Test.stopTest();

        // System.assertEquals('NFM106_callout_update_' + contract.Name, NFM106Controller.debug_msg);
        // System.assertEquals('NFM106_callout_update_::tect contract, execute()', NFM106Controller.debug_msg);
        // BatchIF_Log__c bif = new BatchIF_Log__c();
        // bif.Log__c = '{"RepairContractLogin":{"Monitoring":{"TransmissionDateTime":"201908011404","Text":"","Tag":"MSGH","Sender":"8402","Receiver":"1330","NumberOfRecord":"1","MessageType":"NFM106","MessageGroupNumber":"20190002043482"},"GeneralData":[{"StartDate":"20190716","SalesOfficeCode":"OCM-GZRC","QuotationAmount":"","PaymentInformation":[{"PaymentTime":"1","PaymentDate":"20190702","PaymentAmount":"10000.00"},{"PaymentTime":"2","PaymentDate":"20190709","PaymentAmount":"20000.00"},{"PaymentTime":"3","PaymentDate":"20190717","PaymentAmount":"30000.00"},{"PaymentTime":"4","PaymentDate":"20190718","PaymentAmount":"40000.00"}],"HospitalName":"364427","FSERemark":"","EndUserNoorAgentNo":"0000023817","EndDate":"20190808","ContractPeriod":"1","ContractNo":"SH-RS-TEST1115","ContractDate":"20190701","Amount":""}]}}';
        // bif.retry_cnt__c = 0;
        // insert bif;
        // List<BatchIF_Log__c> rowbl = [Select Id,
        //                                     Log__c,
        //                                     Log2__c,
        //                                     ErrorLog__c,
        //                                     retry_cnt__c
        //                                 from BatchIF_Log__c
        //                                 where Type__c = 'NFM106'
        //                                 and RowDataFlg__c = true
        //                                 order by CreatedDate desc];

                                        
        // System.assertEquals(1, rowbl.size());
        // System.assertEquals(1,rowbl[0].retry_cnt__c);
        
        // NFM106Controller.execute(rowbl[0],null);

        // NFM106Controller.execute(bif,null);
        // List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
        //         Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //         from BatchIF_Log__c
        //         where Id = :bif.id];
        
        // System.assertEquals(2, bl[0].retry_cnt__c);

        // NFM106Controller.execute(bif,null);
        // bl = [Select Id, Is_Error__c,
        //         Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //         from BatchIF_Log__c
        //         where Id = :bif.id];
        // System.assertEquals(3, bl[0].retry_cnt__c);

    }

}