public with sharing class AgencyCampaignController {
    //产品一览画面用
	public List<product2Info> productInfoListview { get; set; }
    //促销信息
    public Consumable_Promotion__c consumable_P { get; set; }
    //促销产品---录入价格适用
	public Dealer_Product__c dpc { get; set; }
    //产品数量
	public Integer productRecoedsCount { get; set; }
    //
    public Boolean productSearch;
    //page用
    public Integer pagesize{get; set;}
    public Integer totalcount{get; set;}
    public Integer pagecount{get; set;}
    public Integer currentpage{get; set;}
    public Boolean hasPrevious{get; set;}
    public Boolean hasNext{get; set;}
    //促销ID
    public String recordId {get; set;}
    //合同一览
    public List<product2Info> lineInfoList { get; set; }
    public Integer lineNo { get; set; }
    public Integer lineInfoListSize {
        get {
            return lineInfoList == null ? 0 : lineInfoList.size();
        }
    }
    private Map<Id,String> nowContactMap = new Map<Id,String>();

    Map<Id,Dealer_Product__c> agencyedHospitalMap = new Map<Id,Dealer_Product__c> ();
    //当前促销中存在的促销产品
    Map<String, Dealer_Product__c> existRecordsMap = new Map<String, Dealer_Product__c>();
    //不是当前促销的已存在的促销产品
    Map<String, String> existRecordsOtherMap = new Map<String, String>();
    //合同
    public String accContact { get; set; }
    public List<SelectOption> accContactList{get;set;}
    //1-5分类选项
    public Boolean pro2DealerFlag {get ; set;}
    public String keyword { get; set; }
    public String category1 { get; set; }
    public String category2 { get; set; }
    public String category3 { get; set; }
    public String category4 { get; set; }
    public String category5 { get; set; }
    public List<SelectOption> category1OptionList{get;set;}
    public List<SelectOption> category2OptionList{get;set;}
    public List<SelectOption> category3OptionList{get;set;}
    public List<SelectOption> category4OptionList{get;set;}
    public List<SelectOption> category5OptionList{get;set;}





	public AgencyCampaignController() {
        pro2DealerFlag = false;
        productSearch = false;
        recordId = ApexPages.currentPage().getParameters().get('id');
        //size = 100;
        //page
        pagesize = Integer.valueof(system.label.orderdetLimitsize);
        currentpage = 0;
        accContactList= new List<SelectOption>();
        accContactList.add(new SelectOption('', '--无--'));
        lineInfoList = new List<product2Info>();
	}

	public void init(){
        //所有产品
        List<product2Info> productInfoList = new List<product2Info>();
		geCategory();
		dpc = new Dealer_Product__c();
		agencyedHospitalMap = new Map<Id,Dealer_Product__c>();
        //获取所有有效合同
        List<Account> validContract = [SELECT Id,Agent_Ref__c,
                                            Agent_Ref__r.Name,Name,ParentId
                                        FROM Account
                                        WHERE Contract_Department_Class__c = 'ET'
                                        AND Contract_Decide_Start_Date__c <= :Date.Today()
                                        AND Contract_Decide_End_Date__c >= :Date.Today()];
        if(validContract.size() > 0){
            Integer nowLine = lineInfoListSize;
            for(Account oldcontact: validContract){
                nowContactMap.put(oldcontact.Id, oldcontact.Name);
                product2Info newopi = new product2Info(nowLine + 1,oldcontact);
                nowLine ++;
                lineInfoList.add(newopi);
            }
        }
        if(String.isNotBlank(recordId)){
            //获取已经存在的促销合同信息
            List<Account> oldcontactList = [select Id,Agent_Ref__c,
                                                Agent_Ref__r.Name,Name,ParentId
                                            from Account
                                            WHERE Id in (
                                                select Dealer_Contact__c
                                                from Dealer_Product__c
                                                WHERE Promotion_No__c = :recordId)];
            for(Account existContact : oldcontactList){
                for(product2Info con : lineInfoList){
                    if(existContact.Id == con.accountIn.Id){
                        con.check = true;
                    }
                }

            }
        }
        lineInfoList.sort();

        productInfoListview = new List<product2Info>();
		productInfoList = searchAllProduct();
        //已存在的产品促销信息
        if(String.isNotBlank(recordId)){
            consumable_P = [select Id,Name FROM Consumable_Promotion__c WHERE Id = :recordId];
            if(nowContactMap.size() > 0){
                List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                            Special_Campaign_Price__c,Campaign_StartDate__c,
                                                            Campaign_EndDate__c,OrderGoods_Limit__c
                                                        FROM Dealer_Product__c
                                                        WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                                        AND Promotion_No__c = :recordId];
                for(Dealer_Product__c ahlc : dealerProductList){
                    existRecordsMap.put('' + ahlc.Dealer_Contact__c + ahlc.Dealer_Product2__c,ahlc);
                    agencyedHospitalMap.put(ahlc.Dealer_Product2__c, ahlc);
                }
                for(product2Info prd : productInfoList){
                    if(agencyedHospitalMap.containsKey(prd.pro.Id)){
                        prd.dealerPro = agencyedHospitalMap.get(prd.pro.Id);
                    }
                }
            }
        }
        productInfoList.sort();

        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize;
        //显示第一页
        moveToFirst();
        makeCurrentPageRecords(productInfoList);
	}
    //获取所有产品
    public List<product2Info> searchAllProduct(){
        //所有的产品
        List<product2Info> allProductList = new List<product2Info>();
        List<Product2__c> accountInfo = [select Id, Name,Name__c,
                                        Asset_Model_No__c,Category1__c,
                                        Category2__c,Category3__c,Category4__c,
                                        Category5__c,SFDA_Status__c
                                    FROM Product2__c
                                    WHERE Estimation_Entry_Possibility__c = '○'
                                    order by Name__c];
        for (Integer i = 0; i < accountInfo.size(); i++) {
            allProductList.add(new product2Info(accountInfo[i]));
        }
        return allProductList;
    }
    //分类列表制作
	public void geCategory(){
		AggregateResult[] categoryList = [select Count(id), Category1_text__c c1c from Product2__c group by Category1_text__c];
		category1OptionList = new List<SelectOption>();
		category1OptionList.add(new SelectOption('', '--无--'));
		for(AggregateResult category3Search : categoryList) {
			String deliverycnt = String.valueOf(category3Search.get('c1c'));
			if(String.isNotBlank(deliverycnt)){
				category1OptionList.add(new SelectOption(deliverycnt,deliverycnt));
			}
		}
		category2OptionList = new List<SelectOption>();
		category2OptionList.add(new SelectOption('', '--无--'));
		category3OptionList = new List<SelectOption>();
		category3OptionList.add(new SelectOption('', '--无--'));
		category4OptionList = new List<SelectOption>();
		category4OptionList.add(new SelectOption('', '--无--'));
		category5OptionList = new List<SelectOption>();
		category5OptionList.add(new SelectOption('', '--无--'));
	}
    //加载第2分类
	public void showcategory2() {
        AggregateResult[] categoryList = [select Count(id), Category2_text__c c2c from Product2__c WHERE Category1_text__c = :category1  group by Category2_text__c];
        category2OptionList = new List<SelectOption>();
		category2OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
			String deliverycnt = String.valueOf(category3Search.get('c2c'));
			if(String.isNotBlank(deliverycnt)){
				category2OptionList.add(new SelectOption(deliverycnt,deliverycnt));
			}
		}

    }
    //加载第3分类
    public void showcategory3() {
        AggregateResult[] categoryList = [select Count(id), Category3_text__c c3c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2  group by Category3_text__c];
        category3OptionList = new List<SelectOption>();
		category3OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
			String deliverycnt = String.valueOf(category3Search.get('c3c'));
			if(String.isNotBlank(deliverycnt)){
				category3OptionList.add(new SelectOption(deliverycnt,deliverycnt));
			}
		}

    }
    //加载第4分类
    public void showcategory4() {
        AggregateResult[] categoryList = [select Count(id), Category4_text__c c4c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2 AND Category3_text__c = :category3  group by Category4_text__c];
        category4OptionList = new List<SelectOption>();
		category4OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
			String deliverycnt = String.valueOf(category3Search.get('c4c'));
			if(String.isNotBlank(deliverycnt)){
				category4OptionList.add(new SelectOption(deliverycnt,deliverycnt));
			}
		}

    }
    //加载第5分类
    public void showcategory5() {
        AggregateResult[] categoryList = [select Count(id), Category5_text__c c5c from Product2__c WHERE Category1_text__c = :category1 AND Category2_text__c = :category2 AND Category3_text__c = :category3 AND Category4_text__c = :category4  group by Category5_text__c];
        category5OptionList = new List<SelectOption>();
		category5OptionList.add(new SelectOption('', '--无--'));
        for(AggregateResult category3Search : categoryList) {
			String deliverycnt = String.valueOf(category3Search.get('c5c'));
			if(String.isNotBlank(deliverycnt)){
				category5OptionList.add(new SelectOption(deliverycnt,deliverycnt));
			}
		}

    }
    //已存在促销产品检测
    public void promotioncheck() {
        List<Dealer_Product__c> existDealerProList = new List<Dealer_Product__c>();
        List<String> selectProId = new List<String>();
        for(product2Info proinfo:productInfoListview){
            if(proinfo.check == true){
                selectProId.add(proinfo.Pro.Id);
            }
        }
        //编辑促销
        if(String.isNotBlank(recordId)){
            existDealerProList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                        Special_Campaign_Price__c,Campaign_StartDate__c,
                                        Campaign_EndDate__c,Promotion_No__r.Name,OrderGoods_Limit__c
                                    FROM Dealer_Product__c
                                    WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                    AND Promotion_No__c != :recordId
                                    AND Dealer_Product2__c in :selectProId
                                    AND Campaign_StartDate__c <= :Date.Today()
                                    AND Campaign_EndDate__c >= :Date.Today()];
        }else{
            existDealerProList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                        Special_Campaign_Price__c,Campaign_StartDate__c,
                                        Campaign_EndDate__c,Promotion_No__r.Name,OrderGoods_Limit__c
                                    FROM Dealer_Product__c
                                    WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                    AND Dealer_Product2__c in :selectProId
                                    AND Campaign_StartDate__c <= :Date.Today()
                                    AND Campaign_EndDate__c >= :Date.Today()];
        }
        for(Dealer_Product__c record : existDealerProList){
            existRecordsOtherMap.put('' + record.Dealer_Contact__c + record.Dealer_Product2__c, record.Promotion_No__r.Name);
        }

    }

	public PageReference save() {
		List<Dealer_Product__c> Ins = new List<Dealer_Product__c>();
        List<Dealer_Product__c> detailsdet = new List<Dealer_Product__c>();
        List<String> productId = new List<String>();
        List<String> contactId = new List<String>();
        if(lineInfoList.size()<= 0){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '没有经销商合同信息，请确认。'));
            return null;
        }
        promotioncheck();
		Savepoint sp = Database.setSavepoint();
		try{
            Consumable_Promotion__c cpc = new Consumable_Promotion__c(Id = recordId);
            upsert cpc;
            recordId = cpc.Id;
            for(product2Info acc : lineInfoList){
                if(acc.check == true){
                    contactId.add(acc.accountIn.Id);
                    for(product2Info proinfo:productInfoListview){
                        if(proinfo.check == true){
                            if(existRecordsOtherMap.containsKey('' + acc.accountIn.Id + proinfo.pro.Id)){
                                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '促销 ' + existRecordsOtherMap.get('' + acc.accountIn.Id + proinfo.pro.Id) + ' 的合同 ' + acc.accountIn.Name + ' 中，已经存在产品 ' + proinfo.pro.Name + ' 的有效促销。'));
                                return null;
                            }
                            if(proinfo.dealerPro.Special_Campaign_Price__c != null){
                                if(proinfo.dealerPro.Campaign_StartDate__c == null){
                                    proinfo.dealerPro.Campaign_StartDate__c.addError('请输入产品 ' + proinfo.Pro.Name__c + ' 的促销开始日。');
                                    return null;
                                }
                                if(proinfo.dealerPro.OrderGoods_Limit__c == null){
                                    proinfo.dealerPro.OrderGoods_Limit__c.addError('请输入产品 ' + proinfo.Pro.Name__c + ' 的采购限制数量。');
                                    return null;
                                }
                                if(proinfo.dealerPro.Campaign_EndDate__c == null){
                                    proinfo.dealerPro.Campaign_EndDate__c.addError('请输入产品 ' + proinfo.Pro.Name__c + ' 的促销结束日。');
                                    return null;
                                }
                                if(proinfo.dealerPro.Campaign_EndDate__c < proinfo.dealerPro.Campaign_StartDate__c){
                                    proinfo.dealerPro.Campaign_EndDate__c.addError('产品 ' + proinfo.Pro.Name__c + ' 的促销结束日必须大于促销开始日。');
                                    return null;
                                }
                            }
                            Dealer_Product__c ahl = new Dealer_Product__c();
                            if(existRecordsMap.containsKey('' + acc.accountIn.Id + proinfo.pro.Id)){
                                ahl.id = existRecordsMap.get('' + acc.accountIn.Id + proinfo.pro.Id).Id;
                            }
                            ahl.Dealer_Contact__c = acc.accountIn.Id;
                            ahl.Dealer_Product2__c = proinfo.pro.Id;
                            ahl.Promotion_No__c = cpc.Id;
                            ahl.Special_Campaign_Price__c = proinfo.dealerPro.Special_Campaign_Price__c;
                            ahl.OrderGoods_Limit__c = proinfo.dealerPro.OrderGoods_Limit__c;
                            ahl.Campaign_StartDate__c = proinfo.dealerPro.Campaign_StartDate__c;
                            ahl.Campaign_EndDate__c = proinfo.dealerPro.Campaign_EndDate__c;
                            Ins.add(ahl);
                        }else if(proinfo.dealerPro.Id != null && proinfo.dealerPro.Special_Campaign_Price__c == null){
                            productId.add(proinfo.Pro.Id);
                            detailsdet.add(proinfo.dealerPro);
                        }
                    }
                }
            }
            //删除合同后的多余数据
            List<Dealer_Product__c> deleteRecords = [select Id FROM Dealer_Product__c
                                                            WHERE Promotion_No__c = :recordId
                                                            AND Dealer_Contact__c not in : contactId
                                                            //AND Dealer_Product2__c in : productId
                                                            ];
            if(deleteRecords.size() >0){
                delete deleteRecords;
            }
            //删除价格后的多余数据
            List<Dealer_Product__c> deleteRecords2 = [select Id FROM Dealer_Product__c
                                                            WHERE Promotion_No__c = :recordId
                                                            AND Dealer_Contact__c in : contactId
                                                            AND Dealer_Product2__c in : productId
                                                            ];
            if(deleteRecords2.size() >0){
                delete deleteRecords2;
            }
            if(Ins.size()>0){
                upsert Ins;
            }
		}catch(Exception e){
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return  null;
        }
        return setpage();

	}
    // 返回
    public PageReference setpage(){
        PageReference ref = new Pagereference('/' + recordId);
        ref.setRedirect(true);
        return ref;
    }

    // 检索
    public void searchHospital() {
        productSearch = true;
        //所有的产品
        List<product2Info> productInfoList = new List<product2Info>();
        //检索产品
        String soql = this.makeSoql(category1,category2,category3,category4,category5,keyword,pro2DealerFlag);
        List<Product2__c> product2Selected = Database.query(soql);
        for (Integer i = 0; i < product2Selected.size(); i++) {
            productInfoList.add(new product2Info(product2Selected[i]));
        }
        //已存在的产品促销信息
        agencyedHospitalMap.clear();
        if(String.isNotBlank(recordId)){
            if(nowContactMap.size() > 0){
                List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                            Special_Campaign_Price__c,Campaign_StartDate__c,
                                                            Campaign_EndDate__c,OrderGoods_Limit__c
                                                        FROM Dealer_Product__c
                                                        WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                                        AND Promotion_No__c = :recordId];
                for(Dealer_Product__c ahlc : dealerProductList){
                    agencyedHospitalMap.put(ahlc.Dealer_Product2__c, ahlc);
                }
                for(product2Info prd : productInfoList){
                    if(agencyedHospitalMap.containsKey(prd.pro.Id)){
                        prd.dealerPro = agencyedHospitalMap.get(prd.pro.Id);
                    }
                }
            }
        }
        productInfoList.sort();
        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize; 
        //显示第一页
        moveToFirst();
        makeCurrentPageRecords(productInfoList);
    }
    //产品检索
    public String makeSoql(String category1,String category2,String category3,String category4,String category5,String keyword,Boolean pro2DealerFlag){
    	String soql = 'select Id, Name,Name__c,Asset_Model_No__c,Category1__c,Category2__c,Category3__c,Category4__c,';
        soql += ' Category5__c,SFDA_Status__c FROM Product2__c WHERE Estimation_Entry_Possibility__c = \'○\' ';
       	if(String.isNotBlank(category1)){
            soql += ' AND Category1__c = \'' +  category1 + '\'';
        }
        if(String.isNotBlank(category2)){
            soql += ' AND Category2__c = \'' +  category2 + '\'';
        }
        if(String.isNotBlank(category3)){
            soql += ' AND Category3__c = \'' +  category3 + '\'';
        }
        if(String.isNotBlank(category4)){
            soql += ' AND Category4__c = \'' +  category4 + '\'';
        }
        if(String.isNotBlank(category5)){
            soql += ' AND Category5__c = \'' +  category5 + '\'';
        }
        if(String.isNotBlank(keyword)){
            soql += ' AND Name__c like \'%' + String.escapeSingleQuotes(keyword.replaceAll('%', '\\%')) + '%\'';
        }
        if(Pro2DealerFlag){
            soql += ' AND Pro2_Dealer_Object__c = true';
        }
        soql += ' order by Name__c ';
        return soql;
    }

    //首页
    public void moveToFirst(){
        if(currentpage == 1) return;
        currentpage = 1;
        canMove();
    }

    //尾页
    public void moveToLast(){
        if(currentpage >= pagecount) return;
        currentpage = pagecount;
        canMove();
    }

    //上一页
    public void moveToPrevious(){
        if(currentpage == 1) return;
        currentpage = currentpage -1;
        canMove();
    }

    //下一页
    public void moveToNext(){
        if(currentpage >= pagecount) return;

        currentpage = currentpage + 1;
        canMove();
    }

    //判断是否可翻页
    public void canMove(){
        hasPrevious = false;
        hasNext = false;
        if(pagecount > 1 && currentpage > 1) hasPrevious = true;
        if(pagecount > 1 && currentpage < pagecount)  hasNext = true;
    }


    //刷新当前页
    public PageReference refreshPageSize() {
        //所有的产品
        List<product2Info> productInfoList = new List<product2Info>();
        if(productSearch){
            //检索产品
            String soql = this.makeSoql(category1,category2,category3,category4,category5,keyword,pro2DealerFlag);
            List<Product2__c> product2Selected = Database.query(soql);
            for (Integer i = 0; i < product2Selected.size(); i++) {
                productInfoList.add(new product2Info(product2Selected[i]));
            }
            //已存在的产品促销信息
            agencyedHospitalMap.clear();
            if(String.isNotBlank(recordId)){
                if(nowContactMap.size() > 0){
                    List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                                Special_Campaign_Price__c,Campaign_StartDate__c,
                                                                Campaign_EndDate__c,OrderGoods_Limit__c
                                                            FROM Dealer_Product__c
                                                            WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                                            AND Promotion_No__c = :recordId];
                    for(Dealer_Product__c ahlc : dealerProductList){
                        agencyedHospitalMap.put(ahlc.Dealer_Product2__c, ahlc);
                    }
                    for(product2Info prd : productInfoList){
                        if(agencyedHospitalMap.containsKey(prd.pro.Id)){
                            prd.dealerPro = agencyedHospitalMap.get(prd.pro.Id);
                        }
                    }
                }
            }
        }else{
            productInfoList = searchAllProduct();
            //已存在的产品促销信息
            if(String.isNotBlank(recordId)){
                consumable_P = [select Id,Name FROM Consumable_Promotion__c WHERE Id = :recordId];
                if(nowContactMap.size() > 0){
                    List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                                Special_Campaign_Price__c,Campaign_StartDate__c,
                                                                Campaign_EndDate__c,OrderGoods_Limit__c
                                                            FROM Dealer_Product__c
                                                            WHERE Dealer_Contact__c in :nowContactMap.keySet()
                                                            AND Promotion_No__c = :recordId];
                    for(Dealer_Product__c ahlc : dealerProductList){
                        existRecordsMap.put('' + ahlc.Dealer_Contact__c + ahlc.Dealer_Product2__c,ahlc);
                        agencyedHospitalMap.put(ahlc.Dealer_Product2__c, ahlc);
                    }
                    for(product2Info prd : productInfoList){
                        if(agencyedHospitalMap.containsKey(prd.pro.Id)){
                            //prd.check = true;
                            prd.dealerPro = agencyedHospitalMap.get(prd.pro.Id);
                        }
                    }
                }
            }
        }
        productInfoList.sort();
        //总件数
        totalcount = productInfoList.size();
        //页数
        pagecount=(totalcount  +  pagesize  - 1) / pagesize;
        makeCurrentPageRecords(productInfoList);
        //makeCurrentPageRecords();
        return null;
    }

    //编辑当前页内容
    public void makeCurrentPageRecords(List<product2Info> productInfoList){
        Integer startIdx;
        Integer endIdx;
        productInfoListview = new List<product2Info>();
        startIdx = (currentpage-1) * pagesize;
        endIdx = (currentpage-1) * pagesize+ pagesize;
        if (endIdx > productInfoList.size()){
            endIdx = productInfoList.size();
        } 

        for(Integer i=startIdx; i < endIdx ; i++){
            productInfoListview.add(productInfoList.get(i));
        }

        productRecoedsCount = productInfoListview.size();
   }

	class product2Info implements Comparable {
        public Boolean check { get; set; }
        public Decimal campaignPrice { get; set; }
        public Date campaignSdate { get; set; }
        public Date campaignEdate { get; set; }
        public Product2__c pro { get; set; }
        public Dealer_Product__c dealerPro { get; set; }
        public Integer line { get; set; }
        public Account accountIn { get; set; }


        public product2Info(Product2__c e) {
            check = false;
            pro = e;
            dealerPro = new Dealer_Product__c();
        }
        public product2Info(Integer in_line, Account e) {
            check = false;
            line = in_line;
            accountIn = e;
        }

        public Integer compareTo(Object compareTo) {
            product2Info compareToesd =(product2Info)compareTo;
            Integer returnValue = 0;
            if(check == true){
                returnValue = -1;
            }else if(dealerPro != null){
                if (dealerPro.Special_Campaign_Price__c == null){
                    returnValue = 1;
                }else{
                    if (dealerPro.Special_Campaign_Price__c > compareToesd.dealerPro.Special_Campaign_Price__c) {
                        returnValue = -1;
                    } else if (dealerPro.Special_Campaign_Price__c < compareToesd.dealerPro.Special_Campaign_Price__c) {
                        returnValue = 1;
                    }
                }
            }else{
                returnValue = 1;
            }
            return returnValue;
        }
    }
}