init
- 框架初始化 - 安装插件 - 修复PHP8.4报错
This commit is contained in:
173
addons/shopro/traits/CouponSend.php
Normal file
173
addons/shopro/traits/CouponSend.php
Normal file
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\traits;
|
||||
|
||||
use think\Db;
|
||||
use think\exception\HttpResponseException;
|
||||
use app\admin\model\shopro\Coupon;
|
||||
use app\admin\model\shopro\user\Coupon as UserCouponModel;
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
*/
|
||||
trait CouponSend
|
||||
{
|
||||
|
||||
/**
|
||||
* 用户自己领取优惠券
|
||||
*
|
||||
* @param [type] $id
|
||||
* @return void
|
||||
*/
|
||||
public function getCoupon($id)
|
||||
{
|
||||
$user = auth_user();
|
||||
|
||||
$userCoupon = Db::transaction(function () use ($user, $id) {
|
||||
$coupon = Coupon::normal() // 正常的可以展示的优惠券
|
||||
->canGet() // 在领取时间之内的
|
||||
->lock(true)
|
||||
->where('id', $id)
|
||||
->find();
|
||||
if (!$coupon) {
|
||||
error_stop('优惠券未找到');
|
||||
}
|
||||
|
||||
$userCoupon = $this->send($user, $coupon);
|
||||
|
||||
return $userCoupon;
|
||||
});
|
||||
|
||||
return $userCoupon;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 赠送优惠券
|
||||
*
|
||||
* @param array $user
|
||||
* @param array|string $ids
|
||||
* @return array
|
||||
*/
|
||||
public function giveCoupons($user, $ids)
|
||||
{
|
||||
$ids = is_array($ids) ? $ids : explode(',', $ids);
|
||||
|
||||
$result = Db::transaction(function () use ($user, $ids) {
|
||||
$errors = []; // 发送失败的优惠券,包含失败原因
|
||||
$success = []; // 发送成功的优惠券
|
||||
$coupons = Coupon::statusHidden() // 只查询隐藏券(后台发放的券)
|
||||
->canGet()
|
||||
->whereIn('id', $ids)
|
||||
->select();
|
||||
|
||||
$findCouponIds = array_column($coupons, 'id'); // 找到的优惠券 ids
|
||||
$nofundIds = array_diff($ids, $findCouponIds);
|
||||
foreach ($nofundIds as $nofund_id) {
|
||||
$errors[] = ['id' => $nofund_id, 'error' => '优惠券未找到'];
|
||||
}
|
||||
foreach ($coupons as $coupon) {
|
||||
try {
|
||||
$userCoupon = $this->send($user, $coupon);
|
||||
$success[] = $coupon->id;
|
||||
} catch (HttpResponseException $e) {
|
||||
$data = $e->getResponse()->getData();
|
||||
$message = $data ? ($data['msg'] ?? '') : $e->getMessage();
|
||||
$errors[] = ['id' => $coupon->id, 'error' => $message];
|
||||
} catch (\Exception $e) {
|
||||
$errors[] = ['id' => $coupon->id, 'error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
$result['success'] = $success;
|
||||
$result['errors'] = $errors;
|
||||
|
||||
return $result;
|
||||
});
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function manualSend($users, $id)
|
||||
{
|
||||
$coupon = Coupon::canGet() // 在领取时间之内的
|
||||
->lock(true)
|
||||
->where('id', $id)
|
||||
->find();
|
||||
|
||||
if (!$coupon) {
|
||||
// 不在发放时间段
|
||||
error_stop('优惠券不在发放时间段');
|
||||
}
|
||||
if ($coupon->stock < count($users)) {
|
||||
// 库存不足
|
||||
error_stop('优惠券库存不足');
|
||||
}
|
||||
|
||||
// 扣除库存
|
||||
$coupon->setDec('stock', count($users));
|
||||
|
||||
$sends = [];
|
||||
foreach ($users as $user) {
|
||||
$current = [
|
||||
'user_id' => $user->id,
|
||||
'coupon_id' => $coupon->id,
|
||||
'use_time' => null,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
];
|
||||
|
||||
$sends[] = $current;
|
||||
}
|
||||
|
||||
UserCouponModel::insertAll($sends);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 发放优惠券
|
||||
*
|
||||
* @param array|object $user 发放用户
|
||||
* @param array|object $coupon 要发放的优惠券
|
||||
* @return array|object
|
||||
*/
|
||||
private function send($user, $coupon) {
|
||||
if ($coupon->get_status == 'cannot_get') {
|
||||
error_stop('您已经领取过了');
|
||||
}
|
||||
|
||||
if ($coupon->stock <= 0) {
|
||||
error_stop('优惠券已经被领完了');
|
||||
}
|
||||
|
||||
$coupon->setDec('stock');
|
||||
|
||||
$userCoupon = new UserCouponModel();
|
||||
$userCoupon->user_id = $user->id;
|
||||
$userCoupon->coupon_id = $coupon->id;
|
||||
$userCoupon->use_time = null;
|
||||
$userCoupon->save();
|
||||
|
||||
return $userCoupon;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 退回用户优惠券
|
||||
*
|
||||
* @param integer $user_coupon_id
|
||||
* @return void
|
||||
*/
|
||||
public function backUserCoupon($user_coupon_id)
|
||||
{
|
||||
$userCoupon = UserCouponModel::where('id', $user_coupon_id)->find();
|
||||
|
||||
if ($userCoupon) {
|
||||
$userCoupon->use_time = null;
|
||||
$userCoupon->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
222
addons/shopro/traits/StockWarning.php
Normal file
222
addons/shopro/traits/StockWarning.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\traits;
|
||||
|
||||
use app\admin\model\shopro\Config;
|
||||
use app\admin\model\shopro\goods\StockWarning as StockWarningModel;
|
||||
use app\admin\model\shopro\goods\StockLog as StockLogModel;
|
||||
use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
|
||||
|
||||
/**
|
||||
* 库存预警
|
||||
*/
|
||||
trait StockWarning
|
||||
{
|
||||
/**
|
||||
* 获取全局库存配置
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getStockConfig()
|
||||
{
|
||||
$stock_warning = Config::getConfigField('shop.goods.stock_warning');
|
||||
|
||||
return intval($stock_warning);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取库存预警阀值
|
||||
*
|
||||
* @param [type] $goodsSkuPrice
|
||||
* @return void
|
||||
*/
|
||||
public function getStockWarning($goodsSkuPrice)
|
||||
{
|
||||
if (!is_null($goodsSkuPrice['stock_warning'])) {
|
||||
// 商品存在库存预警值
|
||||
$stock_warning = $goodsSkuPrice['stock_warning'];
|
||||
} else {
|
||||
// 默认库存预警值
|
||||
$stock_warning = $this->getStockConfig();
|
||||
}
|
||||
|
||||
return $stock_warning;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 检测库存是否低于预警阀值,并且记录
|
||||
*
|
||||
* @param [type] $goodsSkuPrice
|
||||
* @return void
|
||||
*/
|
||||
public function checkStockWarning($goodsSkuPrice, $type = 'edit')
|
||||
{
|
||||
$stock_warning = $this->getStockWarning($goodsSkuPrice);
|
||||
// 读取系统配置库存预警值
|
||||
if ($goodsSkuPrice['stock'] < $stock_warning) {
|
||||
// 增加库存不足记录
|
||||
$this->addStockWarning($goodsSkuPrice, $stock_warning);
|
||||
} else {
|
||||
if ($type == 'edit') {
|
||||
// 如果编辑了并且库存大于预警值需要检查并把记录删除
|
||||
$this->delStockWarning($goodsSkuPrice['id'], $goodsSkuPrice['goods_id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检测这个商品的所有规格库存预警
|
||||
*
|
||||
* @param [type] $goodsSkuPrices
|
||||
* @return void
|
||||
*/
|
||||
public function checkAllStockWarning($goodsSkuPrices, $type = 'add')
|
||||
{
|
||||
foreach ($goodsSkuPrices as $key => $goodsSkuPrice) {
|
||||
$this->checkStockWarning($goodsSkuPrice, $type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 记录库存低于预警值
|
||||
*
|
||||
* @param [type] $goodsSkuPrice
|
||||
* @param [type] $stock_warning
|
||||
* @return void
|
||||
*/
|
||||
public function addStockWarning($goodsSkuPrice, $stock_warning)
|
||||
{
|
||||
$stockWarning = StockWarningModel::where('goods_sku_price_id', $goodsSkuPrice['id'])
|
||||
->where('goods_id', $goodsSkuPrice['goods_id'])->find();
|
||||
|
||||
if ($stockWarning) {
|
||||
if ($stockWarning['stock_warning'] != $stock_warning
|
||||
|| $stockWarning->goods_sku_text != $goodsSkuPrice['goods_sku_text']
|
||||
) {
|
||||
$stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];;
|
||||
$stockWarning->stock_warning = $stock_warning;
|
||||
|
||||
$stockWarning->save();
|
||||
}
|
||||
} else {
|
||||
$stockWarning = new StockWarningModel();
|
||||
|
||||
$stockWarning->goods_id = $goodsSkuPrice['goods_id'];
|
||||
$stockWarning->goods_sku_price_id = $goodsSkuPrice['id'];
|
||||
$stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];
|
||||
$stockWarning->stock_warning = $stock_warning;
|
||||
$stockWarning->save();
|
||||
}
|
||||
|
||||
// 库存预警变动事件
|
||||
$data = ['goodsSkuPrice' => $goodsSkuPrice, 'stock_warning' => $stock_warning];
|
||||
\think\Hook::listen('goods_stock_warning', $data);
|
||||
|
||||
return $stockWarning;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除规格预警,比如:多规格编辑之后,作废的规格预警,补充库存之后的规格预警
|
||||
*
|
||||
* @param array $ids
|
||||
* @param integer $goods_id
|
||||
* @return void
|
||||
*/
|
||||
public function delStockWarning($goodsSkuPriceIds = [], $goods_id = 0)
|
||||
{
|
||||
$goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
|
||||
|
||||
StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
|
||||
$query->where('goods_id', $goods_id)
|
||||
->where('goods_sku_price_id', 'in', $goodsSkuPriceIds);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除商品除了这些规格之外的规格预警
|
||||
*
|
||||
* @param array $ids
|
||||
* @param integer $goods_id
|
||||
* @return void
|
||||
*/
|
||||
public function delNotStockWarning($goodsSkuPriceIds = [], $goods_id = 0)
|
||||
{
|
||||
$goodsSkuPriceIds = is_array($goodsSkuPriceIds) ? $goodsSkuPriceIds : [$goodsSkuPriceIds];
|
||||
|
||||
StockWarningModel::destroy(function ($query) use ($goods_id, $goodsSkuPriceIds) {
|
||||
$query->where('goods_id', $goods_id)
|
||||
->where('goods_sku_price_id', 'not in', $goodsSkuPriceIds);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 补货
|
||||
*
|
||||
* @param think\model $goodsSkuPrice
|
||||
* @param integer $stock
|
||||
* @return void
|
||||
*/
|
||||
public function addStockToSkuPrice($goodsSkuPrice, $stock, $type)
|
||||
{
|
||||
$before = $goodsSkuPrice->stock;
|
||||
|
||||
// 补充库存
|
||||
$goodsSkuPrice->setInc('stock', $stock);
|
||||
|
||||
// 添加补货记录
|
||||
$this->addStockLog($goodsSkuPrice, $before, $stock, $type);
|
||||
|
||||
// 检测库存预警
|
||||
$goodsSkuPrice = SkuPriceModel::find($goodsSkuPrice->id); // 重新获取 skuPrice
|
||||
$this->checkStockWarning($goodsSkuPrice);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加补货记录
|
||||
*
|
||||
* @param array $goodsSkuPrice
|
||||
* @param int $stock
|
||||
* @return void
|
||||
*/
|
||||
public function addStockLog($goodsSkuPrice, $before, $stock, $type = 'add')
|
||||
{
|
||||
$admin = auth_admin();
|
||||
$stockWarning = new StockLogModel();
|
||||
|
||||
$stockWarning->goods_id = $goodsSkuPrice['goods_id'];
|
||||
$stockWarning->admin_id = $admin['id'];
|
||||
$stockWarning->goods_sku_price_id = $goodsSkuPrice['id'];
|
||||
$stockWarning->goods_sku_text = is_array($goodsSkuPrice['goods_sku_text']) ? join(',', $goodsSkuPrice['goods_sku_text']) : $goodsSkuPrice['goods_sku_text'];
|
||||
$stockWarning->before = $before;
|
||||
$stockWarning->stock = $stock;
|
||||
|
||||
switch ($type) {
|
||||
case 'add':
|
||||
$msg = '添加商品';
|
||||
break;
|
||||
case 'goods':
|
||||
$msg = '商品列表补库存';
|
||||
break;
|
||||
case 'stock_warning':
|
||||
$msg = '库存预警补库存';
|
||||
break;
|
||||
default :
|
||||
$msg = '';
|
||||
break;
|
||||
}
|
||||
|
||||
$stockWarning->msg = $msg;
|
||||
$stockWarning->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user