@isTest
private class MCLastMContractControllerTest {

	private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static Asset createAsset(String input, String accountid, String dcId, String hpId, String serialNo, String prId,String lcId) {
        Asset asset = new Asset();
        asset.Name = input;
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId; 
        asset.Hospital__c = hpId;
        asset.SerialNumber = serialNo;
        asset.Product2Id = prId;
        asset.CurrentContract_F__c = lcId;
        asset.InstallDate = Date.today();
        insert asset;
        return asset;
    }

    public class AssetInfo {
        public Integer lineNo {get; private set;}
        public Boolean rec_checkBox_c {get; set;}
        public Asset rec { get; set; }
        public Maintenance_Contract_Asset_Estimate__c mcae { get; set; }
        public Boolean isManual { get; set; }
        public Decimal orgPrice {get; private set;}
        public Id getRecId() {
            Id rtn = null;
            if (rec != null) {
                rtn = rec.Id;
            }
            return rtn;
        }
        public void setRecId(Id value) {
            // なにもしない
        }
        
        // Manual専用
        public AssetInfo(Integer lineNo) {
            this.lineNo = lineNo;
            this.rec = null;
            this.mcae = new Maintenance_Contract_Asset_Estimate__c(
                isNew__c = true
            );
            this.isManual = true;
            this.rec_checkBox_c = false;
            this.orgPrice = 0;
        }
    }


    static testMethod void testMethod1() {
        ControllerUtil.EscapeNFM001Trigger = true;
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售服务';
        u3.Province__c = '北京市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;

        
        system.runAs(u3) {
            // 病院を作る
            
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'test hospital';
            insert hospital;
            
            // 戦略科室を得る
            List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
            StaticParameter.EscapeSyncProduct2Trigger = true;
            StaticParameter.EscapeAccountTrigger = true;
            StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
            // 診療科を作る
            Account dep = new Account();
            dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
            dep.Name = 'test dep';
            dep.ParentId = strategicDep[0].Id;
            dep.Department_Class__c = strategicDep[0].Id;
            dep.Hospital__c = hospital.Id;
            insert dep;
            
            // 製品を作る
            Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
            insert productA;
            
            // 価格表エントリを作成する        
            PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
            entry.UnitPrice = 0;
            entry.IsActive = true;
            entry.UseStandardPrice = false;
            entry.CurrencyIsoCode = 'CNY';
            entry.Product2Id = productA.Id;
            insert entry;

            // 上期维修合同
            Maintenance_Contract__c lastcontract = new Maintenance_Contract__c();
            lastcontract.Name = 'tect contract';
            lastcontract.Not_Upper_limit_reason__c = 'tect contract';
            lastcontract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
            lastcontract.Hospital__c = hospital.Id;
            lastcontract.Department_Class__c = strategicDep[0].Id;
            lastcontract.Department__c = dep.Id;
            lastcontract.Service_Contract_Staff__c = u3.Id;
            insert lastcontract;
            
            // 納入機器を作成する
            Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,lastcontract.Id);
            Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id,lastcontract.Id);
            Asset asset03 = createAsset('asset03', dep.Id, strategicDep[0].Id, hospital.Id, '00003', productA.Id,lastcontract.Id);
            Asset asset04 = createAsset('asset04', dep.Id, strategicDep[0].Id, hospital.Id, '00004', productA.Id,lastcontract.Id);
            Asset asset05 = createAsset('asset05', dep.Id, strategicDep[0].Id, hospital.Id, '00005', productA.Id,lastcontract.Id);
            
            // 上期维修合同保有设备
            Maintenance_Contract_Asset__c lastmca1 = new Maintenance_Contract_Asset__c();
            lastmca1.Asset__c = asset01.Id;
            lastmca1.Estimate_List_Price__c = 100;
            lastmca1.Maintenance_Contract__c = lastcontract.Id;
            
            // insert new Maintenance_Contract_Asset__c[] {lastmca1};
            insert lastmca1;


