global class UpdateGastroscopyBatch implements Database.Batchable<sObject> {
	public String query;
	public String conId;
	public List<String> conIds;

	global UpdateGastroscopyBatch(){
		this.query = query;
	}

	global UpdateGastroscopyBatch(List<String> conIds){
		this.query = query;
		//this.conId = conId;
		this.conIds = conIds;
	}

	global Database.QueryLocator start(Database.BatchableContext bc) {
		// query = 'select Id,Maintenance_Contract__c,IS_GASTROSCOPY__c,IF_URF__c,IF_BRONCHOSCOPE__c,IF_PERIPHERAL__c,IF_OTHERTUBE__c,First_Month_RepairAmount__c,Second_Month_RepairAmount__c,Third_Month_RepairAmount__c,Maintenance_Contract__r.RecordType_DeveloperName__c,Asset__r.Maintenance_Price_Month__c from Maintenance_Contract_Asset__c  ';
		query = 'select Id,Estimate_Asset_Cnt__c,Contract_mirror_type__c,GastroscopyRepairCost__c,Contract_Start_Date__c,';
		query += ' BronchoscopeRepairCost__c,PreipheralRepairCost__c,keepRepairInContractMoney__c,keepRepairInContract__c,Contract_Conclusion_Date__c from Maintenance_Contract__c  ';
		query += ' where (Status__c = \'契約\' or Status__c = \'契約満了\') and Contract_mirror_type__c = null';

		// if (conId != null && conId != '') {
		//     query += ' where  Maintenance_Contract__c = :conId ';
		// }
		if(conIds != null && conIds.size() != 0) {
			query += ' and id in :conIds';
		}

		return Database.getQueryLocator(query);
	}


	global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
		//上传对象
		Map<String,Maintenance_Contract__c> MCMap = new Map<String,Maintenance_Contract__c>();

		// Set<String> contactidList = new Set<String>();

		for (Maintenance_Contract__c mc : scope) {
			MCMap.put(mc.Id,mc);
		}
		List<Maintenance_Contract_Asset__c> McaList = [select Id,Maintenance_Contract__c,Asset_Type__c,
		                                               Maintenance_Contract__r.Estimate_Asset_Cnt__c,IF_OTHERTUBE__c,
		                                               First_Month_RepairAmount__c,Second_Month_RepairAmount__c,Third_Month_RepairAmount__c,
		                                               Maintenance_Contract__r.RecordType_DeveloperName__c,Asset__r.Maintenance_Price_Month__c
		                                               from Maintenance_Contract_Asset__c
		                                               where (Maintenance_Contract__r.Status__c = '契約' or Maintenance_Contract__r.Status__c = '契約満了')
		                                               and Maintenance_Contract__c in :MCMap.keyset()];

		//统计肠胃镜个数
		Map<String,Integer> gastroscopyMap = new Map<String,Integer>();

		//胃肠镜定价金额
		Map<String,Double> gastroscopyDepositMap = new Map<String,Double>();

		//统计URF个数
		Map<String,Integer> URFMap = new Map<String,Integer>();

		//统计支气管镜个数
		Map<String,Integer> bronchoscopeMap = new Map<String,Integer>();

		//支气管镜定价金额
		Map<String,Double> bronchoscopeDepositMap = new Map<String,Double>();

		// gzw 20210926 追加'电子腹腔镜、附属品、光学视管、气管镜 合同类型 start
		//统计电子腹腔镜个数
		Map<String,Integer> dzcountMap = new Map<String,Integer>();
		//统计附属品个数
		Map<String,Integer> fscountMap = new Map<String,Integer>();
		//统计光学视管个数
		Map<String,Integer> gxcountMap = new Map<String,Integer>();
		//统计气管镜个数
		Map<String,Integer> qgcountMap = new Map<String,Integer>();
		// gzw 20210926 追加'电子腹腔镜、附属品、光学视管、气管镜 合同类型 end

		//统计周边设备个数
		Map<String,Integer> preipheralMap = new Map<String,Integer>();

		//统计其他镜种个数
		Map<String,Integer> othertubeMap = new Map<String,Integer>();

		//计算合同报价资产件数
		Map<String,Integer> totalMap = new Map<String,Integer>();

		//肠胃镜第4分类的几种情况
		List<String> gastroscopyForthType = new List<String>();
		gastroscopyForthType.add('GF');
		gastroscopyForthType.add('GIF');
		gastroscopyForthType.add('CF');
		gastroscopyForthType.add('JF');
		gastroscopyForthType.add('SIF');

		//支气管镜第四分类几种情况
		List<String> bronchoscopeForthType = new List<String>();
		bronchoscopeForthType.add('BF');

		//周边设备第四分类几种情况
		List<String> preipheralForthType = new List<String>();
		preipheralForthType.add('CV');
		preipheralForthType.add('CLV');
		preipheralForthType.add('双极电切镜工作把手');
		preipheralForthType.add('其他');

		//修理报价等级
		List<String> repairRank = new List<String>();
		repairRank.add('A');
		repairRank.add('AW');
		repairRank.add('B');
		repairRank.add('BW');
		repairRank.add('C');
		repairRank.add('CW');


		for (Maintenance_Contract_Asset__c mca : McaList) {
			if (mca.Asset_Type__c == '胃肠镜') {
				//存在了就累加
				if(gastroscopyMap.containsKey(mca.Maintenance_Contract__c)) {
					gastroscopyMap.put(mca.Maintenance_Contract__c,gastroscopyMap.get(mca.Maintenance_Contract__c)+1);
					if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
						gastroscopyDepositMap.put(mca.Maintenance_Contract__c,gastroscopyDepositMap.get(mca.Maintenance_Contract__c) + mca.Asset__r.Maintenance_Price_Month__c);
					}
				}else{
					//如果没有就初始化一个
					gastroscopyMap.put(mca.Maintenance_Contract__c,1);
					if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
						gastroscopyDepositMap.put(mca.Maintenance_Contract__c,mca.Asset__r.Maintenance_Price_Month__c);
					}else{
						gastroscopyDepositMap.put(mca.Maintenance_Contract__c,0.00);
					}
				}
			}
			if(mca.Asset_Type__c == 'URF') {
				if(URFMap.containsKey(mca.Maintenance_Contract__c)) {
					URFMap.put(mca.Maintenance_Contract__c,URFMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					URFMap.put(mca.Maintenance_Contract__c,1);
				}
			}

			// gzw 20210926 追加镜种 start

			if(mca.Asset_Type__c == '电子腹腔镜') {
				if(dzcountMap.containsKey(mca.Maintenance_Contract__c)) {
					dzcountMap.put(mca.Maintenance_Contract__c,dzcountMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					dzcountMap.put(mca.Maintenance_Contract__c,1);
				}
			}

			if(mca.Asset_Type__c == '附属品') {
				if(fscountMap.containsKey(mca.Maintenance_Contract__c)) {
					fscountMap.put(mca.Maintenance_Contract__c,fscountMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					fscountMap.put(mca.Maintenance_Contract__c,1);
				}
			}

			if(mca.Asset_Type__c == '光学视管') {
				if(gxcountMap.containsKey(mca.Maintenance_Contract__c)) {
					gxcountMap.put(mca.Maintenance_Contract__c,gxcountMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					gxcountMap.put(mca.Maintenance_Contract__c,1);
				}
			}

			if(mca.Asset_Type__c == '气管镜') {
				if(qgcountMap.containsKey(mca.Maintenance_Contract__c)) {
					qgcountMap.put(mca.Maintenance_Contract__c,qgcountMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					qgcountMap.put(mca.Maintenance_Contract__c,1);
				}
			}

			// gzw 20210926 追加镜种 end

			if(mca.Asset_Type__c == '支气管镜') {
				if(bronchoscopeMap.containsKey(mca.Maintenance_Contract__c)) {
					bronchoscopeMap.put(mca.Maintenance_Contract__c,bronchoscopeMap.get(mca.Maintenance_Contract__c)+1);
					if(mca.Asset__r.Maintenance_Price_Month__c!=null) {
						bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,bronchoscopeDepositMap.get(mca.Maintenance_Contract__c) + mca.Asset__r.Maintenance_Price_Month__c);
					}

				}else{
					bronchoscopeMap.put(mca.Maintenance_Contract__c,1);
					if(mca.Asset__r.Maintenance_Price_Month__c != null) {
						bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,mca.Asset__r.Maintenance_Price_Month__c);
					}else{
						bronchoscopeDepositMap.put(mca.Maintenance_Contract__c,0.00);
					}

				}
			}
			if(mca.Asset_Type__c == '周边设备') {
				if(preipheralMap.containsKey(mca.Maintenance_Contract__c)) {
					preipheralMap.put(mca.Maintenance_Contract__c,preipheralMap.get(mca.Maintenance_Contract__c)+1);
				}else{
					preipheralMap.put(mca.Maintenance_Contract__c,1);
				}
			}
			if(mca.IF_OTHERTUBE__c == true) {
				if(othertubeMap.containsKey(mca.Maintenance_Contract__c)) {
					othertubeMap.put(mca.Maintenance_Contract__c,othertubeMap.get(mca.Maintenance_Contract__c));
				}else{
					othertubeMap.put(mca.Maintenance_Contract__c,1);
				}
			}
		}

		//
		for (String key : MCMap.keyset()) {
			Maintenance_Contract__c mc = MCMap.get(key);
			// Maintenance_Contract__c mc : MCMap) {
			// 1.胃肠镜
			if (gastroscopyMap.containsKey(mc.Id)) {
				mc.GastroscopySize__c = gastroscopyMap.get(mc.Id);
			}
			// 肠胃镜金额
			if (gastroscopyDepositMap.containsKey(mc.Id)) {
				mc.GastroscopyCoast__c = gastroscopyDepositMap.get(mc.Id);
			}
			// 2.支气管镜
			if (bronchoscopeMap.containsKey(mc.Id)) {
				mc.BronchoscopeSize__c = bronchoscopeMap.get(mc.Id);
			}
			// 支气管镜金额
			if (bronchoscopeDepositMap.containsKey(mc.Id)) {
				mc.BronchoscopeCost__c = bronchoscopeDepositMap.get(mc.Id);
			}
			// 3.URF
			if (URFMap.containsKey(mc.Id)) {
				mc.URFSize__c = URFMap.get(mc.Id);
			}
			// 4.电子腹腔镜

			// 5.附属品

			// 6.光学视镜

			// 7.气管镜

			// 周边设备
			if (preipheralMap.containsKey(mc.Id)) {
				mc.PreipheralSize__c = preipheralMap.get(mc.Id);
			}


			// 合同镜种类型计算
			if( gastroscopyMap.containsKey(mc.Id) && gastroscopyMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = '肠胃镜为主合同';
			}
			if( bronchoscopeMap.containsKey(mc.Id) && bronchoscopeMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2) {
				mc.Contract_mirror_type__c = '支气管镜为主合同';
			}
			if( URFMap.containsKey(mc.Id) && URFMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = 'URF为主合同';
			}
			if( URFMap.containsKey(mc.Id) && URFMap.get(mc.Id) == mc.Estimate_Asset_Cnt__c ) {
				mc.Contract_mirror_type__c = 'URF单镜种合同';
			}
			if( dzcountMap.containsKey(mc.Id) && dzcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = '电子腹腔镜为主合同';
			}
			if( fscountMap.containsKey(mc.Id) && fscountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = '附属品为主合同';
			}
			if( gxcountMap.containsKey(mc.Id) && gxcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = '光学视镜为主合同';
			}
			if( qgcountMap.containsKey(mc.Id) && qgcountMap.get(mc.Id) > mc.Estimate_Asset_Cnt__c/2 ) {
				mc.Contract_mirror_type__c = '气管镜为主合同';
			}
			if(String.isBlank(mc.Contract_mirror_type__c)) {
				mc.Contract_mirror_type__c = '其他';
			}
		}
		//

		// //将各个镜种存到对应的Map当中
		// for(String key : gastroscopyMap.keySet()){
		//     Maintenance_Contract__c mc = new Maintenance_Contract__c();
		//     mc.Id = key;
		//     mc.GastroscopySize__c = gastroscopyMap.get(key);
		//     MCMap.put(key,mc);
		// }


		// //存入肠胃镜定价金额
		// if(gastroscopyDepositMap.size()>0){
		//     for(String gac : gastroscopyDepositMap.keySet()){
		//         if(MCMap.containsKey(gac)){
		//             MCMap.get(gac).GastroscopyCoast__c = gastroscopyDepositMap.get(gac);
		//         }
		//     }
		// }


		// for(String key : URFMap.keySet()){
		//     if(MCMap.containsKey(key)){
		//         MCMap.get(key).URFSize__c = URFMap.get(key);
		//     }else{
		//         Maintenance_Contract__c mc = new Maintenance_Contract__c();
		//         mc.Id = key;
		//         mc.URFSize__c = URFMap.get(key);
		//         MCMap.put(key,mc);
		//     }
		// }

		// for(String key : bronchoscopeMap.keySet()){
		//     if(MCMap.containsKey(key)){
		//         MCMap.get(key).BronchoscopeSize__c = bronchoscopeMap.get(key);
		//     }else{
		//         Maintenance_Contract__c mc = new Maintenance_Contract__c();
		//         mc.Id = key;
		//         mc.BronchoscopeSize__c = bronchoscopeMap.get(key);
		//         MCMap.put(key,mc);
		//     }
		// }



		// //存入支气管的定价金额
		// if(bronchoscopeDepositMap.size()>0){
		//     for(String bcc : bronchoscopeDepositMap.keySet()){
		//         if(MCMap.containsKey(bcc)){
		//             MCMap.get(bcc).BronchoscopeCost__c = bronchoscopeDepositMap.get(bcc);
		//         }
		//     }
		// }


		// for(String key : preipheralMap.keySet()){
		//     if(MCMap.containsKey(key)){
		//         MCMap.get(key).PreipheralSize__c = preipheralMap.get(key);
		//     }else{
		//         Maintenance_Contract__c mc = new Maintenance_Contract__c();
		//         mc.Id = key;
		//         mc.PreipheralSize__c = preipheralMap.get(key);
		//         MCMap.put(key,mc);
		//     }
		// }



		// for(String key : othertubeMap.keySet()){
		//     if(MCMap.containsKey(key)){
		//         MCMap.get(key).OthertubeSize__c = othertubeMap.get(key);
		//     }else{
		//         Maintenance_Contract__c mc = new Maintenance_Contract__c();
		//         mc.Id = key;
		//         mc.OthertubeSize__c = othertubeMap.get(key);
		//         MCMap.put(key,mc);
		//     }
		// }

		// //统计总和
		// for(String key : MCMap.keySet()){
		//     if(totalMap.containsKey(key)){
		//         if(MCMap.get(key).GastroscopySize__c != null){
		//             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).GastroscopySize__c.intValue());
		//         }else if(MCMap.get(key).URFSize__c != null){
		//             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).URFSize__c.intValue());
		//         }else if(MCMap.get(key).BronchoscopeSize__c != null){
		//             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).BronchoscopeSize__c.intValue());
		//         }else if(MCMap.get(key).PreipheralSize__c != null){
		//             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).PreipheralSize__c.intValue());
		//         }else if(MCMap.get(key).OthertubeSize__c != null){
		//             totalMap.put(key,totalMap.get(key)+ MCMap.get(key).OthertubeSize__c.intValue());
		//         }
		//     }else{
		//         if(MCMap.get(key).GastroscopySize__c != null){
		//             totalMap.put(key,MCMap.get(key).GastroscopySize__c.intValue());
		//         }else if(MCMap.get(key).URFSize__c != null){
		//             totalMap.put(key,MCMap.get(key).URFSize__c.intValue());
		//         }else if(MCMap.get(key).BronchoscopeSize__c != null){
		//             totalMap.put(key,MCMap.get(key).BronchoscopeSize__c.intValue());
		//         }else if(MCMap.get(key).PreipheralSize__c != null){
		//             totalMap.put(key,MCMap.get(key).PreipheralSize__c.intValue());
		//         }else if(MCMap.get(key).OthertubeSize__c != null){
		//             totalMap.put(key,MCMap.get(key).OthertubeSize__c.intValue());
		//         }
		//     }
		// }




		//对比规则
		/**
		 *
		 * 1.当该合同内肠胃镜条数多于合同报价资产件数的一半------【肠胃镜为主合同】
		 * 2.当该合同内支气管镜条数多于合同报价资产件数的一半------【支气管镜为主合同】
		 * 3.当该合同内URF条数多于合同报价资产件数的一半------【URF为主合同】
		 * 4.当该合同内URF条数等于合同报价资产件数-----【URF单镜种合同】
		 * 5.当该合同内的CHF、CYF、ENF、HD EndoEYE、HYF、LTF、泌尿科用硬性镜、腹腔镜/胸腔镜多于报价资产件数的一半时，取最多的镜种作为【该镜种为主的合同】
		 * 6.以上情况均不符合-----其他
		 *
		 */
		// for(String key : MCMap.keySet()){
		//     if( MCMap.get(key).GastroscopySize__c > totalMap.get(key)/2 ){
		//         MCMap.get(key).Contract_mirror_type__c = '肠胃镜为主合同';
		//     }else if( MCMap.get(key).BronchoscopeSize__c > totalMap.get(key)/2){
		//         MCMap.get(key).Contract_mirror_type__c = '支气管镜为主合同';
		//     }else if( MCMap.get(key).URFSize__c > totalMap.get(key)/2 ){
		//         MCMap.get(key).Contract_mirror_type__c = 'URF为主合同';
		//     }else if( MCMap.get(key).URFSize__c == totalMap.get(key)/2 ){
		//         MCMap.get(key).Contract_mirror_type__c = 'URF单镜种合同';
		//     }
		//     // else if( MCMap.get(key).OthertubeSize__c > totalMap.get(key)/2){
		//     //     MCMap.get(key).Contract_mirror_type__c = '其他镜种合同';
		//     // }
		//     else{
		//         MCMap.get(key).Contract_mirror_type__c = '其他';
		//     }
		// }


		//计算设备生效后的第1.2.3个月修理金额

		// //拿出所有的维修合同
		// Set<String> weixiuContractId = new Set<String>();
		// for(Maintenance_Contract_Asset__c mca :scope){
		//     weixiuContractId.add(mca.Maintenance_Contract__c);
		// }

		//检索所有合同保有设备的修理
		list<Repair__c> newRepiarList = new list<Repair__c>();
		// if(weixiuContractId.size()>0){
		newRepiarList =
		[select Id, Usage_Ratio_Price_Contract__c,
		 Usage_Ratio_Price_Guarantee__c,Usage_Ratio_Price_Service__c,
		 Delivered_Product__c,
		 Repair_cost__c,
		 Maintenance_Contract__c,
		 Maintenance_Contract__r.Contract_Start_Date__c,
		 Maintenance_Contract__r.Contract_End_Date__c,
		 // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
		 Maintenance_Contract__r.Contract_Conclusion_Date__c,
		 // tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
		 Failure_Occurrence_Date__c,
		 Repair_Quotation_Id__c,
		 VM_Maintenance_Contract__c,
		 Delivered_Product__r.Category4__c,
		 Repair_Rank__c
		 from Repair__c
		 where (Maintenance_Contract__c != null or VM_Maintenance_Contract__c != null)
		 and (Maintenance_Contract__c in: MCMap.keyset()
		      or VM_Maintenance_Contract__c in: MCMap.keyset())
		 and Status2__c != '00.删除'
		 and Status2__c != '00.取消'
		 order by Maintenance_Contract__c
		];
		// }



		//找到对应维修合同/保有设备底下的合同
		if(newRepiarList != null && newRepiarList.size()>0) {
			//初始化清空维修合同/保有设备的第一二三月的维修金额
			for(Maintenance_Contract_Asset__c mca : McaList) {
				mca.First_Month_RepairAmount__c = 0.00;
				mca.Second_Month_RepairAmount__c = 0.00;
				mca.Third_Month_RepairAmount__c = 0.00;
			}

		}

		//置空肠胃镜 支气管镜 周边设备的修理金额
		for(String key : MCMap.keySet()) {
			MCMap.get(key).GastroscopyRepairCost__c = 0.00;
			MCMap.get(key).BronchoscopeRepairCost__c = 0.00;
			MCMap.get(key).PreipheralRepairCost__c = 0.00;
		}
		// tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
		// 初始化大修次数和大修金额
		for (Maintenance_Contract_Asset__c mca : McaList) {
			MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = 0;
			MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c = 0.00;
		}
		// tcm 20211011 修复大修次数和大修修理金额的数字对不上 end

		//各种镜种的修理金额
		for (Maintenance_Contract_Asset__c mca : McaList) {
			for(Repair__c rc : newRepiarList) {
				if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '胃肠镜' && gastroscopyForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c ) {
					//肠胃镜修理金额
					if(MCMap.containsKey(mca.Maintenance_Contract__c)) {

						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Contract__c;

						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Service__c;

						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).GastroscopyRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;

						}
					}
					// tcm 20211011	 start
					// else{
					// 	Maintenance_Contract__c mc = new Maintenance_Contract__c();
					// 	mc.Id = mca.Maintenance_Contract__c;
					// 	if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
					// 		mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
					// 		mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Service__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
					// 		mc.GastroscopyRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
					// 	}
					// 	MCMap.put(mca.Maintenance_Contract__c,mc);
					// }
					// tcm 20211011	 end
					//支气管镜维修金额
				}else if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '支气管镜' && bronchoscopeForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c) {
					if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Contract__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Service__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).BronchoscopeRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;
						}
					}
					// tcm 20211011	 start
					// else{
					// 	Maintenance_Contract__c mc = new Maintenance_Contract__c();
					// 	mc.Id = mca.Maintenance_Contract__c;
					// 	if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
					// 		mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
					// 		mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Service__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
					// 		mc.BronchoscopeRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
					// 	}
					// 	MCMap.put(mca.Maintenance_Contract__c,mc);
					// }
					// tcm 20211011	 end
				}
				//周边设备的修理金额
				else if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && mca.Asset_Type__c == '周边设备' && preipheralForthType.contains(rc.Delivered_Product__r.Category4__c) && mca.Asset__c == rc.Delivered_Product__c) {
					if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Contract__c;

							System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Contract__c+'==='+mca.Maintenance_Contract__c);
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Service__c;

							System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Service__c+'==='+mca.Maintenance_Contract__c);
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).PreipheralRepairCost__c += rc.Usage_Ratio_Price_Guarantee__c;

							System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Guarantee__c+'==='+mca.Maintenance_Contract__c);
						}
					}
					// tcm 20211011	 start
					// else{
					// 	Maintenance_Contract__c mc = new Maintenance_Contract__c();
					// 	mc.Id = mca.Maintenance_Contract__c;
					// 	if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
					// 		System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Contract__c+'==='+mca.Maintenance_Contract__c);
					// 		mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Contract__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
					// 		System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Service__c+'==='+mca.Maintenance_Contract__c);
					// 		mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Service__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
					// 		System.debug('IF_PERIPHERAL__c === Maintenance_Contract'+rc.Usage_Ratio_Price_Guarantee__c+'==='+mca.Maintenance_Contract__c);
					// 		mc.PreipheralRepairCost__c = rc.Usage_Ratio_Price_Guarantee__c;
					// 	}
					// 	MCMap.put(mca.Maintenance_Contract__c,mc);
					// }
					// tcm 20211011	 end
				}

				//存在修理 且修理等级为A/AW/B/BW/C/CW
				if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c && repairRank.contains(rc.Repair_Rank__c) && mca.Asset__c == rc.Delivered_Product__c) {
					//查找是否在MCMap里
					if(MCMap.containsKey(mca.Maintenance_Contract__c)) {
						// tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
						// if(MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c == null) {
						// 	//计算修理次数
						// 	MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = 1;
						// 	MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c = 0.00;
						// 	//计算修理金额
						// 	if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
						// 		MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Contract__c;
						// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
						// 		MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Service__c;
						// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
						// 		MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Guarantee__c;
						// 	}
						// }else{
						// tcm 20211011 修复大修次数和大修修理金额的数字对不上 end
						//已经存在的值
						MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c = MCMap.get(mca.Maintenance_Contract__c).keepRepairInContract__c + 1;
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Contract__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Service__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							MCMap.get(mca.Maintenance_Contract__c).keepRepairInContractMoney__c += rc.Usage_Ratio_Price_Guarantee__c;
						}
						// tcm 20211011 修复大修次数和大修修理金额的数字对不上 start
						// }
					}

					// else{
					// 	//不在就新建一个对象
					// 	Maintenance_Contract__c mc = new Maintenance_Contract__c();
					// 	mc.Id = mca.Maintenance_Contract__c;
					// 	mc.keepRepairInContract__c = 1;
					// 	if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
					// 		mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Contract__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
					// 		mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Service__c;
					// 	}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
					// 		mc.keepRepairInContractMoney__c = rc.Usage_Ratio_Price_Guarantee__c;
					// 	}

					// 	MCMap.put(mca.Maintenance_Contract__c,mc);
					// }
					// tcm 20211011 修复大修次数和大修修理金额的数字对不上 end
				}

				//第一二三月修理金额
				if(mca.Maintenance_Contract__c == rc.Maintenance_Contract__c) {
					// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
					//判断 故障发生日 Failure_Occurrence_Date__c 是否在第一个月内
					if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Conclusion_Date__c.addMonths(1)) {
						// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
						//按照维修合同的RecordType_DeveloperName__c 计算维修金额
						//不同种类维修金额不同
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							mca.First_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
						}
					}
					// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
					//判断是否在第二个月内
					// gzw 20230628 DB202306136940 月度消费率跟踪报表反馈 “合同日”，希望替换成“合同开始日”
					if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Start_Date__c.addMonths(3)) {
						system.debug('进入第二个月判断，记录类型名等于'+mca.Maintenance_Contract__r.RecordType_DeveloperName__c);
						// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							mca.Second_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
						}
						system.debug('第二个月维修价格等于'+mca.First_Month_RepairAmount__c);
					}
					// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 start
					//判断是否在第三个月内
					if(rc.Failure_Occurrence_Date__c <= rc.Maintenance_Contract__r.Contract_Conclusion_Date__c.addMonths(3)) {
						system.debug('进入第三个月判断，记录类型名等于'+mca.Maintenance_Contract__r.RecordType_DeveloperName__c);
						// tcm 20211011 生效后第N个月的区间都改为 合同日+N个月 end
						if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('Maintenance_Contract')) {
							mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Contract__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('NewMaintenance_Contract')) {
							mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Service__c;
						}else if(mca.Maintenance_Contract__r.RecordType_DeveloperName__c.equals('VM_Contract')) {
							mca.Third_Month_RepairAmount__c += rc.Usage_Ratio_Price_Guarantee__c;
						}
						system.debug('第三个月维修价格等于'+mca.First_Month_RepairAmount__c);
					}
				}
			}
		}
		update McaList;
		update MCMap.values();
	}

	global void finish(Database.BatchableContext BC) {
	}

	global void calculateSum (Maintenance_Contract_Asset__c mca, Repair__c rc ){

	}

}