public without sharing class MCLastMContractController {
    // public estimateInfo estimate { get; set; }
    public String targetEstimateId { get; private set; }
    public List<estimateInfo> mccList {get; set;}
    public mccInfo mcc {get;set;}
    public Maintenance_Contract_Estimate__c mcec;

    public void init() {

    	this.targetEstimateId = (String)(ApexPages.currentPage().getParameters().get('id'));
    	if (String.isNotBlank(this.targetEstimateId)) {
    		setThisEstimate();
    	}else{

    	}
    }
    //update by rentongxiao 2020-09-09 start
/*
    private void setThisEstimate() {
        Maintenance_Contract_Estimate__c mCestimate = [SELECT Id,
        		LastMContract1__r.Management_Code__c,LastMContract1__c,LastMContract1_AssetCount__c,LastMContract1_ConCount__c,
        		LastMContract1__r.Contract_Esti_Start_Date__c,LastMContract1__r.Contract_Esti_End_Date__c,LastMContract1__r.Contract_Period__c,
        		LastMContract2__r.Management_Code__c,LastMContract2__c,LastMContract2_AssetCount__c,LastMContract2_ConCount__c,
        		LastMContract2__r.Contract_Esti_Start_Date__c,LastMContract2__r.Contract_Esti_End_Date__c,LastMContract2__r.Contract_Period__c,
        		LastMContract3__r.Management_Code__c,LastMContract3__c,LastMContract3_AssetCount__c,LastMContract3_ConCount__c,
        		LastMContract3__r.Contract_Esti_Start_Date__c,LastMContract3__r.Contract_Esti_End_Date__c,LastMContract3__r.Contract_Period__c,
        		LastMContract4__r.Management_Code__c,LastMContract4__c,LastMContract4_AssetCount__c,LastMContract4_ConCount__c,
        		LastMContract4__r.Contract_Esti_Start_Date__c,LastMContract4__r.Contract_Esti_End_Date__c,LastMContract4__r.Contract_Period__c,
        		LastMContract5__r.Management_Code__c,LastMContract5__c,LastMContract5_AssetCount__c,LastMContract5_ConCount__c,
        		LastMContract5__r.Contract_Esti_Start_Date__c,LastMContract5__r.Contract_Esti_End_Date__c,LastMContract5__r.Contract_Period__c,
        		Maintenance_Contract__r.Management_Code__c,Maintenance_Contract__c,Maintenance_Contract__r.Estimate_Asset_Cnt__c,Maintenance_Contract__r.ConAsset_count__c,
        		Contract_Esti_Start_Date__c,Contract_Esti_End_Date__c,Contract_Range__c

                         FROM Maintenance_Contract_Estimate__c WHERE Id = :this.targetEstimateId];
                if (mCestimate != null) {
                    estimate = new estimateInfo(mCestimate);
                }
	}


	public class estimateInfo {
        public Maintenance_Contract_Estimate__c mc { get; set; }
        public Decimal notcon1 { get; set; }
        public Decimal notcon2 { get; set; }
        public Decimal notcon3 { get; set; }
        public Decimal notcon4 { get; set; }
        public Decimal notcon5 { get; set; }
        public Decimal notconall { get; set; }

        public estimateInfo(Maintenance_Contract_Estimate__c in_mc) {
            mc = in_mc;
            if (mc.LastMContract1__c != null) {
                notcon1 = in_mc.LastMContract1_AssetCount__c - in_mc.LastMContract1_ConCount__c;
            }
            if (mc.LastMContract2__c != null) {
            	notcon2 = in_mc.LastMContract2_AssetCount__c - in_mc.LastMContract2_ConCount__c;
            }
            if (mc.LastMContract3__c != null) {
            	notcon3 = in_mc.LastMContract3_AssetCount__c - in_mc.LastMContract3_ConCount__c;
            }
            if (mc.LastMContract4__c != null) {
            	notcon4 = in_mc.LastMContract4_AssetCount__c - in_mc.LastMContract4_ConCount__c;
            }
            if (mc.LastMContract5__c != null) {
            	notcon5 = in_mc.LastMContract5_AssetCount__c - in_mc.LastMContract5_ConCount__c;
            }
            notconall = in_mc.Maintenance_Contract__r.Estimate_Asset_Cnt__c - in_mc.Maintenance_Contract__r.ConAsset_count__c;
        }
    }
*/


    /*
    1.获取当前合同的所有保有设备的上一期维修合同,group by 上一期维修合同id并count出其对应的续签过来的设备个数
    2.将上一期维修合同的id和从该合同续签过来的保有设备的个数组成一个map集合
    3.通过该map集合,获取其对应的维修合同的各个需要展示的数据,其中续签至申请合同的资产件数及未续签资产件数 需要手动计算
    4.设置值
    */

    private void setThisEstimate(){
        //update by rentongxiao 2020-09-25 JZHG-BQ36H8  把传入的id换为维修合同报价的id
        String mcId = '';
        //1. 根据当前的维修合同报价获取该报价所对应的合同信息
        List<Maintenance_Contract_Estimate__c> tmlist = [SELECT Maintenance_Contract__c,Contract_Esti_Start_Date__c,Contract_Esti_End_Date__c,Contract_Range__c FROM Maintenance_Contract_Estimate__c WHERE Id = :this.targetEstimateId];
        if (tmlist.size() > 0) {
            this.mcec = tmlist.get(0);
            mcId = tmlist.get(0).Maintenance_Contract__c;
        }

        //1.获取当前合同的所有保有设备的上一期维修合同(根据上一期合同id去重) 以及许倩过来的设备的个数
        AggregateResult[] aggArr = [
            SELECT LastMContract__c,count(Id) cnt FROM Maintenance_Contract_Asset__c 
            WHERE Maintenance_Contract__c = :mcId  and LastMContract__c != null group by LastMContract__c 
        ];
        //2.获取维修合同id,以及续签过来设备的个数,放进map
        Map<String,Integer> aggMap = new Map<String,Integer>();
        for (AggregateResult ar :aggArr ) {
            if (String.valueOf(ar.get('LastMContract__c')) != null && String.valueOf(ar.get('LastMContract__c')) != '') {
                if (Integer.valueOf(ar.get('cnt')) != null) {
                    aggMap.put(String.valueOf(ar.get('LastMContract__c')), Integer.valueOf(ar.get('cnt')));
                }
            }

        }

        //3.查询需要展示的维修合同信息
        List<Maintenance_Contract__c> mccList1 = [
            SELECT Id,Management_Code__c,Estimate_Asset_Cnt__c,Contract_Start_Date__c,Contract_End_Date__c,Contract_Period__c,ConAsset_count__c
            ,Contract_Consumption_rate__c,Contract_Amount__c  //LJPH-C7Z5GC 20211027 lt add 
            FROM Maintenance_Contract__c WHERE Id in : aggMap.keySet() order by Contract_Start_Date__c desc
        ];
        //4.赋值
        List<estimateInfo> tempmccList = new List<estimateInfo>();
        for(Maintenance_Contract__c mcc : mccList1){
            System.debug('维修合同::'+mcc);
            for (String temp : aggMap.keySet()) {
                if (temp.equals(mcc.Id)) {
                    estimateInfo es = new estimateInfo(mcc,aggMap.get(mcc.Id));
                    tempmccList.add(es);
                }
            }
        }
        mccList = tempmccList;
        //获取当前合同的信息
        Maintenance_Contract__c tempmcc = [
            SELECT Id,Management_Code__c,Estimate_Asset_Cnt__c,Contract_Esti_Start_Date__c,Contract_Esti_End_Date__c,Contract_Period__c,ConAsset_count__c
            ,Contract_Consumption_rate__c,Contract_Amount__c  //LJPH-C7Z5GC 20211027 lt add     
            FROM Maintenance_Contract__c WHERE Id = :mcId
        ];
        //赋值
        if (tempmcc != null) {
             mcc = new mccInfo(tempmcc,mccList,mcec);
         } 
    }

    public class  estimateInfo  {
        //续签至申请合同的资产件数
        public Decimal concount {get;set;}
        //未续签资产件数   
        public Decimal noconcount {get;set;}

        public Maintenance_Contract__c mc {get;set;}

        public estimateInfo(Maintenance_Contract__c in_mc,Integer count) {
            mc = in_mc;
            concount = count;
            noconcount = in_mc.Estimate_Asset_Cnt__c - count;
        }

    }
    public class  mccInfo  {
        //续签至申请合同的资产件数
        public Decimal allconcount {get;set;}
        //未续签资产件数   
        public Decimal allnoconcount {get;set;}
        public String beginDate {get;set;}
        public String endDate {get;set;}
        public Decimal qijian {get;set;}

        public Maintenance_Contract__c mc {get;set;}

        public mccInfo(Maintenance_Contract__c in_mc,List<estimateInfo> mccList,Maintenance_Contract_Estimate__c mcec){
            mc = in_mc;
            allnoconcount = 0;
            allconcount = 0;
            beginDate = String.valueOf(mcec.Contract_Esti_Start_Date__c); 
            endDate = String.valueOf(mcec.Contract_Esti_End_Date__c);
            qijian = mcec.Contract_Range__c;
        

            for (estimateInfo est : mccList ) {
                allconcount += est.concount;
            }
            allnoconcount = in_mc.Estimate_Asset_Cnt__c - allconcount;

        }

    }
    //update by rentongxiao 2020-09-09 end
}