Files
huangjingfen/pro_v3.5.1_副本/app/services/activity/lottery/LuckLotteryServices.php
apple 434aa8c69d 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
2026-03-23 22:32:19 +08:00

816 lines
32 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}