public without sharing class MCESTrigger {
	public static void getLastyearQuotationDayRepairPrice(List<Maintenance_Contract_Estimate__c> newList, Map<Id, Maintenance_Contract_Estimate__c> newMap, List<Maintenance_Contract_Estimate__c> oldList, Map<Id, Maintenance_Contract_Estimate__c> oldMap) {
		if (Trigger.isAfter) {
			List<id> idList = new List<id>();
			Map<id, Date> mceMap = new Map<id, Date>();
			//add    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
			Map<id,Maintenance_Contract_Estimate__c> mapMCE = new Map<id,Maintenance_Contract_Estimate__c>();
			//add    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
			for (Maintenance_Contract_Estimate__c mce : newList) {
				if (mce.Submit_quotation_day__c != null) {
					idList.add(mce.Id);
					mceMap.put(mce.Id, mce.Submit_quotation_day__c);
				}else{
					//add    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
					//1：如果 合同月数 的值是否有变动，并且报价状态为 批准，那么就更新一下 维修合同报价/保有设备 的 合同价格（公式拷文本）字段的值
					//2：如果 报价状态 变为批准状态，那么也更新一下维修合同报价/保有设备 的 合同价格（公式拷文本）字段的值
					if(Trigger.isUpdate){
						if(oldMap.get(mce.id).Contract_Range__c != mce.Contract_Range__c){
							idList.add(mce.Id);
						}
					}
					//add    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
				}
			}

			List<Maintenance_Contract_Asset_Estimate__c> mcaeList = new List<Maintenance_Contract_Asset_Estimate__c>();
			mcaeList = [select id, asset__c, Maintenance_Contract_Estimate__c, Bef_QuotationDay_1Year_Price__c,Maintenance_Contract_Estimate_Status__c from Maintenance_Contract_Asset_Estimate__c where Maintenance_Contract_Estimate__c in : idList];

			List<id> mcaeIdList = new List<id>();
			Map<id, Date> mcaeMap = new Map<id, Date>();
			for (Maintenance_Contract_Asset_Estimate__c mcae : mcaeList) {
				//update    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
				if (newMap.get(mcae.Maintenance_Contract_Estimate__c).Submit_quotation_day__c != null) {
					mcaeIdList.add(mcae.asset__c);
					mcaeMap.put(mcae.asset__c, mceMap.get(mcae.Maintenance_Contract_Estimate__c));
				}
				//update    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
			}
			if (mcaeIdList.size() > 0) {
				List<Repair__c> rpList = [
										select 
                                            Repair_List_Price_formula__c,
                                            Delivered_Product__c,
                                            Repair_Final_Inspection_Date__c
                                        from 
                                            Repair__c
                                        where 
                                            Delivered_Product__c in:mcaeIdList
                                        and (
                                                    (
                                                        Repair_Quotation_Id__c!=null 
                                                            and     (Repair_Quotation_Id__r.New_QIS_free__c = 0 
                                                                    or 
                                                                        Repair_Quotation_Id__r.New_QIS_free__c = null)
                                                            and     (Repair_Quotation_Id__r.Contract_free__c= 0
                                                                    or
                                                                        Repair_Quotation_Id__r.Contract_free__c= null)
                                                    )
                                                or  Repair_Quotation_Id__c =null
                                            )
                                        and Status2__c != '00.关闭' 
                                        and Status2__c != '00.取消'
                                        and Status2__c != '00.删除'
                                        and (
	                                        	(QIS_ID__c != null
	                                        		and QIS_ID__r.next_action__c != '无偿维修'
	                                        		and QIS_ID__r.next_action__c != '无偿更换'
	                                        	)
                                        	or QIS_ID__c = null
                                        )
                                        and Return_Without_Repair_IF__c = false];
            	// 以保有设备为单位做报价申请日范围内金额合计
            	Map<id, Decimal> asPriceMap = new Map<id,Decimal>(); 
            	for (Repair__c r : rpList) {
            		Date nowDt = mcaeMap.get(r.Delivered_Product__c);
            		Date lastYearDate = nowDt.addYears(-1);
        			lastYearDate = lastYearDate.addDays(1);
        			if (r.Repair_Final_Inspection_Date__c > lastYearDate && r.Repair_Final_Inspection_Date__c <= nowDt) {
                		Decimal Defir = r.Repair_List_Price_formula__c == null ? 0 : r.Repair_List_Price_formula__c;
                		if (asPriceMap.get(r.Delivered_Product__c) == null) {
                			asPriceMap.put(r.Delivered_Product__c, Defir);
                		} else {
                			asPriceMap.put(r.Delivered_Product__c, Defir + asPriceMap.get(r.Delivered_Product__c));
                		}
                	}
            	}
            	// 更新List作成
            	List<Maintenance_Contract_Asset_Estimate__c> updList = new List<Maintenance_Contract_Asset_Estimate__c>();
            	for (Maintenance_Contract_Asset_Estimate__c updMcae : mcaeList) {
            		//update    wangweipeng         LJPH-C8F4FK             2021/11/08                  start
            		boolean flagTrue = false;//判断是否需要空更新一下
            		if (asPriceMap.get(updMcae.asset__c) != null) {
            			if (updMcae.Bef_QuotationDay_1Year_Price__c != asPriceMap.get(updMcae.asset__c)) {
            				updMcae.Bef_QuotationDay_1Year_Price__c = asPriceMap.get(updMcae.asset__c);
            				updList.add(updMcae);
            			}else{
            				flagTrue = true;
            			}
            		}else{
            			flagTrue = true;
            		}
            		if(flagTrue){
            			if(Trigger.isUpdate){
            				//如果 合同月数 的值有变化，并且 状态等于 批准，那么对象维修合同报价/保有设备空更新一下
	            			if(newMap.get(updMcae.Maintenance_Contract_Estimate__c).Contract_Range__c 
	            				!= oldMap.get(updMcae.Maintenance_Contract_Estimate__c).Contract_Range__c
	            			){
	            				Maintenance_Contract_Asset_Estimate__c mcaeNull = new Maintenance_Contract_Asset_Estimate__c();
	            				mcaeNull.id = updMcae.id;
	            				updList.add(mcaeNull);
	            			}
            			}
            		}
            		//update    wangweipeng         LJPH-C8F4FK             2021/11/08                  end
            	}
            	if (updList.size() > 0) {
            		update updList;
            	}
			}
		}

		Integer i =0;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;
	    i++;

    
	}

    // HWAG-B3G3FE 2018/08/13 报价提交对象修改批准后更新
    public static void setEstimateTarget(List<Maintenance_Contract_Estimate__c> newList, Map<Id, Maintenance_Contract_Estimate__c> newMap, List<Maintenance_Contract_Estimate__c> oldList, Map<Id, Maintenance_Contract_Estimate__c> oldMap) {
        for (Maintenance_Contract_Estimate__c mce : newList) {
            Maintenance_Contract_Estimate__c oldMce = oldMap.get(mce.Id);
            if (mce.Changed_Target_Approval__c != oldMce.Changed_Target_Approval__c && mce.Changed_Target_Approval__c) {
                mce.Estimate_Target__c = mce.Changed_Estimate_Target__c;
                mce.Dealer__c = mce.Changed_Dealer__c;
            }
			system.debug('setEstimateTarget>>>' + Json.serialize(mce));//add by tiger
        }
    }
}