涉及更改的模型有StoreProductAttrValue模型、StoreProductAttrResult模型、StoreProduct模型
/**
* 调价方案定时任务
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function scheduler(): void
{
// 查询启用中的调价方案
$schedulerList = EbHolidayScheduler::queryByEnabled()->select();
if ($schedulerList->isEmpty()) {
return;
}
$schedulerList->each(function (EbHolidayScheduler $scheduler) {
try {
switch (true) {
case $scheduler->isStepNone():
if (strtotime($scheduler->start_time) <= time()) {
HolidayPricingServices::scheduler($scheduler);
}
break;
case $scheduler->isStepChange():
if (strtotime($scheduler->end_time) <= time()) {
HolidayPricingServices::scheduler($scheduler);
}
break;
case $scheduler->isStepRecover():
default:
$scheduler->enabled = 0;
$scheduler->step = EbHolidayScheduler::ENUMS_STEP_NONE;
$scheduler->save();
break;
}
} catch (Throwable $throwable) {
//Log::error('【节日调价方案】定时任务执行失败:' . $throwable);
Log::error('【节日调价方案】定时任务执行失败:' . $throwable->getMessage());
}
});
}
/**
* 调价方案定时任务
* @param EbHolidayScheduler $scheduler 调价方案
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function scheduler(EbHolidayScheduler $scheduler): void
{
// 查询调价方案关联的产品
$list = EbHolidayPricing::where('holiday_scheduler_id', $scheduler->id)->select();
if ($list->isEmpty()) {
$scheduler->enabled = 0;
$scheduler->step = EbHolidayScheduler::ENUMS_STEP_NONE;
$scheduler->save();
return;
}
// 整理 商品价格,按商品ID和属性值分组
$products = [];
$list->each(function (EbHolidayPricing $pricing) use (&$products) {
$products[$pricing->product_id][$pricing->product_attr_unique] = $pricing;
});
// 循环调价方案关联的产品,修改商品的价格
$query = (new EbHolidayPricing())->db();
foreach ($products as $product_id => $items) {
// 事务
$query->transaction(function () use ($product_id, $items, $scheduler) {
/** @var StoreProductAttrResult $attrResult */
$attrResult = StoreProductAttrResult::generateQueryByProductId($product_id)->findOrEmpty();
$attrValue = StoreProductAttrValue::generateQueryByProductId($product_id)->select();
if ($attrResult->isEmpty() || $attrValue->isEmpty()) {
// 通常不会走到这里
return;
}
// 整理数据结构
$sukToUniqueMaps = $attrValue->column('unique', 'suk');
$result = is_array($attrResult->result) ? $attrResult->result : json_decode($attrResult->result, true);
$uniqueToAttrValueMaps = [];
$attrValue->each(function (StoreProductAttrValue $valueModel) use (&$uniqueToAttrValueMaps) {
$uniqueToAttrValueMaps[$valueModel->unique] = $valueModel;
});
// 先修改StoreProductAttrValue模型
/**
* @var string $product_attr_unique
* @var EbHolidayPricing $pricing
*/
foreach ($items as $product_attr_unique => $pricing) {
/** @var StoreProductAttrValue $valueModel */
$valueModel = $uniqueToAttrValueMaps[$product_attr_unique];
switch (true) {
case $scheduler->isStepNone():
$pricing->run_time = date('Y-m-d H:i:s');
$pricing->run_result = '首次执行,原价 >> 调整至 >> 节日价';
$pricing->save();
$valueModel->price = $pricing->holiday_price;
$valueModel->save();
break;
case $scheduler->isStepChange():
$pricing->run_time = date('Y-m-d H:i:s');
$pricing->run_result = '第二次执行,节日价 >> 恢复至 >> 原价';
$pricing->save();
$valueModel->price = $pricing->original_price;
$valueModel->save();
break;
default:
break;
}
$uniqueToAttrValueMaps[$product_attr_unique] = $valueModel;
}
// 再修改StoreProductAttrResult模型
foreach ($result['value'] as &$attr) {
$sukTemp = array_map(fn($i) => $attr['value' . $i], range(1, count($attr['detail'])));
$suk = implode(',', $sukTemp);
if (isset($sukToUniqueMaps[$suk])) {
$_unique = $sukToUniqueMaps[$suk];
$attr['price'] = $uniqueToAttrValueMaps[$_unique]->price;
}
}
$attrResult->result = $result;
$attrResult->save();
/** @var StoreProduct $product */
$product = StoreProduct::findOrEmpty($product_id);
$product->price = min(array_column($result['value'], 'price'));
$product->save();
});
}
// 修改调价方案状态
$scheduler->step = $scheduler->step + 1;
$scheduler->save();
}
版权属于:大卫科技Blog
本文链接:https://www.iyuu.cn/archives/526/
转载时须注明出处