门店端流量包接口提交
This commit is contained in:
63
Server/application/store/model/FlowPackageModel.php
Normal file
63
Server/application/store/model/FlowPackageModel.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace app\store\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class FlowPackageModel extends Model
|
||||
{
|
||||
|
||||
// 定义字段自动转换
|
||||
protected $type = [
|
||||
// 将特权字段从多行文本转换为数组
|
||||
'privileges' => 'array',
|
||||
];
|
||||
|
||||
/**
|
||||
* 特权字段获取器 - 将多行文本转换为数组
|
||||
* @param $value
|
||||
* @return array
|
||||
*/
|
||||
public function getPrivilegesAttr($value)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 如果已经是数组则直接返回
|
||||
if (is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// 按行分割文本
|
||||
return array_filter(explode("\n", $value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 折扣获取器 - 根据原价和售价计算折扣
|
||||
* @param $value
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
public function getDiscountAttr($value, $data)
|
||||
{
|
||||
if (empty($data['originalPrice']) || $data['originalPrice'] <= 0) {
|
||||
return '原价';
|
||||
}
|
||||
|
||||
$discount = round(($data['price'] / $data['originalPrice']) * 10, 1);
|
||||
return $discount . '折';
|
||||
}
|
||||
|
||||
/**
|
||||
* 总流量获取器 - 计算套餐总流量
|
||||
* @param $value
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public function getTotalFlowAttr($value, $data)
|
||||
{
|
||||
return isset($data['monthlyFlow']) && isset($data['duration']) ?
|
||||
intval($data['monthlyFlow']) * intval($data['duration']) : 0;
|
||||
}
|
||||
}
|
||||
104
Server/application/store/model/UserFlowPackageModel.php
Normal file
104
Server/application/store/model/UserFlowPackageModel.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace app\store\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class UserFlowPackageModel extends Model
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户当前有效的流量套餐
|
||||
*
|
||||
* @param int $userId 用户ID
|
||||
* @return array|null 用户套餐信息
|
||||
*/
|
||||
public static function getUserActivePackage($userId)
|
||||
{
|
||||
if (empty($userId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::where('userId', $userId)
|
||||
->where('status', 1) // 1表示有效
|
||||
->where('expireTime', '>', time()) // 未过期
|
||||
->order('expireTime', 'asc') // 按到期时间排序,最先到期的排在前面
|
||||
->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户套餐订阅记录
|
||||
*
|
||||
* @param int $userId 用户ID
|
||||
* @param int $packageId 套餐ID
|
||||
* @param int $duration 套餐时长(月)
|
||||
* @return bool 是否创建成功
|
||||
*/
|
||||
public static function createSubscription($userId, $packageId, $duration = 0)
|
||||
{
|
||||
if (empty($userId) || empty($packageId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取套餐信息
|
||||
$package = FlowPackageModel::where('id', $packageId)->where('isDel', 0)->find();
|
||||
if (empty($package)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果未指定时长,则使用套餐默认时长
|
||||
if (empty($duration)) {
|
||||
$duration = $package['duration'];
|
||||
}
|
||||
|
||||
// 计算开始时间和到期时间
|
||||
$now = time();
|
||||
$startTime = $now;
|
||||
$expireTime = strtotime("+{$duration} month", $now);
|
||||
|
||||
// 创建新订阅
|
||||
$data = [
|
||||
'userId' => $userId,
|
||||
'packageId' => $packageId,
|
||||
'duration' => $duration,
|
||||
'totalFlow' => $package->totalFlow,
|
||||
'usedFlow' => 0,
|
||||
'status' => 1, // 1表示有效
|
||||
'startTime' => $startTime,
|
||||
'expireTime' => $expireTime,
|
||||
'createTime' => $now,
|
||||
'updateTime' => $now
|
||||
];
|
||||
|
||||
return self::create($data) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户已使用流量
|
||||
*
|
||||
* @param int $id 用户套餐ID
|
||||
* @param int $usedFlow 已使用流量
|
||||
* @return bool 是否更新成功
|
||||
*/
|
||||
public static function updateUsedFlow($id, $usedFlow)
|
||||
{
|
||||
if (empty($id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$userPackage = self::where('id', $id)->find();
|
||||
if (empty($userPackage)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 确保使用量不超过总量
|
||||
$maxFlow = $userPackage['totalFlow'];
|
||||
$usedFlow = $usedFlow > $maxFlow ? $maxFlow : $usedFlow;
|
||||
|
||||
return self::where('id', $id)->update([
|
||||
'usedFlow' => $usedFlow,
|
||||
'updateTime' => time()
|
||||
]) ? true : false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user