为亚马逊采购订单报告生成唯一哈希



我的问题是,我无法找到一种为亚马逊采购订单报告生成唯一哈希的方法,这样我就可以在MySQL表中导入它们而不会重复。

我现在的做法是:

  1. 我加载CSV文件(示例=>https://gofile.io/?c=A8McUw)转换为关联阵列

  2. 我使用md5生成了一个唯一的密钥,如下所示:

$hash = md5($row['order_id'] . $row['asin'] . $row['product_category'] . $row['seller_name'] . $row['order_quantity'] . $row['item_quantity'])

  1. 我检查$hash密钥是否存在,如果存在,我更新条目,如果不存在,它将插入一个新条目

这已经起了几次作用,但现在我不断收到重复的条目,我猜这是因为每次添加新订单时,行的顺序都会不断变化。我查看了报告,这些列是唯一不变的,可以用来唯一标识行。

我发现了一个简单的解决方案,它使用亚马逊生成的CSV中的项目顺序来工作。唯一不利的是,如果亚马逊突然决定更改订单。

class Foo
{
private $itemIndex = [];
private function generateUniqueIdentifierForRow(array $row, $i = 1)
{
$str = $row['order_id'] . $row['asin'] . $i;
return hash('sha256', $str);
}
public function uniqueRows($rows)
{
$uniqueRows = [];
foreach ($rows as $row) {
$hash = $this->generateUniqueIdentifierForRow($row);
if (isset($this->hashDuplicatesIndexes[$hash]) || array_key_exists($hash, $this->itemIndex)) {
$this->itemIndex[$hash]++;
$hash = $this->generateUniqueIdentifierForRow($row, $this->itemIndex[$hash]);
} else {
$this->itemIndex[$hash] = 1;
}
$row['hash'] = $hash;
$uniqueRows[] = $row;
}
return $uniqueRows;
}
}

最新更新