FastMovieAI 二开基线 v1.0 (完整源码)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\expose\utils\Str;
|
||||
use app\model\Basic;
|
||||
use plugin\developer\app\model\PluginDeveloperTeam;
|
||||
use plugin\marketing\utils\enum\CouponRule;
|
||||
use plugin\user\app\model\PluginUser;
|
||||
|
||||
class PluginMarketingCoupon extends Basic
|
||||
{
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'type' => [
|
||||
// 设置JSON字段的类型
|
||||
'discount' => 'float',
|
||||
'full_price' => 'float',
|
||||
'money' => 'float',
|
||||
]
|
||||
];
|
||||
}
|
||||
public function password()
|
||||
{
|
||||
return $this->hasMany(PluginMarketingCouponPassword::class, 'id', 'coupon_id');
|
||||
}
|
||||
public function server()
|
||||
{
|
||||
return $this->hasMany(PluginMarketingCouponServer::class, 'coupon_id', 'id');
|
||||
}
|
||||
/* public function user()
|
||||
{
|
||||
return $this->hasOne();
|
||||
} */
|
||||
public static function options($where = [])
|
||||
{
|
||||
$models = self::where($where)->order('id desc')->select();
|
||||
$data = [];
|
||||
foreach ($models as $item) {
|
||||
$data[] = [
|
||||
'label' => $item->title,
|
||||
'value' => $item->id,
|
||||
'tips' => "折扣:{$item->discount_text}"
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
protected function sceneExternal()
|
||||
{
|
||||
return $this->visible(['title', 'coupon_rule', 'discount', 'full_price', 'money', 'receive_type', 'use_type', 'stackable', 'num', 'receive_num', 'sum', 'discount_text', 'code', 'percentage', 'receive', 'exclusive', 'scope', 'server', 'receive_type_text', 'use_type_text']);
|
||||
}
|
||||
public static function onAfterRead($model)
|
||||
{
|
||||
if ($model->coupon_rule) {
|
||||
if ($model->coupon_rule === CouponRule::DISCOUNT['value']) {
|
||||
$model->discount_text = ($model->discount * 10) . '折';
|
||||
} else {
|
||||
if ($model->full_price > 0) {
|
||||
$model->discount_text = "满{$model->full_price}元减{$model->money}元";
|
||||
} else {
|
||||
$model->discount_text = "直减{$model->money}元";
|
||||
}
|
||||
}
|
||||
}
|
||||
// coupon_id转16进制
|
||||
$coupon_id = dechex((int)$model->id);
|
||||
// coupon_id长度为6位,不足6位前面补0
|
||||
$model->code = 'A' . str_pad($coupon_id, 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
public static function codeToId($code)
|
||||
{
|
||||
return hexdec(substr($code, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取优惠券码
|
||||
*/
|
||||
public static function getCouponCode($prefix, $coupon_id)
|
||||
{
|
||||
// coupon_id转16进制
|
||||
$coupon_id = dechex((int)$coupon_id);
|
||||
// coupon_id长度为6位,不足6位前面补0
|
||||
$coupon_id = str_pad($coupon_id, 6, '0', STR_PAD_LEFT);
|
||||
$createDate = date('yWz');
|
||||
$round = Str::random(6);
|
||||
// 生成优惠券码
|
||||
$code = "{$prefix}{$coupon_id}-{$createDate}-{$round}";
|
||||
$couponCode = PluginMarketingCouponCode::where(['coupon_code' => $code])->find();
|
||||
if ($couponCode) {
|
||||
self::getCouponCode($prefix, $coupon_id);
|
||||
}
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
use plugin\finance\app\model\PluginFinanceOrders;
|
||||
use plugin\finance\utils\enum\OrdersState;
|
||||
use plugin\marketing\utils\enum\CouponRule;
|
||||
use plugin\marketing\utils\enum\UseType;
|
||||
use plugin\user\app\model\PluginUser;
|
||||
|
||||
class PluginMarketingCouponCode extends Basic
|
||||
{
|
||||
// 所有可识别订单状态
|
||||
public static $validOrderStates = [
|
||||
OrdersState::WAIT_PAY['value'],
|
||||
OrdersState::PAID['value'],
|
||||
OrdersState::FINISH['value'],
|
||||
OrdersState::REFUND_ING['value'],
|
||||
OrdersState::REFUND_SUCCESS['value'],
|
||||
OrdersState::REFUND_FAIL['value'],
|
||||
OrdersState::INVALID['value'],
|
||||
OrdersState::NOTIFY_FAIL['value'],
|
||||
];
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'type' => [
|
||||
// 设置JSON字段的类型
|
||||
'discount' => 'float',
|
||||
'full_price' => 'float',
|
||||
'money' => 'float',
|
||||
]
|
||||
];
|
||||
}
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne(PluginUser::class, 'id', 'uid');
|
||||
}
|
||||
public function coupon()
|
||||
{
|
||||
return $this->hasOne(PluginMarketingCoupon::class, 'id', 'coupon_id');
|
||||
}
|
||||
protected function sceneExternal()
|
||||
{
|
||||
return $this->visible(['coupon_code', 'coupon_rule', 'state', 'state_text', 'discount', 'full_price', 'money', 'start_time', 'end_time', 'expire_time', 'receive_time', 'coupon', 'exclusive', 'scope', 'update_scope', 'support_scope', 'role', 'receive_type_text', 'use_type_text', 'server']);
|
||||
}
|
||||
/**
|
||||
* 使用优惠券
|
||||
* @param PluginFinanceOrders 订单
|
||||
* @param PluginMarketingCouponCode 所选优惠券列表
|
||||
* @param null $extra 额外参数
|
||||
* @return array 返回可使用优惠券码id
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function useCoupon($Order, $Coupons, $extra = null)
|
||||
{
|
||||
$Order->system_money = 0;
|
||||
$result = [];
|
||||
|
||||
foreach ($Coupons as $item) {
|
||||
$coupon = $item->coupon;
|
||||
$server = $coupon->server()->column('server');
|
||||
if (!empty($server) && !in_array($Order->type, $server)) {
|
||||
throw new \Exception("优惠券「{$coupon->title}」不适用于当前服务");
|
||||
}
|
||||
|
||||
// 3. 校验使用类型
|
||||
switch ($coupon->use_type) {
|
||||
case UseType::FIRST_ORDER['value']:
|
||||
$hasPaidOrder = PluginFinanceOrders::where(['uid' => $Order->uid])
|
||||
->whereIn('state', self::$validOrderStates)
|
||||
->count();
|
||||
if ($hasPaidOrder > 0) {
|
||||
throw new \Exception("优惠券「{$coupon->title}」仅限首单使用");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 5. 优惠金额计算(先复制当前金额)
|
||||
$newMoney = $Order->money;
|
||||
|
||||
switch ($coupon->coupon_rule) {
|
||||
case CouponRule::DISCOUNT['value']:
|
||||
if ($coupon->discount > 0 && $coupon->discount < 1) {
|
||||
$newMoney = $newMoney * $coupon->discount;
|
||||
}
|
||||
break;
|
||||
|
||||
case CouponRule::FULL_PRICE['value']:
|
||||
if ($coupon->full_price > 0 && $Order->origin_money < $coupon->full_price) {
|
||||
throw new \Exception("优惠券「{$coupon->title}」不满足满减条件");
|
||||
}
|
||||
$newMoney -= $coupon->money;
|
||||
break;
|
||||
}
|
||||
|
||||
$newMoney = max(0, $newMoney);
|
||||
$discountAmount = $Order->money - $newMoney;
|
||||
|
||||
// 6. 更新订单金额及归属
|
||||
$Order->money = $newMoney;
|
||||
$Order->system_money += $discountAmount;
|
||||
|
||||
// 7. 记录已使用的券码ID
|
||||
$result[] = $item->id;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
|
||||
class PluginMarketingCouponPassword extends Basic
|
||||
{
|
||||
protected $pk='coupon_id';
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
|
||||
class PluginMarketingCouponServer extends Basic
|
||||
{
|
||||
protected $pk='coupon_id';
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
|
||||
class PluginMarketingPlan extends Basic
|
||||
{
|
||||
public function price()
|
||||
{
|
||||
return $this->hasMany(PluginMarketingPlanPrice::class, 'plan_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
|
||||
class PluginMarketingPlanPrice extends Basic
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\marketing\app\model;
|
||||
|
||||
use app\model\Basic;
|
||||
|
||||
class PluginMarketingPoints extends Basic
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user