            // 维修合同を作成する
            Maintenance_Contract__c contract = new Maintenance_Contract__c();
            contract.Name = 'tect contract';
            contract.Not_Upper_limit_reason__c = 'tect contract';
            contract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
            contract.Hospital__c = hospital.Id;
            contract.Department_Class__c = strategicDep[0].Id;
            contract.Department__c = dep.Id;
            contract.LastMContract1__c = lastcontract.Id;
            contract.LastMContract1_ConCount__c = 5;
            contract.LastMContract2__c = lastcontract.Id;
            contract.LastMContract2_ConCount__c = 5;
            contract.LastMContract3__c = lastcontract.Id;
            contract.LastMContract3_ConCount__c = 5;
            contract.LastMContract4__c = lastcontract.Id;
            contract.LastMContract4_ConCount__c = 5;
            contract.LastMContract5__c = lastcontract.Id;
            contract.LastMContract5_ConCount__c = 5;
            contract.Service_Contract_Staff__c = u3.Id;
            insert contract;

            Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
            mca1.Asset__c = asset01.Id;
            mca1.Estimate_List_Price__c = 100;
            mca1.Maintenance_Contract__c = contract.Id;
            mca1.LastMContract__c = lastcontract.Id;
            
            insert new Maintenance_Contract_Asset__c[] {mca1};
            
