@isTest
private class EquipmentRenewTargetBatchTest {

    static testMethod void testMethod1() {
        system.Test.startTest();
        database.executeBatch(new EquipmentRenewTargetBatch(),200);
       
        system.Test.stopTest();
    }
     static testMethod void testMethod2() {
        List<Account_Service_Of_Target__c> asotList = [select Id,Account_HP__c,OCSM_Period_half__c from Account_Service_Of_Target__c];
        for ( Account_Service_Of_Target__c ac: asotList) {
            ac.OCSM_Period__c = 'FY2022';
            update ac;
        }
        // Account a1 = [select id from account where name = 'NFM007Testhospital'];
        system.Test.startTest();
        // List<String> ids = new List<String>();
        // ids.add(a1.id);
        database.executeBatch(new EquipmentRenewTargetBatch(),200);
       
        system.Test.stopTest();
    }
    @testSetup
     static void makeData(){
         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;
        } 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User MacOwner = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner@sunbridge.com',Job_Category__c = '销售服务', Username = 'olympus_hpowner@sunbridge.com1', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert MacOwner;

        Account hospital = new Account();
        hospital.RecordTypeId = rectCo[0].Id;
        hospital.Name         = 'NFM007Testhospital';
        insert hospital;
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = hospital.Id;
        section.Hospital_Department_Class__c = hospital.Id;
        section.AgentCode_Ext__c = '9999900';
        insert section;

        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId = strategicDep[0].Id;
        depart.Department_Class__c = strategicDep[0].Id;
        depart.Hospital__c = hospital.Id;
        insert depart;

        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        prd1.Category3__c = '纤维镜';
        prd1.Category4__c = '妇科用硬性镜';
        prd1.Maintenance_Price_Year__c = 9999;
        // prd1.Category5__c = '260SL系列';
        prd1.Asset_Model_No__c = '1001';
        prd1.Important_product__c = true;
        prd1.Important_Rroduct_1GI__c = true;
        insert prd1;

        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'Prd2';
        prd2.ProductCode            = 'Prd2';
        prd2.Repair_Product_Code__c = 'Prd2_RP';
        prd2.Name                   = 'Prd2';
        prd2.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        prd2.Category3__c = '光源';
        // prd2.Category4__c = 'SD EndoEYE';
        // prd1.Category5__c = '260SL系列';
        prd2.Asset_Model_No__c = '1001';
        prd2.Maintenance_Price_Year__c = 9999;
        prd2.Important_product__c = true;
        prd2.Important_Rroduct_1GI__c = true;
        insert prd2;

        Product2 prd3 = new Product2();
        prd3.ProductCode_Ext__c     = 'Prd3';
        prd3.ProductCode            = 'Prd3';
        prd3.Repair_Product_Code__c = 'Prd3_RP';
        prd3.Name                   = 'Prd3';
        prd3.Manual_Entry__c        = false;
        prd3.Maintenance_Price_Year__c = 9999;
        // prd1.Category2__c = '本体';
        prd3.Category3__c = '纤维镜';
        // prd2.Category4__c = 'SD EndoEYE';
        // prd1.Category5__c = '260SL系列';
        prd3.Asset_Model_No__c = '1001';
        prd3.Important_product__c = true;
        prd3.Important_Rroduct_1GI__c = true;
        insert prd3;

        Product2 prd4 = new Product2();
        prd4.ProductCode_Ext__c     = 'Prd4';
        prd4.ProductCode            = 'Prd4';
        prd4.Repair_Product_Code__c = 'Prd4_RP';
        prd4.Maintenance_Price_Year__c = 9999;
        prd4.Name                   = 'Prd4';
        prd4.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        prd4.Category4__c = '妇科用硬性镜';
        // prd2.Category4__c = 'SD EndoEYE';
        // prd1.Category5__c = '260SL系列';
        prd4.Asset_Model_No__c = '1001';
        prd4.Important_product__c = true;
        prd4.Important_Rroduct_1GI__c = true;
        insert prd4;

