@isTest
private class NFM602ControllerTest {
    static testMethod void testMethod1() {
        
    }
    @testSetup
    static void makeTestRepair(){
    	List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        upsert company;
        // Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
        Account section = [Select Management_Code__c,  Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        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 = 'hpOwner01', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner01@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        Inspection_Report__c ins = new Inspection_Report__c(Name = '*',
            Hospital__c = company.Id
           
        );
        insert ins;
        Inspection_Item__c item = new Inspection_Item__c(
                Inspection_ReportId__c =ins.Id, //'a1Y9D000000i9tWUAQ',
                ItemStatus__c = 'NG'
            );
        insert item;
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Log__c = '[{"strategicDepartmenCd":null,"spotCheckPerson":"00510000005sEEMAA2","spotCheckEquipTotalQty":1,"spotCheckDate":"20210806000000","spotCheckBatch":"10","spotCheckAbnormalQty":0,"ListOfAbnormalDevicesInfo":null,"hospitalCd":"8089815","departmentCd":null,"contractStartDate":null,"contractEquipQty":0,"contractEndDate":null,"contractCd":null,"checkStartTime":"20210806100000","checkEndTime":"20210806120000"}]';
        iflog.Type__c = '602test';
        insert iflog;
    }
    @isTest
    static void testcallout1(){
    	List<Inspection_Report__c> reportList = [select id from Inspection_Report__c];

        Test.startTest();

        List<Id> idList = new List<Id>();
        for(Inspection_Report__c inspection : reportList){
        	idList.add(inspection.Id);
        }
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Log__c = 'test start \n';
        insert iflog;
        NFM602Controller.callout(iflog.Id,idList);
        Test.stopTest();
    }

    @isTest
    static void testcallout2(){
        List<Inspection_Report__c> insList = [select id from Inspection_Report__c];

        Test.startTest();

        List<Id> idList = new List<Id>();
        for(Inspection_Report__c ins : insList){
        	idList.add(ins.Id);
        }
        NFM602Controller.executeNotFuture(null,idList);
        
        Test.stopTest();
    }

    @isTest
    static void testcallout3(){
    	BatchIF_Log__c iflog = [select id from BatchIF_Log__c where Type__c = '602test'];
        NFM602Controller.ManualExecute(iflog.Id);
    }

    @isTest
    static void testcallout4(){
        //维修合同
        Maintenance_Contract__c macnt = new Maintenance_Contract__c();
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, Job_Category__c='销售服务' ,LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner02', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner02@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        RecordType rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        Account hp = new Account(RecordTypeId = rectHp.Id, Name = 'InsReportPDFControllerTestHp1', OwnerId = hpOwner.Id);
        insert hp;
        Account section = [Select Id from Account where RecordType.Name = '戦略科室分類 消化科' and ParentId =: hp.Id];
        Account depart = new Account();
        depart.RecordTypeId = rectDpt.Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'HistoryTestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        macnt.Name = '测试服务合同1';
        macnt.Status__c = '合同中';
        //科室
        macnt.Department__c = depart.id;
        //主担当者
        macnt.Service_Contract_Staff__c = hpOwner.Id ;
        insert macnt;

        //点检计划
        Inspectup_Plan__c Inplan = new Inspectup_Plan__c();
        //币种
        Inplan.CurrencyIsoCode = 'CNY';
        //点检计划编码
        Inplan.Name  = 'Check_plan_Test1';
        //实际执行数      
        Inplan.Actual_Execution_Quantity__c = 0;
        //Maintenance_Contract__c：维修合同
        Inplan.Maintenance_Contract__c = macnt.id;
        //Planned_Start_Date__c：计划开始日
        Inplan.Planned_Start_Date__c = Date.today().addDays(-1);
        // Planned_End_Date__c：计划结束日
        Inplan.Planned_End_Date__c = Date.today().addDays(+2);
        //Timeliness_Rate__c：本次点检及时率
        Inplan.Timeliness_Rate__c = 80;
        //Implementation_Rate__c：本次点检实施率
        Inplan.Implementation_Rate__c = 80;
        insert Inplan;

        Asset ass1 = new Asset(
            AccountId = depart.Id,
            Hospital__c = hp.Id,
            SerialNumber = 'lw0001',
            Name = '测试设备0001:aaaaa'
        );
        Asset ass2 = new Asset(
            AccountId = depart.Id,
            Hospital__c = hp.Id,
            SerialNumber = 'lw0002',
            Name = '测试设备0002:aaaaa'
        );
        insert new Asset[] {ass1, ass2};
        RecordType irType = [select Id,Name from RecordType where IsActive = true and SobjectType = 'Inspection_Report__c' and DeveloperName = 'ContractInspection'];
        
        Inspection_Report__c ir2 = new Inspection_Report__c(Name = 'Test01',
            Hospital__c = hp.Id,
            Reporter__c = hpOwner.id,
            Status__c = '草案中',
            Inspectup_Plan__c = Inplan.Id,
            Contract__c = macnt.id,
            Inspection_Date__c = Date.today(),
            RecordTypeId = irType.Id
        );//点检报告书
        insert ir2;


        Inspection_Item__c ii2 = new Inspection_Item__c(
            Inspection_ReportId__c = ir2.Id,
            AssetId__c = ass2.Id,
            ItemStatus__c = 'NG'
        );
        insert new Inspection_Item__c[] {ii2};

        
        // ir2.Status__c = '申请中';
        // update ir2;
        ir2.Status__c = '批准';
        update ir2;

        // delete ir;
    }
    
}