public without sharing class ServiceMaintenanceTaskController {
	// public List<Maintenance_Contract__c> mcList {get;set;}
	// public List<Maintenance_Task__c> mcTaskList {get;set;}
	// public List<MCTaskInfo> mcTaskInfos ;
	public Integer mcTaskCount {get;set;} //总件数
	public String mcIdList {get; set;}
	public String CSVTitle {get; set;}
	public List<MCTaskInfo> exportInfos {get;set;}
	public String exportIdList {get; set;}
	public Boolean isShowDetail {get; set;}
	Map<String,List<String>> mc2LocationNames;
	Map<String,List<String>> mc2AssetModelNos;

	// Map<String,Maintenance_Task__c> MCTaskMap; //维修合同任务Id
	Map<String,Maintenance_Task__c> MC2TaskMap;

	//page
    public Integer pagesize{get; set;} 
    public Integer pagecount{get; set;} 
    public Integer currentpage{get; set;} 
    public Boolean hasPrevious{get; set;}
    public Boolean hasNext{get; set;}
	public List<MCTaskInfo> pageRecords { get; set; }

	public ServiceMaintenanceTaskController(){
		isShowDetail = false;
		exportInfos = new List<MCTaskInfo>();
		// MCTaskMap = new Map<String,Maintenance_Task__c>();
		// MCtask2InfoMap = new Map<String,MCTaskInfo>();
		if ('005100000069saqAAA'.equals(UserInfo.getUserId()) || '00510000005sEEMAA2'.equals(UserInfo.getUserId())  || 'wenjun_gao@olympus.com.cn'.equals(UserInfo.getUserName())) {
			isShowDetail = true;
		}
		pagesize = Integer.valueof(System.Label.ServiceMainTaskPageSize);
        currentpage = 0;
		init();
	}
    public void init(){
		List<String> mcIds = new List<String>();
		MC2TaskMap = new Map<String,Maintenance_Task__c>(); //维修合同Id
		String query = 'SELECT Contract_RecordType__c,Interval_End_Date__c,Interval_Start_Date__c,Interval__c,'
						+'Is_Generate_Task__c,Maintenance_Consumption_rate__c,Maintenance_Contract__c,'
						+'Name,Nper__c,Reference_Consumption_Rate__c,Task_End_Date__c,'
						+'Task_Status__c,Task_Type__c,Task__c,Maintenance_Contract__r.Department__r.Name,Maintenance_Contract__r.LastMContract1__r.Management_Code__c,Maintenance_Contract__r.LastMContract2__r.Management_Code__c,Maintenance_Contract__r.LastMContract3__r.Management_Code__c,'
						+'Maintenance_Contract__r.LastMContract1__r.Contract_End_Date__c,Maintenance_Contract__r.LastMContract2__r.Contract_End_Date__c,Maintenance_Contract__r.LastMContract3__r.Contract_End_Date__c,'
						+'Maintenance_Contract__r.Hospital__r.Name,Maintenance_Contract__r.Service_Contract_Staff__r.Name,Task__r.Name,Maintenance_Contract__r.Estimate_Asset_Cnt__c,';
		CSVTitle = '';
		// Maintenance_Contract__r.LastMContract4__r.Contract_End_Date__c,Maintenance_Contract__r.LastMContract4__r.Management_Code__c,Maintenance_Contract__r.LastMContract5__r.Management_Code__c,
		List<Schema.FieldSetMember> fieldList=SObjectType.Maintenance_Task__c.FieldSets.Maintenance_Contract_Task.getFields();
		for(Schema.FieldSetMember f : fieldList) {
			query = query+f.getFieldPath() +','; 
			CSVTitle = CSVTitle + '"'+f.getLabel()+'",';
		}
		CSVTitle = CSVTitle + '"上期合同1空白期","上期合同2空白期","上期合同3空白期","故障型号1","故障型号2","故障型号3","故障型号4","故障型号5",'
					+'"故障部位1","故障部位2","故障部位3","故障部位4","故障部位5"';
		query = query + ' Id FROM Maintenance_Task__c WHERE Maintenance_Contract__r.Status__c = \'契約\' '
					+' AND Maintenance_Contract__r.Contract_Consumption_rate__c > '+System.Label.ContractAlert+' AND Contract_RecordType__c != \'VM_Contract\' ORDER BY Nper__c  DESC NULLS FIRST';
		List<Maintenance_Task__c> mcTasks = Database.query(query);
		// List<Maintenance_Task__c> mcTasks = [SELECT Contract_RecordType__c,Id,Interval_End_Date__c,Interval_Start_Date__c,Interval__c,
		// 				Is_Generate_Task__c,Maintenance_Consumption_rate__c,Maintenance_Contract__c,
		// 				Name,Nper__c,Reference_Consumption_Rate__c,Task_End_Date__c,
		// 				Task_Status__c,Task_Type__c,Task__c, Maintenance_Contract__r.Management_Code__c,
		// 				Maintenance_Contract__r.Hospital__c,Maintenance_Contract__r.Service_Contract_Staff__c,
		// 				Maintenance_Contract__r.Contract_End_Date__c,Maintenance_Contract__r.Contract_Consumption_rate__c,
		// 				Maintenance_Contract__r.Estimate_Target__c,Maintenance_Contract__r.Contract_Start_Date__c  
		// 				FROM Maintenance_Task__c
		// 				WHERE Maintenance_Contract__r.Status__c = '契約'AND Contract_RecordType__c != 'VM_Contract' ORDER BY Nper__c  DESC NULLS FIRST];
		//Maintenance_Contract__r.Contract_Consumption_rate__c > 200 AND 
		// mcTaskList = new List<Maintenance_Task__c>();
		// Map<String,Maintenance_Task__c> tempMCTaskMap = new Map<String,Maintenance_Task__c>();
		
		for (Maintenance_Task__c mctask : mcTasks) {
			if (!MC2TaskMap.containsKey(mctask.Maintenance_Contract__c)) {
				MC2TaskMap.put(mctask.Maintenance_Contract__c,mctask);
				// MCTaskMap.put(mctask.Id,mctask);
				mcIds.add(mctask.Maintenance_Contract__c);
				// mcTaskList.add(mctask);
			}
		}
		mc2LocationNames = new Map<String,List<String>>();
		mc2AssetModelNos = new Map<String,List<String>>();
		if (mcIds.size() > 0) {
			//维修合同下修理频率最高的5个故障部位
			List<AggregateResult> locationNames = [SELECT Count(Id),LocationGroupName__c,Repair__r.Maintenance_Contract__c main 
								FROM Repair_reason__c 
								WHERE Repair__c IN ( SELECT Id FROM Repair__c WHERE Maintenance_Contract__c IN :mcIds)  
								AND (Status2__c != '00.删除' AND Status2__c != '00.取消' AND Status2__c != '00.关闭') 
								AND LocationGroupName__c != NULL 
								GROUP BY Repair__r.Maintenance_Contract__c,LocationGroupName__c ORDER BY Count(Id) DESC];
			
			//维修合同下修理频率最高的5个故障型号
			List<AggregateResult> assetModelNos = [SELECT Count(Id),
								Repair__r.Delivered_Product__r.Product2.Asset_Model_No__c no,Repair__r.Maintenance_Contract__c main 
								FROM Repair_reason__c 
								WHERE Repair__c IN ( SELECT Id FROM Repair__c WHERE Maintenance_Contract__c IN :mcIds)  
								AND (Status2__c != '00.删除' AND Status2__c != '00.取消' AND Status2__c != '00.关闭') 
								GROUP BY Repair__r.Maintenance_Contract__c,Repair__r.Delivered_Product__r.Product2.Asset_Model_No__c 
								ORDER BY Count(Id) DESC];
			
			for (String mcId : mcIds) {
				List<String> tempLocationNames = new List<String>();
				for (AggregateResult tempReason : locationNames) {
					if (String.valueOf(tempReason.get('main')) == mcId && tempLocationNames.size() < 5) {
						tempLocationNames.add(String.valueOf(tempReason.get('LocationGroupName__c')));
					}
				}
				mc2LocationNames.put(mcId, tempLocationNames);
				List<String> tempAssetModelNos = new List<String>();
				for (AggregateResult tempReason : assetModelNos) {
					if (String.valueOf(tempReason.get('main')) == mcId && tempAssetModelNos.size() < 5) {
						tempAssetModelNos.add(String.valueOf(tempReason.get('no')));
					}
				}
				mc2AssetModelNos.put(mcId, tempAssetModelNos);
			}
		}
		System.debug('mc2AssetModelNos---'+mc2AssetModelNos+'---mc2LocationNames+---'+mc2LocationNames);
		
		// mcTaskList = MC2TaskMap.values();
		
		// mcTaskInfos = new List<MCTaskInfo>();
		// for (Maintenance_Task__c mctask : MC2TaskMap.values()) {
		// 	MCTaskInfo mcTaskInfo = new MCTaskInfo();
		// 	mcTaskInfo.MCTask = new Maintenance_Task__c();
		// 	mcTaskInfo.MCTask.Maintenance_Contract__c = mctask.Maintenance_Contract__c;
		// 	mcTaskInfo.MCTask.Task__c = mctask.Task__c;
		// 	mcTaskInfo.MCTask.Id = mctask.Id;
		// 	mcTaskInfo.MCTask.Name = mctask.Name;
		// 	mcTaskInfo.MCTask.Maintenance_Consumption_rate__c = mctask.Maintenance_Consumption_rate__c;
		// 	mcTaskInfo.TaskName = mctask.Task__r.Name;
		// 	mcTaskInfo.MC = new Maintenance_Contract__c();
		// 	mcTaskInfo.MC.Management_Code__c = mctask.Maintenance_Contract__r.Management_Code__c; //合同编号
		// 	mcTaskInfo.MC.Hospital__c = mctask.Maintenance_Contract__r.Hospital__c; //医院
		// 	mcTaskInfo.Hospital = mctask.Maintenance_Contract__r.Hospital__r.Name; //医院名
		// 	mcTaskInfo.MC.Department__c = mctask.Maintenance_Contract__r.Department__c; //科室
		// 	mcTaskInfo.Department = mctask.Maintenance_Contract__r.Department__r.Name; //科室名
		// 	mcTaskInfo.MC.Service_Contract_Staff__c = mctask.Maintenance_Contract__r.Service_Contract_Staff__c; //主担当
		// 	mcTaskInfo.ServiceContractStaff = mctask.Maintenance_Contract__r.Service_Contract_Staff__r.Name; //主担当名
		// 	// mcTaskInfo.MC.Dealer__c = mctask.Maintenance_Contract__r.Dealer__c;//经销商名
		// 	// mcTaskInfo.MC.Estimate_Target__c = mcTask.Maintenance_Contract__r.Estimate_Target__c; //报价提交对象
		// 	mcTaskInfo.ContractStartDate = Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).year() + '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).month()+ '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).day() ; //合同开始日
		// 	mcTaskInfo.ContractEndDate = Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).year() + '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).month()+ '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).day() ; //合同结束日
		// 	mcTaskInfo.MC.Contract_Amount__c = mcTask.Maintenance_Contract__r.Contract_Amount__c;//最终用户合同金额
		// 	mcTaskInfo.EstimatePricerangeF = mcTask.Maintenance_Contract__r.Estimate_Price_rangeF__c;//价格范围
		// 	mcTaskInfo.ServiceDiscountRateF = mcTask.Maintenance_Contract__r.Service_discount_RateF__c;//相对标准价格范围的折扣率
		// 	mcTaskInfo.ContractConsumptionrate = mcTask.Maintenance_Contract__r.Contract_Consumption_rate__c; //消费率
		// 	mcTaskInfo.EstimateAssetCnt = mcTask.Maintenance_Contract__r.Estimate_Asset_Cnt__c; //报价资产件数
		// 	mcTaskInfo.LastMContract1 = mcTask.Maintenance_Contract__r.LastMContract1__r.Management_Code__c; //上期合同1
		// 	mcTaskInfo.LastMContract2 = mcTask.Maintenance_Contract__r.LastMContract2__r.Management_Code__c; //上期合同2
		// 	mcTaskInfo.LastMContract3 = mcTask.Maintenance_Contract__r.LastMContract3__r.Management_Code__c; //上期合同3
		// 	// mcTaskInfo.LastMContract4 = mcTask.Maintenance_Contract__r.LastMContract4__r.Management_Code__c; //上期合同4
		// 	// mcTaskInfo.LastMContract5 = mcTask.Maintenance_Contract__r.LastMContract5__r.Management_Code__c; //上期合同5
		// 	if (mcTask.Maintenance_Contract__r.Contract_Start_Date__c != null ) {
		// 		if (mcTask.Maintenance_Contract__r.LastMContract1__r.Contract_End_Date__c!= null) {
		// 			mcTaskInfo.LastMContract1gapTime = mcTask.Maintenance_Contract__r.LastMContract1__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同1空白期
		// 		}
		// 		if (mcTask.Maintenance_Contract__r.LastMContract2__r.Contract_End_Date__c!= null) {
		// 			mcTaskInfo.LastMContract2gapTime = mcTask.Maintenance_Contract__r.LastMContract2__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同2空白期
		// 		}
		// 		if (mcTask.Maintenance_Contract__r.LastMContract3__r.Contract_End_Date__c!= null) {
		// 			mcTaskInfo.LastMContract3gapTime = mcTask.Maintenance_Contract__r.LastMContract3__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同3空白期
		// 		}
		// 		// if (mcTask.Maintenance_Contract__r.LastMContract4__r.Contract_End_Date__c!= null) {
		// 		// 	mcTaskInfo.LastMContract4gapTime = mcTask.Maintenance_Contract__r.LastMContract4__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同4空白期
		// 		// }
				
		// 	}
		// 	if (mc2LocationNames.containsKey(mctask.Maintenance_Contract__c)) {
		// 		if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 0) {
		// 			mcTaskInfo.LocationName1 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(0);//故障部位1
		// 		}
		// 		if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 1) {
		// 			mcTaskInfo.LocationName2 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(1);//故障部位2
		// 		}
		// 		if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 2) {
		// 			mcTaskInfo.LocationName3 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(2);//故障部位3
		// 		}
		// 		if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 3) {
		// 			mcTaskInfo.LocationName4 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(3);//故障部位4
		// 		}
		// 		if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 4) {
		// 			mcTaskInfo.LocationName5 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(4);//故障部位5
		// 		}
		// 	}
		// 	if (mc2AssetModelNos.containsKey(mctask.Maintenance_Contract__c)) {
		// 		if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 0) {
		// 			mcTaskInfo.OwnershipMachineNo1 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(0);//故障型号1
		// 		}
		// 		if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 1) {
		// 			mcTaskInfo.OwnershipMachineNo2 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(1);//故障型号2
		// 		}
		// 		if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 2) {
		// 			mcTaskInfo.OwnershipMachineNo3 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(2);//故障型号3
		// 		}
		// 		if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 3) {
		// 			mcTaskInfo.OwnershipMachineNo4 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(3);//故障型号4
		// 		}
		// 		if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 4) {
		// 			mcTaskInfo.OwnershipMachineNo5 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(4);//故障型号5
		// 		}
		// 	}
			
		// 	mcTaskInfo.Task = new task__c();
		// 	mcTaskInfo.Task.taskStatus__c = mcTask.Task_Status__c; //任务状态
		// 	mcTaskInfo.Task.cancelReason__c = mcTask.Task__r.cancelReason__c; //取消原因
		// 	if (mcTask.Task__r.taskHandleDate__c != null) {
		// 		mcTaskInfo.taskHandleDate = mcTask.Task__r.taskHandleDate__c.year() + '/'+mcTask.Task__r.taskHandleDate__c.month()+'/'+mcTask.Task__r.taskHandleDate__c.day(); //任务确认日
		// 	}
		// 	if (Date.valueOf(mcTask.Task__r.Submit_DateTime__c) != null) {
		// 		mcTaskInfo.SubmitDate = Date.valueOf(mcTask.Task__r.Submit_DateTime__c).year() + '/'+Date.valueOf(mcTask.Task__r.Submit_DateTime__c).month()+ '/'+Date.valueOf(mcTask.Task__r.Submit_DateTime__c).day() ; //日报提交日期
		// 	}
			
		// 	mcTaskInfos.add(mcTaskInfo);
		// 	// MCtask2InfoMap.put(mctask.Id, mcTaskInfo);
		// }
		mcTaskCount = MC2TaskMap.values().size(); //总件数
		pagecount=(mcTaskCount  +  pagesize  - 1) / pagesize; //页数
		//显示第一页
        moveToFirst();
		makeCurrentPageRecords();
	}

	public PageReference assignTasks(){
		Savepoint sp = Database.setSavepoint();
		List<String> mcIds = new List<String>();
		List<task__c> taskList = new List<task__c>();
		Map<String,Maintenance_Task__c> tempMC2McTask = new Map<String,Maintenance_Task__c>();
		List<Maintenance_Task__c> updateMCTask = new List<Maintenance_Task__c>();
		Id recordTypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('MaintenanceTask').getRecordTypeId();
		if (this.mcIdList != null && this.mcIdList != '') {
			mcIds = mcIdList.split(',');
		}
		// System.debug(mcTaskIds+'===='+MCTaskMap);
		Map<String,task__c> taskMap = new Map<String,task__c>();
		if (mcIds.size() > 0) {
			for (String mcId : mcIds) {
				if (MC2TaskMap.containsKey(mcId)) {
					task__c task = new task__c();
                    task.RecordTypeId = recordTypeId;
                    task.taskDifferent__c = '被动任务';
                    task.taskStatus__c = '01 分配';
                    task.Maintenance_Contract__c = MC2TaskMap.get(mcId).Maintenance_Contract__c;
                    task.assignee__c = MC2TaskMap.get(mcId).Maintenance_Contract__r.Service_Contract_Staff__c;
                    task.account__c = MC2TaskMap.get(mcId).Maintenance_Contract__r.Department__c;
                    task.Name = '维修合同消费率预警任务（' + MC2TaskMap.get(mcId).Maintenance_Contract__r.Department__r.Name + '）';
					task.OwnerId = MC2TaskMap.get(mcId).Maintenance_Contract__r.Service_Contract_Staff__c;
					System.debug('name---'+task.Name);
					taskList.add(task);
					tempMC2McTask.put(MC2TaskMap.get(mcId).Maintenance_Contract__c, MC2TaskMap.get(mcId));
					taskMap.put(MC2TaskMap.get(mcId).Id,task);//任务创建后回显任务状态
				}
			}
		}
		// try {

			if (taskList.size() > 0) {
				insert taskList;
				for (task__c task : taskList) {
					if (tempMC2McTask.containsKey(task.Maintenance_Contract__c)) {
						tempMC2McTask.get(task.Maintenance_Contract__c).Task__c = task.Id;
						updateMCTask.add(tempMC2McTask.get(task.Maintenance_Contract__c));
					}
				}
			}
			if (updateMCTask.size() > 0) {
				update updateMCTask;
				for (Maintenance_Task__c tempMCTask : updateMCTask) {
					for (MCTaskInfo mcTaskInfo : pageRecords) {
						if (mcTaskInfo.MCTask.Id == tempMCTask.Id) {
							mcTaskInfo.MCTask.Task__c = tempMCTask.Task__c;
							mcTaskInfo.Task.taskStatus__c = taskMap.get(tempMCTask.Id).taskStatus__c; //任务状态
						}
					}
				}
			}
			ApexPages.Message successMsg = new ApexPages.Message(ApexPages.severity.INFO, '分配成功');
			ApexPages.addMessage(successMsg);
		// } catch (Exception ex) {
		// 	System.debug(ex.getStackTraceString());
        //     ApexPages.addMessages(ex);
        //     Database.rollback(sp);
		// }

		return null;
	}
	public PageReference exportAll(){
		exportInfos = pageRecords;
		return ToPage();
	}
	public void exportSelected(){
		List<String> mcIds = new List<String>();
		if (this.exportIdList != null && this.exportIdList != '') {
			mcIds = exportIdList.split(',');
		}
		if (mcIds.size() > 0) {
			for (String mcId : mcIds) {
				// if (MCtask2InfoMap.containsKey(mcTaskId)) {
				// 	exportInfos.add(MCtask2InfoMap.get(mcTaskId));
				// }
				for (MCTaskInfo info : pageRecords) {
					if (mcId.equals(info.MC.Id)) {
						exportInfos.add(info);
					}
				}
			}
		}
		// System.debug('mcTaskIds:--'+mcTaskIds+'   exportInfos:--'+exportInfos+'   MCtask2InfoMap:-'+MCtask2InfoMap);
		
	}
	public PageReference ToPage(){
		PageReference ref = new Pagereference('/apex/MaintenanceTaskCSVExport');
		
        return ref;
	}
	
	public class MCTaskInfo {
		public Maintenance_Contract__c MC {get; set;}
		public task__c Task {get; set;}
		public Maintenance_Task__c MCTask {get; set;}
		// public String ManagementCode {get;set;} //维修合同询价编码
		public String Hospital {get;set;} //维修合同医院
		public String Department {get;set;} //维修科室
		public String ServiceContractStaff {get;set;} //维修合同主担当
		public String TaskName {get;set;} //任务名
		// public String EstimateTarget {get;set;} //维修合同报价提交对象
		// public Date ContractStartDate {get;set;} //维修合同开始日期
		// public Date ContractEndDate {get;set;} //维修合同结束日期
		public String LastMContract1 {get;set;}
		public String LastMContract2 {get;set;}
		public String LastMContract3 {get;set;}
		// public String LastMContract4 {get;set;}
		// public String LastMContract5 {get;set;}
		public String EstimatePricerangeF {get;set;} //价格范围
		public Decimal ServiceDiscountRateF {get;set;} //相对标准价格范围的折扣率
		public Decimal ContractConsumptionrate {get;set;} //维修合同消费率
		public String LocationName1 {get; set;} //故障部位1
		public String LocationName2 {get; set;} //故障部位2
		public String LocationName3 {get; set;} //故障部位3
		public String LocationName4 {get; set;} //故障部位4
		public String LocationName5 {get; set;} //故障部位5
		public String OwnershipMachineNo1 {get; set;} //故障型号1
		public String OwnershipMachineNo2 {get; set;} //故障型号2
		public String OwnershipMachineNo3 {get; set;} //故障型号3
		public String OwnershipMachineNo4 {get; set;} //故障型号4
		public String OwnershipMachineNo5 {get; set;} //故障型号5
		public Integer LastMContract1gapTime {get; set;} //上期合同1空白期
		public Integer LastMContract2gapTime {get; set;} //上期合同2空白期
		public Integer LastMContract3gapTime {get; set;} //上期合同3空白期
		// public Integer LastMContract4gapTime {get; set;} //上期合同4空白期
		// public String TaskStatus {get; set;} //任务状态
		public String taskHandleDate {get; set;} //任务确认日
		public String SubmitDate {get; set;} //日报提交日期（任务完成日期）
		// public String TaskId {get; set;} //任务Id
		// public String cancelReason {get; set;} //取消理由
		public String ContractStartDate {get; set;} //合同开始日
		public String ContractEndDate {get; set;} //合同结束日
		public Decimal EstimateAssetCnt {get; set;} //报价资产总数
	}

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

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

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

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

        currentpage = currentpage + 1;
        canMove();
        //makeCurrentPageRecords();
    }

    //判断是否可翻页
    public void canMove(){
        hasPrevious = false;
        hasNext = false;
        if(pagecount > 1 && currentpage > 1) hasPrevious = true;
        if(pagecount > 1 && currentpage < pagecount)  hasNext = true;
    }
	
	//刷新当前页
    public PageReference refreshPageSize() {
        makeCurrentPageRecords();
        return null;
    }

	//编辑当前页内容
    public void makeCurrentPageRecords(){
        Integer startIdx;
        Integer endIdx;

        pageRecords = new List<MCTaskInfo>();
        startIdx = (currentpage-1) * pagesize;
        endIdx = (currentpage-1) * pagesize+ pagesize;
        if (endIdx > MC2TaskMap.values().size()){
            endIdx = MC2TaskMap.values().size();
        } 

        for(Integer i=startIdx; i < endIdx ; i++){
			Maintenance_Task__c mctask = MC2TaskMap.values().get(i);
			MCTaskInfo mcTaskInfo = new MCTaskInfo();
			mcTaskInfo.MCTask = new Maintenance_Task__c();
			mcTaskInfo.MCTask.Maintenance_Contract__c = mctask.Maintenance_Contract__c;
			mcTaskInfo.MCTask.Task__c = mctask.Task__c;
			mcTaskInfo.MCTask.Id = mctask.Id;
			mcTaskInfo.MCTask.Name = mctask.Name;
			mcTaskInfo.MCTask.Maintenance_Consumption_rate__c = mctask.Maintenance_Consumption_rate__c;
			mcTaskInfo.TaskName = mctask.Task__r.Name;
			mcTaskInfo.MC = new Maintenance_Contract__c();
			mcTaskInfo.MC.Id = mctask.Maintenance_Contract__r.Id;
			mcTaskInfo.MC.Management_Code__c = mctask.Maintenance_Contract__r.Management_Code__c; //合同编号
			mcTaskInfo.MC.Hospital__c = mctask.Maintenance_Contract__r.Hospital__c; //医院
			mcTaskInfo.Hospital = mctask.Maintenance_Contract__r.Hospital__r.Name; //医院名
			mcTaskInfo.MC.Department__c = mctask.Maintenance_Contract__r.Department__c; //科室
			mcTaskInfo.Department = mctask.Maintenance_Contract__r.Department__r.Name; //科室名
			mcTaskInfo.MC.Service_Contract_Staff__c = mctask.Maintenance_Contract__r.Service_Contract_Staff__c; //主担当
			mcTaskInfo.ServiceContractStaff = mctask.Maintenance_Contract__r.Service_Contract_Staff__r.Name; //主担当名
			// mcTaskInfo.MC.Dealer__c = mctask.Maintenance_Contract__r.Dealer__c;//经销商名
			// mcTaskInfo.MC.Estimate_Target__c = mcTask.Maintenance_Contract__r.Estimate_Target__c; //报价提交对象
			mcTaskInfo.ContractStartDate = Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).year() + '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).month()+ '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_Start_Date__c).day() ; //合同开始日
			mcTaskInfo.ContractEndDate = Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).year() + '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).month()+ '/'+Date.valueOf(mcTask.Maintenance_Contract__r.Contract_End_Date__c).day() ; //合同结束日
			mcTaskInfo.MC.Contract_Amount__c = mcTask.Maintenance_Contract__r.Contract_Amount__c;//最终用户合同金额
			mcTaskInfo.EstimatePricerangeF = mcTask.Maintenance_Contract__r.Estimate_Price_rangeF__c;//价格范围
			mcTaskInfo.ServiceDiscountRateF = mcTask.Maintenance_Contract__r.Service_discount_RateF__c;//相对标准价格范围的折扣率
			mcTaskInfo.ContractConsumptionrate = mcTask.Maintenance_Contract__r.Contract_Consumption_rate__c; //消费率
			mcTaskInfo.EstimateAssetCnt = mcTask.Maintenance_Contract__r.Estimate_Asset_Cnt__c; //报价资产件数
			mcTaskInfo.LastMContract1 = mcTask.Maintenance_Contract__r.LastMContract1__r.Management_Code__c; //上期合同1
			mcTaskInfo.LastMContract2 = mcTask.Maintenance_Contract__r.LastMContract2__r.Management_Code__c; //上期合同2
			mcTaskInfo.LastMContract3 = mcTask.Maintenance_Contract__r.LastMContract3__r.Management_Code__c; //上期合同3
			// mcTaskInfo.LastMContract4 = mcTask.Maintenance_Contract__r.LastMContract4__r.Management_Code__c; //上期合同4
			// mcTaskInfo.LastMContract5 = mcTask.Maintenance_Contract__r.LastMContract5__r.Management_Code__c; //上期合同5
			if (mcTask.Maintenance_Contract__r.Contract_Start_Date__c != null ) {
				if (mcTask.Maintenance_Contract__r.LastMContract1__r.Contract_End_Date__c!= null) {
					mcTaskInfo.LastMContract1gapTime = mcTask.Maintenance_Contract__r.LastMContract1__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同1空白期
				}
				if (mcTask.Maintenance_Contract__r.LastMContract2__r.Contract_End_Date__c!= null) {
					mcTaskInfo.LastMContract2gapTime = mcTask.Maintenance_Contract__r.LastMContract2__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同2空白期
				}
				if (mcTask.Maintenance_Contract__r.LastMContract3__r.Contract_End_Date__c!= null) {
					mcTaskInfo.LastMContract3gapTime = mcTask.Maintenance_Contract__r.LastMContract3__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同3空白期
				}
				// if (mcTask.Maintenance_Contract__r.LastMContract4__r.Contract_End_Date__c!= null) {
				// 	mcTaskInfo.LastMContract4gapTime = mcTask.Maintenance_Contract__r.LastMContract4__r.Contract_End_Date__c.daysBetween(mcTask.Maintenance_Contract__r.Contract_Start_Date__c);//上期合同4空白期
				// }
				
			}
			if (mc2LocationNames.containsKey(mctask.Maintenance_Contract__c)) {
				if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 0) {
					mcTaskInfo.LocationName1 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(0);//故障部位1
				}
				if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 1) {
					mcTaskInfo.LocationName2 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(1);//故障部位2
				}
				if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 2) {
					mcTaskInfo.LocationName3 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(2);//故障部位3
				}
				if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 3) {
					mcTaskInfo.LocationName4 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(3);//故障部位4
				}
				if (mc2LocationNames.get(mctask.Maintenance_Contract__c).size() > 4) {
					mcTaskInfo.LocationName5 = mc2LocationNames.get(mctask.Maintenance_Contract__c).get(4);//故障部位5
				}
			}
			if (mc2AssetModelNos.containsKey(mctask.Maintenance_Contract__c)) {
				if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 0) {
					mcTaskInfo.OwnershipMachineNo1 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(0);//故障型号1
				}
				if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 1) {
					mcTaskInfo.OwnershipMachineNo2 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(1);//故障型号2
				}
				if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 2) {
					mcTaskInfo.OwnershipMachineNo3 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(2);//故障型号3
				}
				if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 3) {
					mcTaskInfo.OwnershipMachineNo4 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(3);//故障型号4
				}
				if (mc2AssetModelNos.get(mctask.Maintenance_Contract__c).size() > 4) {
					mcTaskInfo.OwnershipMachineNo5 = mc2AssetModelNos.get(mctask.Maintenance_Contract__c).get(4);//故障型号5
				}
			}
			
			mcTaskInfo.Task = new task__c();
			mcTaskInfo.Task.taskStatus__c = mcTask.Task_Status__c; //任务状态
			mcTaskInfo.Task.cancelReason__c = mcTask.Task__r.cancelReason__c; //取消原因
			if (mcTask.Task__r.taskHandleDate__c != null) {
				mcTaskInfo.taskHandleDate = mcTask.Task__r.taskHandleDate__c.year() + '/'+mcTask.Task__r.taskHandleDate__c.month()+'/'+mcTask.Task__r.taskHandleDate__c.day(); //任务确认日
			}
			if (Date.valueOf(mcTask.Task__r.Submit_DateTime__c) != null) {
				mcTaskInfo.SubmitDate = Date.valueOf(mcTask.Task__r.Submit_DateTime__c).year() + '/'+Date.valueOf(mcTask.Task__r.Submit_DateTime__c).month()+ '/'+Date.valueOf(mcTask.Task__r.Submit_DateTime__c).day() ; //日报提交日期
			}
			pageRecords.add(mcTaskInfo);
            // pageRecords.add(mcTaskInfos.get(i));

        }
   }
}