        Consumable_order_details2__c saledet1 = new Consumable_order_details2__c();
        saledet1.Name = 'OCM_01_001001';
        // saledet1.Consumable_order_minor__c = Order1.Id;
        saledet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        saledet1.Bar_Code__c = '0114953170032070111804001082K250BBBBB';
        // saledet1.Consumable_Product__c = prd1.Id;
        saledet1.Used_account__c = section.Id;
        saledet1.Delivery_List_RMB__c = 11;
        saledet1.Box_Piece__c = '盒';
        saledet1.TracingCode__c = 'BBBBB';
        saledet1.SerialLotNo__c = '82K';
        saledet1.Guarantee_period_for_products__c = Date.today().addDays(5);
        saledet1.Sterilization_limit__c = Date.today().addDays(5);
        saledet1.Send_Date__c = null;
        //saledet1.Dealer_Shipment__c = false;
        saledet1.Arrive_date__c = null;
        //saledet1.Dealer_Arrive__c = false;
        saledet1.Used_date__c = null;
        //saledet1.Dealer_Saled__c = false;
        saledet1.Deliver_date__c = Date.today().addDays(-10);
        insert saledet1;

        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = depart.Id;
        contract.Service_Contract_Staff__c = MacOwner.Id;
        contract.Payment_Plan_Sum_First__c = 1;
        contract.Status__c = '合同结束';
        contract.IF_Taget_MC__c = '1';
        contract.Maintenance_Contract_No__c = '11123';
         contract.recordtypeId = 
             Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        contract.Not_Upper_limit_reason__c = ' 1';
        contract.Contract_Start_Date__c = Date.newInstance(2021,10,5);   
        contract.Contract_End_Date__c = Date.newInstance(2021,10,15);       
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.New_Contract_TypeF_Text__c = '续签合同';
        contract.IF_Target_Renew__c='1';
        contract.Contract_Conclusion_Date__c=Date.newInstance(2021,10,5);
        
        insert contract;

        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = depart.Id;
        contract2.IF_Taget_MC__c = '1';
        contract2.Service_Contract_Staff__c = MacOwner.Id;
        contract2.Payment_Plan_Sum_First__c = 1;
        contract2.Status__c = '契約';
        contract2.Maintenance_Contract_No__c = '11123111';
        contract2.Not_Upper_limit_reason__c = ' 2';
        contract2.Contract_Start_Date__c = Date.newInstance(2021,10,5);   
        contract2.Contract_End_Date__c = Date.newInstance(2022,2,1);       
        contract2.SalesOfficeCode_selection__c = '北京RC';
        contract2.IF_Target_Renew__c='1';
        contract2.Contract_Conclusion_Date__c=Date.newInstance(2021,10,5);
        system.assertEquals(contract2.Contract_End_Date__c >= Date.newInstance(2021,10,1) && contract2.Contract_End_Date__c <= Date.newInstance(2022,6,30),true);
        insert contract2;

        asset ast = new asset();
        ast.Name                             = prd1.Name;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = saledet1.SerialLotNo__c + '(' + saledet1.TracingCode__c + ')';
        ast.SLMark__c                        = 'Serial Number';         // 固定 dnInfo.SorLMark;
        ast.Guarantee_period_for_products__c = saledet1.Guarantee_period_for_products__c;
        ast.TracingCode__c                   = saledet1.TracingCode__c;
        ast.Guaranteen_end__c                = saledet1.Sterilization_limit__c;
        ast.Barcode__c                       = saledet1.Bar_Code__c;
        ast.Product_Serial_No__c             = saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
        ast.Asset_Owner__c                   = '经销商资产';
        ast.AccountId                        = depart.Id;
        ast.If_Renew_TargetList__c           = '1';
        ast.Department_Class__c              = section.Id;
        // ast.Asset_owner_delaer_name__c       = hospital.Id;
        ast.Hospital__c                      = hospital.Id;
        ast.CurrentContract__c = contract.Id;
        ast.CurrentContract_Asset_Price__c = 0;
        ast.CurrentContract_F__c=contract.Id;
        insert ast;