            // 维修合同报价を作成する
            Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
                Name = 'contract estimate 1',
                RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract_Estimate__c' and DeveloperName = 'NewMaintenance_Quote'].id,
                Contract_Esti_Start_Date__c = Date.today(),
                Contract_Range__c = 2,
                Maintenance_Contract__c = contract.Id,
                Request_quotation_Amount__c = 1,
                EndUserType__c = '既有用户',
                mainTalksTime__c = 1,
                talksStartDate__c = Date.today(),
                Discount_reason__c = 'test',
                Improve_ConsumptionRate_Idea__c = 'test',
                LastMContract1__c = lastcontract.Id,
                LastMContract1_ConCount__c = 5,
                LastMContract2__c = lastcontract.Id,
                LastMContract2_ConCount__c = 5,
                LastMContract3__c = lastcontract.Id,
                LastMContract3_ConCount__c = 5,
                LastMContract4__c = lastcontract.Id,
                LastMContract4_ConCount__c = 5,
                LastMContract5__c = lastcontract.Id,
                LastMContract5_ConCount__c = 5
            );
            insert contactEsti1;
            //维修合同报价/保有设备
            Maintenance_Contract_Asset_Estimate__c MCAE = new 	Maintenance_Contract_Asset_Estimate__c(
                Maintenance_Contract_Estimate__c = contactEsti1.Id,
                CurrencyIsoCode = 'CNY',
                Asset__c = asset01.Id,
                Last_MContract__c = lastcontract.Id
            );
            insert MCAE;

            System.Test.startTest();
            // 新規
            

            Maintenance_Contract__c mc = [select id,LastMContract1__c,LastMContract2__c,LastMContract3__c,LastMContract4__c,LastMContract5__c FROM Maintenance_Contract__c WHERE id = :contract.Id];
            System.assertEquals(lastcontract.Id, mc.LastMContract1__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract2__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract3__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract4__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract5__c);

            
            // 维修合同报价1
            Apexpages.currentPage().getParameters().put('id', contactEsti1.Id);
            MCLastMContractController controller = new MCLastMContractController();
            controller.init();
            //System.assertEquals(5, controller.productCount2);

            System.Test.stopTest();
        }
    }

    // static testMethod void testMethod2(){            
    //     Account hospital = new Account();
    //     hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
    //     hospital.Name = 'test hospital';
    //     insert hospital;
    //     List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
    //     Account dep = new Account();
    //     dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
    //     dep.Name = 'test dep';
    //     dep.ParentId = strategicDep[0].Id;
    //     dep.Department_Class__c = strategicDep[0].Id;
    //     dep.Hospital__c = hospital.Id;
    //     insert dep;
    //     //1.创建当前合同
    //     Maintenance_Contract__c contract = new Maintenance_Contract__c();
    //     contract.Name = 'tect contract';
    //     contract.Not_Upper_limit_reason__c = 'tect contract';
    //     contract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     contract.Hospital__c = hospital.Id;
    //     contract.Department_Class__c = strategicDep[0].Id;
    //     contract.Department__c = dep.Id;
    //     insert contract;

    //     // 维修合同报价を作成する
    //         Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
    //             Name = 'contract estimate 1',
    //             RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract_Estimate__c' and DeveloperName = 'NewMaintenance_Quote'].id,
    //             Contract_Esti_Start_Date__c = Date.today(),
    //             Contract_Range__c = 2,
    //             Maintenance_Contract__c = contract.Id,
    //             Request_quotation_Amount__c = 1,
    //             EndUserType__c = '既有用户',
    //             mainTalksTime__c = 1,
    //             talksStartDate__c = Date.today(),
    //             Discount_reason__c = 'test',
    //             Improve_ConsumptionRate_Idea__c = 'test',
    //             // LastMContract1__c = lastcontract.Id,
    //             // LastMContract1_ConCount__c = 5,
    //             // LastMContract2__c = lastcontract.Id,
    //             // LastMContract2_ConCount__c = 5,
    //             // LastMContract3__c = lastcontract.Id,
    //             // LastMContract3_ConCount__c = 5,
    //             // LastMContract4__c = lastcontract.Id,
    //             // LastMContract4_ConCount__c = 5,
    //             // LastMContract5__c = lastcontract.Id,
    //             // LastMContract5_ConCount__c = 5
    //         );
    //         insert contactEsti1;

    //     Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
    //     insert productA;
    //     Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,contract.Id);
    //     Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id,contract.Id);

    //     //2.为当前合同下的保有设备创建上一期维修合同
    //     Maintenance_Contract__c lastcontract1 = new Maintenance_Contract__c();
    //     lastcontract1.Name = 'tect contract1';
    //     lastcontract1.Not_Upper_limit_reason__c = 'tect contract1';
    //     lastcontract1.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     lastcontract1.Hospital__c = hospital.Id;
    //     lastcontract1.Department_Class__c = strategicDep[0].Id;
    //     lastcontract1.Department__c = dep.Id;
    //     lastcontract1.Management_Code__c = 'teste11111';
    //     lastcontract1.Contract_Start_Date__c = Date.today();
    //     lastcontract1.Contract_End_Date__c = Date.today();
    //     insert lastcontract1;

    //     Maintenance_Contract__c lastcontract2 = new Maintenance_Contract__c();
    //     lastcontract2.Name = 'tect contract2';
    //     lastcontract2.Not_Upper_limit_reason__c = 'tect contract2';
    //     lastcontract2.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     lastcontract2.Hospital__c = hospital.Id;
    //     lastcontract2.Department_Class__c = strategicDep[0].Id;
    //     lastcontract2.Department__c = dep.Id;
    //     lastcontract2.Management_Code__c = 'teste22222';
    //     lastcontract2.Contract_Start_Date__c = Date.today();
    //     lastcontract2.Contract_End_Date__c = Date.today();
    //     insert lastcontract2;

    //     //3.为当前合同创建保有设备
    //     Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
    //     mca1.Asset__c = asset01.Id;
    //     mca1.Estimate_List_Price__c = 100;
    //     mca1.Maintenance_Contract__c = contract.Id;
    //     mca1.LastMContract__c = lastcontract1.Id;
    //     insert mca1;

    //     Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
    //     mca2.Asset__c = asset02.Id;
    //     mca2.Estimate_List_Price__c = 100;
    //     mca2.Maintenance_Contract__c = contract.Id;
    //     mca2.LastMContract__c = lastcontract1.Id;
    //     insert mca2;


    //     Maintenance_Contract_Asset__c mca3 = new Maintenance_Contract_Asset__c();
    //     mca3.Asset__c = asset02.Id;
    //     mca3.Estimate_List_Price__c = 100;
    //     mca3.Maintenance_Contract__c = contract.Id;
    //     // mca3.LastMContract__c = lastcontract1.Id;
    //     insert mca3;


    //     Maintenance_Contract_Asset__c mca4 = new Maintenance_Contract_Asset__c();
    //     mca4.Asset__c = asset01.Id;
    //     mca4.Estimate_List_Price__c = 100;
    //     mca4.Maintenance_Contract__c = contract.Id;
    //     mca3.LastMContract__c = lastcontract2.Id;
    //     insert mca4;


    //     Maintenance_Contract_Asset__c mca5 = new Maintenance_Contract_Asset__c();
    //     mca5.Asset__c = asset01.Id;
    //     mca5.Estimate_List_Price__c = 100;
    //     mca5.Maintenance_Contract__c = contract.Id;
    //     mca5.LastMContract__c = lastcontract2.Id;
    //     insert mca5;

    //     System.Test.startTest();
    //     Apexpages.currentPage().getParameters().put('id', contactEsti1.Id);
    //     MCLastMContractController controller = new MCLastMContractController();
    //     controller.init();
    //     System.Test.stopTest();
    // }
}