feat(fsgx): 完成全部24项开发任务 Phase1-7
Phase1 后端核心:
- 新增 fsgx_v1.sql 迁移脚本(is_queue_goods/frozen_points/available_points/no_assess)
- SystemConfigServices 返佣设置扩展(周期人数/分档比例/范围/时机)
- StoreOrderCreateServices 周期循环佣金计算
- StoreOrderTakeServices 佣金发放后同步冻结积分
- StoreProductServices/StoreProduct 保存 is_queue_goods
Phase2 后端接口:
- GET /api/hjf/brokerage/progress 佣金周期进度
- GET /api/hjf/assets/overview 资产总览
- HjfPointsServices 每日 frozen_points 0.4‰ 释放定时任务
- PUT /adminapi/hjf/member/{uid}/no_assess 不考核接口
- GET /adminapi/hjf/points/release_log 积分日志接口
Phase3 前端清理:
- hjfCustom.js 路由精简(仅保留 points/log)
- hjfQueue.js/hjfMember.js API 清理/重定向至 CRMEB 原生接口
- pages.json 公排→推荐佣金/佣金记录/佣金规则
Phase4-5 前端改造:
- queue/status.vue 推荐佣金进度页整体重写
- 商品详情/订单确认/支付结果页文案与逻辑改造
- 个人中心/资产页/引导页/规则页文案改造
- HjfQueueProgress/HjfRefundNotice/HjfAssetCard 组件改造
- 推广中心嵌入佣金进度摘要
- hjfMockData.js 全量更新(公排字段→佣金字段)
Phase6 Admin 增强:
- 用户列表新增 frozen_points/available_points 列及不考核操作按钮
- hjfPoints.js USE_MOCK=false 对接真实积分日志接口
Phase7 配置文档:
- docs/fsgx-phase7-config-checklist.md 后台配置与全链路验收清单
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\lottery;
|
||||
|
||||
use app\jobs\system\CapitalFlowJob;
|
||||
use app\services\BaseServices;
|
||||
use app\dao\activity\lottery\LuckLotteryRecordDao;
|
||||
use app\services\activity\coupon\StoreCouponIssueServices;
|
||||
use app\services\order\StoreOrderCreateServices;
|
||||
use app\services\user\UserBillServices;
|
||||
use app\services\user\UserMoneyServices;
|
||||
use app\services\user\UserServices;
|
||||
use app\services\wechat\WechatUserServices;
|
||||
use crmeb\services\wechat\Payment;
|
||||
use think\annotation\Inject;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 抽奖记录
|
||||
* Class LuckLotteryRecordServices
|
||||
* @package app\services\activity\lottery
|
||||
* @mixin LuckLotteryRecordDao
|
||||
*/
|
||||
class LuckLotteryRecordServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var LuckLotteryRecordDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LuckLotteryRecordDao $dao;
|
||||
|
||||
/**
|
||||
* 获取抽奖记录列表
|
||||
* @param array $where
|
||||
* @return array
|
||||
*/
|
||||
public function getList(array $where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$where['not_type'] = 1;
|
||||
if (isset($where['factor']) && $where['factor']) {
|
||||
/** @var LuckLotteryServices $luckServices */
|
||||
$luckServices = app()->make(LuckLotteryServices::class);
|
||||
$where['lottery_id'] = $luckServices->value(['factor' => $where['factor'], 'status' => 1], 'id');
|
||||
if (!$where['lottery_id']) {
|
||||
$list = [];
|
||||
$count = 0;
|
||||
return compact('list', 'count');
|
||||
}
|
||||
unset($where['factor']);
|
||||
}
|
||||
$list = $this->dao->getList($where, '*', ['lottery', 'prize', 'user'], $page, $limit);
|
||||
$count = 0;
|
||||
if ($list) {
|
||||
$prizeType = app()->make(LuckPrizeServices::class)->prize_type;
|
||||
foreach ($list as &$item) {
|
||||
if (isset($item['prize_info']) && $item['prize_info']) {
|
||||
$prize = json_decode($item['prize_info'], true);
|
||||
} else {
|
||||
$prize = $item['prize'];
|
||||
}
|
||||
if (!$item['user']) {
|
||||
$item['user']['nickname'] = '用户已注销';
|
||||
$item['user']['real_name'] = '用户已注销';
|
||||
$item['user']['phone'] = '';
|
||||
$item['user']['uid'] = 0;
|
||||
$item['user']['delete_time'] = null;
|
||||
}
|
||||
$prize['type_name'] = $prizeType[$prize['type']] ?? '未知';
|
||||
$item['prize'] = $prize;
|
||||
$item['add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
|
||||
}
|
||||
$count = $this->dao->count($where);
|
||||
}
|
||||
|
||||
return compact('list', 'count');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中奖记录
|
||||
* @param array $where
|
||||
* @param int $limit
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getWinList(array $where, int $limit = 10)
|
||||
{
|
||||
//去除未中奖的
|
||||
$where = $where + ['not_type' => 1];
|
||||
$list = $this->dao->getList($where, 'id,uid,prize_id,lottery_id,receive_time,prize_info,add_time', ['user', 'prize'], 0, $limit);
|
||||
foreach ($list as &$item) {
|
||||
if (isset($item['prize_info']) && $item['prize_info']) {
|
||||
$prize = json_decode($item['prize_info'], true);
|
||||
unset($item['prize_info']);
|
||||
} else {
|
||||
$prize = $item['prize'];
|
||||
}
|
||||
$item['prize'] = [
|
||||
'id' => $prize['id'] ?? 0,
|
||||
'type' => $prize['type'] ?? 1,
|
||||
'name' => $prize['name'] ?? '',
|
||||
'image' => $prize['image'] ?? '',
|
||||
'prompt' => $item['prompt'] ?? '',
|
||||
];
|
||||
$item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
|
||||
$item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '';
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参与抽奖数据统计
|
||||
* @param int $lottery_id
|
||||
* @return int[]
|
||||
*/
|
||||
public function getLotteryRecordData(int $lottery_id)
|
||||
{
|
||||
$data = ['all' => 0, 'people' => 0, 'win' => 0];
|
||||
if ($lottery_id) {
|
||||
$where = [['lottery_id', '=', $lottery_id]];
|
||||
$data['all'] = $this->dao->getCount($where);
|
||||
$data['people'] = $this->dao->getCount($where, 'uid');
|
||||
$data['win'] = $this->dao->getCount($where + [['type', '>', 1]], 'uid');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入中奖纪录
|
||||
* @param int $uid
|
||||
* @param array $prize
|
||||
* @return mixed
|
||||
*/
|
||||
public function insertPrizeRecord(int $uid, array $prize, array $userInfo = [])
|
||||
{
|
||||
if (!$userInfo) {
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserInfo($uid);
|
||||
}
|
||||
if (!$userInfo) {
|
||||
throw new ValidateException('用户不存在');
|
||||
}
|
||||
if (!$prize) {
|
||||
throw new ValidateException('奖品不存在');
|
||||
}
|
||||
$services = app()->make(StoreOrderCreateServices::class);
|
||||
$data = [];
|
||||
$data['uid'] = $uid;
|
||||
$data['order_id'] = $services->getNewOrderId('hb');
|
||||
$data['lottery_id'] = $prize['lottery_id'];
|
||||
$data['prize_id'] = $prize['id'];
|
||||
$data['type'] = $prize['type'];
|
||||
$data['prize_info'] = json_encode($prize);
|
||||
$data['add_time'] = time();
|
||||
if (!$res = $this->dao->save($data)) {
|
||||
throw new ValidateException('写入中奖记录失败');
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 领取奖品
|
||||
* @param int $uid
|
||||
* @param int $lottery_record_id
|
||||
* @param string $receive_info
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function receivePrize(int $uid, int $lottery_record_id, array $receive_info = [], string $channel_type = '')
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserCacheInfo($uid);
|
||||
if (!$userInfo) {
|
||||
throw new ValidateException('用户不存在');
|
||||
}
|
||||
$lotteryRecord = $this->dao->get($lottery_record_id, ['prize_id', 'lottery_id', 'id', 'is_receive', 'order_id'], ['prize']);
|
||||
if (!$lotteryRecord || !isset($lotteryRecord['prize'])) {
|
||||
throw new ValidateException('请继续参与活动抽奖');
|
||||
}
|
||||
if ($lotteryRecord['is_receive'] == 1) {
|
||||
throw new ValidateException('已经领取成功');
|
||||
}
|
||||
$data = ['is_receive' => 1, 'receive_time' => time(), 'receive_info' => $receive_info];
|
||||
if (isset($lotteryRecord['prize_info']) && $lotteryRecord['prize_info']) {
|
||||
$prize = json_decode($lotteryRecord['prize_info'], true);
|
||||
} else {
|
||||
$prize = $lotteryRecord['prize'];
|
||||
}
|
||||
$this->transaction(function () use ($uid, $userInfo, $lottery_record_id, $data, $prize, $userServices, $receive_info, $channel_type, $lotteryRecord) {
|
||||
//奖品类型1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
|
||||
switch ($prize['type']) {
|
||||
case 1:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
break;
|
||||
case 2:
|
||||
/** @var UserBillServices $userBillServices */
|
||||
$userBillServices = app()->make(UserBillServices::class);
|
||||
$integral = bcadd((string)$userInfo['integral'], (string)$prize['num'], 0);
|
||||
$userBillServices->income('lottery_give_integral', $uid, (int)$prize['num'], (int)$integral, $prize['id']);
|
||||
$userServices->update($uid, ['integral' => $integral], 'uid');
|
||||
break;
|
||||
case 3:
|
||||
/** @var UserMoneyServices $userMoneyServices */
|
||||
$userMoneyServices = app()->make(UserMoneyServices::class);
|
||||
$now_money = bcadd((string)$userInfo['now_money'], (string)$prize['num'], 2);
|
||||
$userMoneyServices->income('lottery_give_money', $uid, $prize['num'], $now_money, $prize['id']);
|
||||
$userServices->update($uid, ['now_money' => $now_money], 'uid');
|
||||
break;
|
||||
case 4:
|
||||
$wechat_order_id = $lotteryRecord['order_id'];
|
||||
$transferDetailList = [
|
||||
[
|
||||
'info_type' => '活动名称',
|
||||
'info_content' => '抽奖红包'
|
||||
],
|
||||
[
|
||||
'info_type' => '奖励说明',
|
||||
'info_content' => '抽奖红包'
|
||||
],
|
||||
];
|
||||
$userName = $userInfo['real_name'] ?? '';
|
||||
$res = Payment::merchantPay($channel_type, $uid, $wechat_order_id, (string)$prize['num'], '抽奖中奖红包', $userName, $transferDetailList);
|
||||
if (sys_config('pay_wechat_type') == 1) {
|
||||
$data = array_merge($data, [
|
||||
'transfer_bill_no' => $res['transfer_bill_no'] ?? '',
|
||||
'wechat_state' => $res['state'] ?? '',
|
||||
'fail_reason' => $res['fail_reason'] ?? '',
|
||||
'package_info' => $res['package_info'] ?? ''
|
||||
]);
|
||||
event('notice.notice', [[
|
||||
'uid' => $uid,
|
||||
'extract_price' => $prize['num'],
|
||||
'type' => 2,
|
||||
'order_id' => $wechat_order_id
|
||||
], 'revenue_received']);
|
||||
}
|
||||
//记录资金流水
|
||||
CapitalFlowJob::dispatch([['order_id' => $wechat_order_id, 'store_id' => 0, 'uid' => $uid, 'price' => $prize['num'], 'pay_type' => 'weixin', 'nickname' => $userInfo['nickname'], 'phone' => $userInfo['phone']], 'luck']);
|
||||
break;
|
||||
case 5:
|
||||
/** @var StoreCouponIssueServices $couponIssueService */
|
||||
$couponIssueService = app()->make(StoreCouponIssueServices::class);
|
||||
try {
|
||||
$couponIssueService->issueUserCoupon($uid, (int)$prize['coupon_id'], true, 'luck_lottery');
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('抽奖领取优惠券失败,原因:' . $e->getMessage());
|
||||
}
|
||||
break;
|
||||
case 6:
|
||||
if (!$receive_info['name'] || !$receive_info['phone'] || !$receive_info['address']) {
|
||||
throw new ValidateException('请输入收货人信息');
|
||||
}
|
||||
if (!check_phone($receive_info['phone'])) {
|
||||
throw new ValidateException('请输入正确的收货人电话');
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->dao->update($lottery_record_id, $data, 'id');
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发货、备注
|
||||
* @param int $lottery_record_id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function setDeliver(int $lottery_record_id, array $data)
|
||||
{
|
||||
$lotteryRecord = $this->dao->get($lottery_record_id, ['deliver_info', 'type', 'id']);
|
||||
if (!$lotteryRecord) {
|
||||
throw new ValidateException('抽奖记录不存在');
|
||||
}
|
||||
$deliver_info = $lotteryRecord['deliver_info'];
|
||||
$edit = [];
|
||||
//备注
|
||||
if (!$data['deliver_name'] && !$data['deliver_number']) {
|
||||
$deliver_info['mark'] = $data['mark'];
|
||||
} else {
|
||||
if ($lotteryRecord['type'] != 6 && ($data['deliver_name'] || $data['deliver_number'])) {
|
||||
throw new ValidateException('该奖品不需要发货');
|
||||
}
|
||||
if ($lotteryRecord['type'] == 6 && (!$data['deliver_name'] || !$data['deliver_number'])) {
|
||||
throw new ValidateException('请选择快递公司或输入快递单号');
|
||||
}
|
||||
$deliver_info['deliver_name'] = $data['deliver_name'];
|
||||
$deliver_info['deliver_number'] = $data['deliver_number'];
|
||||
$edit['is_deliver'] = 1;
|
||||
$edit['deliver_time'] = time();
|
||||
}
|
||||
$edit['deliver_info'] = $deliver_info;
|
||||
if (!$this->dao->update($lottery_record_id, $edit, 'id')) {
|
||||
throw new ValidateException('处理失败');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取中奖记录
|
||||
* @param int $uid
|
||||
* @return array
|
||||
*/
|
||||
public function getRecord(int $uid, $where = [])
|
||||
{
|
||||
if (!$where) {
|
||||
$where['uid'] = $uid;
|
||||
$where['not_type'] = 1;
|
||||
}
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
|
||||
foreach ($list as &$item) {
|
||||
if (isset($item['prize_info']) && $item['prize_info']) {
|
||||
$prize = $item['prize_info'] = json_decode($item['prize_info'], true);
|
||||
} else {
|
||||
$prize = $item['prize'];
|
||||
}
|
||||
$item['prize'] = $prize;
|
||||
$item['deliver_time'] = $item['deliver_time'] ? date('Y-m-d H:i:s', $item['deliver_time']) : '';
|
||||
$item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖次数
|
||||
* @param int $uid
|
||||
* @param int $lottery_id
|
||||
* @return array
|
||||
*/
|
||||
public function getLotteryNum(int $uid, int $lottery_id)
|
||||
{
|
||||
$where['uid'] = $uid;
|
||||
$where['lottery_id'] = $lottery_id;
|
||||
$now_day = strtotime(date('Y-m-d'));//今日
|
||||
$todayCount = $this->dao->getCount($where + ['add_time' => $now_day]);
|
||||
$totalCount = $this->dao->getCount($where);
|
||||
return compact('todayCount', 'totalCount');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,815 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\lottery;
|
||||
|
||||
use app\services\BaseServices;
|
||||
use app\dao\activity\lottery\LuckLotteryDao;
|
||||
use app\services\activity\coupon\StoreCouponIssueServices;
|
||||
use app\services\product\product\StoreProductServices;
|
||||
use app\services\user\label\UserLabelServices;
|
||||
use app\services\user\UserBillServices;
|
||||
use app\services\user\label\UserLabelRelationServices;
|
||||
use app\services\user\UserMoneyServices;
|
||||
use app\services\user\UserServices;
|
||||
use crmeb\exceptions\AdminException;
|
||||
use crmeb\services\CacheService;
|
||||
use think\annotation\Inject;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class LuckLotteryServices
|
||||
* @package app\services\activity\lottery
|
||||
* @mixin LuckLotteryDao
|
||||
*/
|
||||
class LuckLotteryServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* 抽奖形式,奖品数量
|
||||
* @var int[]
|
||||
*/
|
||||
protected array $lottery_type = [
|
||||
'1' => 8 //九宫格
|
||||
];
|
||||
/**
|
||||
* 抽奖类型
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $lottery_factor = [
|
||||
'1' => '积分抽奖',
|
||||
'2' => '余额抽奖',
|
||||
'3' => '订单支付',
|
||||
'4' => '订单评价',
|
||||
'5' => '关注公众号'
|
||||
];
|
||||
|
||||
/**
|
||||
* 抽奖次数缓存时间
|
||||
* @var int
|
||||
*/
|
||||
public int $luck_cache_time = 120;
|
||||
|
||||
/**
|
||||
* @var LuckLotteryDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LuckLotteryDao $dao;
|
||||
|
||||
/**
|
||||
* 获取抽奖缓存
|
||||
* @param int $factor
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/11/16
|
||||
*/
|
||||
public function getFactorLotteryCache(int $factor)
|
||||
{
|
||||
return $this->dao->cacheTag()->remember('' . $factor, function () use ($factor) {
|
||||
$lottery = $this->dao->getFactorLottery($factor);
|
||||
return $lottery ? $lottery->toArray() : null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖活动列表
|
||||
* @param array $where 查询条件
|
||||
* @return array
|
||||
*/
|
||||
public function getList(array $where)
|
||||
{
|
||||
[$page, $limit] = $this->getPageValue();
|
||||
$where['is_del'] = 0;
|
||||
$list = $this->dao->getList($where, '*', ['prize'], $page, $limit);
|
||||
$lottery_factor = $this->lottery_factor;
|
||||
/** @var LuckLotteryRecordServices $luckLotteryRecordServices */
|
||||
$luckLotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
|
||||
$ids = [];
|
||||
foreach ($list as &$item) {
|
||||
$item['lottery_type'] = $lottery_factor[$item['factor']] ?? '';
|
||||
$data = $luckLotteryRecordServices->getLotteryRecordData((int)$item['id']);
|
||||
$item['lottery_all'] = $data['all'] ?? 0;
|
||||
$item['lottery_people'] = $data['people'] ?? 0;
|
||||
$item['lottery_win'] = $data['win'] ?? 0;
|
||||
if ($item['status']) {
|
||||
if ($item['start_time'] == 0 && $item['end_time'] == 0) {
|
||||
$item['lottery_status'] = '进行中';
|
||||
} else {
|
||||
if ($item['start_time'] > time()) {
|
||||
$item['lottery_status'] = '未开始';
|
||||
} else if ($item['end_time'] < time()) {
|
||||
$item['lottery_status'] = '已结束';
|
||||
$item['status'] = 0;
|
||||
$ids[] = $item['id'];
|
||||
} else if ($item['end_time'] > time() && $item['start_time'] < time()) {
|
||||
$item['lottery_status'] = '进行中';
|
||||
}
|
||||
}
|
||||
} else $item['lottery_status'] = '已结束';
|
||||
$item['start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '';
|
||||
$item['end_time'] = $item['end_time'] ? date('Y-m-d H:i:s', $item['end_time']) : '';
|
||||
if (!count($item['records'])) {
|
||||
$item['records_total_user'] = 0;
|
||||
$item['records_wins_user'] = 0;
|
||||
$item['records_total_num'] = 0;
|
||||
$item['records_wins_num'] = 0;
|
||||
} else {
|
||||
$item['records_total_user'] = $item['records'][0]['total_user'];
|
||||
$item['records_wins_user'] = $item['records'][0]['wins_user'];
|
||||
$item['records_total_num'] = $item['records'][0]['total_num'];
|
||||
$item['records_wins_num'] = $item['records'][0]['wins_num'];
|
||||
}
|
||||
}
|
||||
foreach ($ids as $id) {
|
||||
$this->setStatus((int)$id, 0);
|
||||
}
|
||||
$count = $this->dao->count($where);
|
||||
return compact('list', 'count');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖详情
|
||||
* @param int $id
|
||||
* @return array|\think\Model
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getlotteryInfo(int $id)
|
||||
{
|
||||
$lottery = $this->dao->getLottery($id, '*', ['prize']);
|
||||
if (!$lottery) {
|
||||
throw new ValidateException('活动不存在或已删除');
|
||||
}
|
||||
$lottery = $lottery->toArray();
|
||||
if (isset($lottery['prize']) && $lottery['prize']) {
|
||||
$products = $coupons = [];
|
||||
$product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
|
||||
$coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
|
||||
/** @var StoreProductServices $productServices */
|
||||
$productServices = app()->make(StoreProductServices::class);
|
||||
$products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
|
||||
/** @var StoreCouponIssueServices $couponServices */
|
||||
$couponServices = app()->make(StoreCouponIssueServices::class);
|
||||
$coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
|
||||
foreach ($lottery['prize'] as &$prize) {
|
||||
$prize['coupon_title'] = $prize['goods_image'] = '';
|
||||
if ($prize['type'] == 6) {
|
||||
$prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
|
||||
}
|
||||
if ($prize['type'] == 5) {
|
||||
$prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $lottery;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取数据
|
||||
* @param int $factor
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getlotteryFactorInfo(int $factor)
|
||||
{
|
||||
$lottery = $this->dao->getFactorLottery($factor, '*', ['prize'], false);
|
||||
if (!$lottery) {
|
||||
return [];
|
||||
}
|
||||
$lottery = $lottery->toArray();
|
||||
if (isset($lottery['prize']) && $lottery['prize']) {
|
||||
$products = $coupons = [];
|
||||
$product_ids = array_unique(array_column($lottery['prize'], 'product_id'));
|
||||
$coupon_ids = array_unique(array_column($lottery['prize'], 'coupon_id'));
|
||||
/** @var StoreProductServices $productServices */
|
||||
$productServices = app()->make(StoreProductServices::class);
|
||||
$products = $productServices->getColumn([['id', 'in', $product_ids]], 'id,store_name,image', 'id');
|
||||
/** @var StoreCouponIssueServices $couponServices */
|
||||
$couponServices = app()->make(StoreCouponIssueServices::class);
|
||||
$coupons = $couponServices->getColumn([['id', 'in', $coupon_ids]], 'id,coupon_title', 'id');
|
||||
foreach ($lottery['prize'] as &$prize) {
|
||||
$prize['coupon_title'] = $prize['goods_image'] = '';
|
||||
if ($prize['type'] == 6) {
|
||||
$prize['goods_image'] = $products[$prize['product_id']]['image'] ?? '';
|
||||
}
|
||||
if ($prize['type'] == 5) {
|
||||
$prize['coupon_title'] = $coupons[$prize['coupon_id']]['coupon_title'] ?? '';
|
||||
}
|
||||
}
|
||||
/** @var UserLabelServices $labelService */
|
||||
$labelService = app()->make(UserLabelServices::class);
|
||||
$lottery['user_label'] = $lottery['user_label'] ? $labelService->getColumn([
|
||||
['id', 'in', $lottery['user_label']],
|
||||
], 'id,label_name') : [];
|
||||
}
|
||||
return $lottery;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加抽奖活动以及奖品
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function add(array $data)
|
||||
{
|
||||
$prizes = $data['prize'];
|
||||
$prize_num = $this->lottery_type[1];
|
||||
if (count($prizes) != $prize_num) {
|
||||
throw new ValidateException('请选择' . $prize_num . '个奖品');
|
||||
}
|
||||
if (array_sum(array_column($prizes, 'percent')) != 100) {
|
||||
throw new ValidateException('奖品概率和必须为100');
|
||||
}
|
||||
unset($data['prize']);
|
||||
$this->transaction(function () use ($data, $prizes) {
|
||||
$time = time();
|
||||
$data['add_time'] = $time;
|
||||
if (!$lottery = $this->dao->save($data)) {
|
||||
throw new ValidateException('添加抽奖活动失败');
|
||||
}
|
||||
if ($data['status']) {
|
||||
$this->setStatus((int)$lottery->id, $data['status']);
|
||||
}
|
||||
/** @var LuckPrizeServices $luckPrizeServices */
|
||||
$luckPrizeServices = app()->make(LuckPrizeServices::class);
|
||||
$sort = 1;
|
||||
foreach ($prizes as $prize) {
|
||||
$prize = $luckPrizeServices->checkPrizeData($prize);
|
||||
$prize['lottery_id'] = $lottery->id;
|
||||
unset($prize['id']);
|
||||
$prize['add_time'] = $time;
|
||||
$prize['sort'] = $sort;
|
||||
|
||||
$res = $luckPrizeServices->save($prize);
|
||||
if (!$res) {
|
||||
throw new ValidateException('添加抽奖奖品失败');
|
||||
}
|
||||
$sort++;
|
||||
//抽奖商品库存加入redis
|
||||
CacheService::setStock((string)$res->id, (int)$prize['total'], 6);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
$this->dao->cacheTag()->clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改抽奖活动以及奖品
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function edit(int $id, array $data)
|
||||
{
|
||||
$lottery = $this->dao->getLottery($id);
|
||||
if (!$lottery) {
|
||||
throw new ValidateException('抽奖活动不存在');
|
||||
}
|
||||
$newPrizes = $data['prize'];
|
||||
if (array_sum(array_column($newPrizes, 'percent')) != 100) {
|
||||
throw new ValidateException('奖品概率和必须为100');
|
||||
}
|
||||
unset($data['prize'], $data['id']);
|
||||
$prize_num = $this->lottery_type[1];
|
||||
if (count($newPrizes) != $prize_num) {
|
||||
throw new ValidateException('请选择' . $prize_num . '个奖品');
|
||||
}
|
||||
/** @var LuckPrizeServices $luckPrizeServices */
|
||||
$luckPrizeServices = app()->make(LuckPrizeServices::class);
|
||||
$prizes = $luckPrizeServices->getLotteryPrizeList($id);
|
||||
$this->transaction(function () use ($id, $lottery, $data, $newPrizes, $prizes, $luckPrizeServices) {
|
||||
$updateIds = array_column($newPrizes, 'id');
|
||||
$oldIds = array_column($prizes, 'id');
|
||||
$delIds = array_merge(array_diff($oldIds, $updateIds));
|
||||
$insert = [];
|
||||
$time = time();
|
||||
$sort = 1;
|
||||
foreach ($newPrizes as $prize) {
|
||||
$prize = $luckPrizeServices->checkPrizeData($prize);
|
||||
$prize['sort'] = $sort;
|
||||
if (isset($prize['id']) && $prize['id']) {
|
||||
$prizeId = $prize['id'];
|
||||
if (!$prize['lottery_id']) {
|
||||
throw new ValidateException('缺少活动ID');
|
||||
}
|
||||
if (!$luckPrizeServices->update($prize['id'], $prize, 'id')) {
|
||||
throw new ValidateException('修改奖品失败');
|
||||
}
|
||||
} else {
|
||||
unset($prize['id']);
|
||||
$prize['lottery_id'] = $id;
|
||||
$prize['add_time'] = $time;
|
||||
$prize['sort'] = $sort;
|
||||
$res = $luckPrizeServices->save($prize);
|
||||
if (!$res) {
|
||||
throw new ValidateException('新增奖品失败');
|
||||
}
|
||||
$prizeId = $res->id;
|
||||
}
|
||||
$sort++;
|
||||
|
||||
//抽奖商品库存加入redis
|
||||
CacheService::setStock((string)$prizeId, (int)$prize['total'], 6);
|
||||
}
|
||||
if ($delIds) {
|
||||
if (!$luckPrizeServices->update([['id', 'in', $delIds]], ['is_del' => 1])) {
|
||||
throw new ValidateException('删除奖品失败');
|
||||
}
|
||||
}
|
||||
if (!$this->dao->update($id, $data)) {
|
||||
throw new ValidateException('修改失败');
|
||||
}
|
||||
//上架
|
||||
if (!$lottery['status'] && $data['status']) {
|
||||
$this->setStatus($id, $data['status']);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
$this->dao->cacheTag()->clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户某个抽奖活动剩余抽奖次数
|
||||
* @param int $uid
|
||||
* @param int $lottery_id
|
||||
* @param array $userInfo
|
||||
* @param array $lottery
|
||||
* @return false|float|int|mixed
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLotteryNum(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
if (!$userInfo) {
|
||||
$userInfo = $userServices->getUserInfo($uid, ['integral', 'uid', 'now_money', 'spread_lottery']);
|
||||
}
|
||||
if (!$userInfo) {
|
||||
throw new ValidateException('用户不存在');
|
||||
}
|
||||
if (!$lottery) {
|
||||
$lottery = $this->dao->getLottery($lottery_id, '*', [], true);
|
||||
}
|
||||
if (!$lottery) {
|
||||
throw new ValidateException('该活动已经下架,请持续关注');
|
||||
}
|
||||
//抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
|
||||
switch ($lottery['factor']) {
|
||||
case 1:
|
||||
$integral = $userInfo['integral'] - app()->make(UserBillServices::class)->getBillSum(['uid' => $uid, 'is_frozen' => 1]);
|
||||
return $userInfo['integral'] > 0 && $lottery['factor_num'] > 0 ? floor($integral / $lottery['factor_num']) : 0;
|
||||
break;
|
||||
case 2:
|
||||
return $userInfo['now_money'] > 0 && $lottery['factor_num'] > 0 ? floor($userInfo['now_money'] / $lottery['factor_num']) : 0;
|
||||
break;
|
||||
case 3:
|
||||
return $this->getCacheLotteryNum($uid, 'order');
|
||||
break;
|
||||
case 4:
|
||||
return $this->getCacheLotteryNum($uid, 'comment');
|
||||
break;
|
||||
case 5:
|
||||
return $userInfo['spread_lottery'] ?? 0;
|
||||
break;
|
||||
default:
|
||||
throw new ValidateException('暂未有该类型活动');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证用户抽奖资格(用户等级、付费会员、用户标签)
|
||||
* @param int $uid
|
||||
* @param int $lottery_id
|
||||
* @param array $userInfo
|
||||
* @param array $lottery
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function checkoutUserAuth(int $uid, int $lottery_id, array $userInfo = [], array $lottery = [])
|
||||
{
|
||||
if (!$userInfo) {
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserCacheInfo($uid);
|
||||
}
|
||||
if (!$userInfo) {
|
||||
throw new ValidateException('用户不存在');
|
||||
}
|
||||
if (!$lottery) {
|
||||
$lottery = $this->dao->getLottery($lottery_id, '*', [], true);
|
||||
}
|
||||
if (!$lottery) {
|
||||
throw new ValidateException('该活动已经下架,请持续关注');
|
||||
}
|
||||
//部分用户参与
|
||||
if ($lottery['attends_user'] == 2) {
|
||||
//用户等级
|
||||
if ($lottery['user_level'] && !in_array($userInfo['level'], $lottery['user_level'])) {
|
||||
throw new ValidateException('您暂时无法参与该活动');
|
||||
}
|
||||
//用户标签
|
||||
if ($lottery['user_label']) {
|
||||
/** @var UserLabelRelationServices $userlableRelation */
|
||||
$userlableRelation = app()->make(UserLabelRelationServices::class);
|
||||
$user_labels = $userlableRelation->getUserLabels($uid);
|
||||
if (!array_intersect($lottery['user_label'], $user_labels)) {
|
||||
throw new ValidateException('您暂时无法参与该活动');
|
||||
}
|
||||
}
|
||||
//是否是付费会员
|
||||
if ($lottery['is_svip'] != -1) {
|
||||
if (($lottery['is_svip'] == 1 && $userInfo['is_money_level'] <= 0) || ($lottery['is_svip'] == 0 && $userInfo['is_money_level'] > 0)) {
|
||||
throw new ValidateException('您暂时无法参与该活动');
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽奖
|
||||
* @param int $uid
|
||||
* @param int $lottery_id
|
||||
* @return mixed
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function luckLottery(int $uid, int $lottery_id, string $channel_type = '')
|
||||
{
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$userInfo = $userServices->getUserInfo($uid);
|
||||
if (!$userInfo) {
|
||||
throw new ValidateException('用户不存在');
|
||||
}
|
||||
|
||||
$lottery = $this->dao->getLottery($lottery_id, '*', [], true);
|
||||
if (!$lottery) {
|
||||
throw new ValidateException('该活动已经下架,请持续关注');
|
||||
}
|
||||
$userInfo = $userInfo->toArray();
|
||||
$lottery = $lottery->toArray();
|
||||
//验证用户身份
|
||||
$this->checkoutUserAuth($uid, $lottery_id, $userInfo, $lottery);
|
||||
|
||||
/** @var LuckPrizeServices $lotteryPrizeServices */
|
||||
$lotteryPrizeServices = app()->make(LuckPrizeServices::class);
|
||||
$lotteryPrize = $lotteryPrizeServices->getPrizeList($lottery_id);
|
||||
if (!$lotteryPrize) {
|
||||
throw new ValidateException('该活动状态有误,请联系管理员');
|
||||
}
|
||||
if ($this->getLotteryNum($uid, $lottery_id, $userInfo, $lottery) < 1) {
|
||||
//抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
|
||||
switch ($lottery['factor']) {
|
||||
case 1:
|
||||
throw new ValidateException('积分不足,没有更多抽奖次数');
|
||||
break;
|
||||
case 2:
|
||||
throw new ValidateException('余额不足,没有更多抽奖次数');
|
||||
break;
|
||||
case 3:
|
||||
throw new ValidateException('购买商品之后获得更多抽奖次数');
|
||||
break;
|
||||
case 4:
|
||||
throw new ValidateException('订单完成评价之后获得更多抽奖次数');
|
||||
break;
|
||||
case 5:
|
||||
throw new ValidateException('邀请更多好友获取抽奖次数');
|
||||
break;
|
||||
default:
|
||||
throw new ValidateException('暂未有该类型活动');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($lottery['factor'] == 1) {//积分抽奖
|
||||
/** @var LuckLotteryRecordServices $lotteryRecordServices */
|
||||
$lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
|
||||
$data = $lotteryRecordServices->getLotteryNum($uid, (int)$lottery['id']);
|
||||
$lotteryData['todayCount'] = (int)max(bcsub((string)$lottery['lottery_num'], (string)$data['todayCount'], 0), 0);
|
||||
$lotteryData['totalCount'] = (int)max(bcsub((string)$lottery['total_lottery_num'], (string)$data['totalCount'], 0), 0);
|
||||
if ($lotteryData['todayCount'] <= 0) {
|
||||
throw new ValidateException('本次活动当天抽奖次数已用完!');
|
||||
}
|
||||
if ($lotteryData['totalCount'] <= 0) {
|
||||
throw new ValidateException('本次活动总抽奖次数已用完!');
|
||||
}
|
||||
}
|
||||
|
||||
/** @var LuckPrizeServices $luckPrizeServices */
|
||||
$luckPrizeServices = app()->make(LuckPrizeServices::class);
|
||||
//随机抽奖
|
||||
$prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
|
||||
if (!$prize) {
|
||||
throw new ValidateException('该活动状态有误,请联系管理员');
|
||||
}
|
||||
|
||||
return $this->transaction(function () use ($prize, $luckPrizeServices, $uid, $lotteryPrize, $userInfo, $lottery, $channel_type) {
|
||||
//中奖扣除积分、余额
|
||||
$this->lotteryFactor($uid, $userInfo, $lottery);
|
||||
//中奖减少奖品数量
|
||||
$luckPrizeServices->decPrizeNum($prize['id'], $prize);
|
||||
/** @var LuckLotteryRecordServices $lotteryRecordServices */
|
||||
$lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
|
||||
//中奖写入记录
|
||||
$record = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo);
|
||||
//不是站内商品直接领奖
|
||||
if ($prize['type'] != 6) {
|
||||
$lotteryRecordServices->receivePrize($uid, (int)$record->id, [], $channel_type);
|
||||
}
|
||||
$prize['lottery_record_id'] = $record->id;
|
||||
return $prize;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽奖消耗扣除用户积分、余额等
|
||||
* @param int $uid
|
||||
* @param array $userInfo
|
||||
* @param array $lottery
|
||||
* @return bool
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function lotteryFactor(int $uid, array $userInfo, array $lottery)
|
||||
{
|
||||
if (!$userInfo || !$lottery) {
|
||||
return true;
|
||||
}
|
||||
//抽奖类型:1:积分2:余额3:下单支付成功4:订单评价5:拉新人
|
||||
switch ($lottery['factor']) {
|
||||
case 1:
|
||||
if ($userInfo['integral'] > $lottery['factor_num']) {
|
||||
$integral = bcsub((string)$userInfo['integral'], (string)$lottery['factor_num'], 0);
|
||||
} else {
|
||||
$integral = 0;
|
||||
}
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
/** @var UserBillServices $userBillServices */
|
||||
$userBillServices = app()->make(UserBillServices::class);
|
||||
$userBillServices->income('lottery_use_integral', $uid, (int)$lottery['factor_num'], (int)$integral, $lottery['id']);
|
||||
if (!$userServices->update($uid, ['integral' => $integral], 'uid')) {
|
||||
throw new ValidateException('抽奖扣除用户积分失败');
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if ($userInfo['now_money'] >= $lottery['factor_num']) {
|
||||
$now_money = bcsub((string)$userInfo['now_money'], (string)$lottery['factor_num'], 2);
|
||||
} else {
|
||||
throw new ValidateException('抽奖失败,余额不足!');
|
||||
}
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
/** @var UserMoneyServices $userMoneyServices */
|
||||
$userMoneyServices = app()->make(UserMoneyServices::class);
|
||||
$userMoneyServices->income('lottery_use_money', $uid, $lottery['factor_num'], $now_money, $lottery['id']);
|
||||
if (!$userServices->update($uid, ['now_money' => $now_money], 'uid')) {
|
||||
throw new ValidateException('抽奖扣除用户余额失败');
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
//销毁抽奖次数缓存
|
||||
$this->delCacheLotteryNum($uid, $lottery['factor'] == 3 ? 'order' : 'comment');
|
||||
break;
|
||||
case 5:
|
||||
/** @var UserServices $userServices */
|
||||
$userServices = app()->make(UserServices::class);
|
||||
$spread_lottery = 0;
|
||||
if ($userInfo['spread_lottery'] > 1) {
|
||||
$spread_lottery = $userInfo['spread_lottery'] - 1;
|
||||
}
|
||||
if (!$userServices->update($uid, ['spread_lottery' => $spread_lottery], 'uid')) {
|
||||
throw new ValidateException('抽奖扣除用户推广获取抽奖次数失败');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new ValidateException('暂未有该类型活动');
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param int $id
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function delLottery(int $id)
|
||||
{
|
||||
if ($lottery = $this->dao->getLottery($id)) {
|
||||
if (!$this->dao->update(['id' => $id], ['is_del' => 1])) {
|
||||
throw new AdminException('删除失败,请稍候重试');
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置抽奖活动状态
|
||||
* @param int $id
|
||||
* @param $status
|
||||
* @return false|mixed
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function setStatus(int $id, $status)
|
||||
{
|
||||
if (!$id) return false;
|
||||
$lottery = $this->dao->getLottery($id, 'id,factor');
|
||||
if (!$lottery) {
|
||||
return false;
|
||||
}
|
||||
//每一种抽奖类型只有一个上架
|
||||
// if ($status) {
|
||||
// $this->dao->update(['factor' => $lottery['factor']], ['status' => 0]);
|
||||
// }
|
||||
return $this->dao->update($id, ['status' => $status], 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 下单支付、评论缓存抽奖次数
|
||||
* @param int $uid
|
||||
* @param string $type
|
||||
* @return bool
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function setCacheLotteryNum(int $uid, string $type = 'order')
|
||||
{
|
||||
$factor = $type == 'order' ? 3 : 4;
|
||||
$lottery = $this->dao->getFactorLottery($factor, 'id,factor_num');
|
||||
if (!$lottery || !$lottery['factor_num']) {
|
||||
return true;
|
||||
}
|
||||
$key = 'user_' . $type . '_luck_lottery_' . $uid;
|
||||
$timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
|
||||
$cache = CacheService::redisHandler();
|
||||
// $num = (int)$this->getCacheLotteryNum($uid, $type);
|
||||
$cache->set($timeKey, bcadd((string)time(), (string)$this->luck_cache_time), bcadd('10', (string)$this->luck_cache_time));
|
||||
return $cache->set($key, $lottery['factor_num'], $this->luck_cache_time);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖到期时间
|
||||
* @param int $uid
|
||||
* @param string $type
|
||||
* @return int|mixed
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function getCacheLotteryExpireTime(int $uid, string $type = 'order')
|
||||
{
|
||||
$timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
|
||||
$time = CacheService::redisHandler()->get($timeKey);
|
||||
return empty($time) ? 0 : $time;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出下单支付、评论得到的抽奖此处
|
||||
* @param int $uid
|
||||
* @param string $type
|
||||
* @return int|mixed
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function getCacheLotteryNum(int $uid, string $type = 'order')
|
||||
{
|
||||
$key = 'user_' . $type . '_luck_lottery_' . $uid;
|
||||
$num = CacheService::redisHandler()->get($key);
|
||||
return empty($num) ? 0 : $num;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽奖之后销毁缓存
|
||||
* @param int $uid
|
||||
* @param string $type
|
||||
* @return bool
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function delCacheLotteryNum(int $uid, string $type = 'order')
|
||||
{
|
||||
$key = 'user_' . $type . '_luck_lottery_' . $uid;
|
||||
$timeKey = 'expire_time_user_' . $type . '_luck_lottery_' . $uid;
|
||||
$num = $this->getCacheLotteryNum($uid, $type);
|
||||
$cache = CacheService::redisHandler();
|
||||
if ($num > 1) {
|
||||
$cache->set($timeKey, bcadd((string)time(), (string)$this->luck_cache_time), bcadd('10', (string)$this->luck_cache_time));
|
||||
$cache->set($key, $num - 1, $this->luck_cache_time);
|
||||
} else {
|
||||
$cache->delete($timeKey);
|
||||
$cache->delete($key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖列表以及正在使用的抽奖活动
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2025/3/18
|
||||
*/
|
||||
public function factorList()
|
||||
{
|
||||
$list = $this->dao->selectList([
|
||||
['status', '=', 1],
|
||||
['is_del', '=', 0],
|
||||
['end_time', '>', time()],
|
||||
['start_time', '<', time()],
|
||||
], 'id,name,factor,is_use')->toArray();
|
||||
$data = [
|
||||
'info' => [
|
||||
'point' => '',
|
||||
'pay' => '',
|
||||
'evaluate' => '',
|
||||
'follow' => ''
|
||||
],
|
||||
'point' => [],
|
||||
'pay' => [],
|
||||
'evaluate' => [],
|
||||
'follow' => []
|
||||
];
|
||||
foreach ($list as $item) {
|
||||
if ($item['factor'] == 1) {
|
||||
$data['point'][] = $item;
|
||||
if ($data['info']['point'] == '') {
|
||||
$data['info']['point'] = $item['is_use'] ? $item['id'] : '';
|
||||
}
|
||||
} elseif ($item['factor'] == 3) {
|
||||
$data['pay'][] = $item;
|
||||
if ($data['info']['pay'] == '') {
|
||||
$data['info']['pay'] = $item['is_use'] ? $item['id'] : '';
|
||||
}
|
||||
} elseif ($item['factor'] == 4) {
|
||||
$data['evaluate'][] = $item;
|
||||
if ($data['info']['evaluate'] == '') {
|
||||
$data['info']['evaluate'] = $item['is_use'] ? $item['id'] : '';
|
||||
}
|
||||
} else {
|
||||
$data['follow'][] = $item;
|
||||
if ($data['info']['follow'] == '') {
|
||||
$data['info']['follow'] = $item['is_use'] ? $item['id'] : '';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动类型使用
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @author wuhaotian
|
||||
* @email 442384644@qq.com
|
||||
* @date 2025/3/18
|
||||
*/
|
||||
public function factorUse($data)
|
||||
{
|
||||
$this->dao->update(['is_del' => 0], ['is_use' => 0]);
|
||||
if ($data['point']) {
|
||||
$this->dao->update(['factor' => 1, 'id' => $data['point']], ['is_use' => 1]);
|
||||
}
|
||||
if ($data['pay']) {
|
||||
$this->dao->update(['factor' => 3, 'id' => $data['pay']], ['is_use' => 1]);
|
||||
}
|
||||
if ($data['evaluate']) {
|
||||
$this->dao->update(['factor' => 4, 'id' => $data['evaluate']], ['is_use' => 1]);
|
||||
}
|
||||
if ($data['follow']) {
|
||||
$this->dao->update(['factor' => 5, 'id' => $data['follow']], ['is_use' => 1]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: CRMEB Team <admin@crmeb.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\services\activity\lottery;
|
||||
|
||||
use app\services\BaseServices;
|
||||
use app\dao\activity\lottery\LuckPrizeDao;
|
||||
use app\services\activity\coupon\StoreCouponIssueServices;
|
||||
use crmeb\services\CacheService;
|
||||
use think\annotation\Inject;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class LuckPrizeServices
|
||||
* @package app\services\activity\lottery
|
||||
* @mixin LuckPrizeDao
|
||||
*/
|
||||
class LuckPrizeServices extends BaseServices
|
||||
{
|
||||
/**
|
||||
* @var array 1:未中奖2:积分3:余额4:红包5:优惠券6:站内商品7:等级经验8:用户等级 9:svip天数
|
||||
*/
|
||||
public array $prize_type = [
|
||||
'1' => '未中奖',
|
||||
'2' => '积分',
|
||||
'3' => '余额',
|
||||
'4' => '红包',
|
||||
'5' => '优惠券',
|
||||
'6' => '站内商品',
|
||||
'7' => '等级经验',
|
||||
'8' => '用户等级',
|
||||
'9' => 'svip天数'
|
||||
];
|
||||
|
||||
/**
|
||||
* 奖品数据字段
|
||||
* @var array
|
||||
*/
|
||||
public array $prize = [
|
||||
'id' => 0,
|
||||
'type' => 1,
|
||||
'lottery_id' => 0,
|
||||
'name' => '',
|
||||
'prompt' => '',
|
||||
'image' => '',
|
||||
'chance' => 0,
|
||||
'percent' => 0,
|
||||
'total' => 0,
|
||||
'coupon_id' => 0,
|
||||
'product_id' => 0,
|
||||
'unique' => '',
|
||||
'num' => 1,
|
||||
'sort' => 0,
|
||||
'status' => 1,
|
||||
'is_del' => 0,
|
||||
'add_time' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var LuckPrizeDao
|
||||
*/
|
||||
#[Inject]
|
||||
protected LuckPrizeDao $dao;
|
||||
|
||||
/**
|
||||
* 奖品数据验证
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function checkPrizeData(array $data)
|
||||
{
|
||||
$data = array_merge($this->prize, array_intersect_key($data, $this->prize));
|
||||
if (!isset($data['name']) || !$data['name']) {
|
||||
throw new ValidateException('请填写奖品名称');
|
||||
}
|
||||
if (!isset($data['image']) || !$data['image']) {
|
||||
throw new ValidateException('请选择奖品图片');
|
||||
}
|
||||
if (!isset($data['chance'])) {
|
||||
throw new ValidateException('请填写奖品中奖权重');
|
||||
}
|
||||
if (!isset($data['type']) || !isset($this->prize_type[$data['type']])) {
|
||||
throw new ValidateException('请选择奖品类型');
|
||||
}
|
||||
if (in_array($data['type'], [2, 3, 4]) && (!isset($data['num']) || !$data['num'])) {
|
||||
$msg = '';
|
||||
switch ($data['type']) {
|
||||
case 2:
|
||||
$msg = '积分';
|
||||
break;
|
||||
case 3:
|
||||
$msg = '余额';
|
||||
break;
|
||||
case 4:
|
||||
$msg = '红包';
|
||||
break;
|
||||
}
|
||||
throw new ValidateException('请填写奖品赠送' . $msg . '数');
|
||||
}
|
||||
if ($data['type'] == 2) {
|
||||
$data['num'] = (int)$data['num'];
|
||||
}
|
||||
if ($data['type'] == 5 && (!isset($data['coupon_id']) || !$data['coupon_id'])) {
|
||||
throw new ValidateException('请选择优惠券');
|
||||
}
|
||||
if ($data['type'] == 6 && (!isset($data['product_id']) || !$data['product_id'])) {
|
||||
throw new ValidateException('请选择商品');
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改奖品
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function edit(int $id, array $data)
|
||||
{
|
||||
$this->checkPrizeData($data);
|
||||
$prize = $this->dao->get($id);
|
||||
if (!$prize) {
|
||||
throw new ValidateException('奖品不存在');
|
||||
}
|
||||
if (!$this->dao->update($id, $data, 'id')) {
|
||||
throw new ValidateException('修改失败');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个抽奖活动的所有奖品
|
||||
* @param int $lottery_id
|
||||
* @param string $field
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function getLotteryPrizeList(int $lottery_id, string $field = '*')
|
||||
{
|
||||
return $this->dao->getPrizeList($lottery_id, $field);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机奖品
|
||||
* @param array $data
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getLuckPrize(array $data)
|
||||
{
|
||||
$totalPercent = array_sum(array_column($data, 'percent')) * 100;
|
||||
$prize = [];
|
||||
if (!$data) return $prize;
|
||||
mt_srand();
|
||||
$random = mt_rand(1, (int)$totalPercent);
|
||||
$range = 0;
|
||||
$newPrize = array_combine(array_column($data, 'type'), $data);
|
||||
foreach ($data as $item) {
|
||||
// 转换百分比为千分位范围
|
||||
$range += $item['percent'] * 100; // 例如 12.34% -> 1234
|
||||
if ($random <= $range) {
|
||||
if (($item['sort'] != 1 && $item['total'] != -1 && $item['total'] <= 0)) {
|
||||
$prize = $newPrize[1] ?? [];
|
||||
} else {
|
||||
if (CacheService::checkStock((string)$item['id'], 1, 6)) {
|
||||
$prize = $item;
|
||||
} else {
|
||||
$prize = $newPrize[1] ?? [];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $prize;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// $prize = [];
|
||||
// if (!$data) return $prize;
|
||||
// $coupon = [];
|
||||
// $coupon_ids = array_unique(array_column($data, 'coupon_id'));
|
||||
// if ($coupon_ids) {
|
||||
// /** @var StoreCouponIssueServices $couponServices */
|
||||
// $couponServices = app()->make(StoreCouponIssueServices::class);
|
||||
// $coupon = $couponServices->getGiveCoupon([['id', 'IN', $coupon_ids]]);
|
||||
// if ($coupon) $coupon = array_combine(array_column($coupon, 'id'), $coupon);
|
||||
// }
|
||||
// $totalChance = 100;
|
||||
// $startChance = 0;
|
||||
// mt_srand();
|
||||
// $prizeChance = rand(0, $totalChance - 1);
|
||||
// $newPrize = array_combine(array_column($data, 'type'), $data);
|
||||
// foreach ($data as $item) {
|
||||
// $newStartChance = (int)bcadd((string)$item['chance'], (string)$startChance);
|
||||
// //随机数在这个基数端内 且该商品数量大于0 中奖
|
||||
// if ($prizeChance >= $startChance && $prizeChance < $newStartChance) {
|
||||
// //奖品权重<=0 || 随机到不是未中奖奖品-》设置了奖品数量-》数量不足时 返回未中奖奖品 || 抽到优惠券 数量不足
|
||||
// if ((int)$item['chance'] <= 0 ||
|
||||
// (
|
||||
// $item['type'] != 1 &&
|
||||
// $item['total'] != -1 &&
|
||||
// $item['total'] <= 0
|
||||
// )
|
||||
// ||
|
||||
// (
|
||||
// $item['coupon_id'] &&
|
||||
// $coupon &&
|
||||
// !isset($coupon[$item['coupon_id']])
|
||||
// )
|
||||
// ) {
|
||||
// $prize = $newPrize[1] ?? [];
|
||||
// } else {
|
||||
// if (CacheService::checkStock((string)$item['id'], 1, 6)) {
|
||||
// $prize = $item;
|
||||
// } else {
|
||||
// $prize = $newPrize[1] ?? [];
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// $startChance = $newStartChance;
|
||||
// }
|
||||
// return $prize;
|
||||
}
|
||||
|
||||
/**
|
||||
* 中奖后减少奖品数量
|
||||
* @param int $id
|
||||
* @param array $prize
|
||||
* @return bool
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function decPrizeNum(int $id, array $prize = [])
|
||||
{
|
||||
if (!$id) return false;
|
||||
if (!$prize) {
|
||||
$prize = $this->dao->get($id);
|
||||
}
|
||||
if (!$prize) {
|
||||
throw new ValidateException('该奖品不存在');
|
||||
}
|
||||
//不是未中奖奖品 减少奖品数量
|
||||
if ($prize['type'] != 1 && $prize['total'] >= 1) {
|
||||
$total = $prize['total'] - 1;
|
||||
if (!$this->dao->update($id, ['total' => $total], 'id')) {
|
||||
throw new ValidateException('抽奖减少奖品总数失败');
|
||||
}
|
||||
CacheService::popStock((string)$id, 1, 6);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user