        asset ast1 = new asset();
        ast1.Name                             = prd2.Name;
        ast1.Product2Id                       = prd2.Id;
        ast1.SerialNumber                     = saledet1.SerialLotNo__c + '(' + saledet1.TracingCode__c + ')';
        ast1.SLMark__c                        = 'Serial Number';         // 固定 dnInfo.SorLMark;
        ast1.Guarantee_period_for_products__c = saledet1.Guarantee_period_for_products__c;
        ast1.TracingCode__c                   = saledet1.TracingCode__c;
        ast1.Guaranteen_end__c                = saledet1.Sterilization_limit__c;
        ast1.Barcode__c                       = saledet1.Bar_Code__c;
        ast1.Product_Serial_No__c             = saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
        ast1.Asset_Owner__c                   = '经销商资产';
        ast1.AccountId                        = depart.Id;
        ast1.If_Renew_TargetList__c           = '1';
        ast1.Department_Class__c              = section.Id;
        // ast.Asset_owner_delaer_name__c       = hospital.Id;
        ast1.Hospital__c                      = hospital.Id;
        ast1.CurrentContract__c = contract2.Id;
        ast1.CurrentContract_Asset_Price__c = 0;
        ast.CurrentContract_F__c=contract.Id;
        insert ast1;

        asset ast2 = new asset();
        ast2.Name                             = prd3.Name;
        ast2.Product2Id                       = prd3.Id;
        ast2.SerialNumber                     = saledet1.SerialLotNo__c + '(' + saledet1.TracingCode__c + ')';
        ast2.SLMark__c                        = 'Serial Number';         // 固定 dnInfo.SorLMark;
        ast2.Guarantee_period_for_products__c = saledet1.Guarantee_period_for_products__c;
        ast2.TracingCode__c                   = saledet1.TracingCode__c;
        ast2.Guaranteen_end__c                = saledet1.Sterilization_limit__c;
        ast2.Barcode__c                       = saledet1.Bar_Code__c;
        ast2.Product_Serial_No__c             = saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
        ast2.Asset_Owner__c                   = '经销商资产';
        ast2.AccountId                        = depart.Id;
        ast2.If_Renew_TargetList__c           = '1';
        ast2.Department_Class__c              = section.Id;
        // ast.Asset_owner_delaer_name__c       = hospital.Id;
        ast2.Hospital__c                      = hospital.Id;
        ast2.CurrentContract__c = contract.Id;
        ast2.CurrentContract_Asset_Price__c = 0;
        ast.CurrentContract_F__c=contract.Id;
        insert ast2;

        asset ast3 = new asset();
        ast3.Name                             = prd4.Name;
        ast3.Product2Id                       = prd4.Id;
        ast3.SerialNumber                     = saledet1.SerialLotNo__c + '(' + saledet1.TracingCode__c + ')';
        ast3.SLMark__c                        = 'Serial Number';         // 固定 dnInfo.SorLMark;
        ast3.Guarantee_period_for_products__c = saledet1.Guarantee_period_for_products__c;
        ast3.TracingCode__c                   = saledet1.TracingCode__c;
        ast3.Guaranteen_end__c                = saledet1.Sterilization_limit__c;
        ast3.Barcode__c                       = saledet1.Bar_Code__c;
        ast3.Product_Serial_No__c             = saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
        ast3.Asset_Owner__c                   = '经销商资产';
        ast3.AccountId                        = depart.Id;
        ast3.If_Renew_TargetList__c           = '1';
        ast3.Department_Class__c              = section.Id;
        // ast.Asset_owner_delaer_name__c       = hospital.Id;
        ast3.Hospital__c                      = hospital.Id;
        ast3.CurrentContract__c = contract.Id;
        ast3.CurrentContract_Asset_Price__c = 0;
        ast.CurrentContract_F__c=contract.Id;
        insert ast3;


        Account_Service_Of_Target__c asot = new Account_Service_Of_Target__c();
        asot.OCSM_Period_half__c='1H';
        asot.Account_HP__c=hospital.Id;
        insert asot;

        Maintenance_Contract_Asset__c mca = new Maintenance_Contract_Asset__c();
        mca.Asset__c = ast.id;
        mca.Maintenance_Contract__c = contract.id;
        mca.IF_NA_MCA__c = '1';
        insert mca;
    }
}