//create by rentx 2020-10-15
global class UpdateSAPContractNoTextBatch implements Database.Batchable<sObject>, Database.Stateful {
	public List<String> tempIds;

	global UpdateSAPContractNoTextBatch(List<String> tempId) {
		this.tempIds = tempId;


	}
	global UpdateSAPContractNoTextBatch() {
		this.tempIds = new List<String>();

	}

	global Database.QueryLocator start(Database.BatchableContext bc) {
		//1.拿到明细2
		String stasql = 'SELECT Id,sNo__c,Consumable_product__r.OT_CODE_Text__c,Consumable_order_minor__r.ContractNo__c,SerialLotNo__c,TracingCode__c,Consumable_product__c,Opportunity_No__c  from Consumable_order_details2__c where Consumable_product__c != null and ((sNo__c != null  and Consumable_order_minor__r.Opportunity__c != null) or Opportunity_No__c=null ) AND Consumable_product__r.OT_CODE_Text__c != null ';
		if (tempIds != null && tempIds.size() > 0) {
			stasql += 'AND Id in :tempIds ';
		}
		return Database.getQueryLocator(stasql);
	}

	global void execute(Database.BatchableContext BC, list<Consumable_order_details2__c> codList) {
		//直接跳过明细2的触发器
		StaticParameter.ConsumableAssetHanderTrigger = true;
		StaticParameter.EscapeOrderDetail2Trigger = true;

		//使用 消耗品明细2中的 消耗品名称 中的'产品编码.' + 明细2上的机身编码 拼接为保有设备标记为耗材的查询条件
		//使用 消耗品明细2中的 消耗品名称 中的'产品编码.' + 明细2上的批次号 拼接为保有设备标记为主机的查询条件
		Map<String,Consumable_order_details2__c> codMap = new Map<String,Consumable_order_details2__c>();

		for(Consumable_order_details2__c codc : codList) {
			//耗材
			String tempStr = codc.Consumable_product__r.OT_CODE_Text__c + ':' + codc.sNo__c;
			codMap.put(tempStr,codc);
			//主机 add by rentx 2020-12-02
			if (codc.SerialLotNo__c != null) {
				String tempStr2 = codc.Consumable_product__r.OT_CODE_Text__c + ':' + codc.SerialLotNo__c;
				codMap.put(tempStr2,codc);
			}
		}


		//3.根据拼接好的值 查找对应的保有设备
		List<Asset> assList = new List<Asset>();
		assList = [SELECT Id,Product_Serial_No__c,TocodeAndSerialNumber__c,Backorder__r.Opportunity__r.Id,Backorder__r.Id,Backorder__r.ContractNO__c,Product_name_serial__c,SerialNumber,OT_CODE__c
		           FROM Asset WHERE Backorder__c != null
		           AND TocodeAndSerialNumber__c <> null
		           AND TocodeAndSerialNumber__c in :codMap.keySet()];
		Map<String,Asset> assMap = new Map<String,Asset>();
		for (Asset ass : assList) {
			if(assMap.containsKey(ass.TocodeAndSerialNumber__c)) {
				continue;
			}else{
				assMap.put(ass.TocodeAndSerialNumber__c, ass);
			}
		}

		//4.获取询价
		List<String> oppIds = new List<String>();
		//存放保有设备的OTCode+机身编码 检索用 和 询价id
		Map<String,String> ssMap = new Map<String,String>();
		//存放询价id和拼好的SAP合同号字符串
		Map<String,String> ssMap2 = new Map<String,String>();
		//存放询价id和拼好的询价编码字符串 tcm 20211109
		Map<String,String> ssMap3 = new Map<String,String>();

		for(Asset ass :assMap.values()) {
			//如果保有设备的注残不为空 且注残上的询价也不为空 并且 OTCode+机身编码 检索用 也不为空
			//添加保有设备上的 OTCode+机身编码 检索用 为key 询价的id为value
			if (ass.Backorder__c != null && ass.Backorder__r.Opportunity__c != null  && ass.TocodeAndSerialNumber__c != null) {
				oppIds.add(ass.Backorder__r.Opportunity__r.Id);
				//将保有设备的TocodeAndSerialNumber__c作为key,oppid作为value
				ssMap.put(ass.TocodeAndSerialNumber__c, ass.Backorder__r.Opportunity__r.Id);
			}
		}

		//注残集合
		List<Statu_Achievements__c> statuList = new List<Statu_Achievements__c>();
		// tcm 添加查询字段 Opp_Number__c 20211109
		statuList = [SELECT Id,Opportunity__c,ContractNO__c,Opp_Number__c FROM Statu_Achievements__c WHERE Opportunity__c in :oppIds];

		for (String oppid : oppIds) {
			String str = '';
			// tcm 20211110 str2
			String str2;
			//遍历注残
			for (Statu_Achievements__c statu : statuList) {
				if (statu.Opportunity__c == oppId && statu.ContractNO__c != null) {
					str += statu.ContractNO__c+',';
				}

				// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211109 start
				if (statu.Opportunity__c == oppId && statu.Opp_Number__c != null) {
					str2 = statu.Opp_Number__c;
				}
				// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211109 end
			}

			//将 询价id SAP合同号放入map
			if (str.length() > 0) {
				ssMap2.put(oppId, str.substring(0,str.length()-1));
			}
			// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211109 start
			//将 询价id 询价编码放入map
			if (str2.length() > 0) {ssMap3.put(oppId, str2);}
			// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211109 end
		}

		//遍历查到的所有明细2 拼接明细2对应的保有设备中的 otcode加机身编码,以此为key获取ssmap中value->询价id,以询价id为key 获取ssmap2中的value->SAP合同号
		for(Consumable_order_details2__c codc : codList) {
			String tempStr = codc.Consumable_product__r.OT_CODE_Text__c + ':' + codc.sNo__c;
			String tempStr2 = codc.Consumable_product__r.OT_CODE_Text__c + ':' + codc.SerialLotNo__c;
			if (ssMap.containsKey(tempStr) || ssMap.containsKey(tempStr2)) {
				String oppid = ssMap.get(tempStr);
				String oppid2 = ssMap.get(tempStr2);
				if (String.isNotBlank(ssMap2.get(oppid2))) {
					codc.ContractNo_text__c = ssMap2.get(oppid2);
				}else if (String.isNotBlank(ssMap2.get(oppid))) {
					codc.ContractNo_text__c = ssMap2.get(oppid);
				}
				// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211110 start
				//询价编码赋值
				if (String.isNotBlank(ssMap3.get(oppid2))) {
					codc.Opportunity_No__c = ssMap3.get(oppid2);
				}else if (String.isNotBlank(ssMap3.get(oppid))) {
					codc.Opportunity_No__c = ssMap3.get(oppid);
				}
				// 【委托】ET消耗品系统自动生成出库数量大于当月发货数量 tcm 20211110 end
			}
		}

		if (codList.size() > 0) {
			update codList;
		}
	}

	global void finish(Database.BatchableContext BC) {
	}
}