我有一个“有效”的脚本,但我对逻辑有疏忽,我正在努力纠正它。
基本上,下面的脚本从db2数据库和每个订单上的每个项目中选择订单。我正在做的是在一段时间内为产品添加日期间隔,我们相信产品将上架。在到期时间内,客户应该只有任何单个产品的记录数等于他们拥有的商店数量。
一个例子:如果我是客户123,我有5家商店。今天我订购了skus 1,2和3,我买了2个。这是每个产品2个记录。明天我会订购6件产品;应该为每个产品添加3条记录,然后将这些产品的最旧记录覆盖3次,并将到期日期更新为另一天。因此,在2天内,我为相同的3个产品下了多个订单,但是计件数量超过了我的商店数量,因此这是更新的地方。在一天结束时,对于任何到期窗口内的个人sku,我应该每个sku的记录数量绝不会超过位置数量。
无论如何,这在插入方面起作用,并且当我进行选择时,如果记录已存在,则它适用于更新。但我的问题是:如果select有昨天和今天的订单,其中包含应该更新的项目,它只会选择全部并插入,因为在选择时,它们不存在。< / p>
我在脑海中的解决方案是打破这个问题并按订单号批量处理脚本,这样我只会插入一个订单号的记录,然后返回并选择下一个订单并进行操作它再次。这样,每个订单号都有机会被选中,以便我可以准确地确定是否需要更新或插入。我觉得这应该很简单,但我不知道最好的方法。
基本上,我会选择我的订单号,检查我的目的地表中的物品,然后插入/更新,然后再次为下一个订单号完成,而不是选择一个订单号数组,只是从可能会导致插入太多记录而不是更新。
如果需要,我可以澄清,但是非常感谢任何帮助。
<?php
//Select orders from DB2 side
$detailStatCheck = "
SELECT
inv as INVOICE,
stat as STATUS,
cust AS CUSTOMER,
frm AS BODY,
cov AS MTRL,
col AS ATTR ,
shpdte AS SHIPPED,
qty AS QUANTITY,
p.stores as STORES
FROM goods g
inner join plant p on g.cust = p.cstno
WHERE to_date(char(shpdte), 'YYYYMMDD') >= '2018-01-01'
";
//prepare and execute to select orders
try {
$detailCheck = $DB2conn->prepare($detailStatCheck);
$detailRslt = $detailCheck->execute();
$count2 = $detailCheck->fetch();
print_r($count2);
} catch(PDOException $ex) {
echo "QUERY FAILED!: " .$ex->getMessage();
}
//Create prepared INSERT statement
$insertPlacement = "
INSERT ignore INTO placedOrders (sku, category, CUSTOMER_id, start_date, expire_date, locations, order_num)
SELECT
id,
category,
:CUSTOMER,
DATE_ADD(:SHIPPED),INTERVAL 7 DAY) as start_date,
DATE_ADD(DATE_FORMAT(CONVERT(:SHIPPED, CHAR(20)), '%Y-%m-%d'),INTERVAL 127 DAY) as expire_date,
:STORES,
:INVOICE
FROM sku s
WHERE s.BODY=:BODY AND s.MTRL1=:MTRL AND s.ATTR1=:ATTR
";
//check for existing records that are expired, which would trigger insert
$expiredCheck = "
SELECT
sku,
CUSTOMER_id,
expire_date
FROM placedOrders p
INNER JOIN sku s
ON p.sku = s.id
WHERE p.CUSTOMER_id = :CUSTOMER
AND s.BODY = :BODY
AND s.MTRL1 = :MTRL
AND s.ATTR1 = :ATTR
AND p.expire_date <= current_date()
";
//check for existing records that are not expired, triggering update
$validCheck = "
SELECT
sku,
CUSTOMER_id,
expire_date
FROM placedOrders p
INNER JOIN sku s
ON p.sku = s.id
WHERE p.CUSTOMER_id = :CUSTOMER
AND s.BODY = :BODY
AND s.MTRL1 = :MTRL
AND s.ATTR1 = :ATTR
AND p.expire_date > current_date()
";
//prepare all update and insert queries
$checkExisting = $MysqlConn->prepare($expiredCheck);
$checkExistingValid = $MysqlConn->prepare($validCheck);
$insert = $MysqlConn->prepare($insertPlacement);
$update = $MysqlConn->prepare($updatePlacement);
//while we have results from the order selection
while ($row2 = $detailCheck->fetch(PDO::FETCH_ASSOC)) {
$executionValues = [
":CUSTOMER" => $row2["CUSTOMER"],
":SHIPPED" => $row2["SHIPPED"],
":STORES" => $row2["STORES"],
":QUANTITY" => $row2["QUANTITY"],
":INVOICE" => $row2["INVOICE"],
":BODY" => $row2["BODY"],
":MTRL" => $row2["MTRL"],
":ATTR" => $row2["ATTR"],
];
$checkValues = [
":CUSTOMER" => $row2["CUSTOMER"],
":BODY" => $row2["BODY"],
":MTRL" => $row2["MTRL"],
":ATTR" => $row2["ATTR"],
];
try{
//Array will contain records that are expired
$existingRslt = $checkExisting->execute($checkValues);
$count3 = $checkExisting->fetch(PDO::FETCH_ASSOC);
//Array will contain records that are valid
$existingVldRslt = $checkExistingValid->execute($checkValues);
$count4 = $checkExistingValid->fetch(PDO::FETCH_ASSOC);
}catch(PDOException $ex){
echo "QUERY FAILED!!!: " . $ex->getMessage();
}
// IF records do not exist, or records exist and today is after expiration date
if(empty($count3) && empty($count4)){
print_r("Inserting");
if($row2["QUANTITY"] > $row2["STORES"]){
for($i=0; $i<$row2["STORES"]; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "QUERY THREE FAILED!!!: " . $ex->getMessage();
}
}
}elseif($row2["QUANTITY"] < $row2["STORES"]){
for($i=0; $i<$row2["QUANTITY"]; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "QUERY THREE FAILED!!!: " . $ex->getMessage();
}
}
}
}elseif(!empty($count3)){
print_r("Inserting");
if($row2["QUANTITY"] > $row2["STORES"]){
for($i=0; $i<$row2['STORES']; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "QUERY FOUR FAILED!!!: " . $ex->getMessage();
}
}
}elseif($row2["QUANTITY"] < $row2["STORES"]){
for($i=0; $i<$row2["QUANTITY"]; $i++){
try{
$insertRslt = $insert->execute($values);
}catch(PDOException $ex){
echo "QUERY FOUR FAILED!!!: " . $ex->getMessage();
}
}
}
}elseif(!empty($count4)){
print_r("updating");
if($row2["QUANTITY"] > $row2["STORES"]){
for($i=0; $i<$row2['STORES']; $i++){
try{
$updateRslt = $update->execute($values);
}catch(PDOException $ex){
echo "QUERY FIVE FAILED!!!: " . $ex->getMessage();
}
}
}elseif($row2["QUANTITY"] < $row2["STORES"]){
for($i=0; $i<$row2['QUANTITY']; $i++){
try{
$updateRslt = $update->execute($values);
}catch(PDOException $ex){
echo "QUERY FIVE FAILED!!!: " . $ex->getMessage();
}
}
}
}
}
?>
编辑:
主要想法是这样的 - 如果我有10个地点,我今天订购了10个sku,明天另外订购了30个sku,另一个订单是20天后的sku,并且脚本开始运行最后一天.......它将捕获所有3个订单。共有60个skus,但在脚本的最后我应该只有10个sku记录。
这有意义吗?