• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integrity/duplicatebankdetailmatchcode.php
  • Класс: Bitrix\Crm\Integrity\DuplicateBankDetailMatchCodeTable
  • Вызов: DuplicateBankDetailMatchCodeTable::bulkReplaceValues
static function bulkReplaceValues($entityTypeID, $entityID, array $data)
{
	$connection = \Bitrix\Main\Application::getConnection();
	if(empty($data))
	{
		$connection->queryExecute(
			/** @lang MySQL */
			"DELETE FROM b_crm_dp_bd_mcd WHERE ENTITY_TYPE_ID = {$entityTypeID} AND ENTITY_ID = {$entityID}"
		);
		return;
	}

	$currentMap = array();
	foreach($data as $bdCountryId => $rqFields)
	{
		if(is_array($rqFields))
		{
			if (!isset($currentMap[$bdCountryId]))
			{
				$currentMap[$bdCountryId] = array();
			}

			foreach ($rqFields as $bdFieldName => $values)
			{
				if (is_array($values))
				{
					if (!isset($currentMap[$bdCountryId][$bdFieldName]))
					{
						$currentMap[$bdCountryId][$bdFieldName] = array();
					}

					foreach($values as $value)
					{
						$hash = md5($value);
						$currentMap[$bdCountryId][$bdFieldName][$hash] = array('value' => $value);
					}
				}
			}
		}
	}

	$persistentMap = array();
	$result = $connection->query(
		/** @lang MySQL */
		"SELECT ID, BD_COUNTRY_ID, BD_FIELD_NAME, VALUE".PHP_EOL.
		"FROM b_crm_dp_bd_mcd".PHP_EOL.
		"\tWHERE ENTITY_TYPE_ID = {$entityTypeID} AND ENTITY_ID = {$entityID}"
	);
	while($fields = $result->fetch())
	{
		$bdCountryId = $fields['BD_COUNTRY_ID'];
		$bdFieldName = $fields['BD_FIELD_NAME'];
		if(!isset($persistentMap[$bdCountryId]))
		{
			$persistentMap[$bdCountryId] = array();
		}
		if(!isset($persistentMap[$bdCountryId][$bdFieldName]))
		{
			$persistentMap[$bdCountryId][$bdFieldName] = array();
		}

		$ID = (int)$fields['ID'];
		$value = $fields['VALUE'];
		$hash = md5($value);
		$persistentMap[$bdCountryId][$bdFieldName][$hash] = array('id' => $ID, 'value' => $value);
	}

	$deleteIDs = array();
	foreach($persistentMap as $bdCountryId => $rqFields)
	{
		foreach ($rqFields as $bdFieldName => $items)
		{
			$currentItems = isset($currentMap[$bdCountryId][$bdFieldName]) ?
				$currentMap[$bdCountryId][$bdFieldName] : array();
			foreach($items as $hash => $item)
			{
				if(!isset($currentItems[$hash]))
				{
					$deleteIDs[] = $item['id'];
				}
			}
		}
	}

	$insertItems = array();
	foreach($currentMap as $bdCountryId => $rqFields)
	{
		foreach ($rqFields as $bdFieldName => $items)
		{
			$presentItems = isset($persistentMap[$bdCountryId][$bdFieldName]) ?
				$persistentMap[$bdCountryId][$bdFieldName] : array();
			foreach($items as $hash => $item)
			{
				if(!isset($presentItems[$hash]))
				{
					$insertItems[] = array(
						'bdCountryId' => $bdCountryId,
						'bdFieldName' => $bdFieldName,
						'value' => $item['value']
					);
				}
			}
		}
	}

	$sqlHelper = $connection->getSqlHelper();
	if(!empty($deleteIDs))
	{
		$idsSql = implode(',', $deleteIDs);
		$connection->queryExecute(
			/** @lang MySQL */
			"DELETE FROM b_crm_dp_bd_mcd WHERE ID IN ({$idsSql})"
		);
	}

	if(!empty($insertItems))
	{
		$valueData = array();
		foreach($insertItems as $item)
		{
			$bdCountryIdSql = (int)$item['bdCountryId'];
			$bdFieldNameSql = $sqlHelper->forSql($item['bdFieldName']);
			$valueSql = $sqlHelper->forSql($item['value']);
			$valueData[] =
				"({$entityTypeID}, {$entityID}, {$bdCountryIdSql}, '{$bdFieldNameSql}', '{$valueSql}')";
		}

		$valuesSql = implode(', ', $valueData);
		$connection->queryExecute(
			/** @lang MySQL */
			"INSERT INTO b_crm_dp_bd_mcd".PHP_EOL.
			"\t(ENTITY_TYPE_ID, ENTITY_ID, BD_COUNTRY_ID, BD_FIELD_NAME, VALUE) VALUES ".PHP_EOL.
			"\t{$valuesSql}"
		);
	}
}