LotteryActivityService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace app\common\Service\Lottery;
  3. use app\common\model\lottery\LotteryActivity;
  4. use app\common\Enum\LotteryEnum;
  5. /**
  6. * 抽奖活动服务类
  7. */
  8. class LotteryActivityService
  9. {
  10. /**
  11. * 检查活动是否正在进行
  12. */
  13. public static function isRunning(LotteryActivity $activity)
  14. {
  15. $now = time();
  16. return $activity->status == LotteryEnum::STATUS_ONGOING
  17. && $activity->start_time <= $now
  18. && $activity->end_time >= $now;
  19. }
  20. /**
  21. * 检查活动是否已结束
  22. */
  23. public static function isEnded(LotteryActivity $activity)
  24. {
  25. $now = time();
  26. return $activity->status == LotteryEnum::STATUS_ENDED
  27. || $activity->end_time < $now;
  28. }
  29. /**
  30. * 检查活动是否未开始
  31. */
  32. public static function isNotStarted(LotteryActivity $activity)
  33. {
  34. $now = time();
  35. return $activity->status == LotteryEnum::STATUS_NOT_STARTED
  36. && $activity->start_time > $now;
  37. }
  38. /**
  39. * 检查活动是否已暂停
  40. */
  41. public static function isSuspended(LotteryActivity $activity)
  42. {
  43. return $activity->status == LotteryEnum::STATUS_SUSPENDED;
  44. }
  45. /**
  46. * 检查活动是否已取消
  47. */
  48. public static function isCancelled(LotteryActivity $activity)
  49. {
  50. return $activity->status == LotteryEnum::STATUS_CANCELLED;
  51. }
  52. /**
  53. * 检查是否在抽奖时间内
  54. * 注意:当前数据库表结构中没有draw_time相关字段,所以始终返回true
  55. */
  56. public static function isInDrawTime(LotteryActivity $activity)
  57. {
  58. // 当前表结构中没有draw_time_enable, draw_time_start, draw_time_end字段
  59. // 所以直接返回true,表示始终可以抽奖
  60. return true;
  61. }
  62. /**
  63. * 获取正在进行的活动
  64. */
  65. public static function getRunningActivities()
  66. {
  67. $now = time();
  68. return LotteryActivity::where('status', LotteryEnum::STATUS_ONGOING)
  69. ->where('start_time', '<=', $now)
  70. ->where('end_time', '>=', $now)
  71. ->select();
  72. }
  73. /**
  74. * 获取未开始的活动
  75. */
  76. public static function getNotStartedActivities()
  77. {
  78. $now = time();
  79. return LotteryActivity::where('status', LotteryEnum::STATUS_NOT_STARTED)
  80. ->where('start_time', '>', $now)
  81. ->select();
  82. }
  83. /**
  84. * 获取已结束的活动
  85. */
  86. public static function getEndedActivities()
  87. {
  88. $now = time();
  89. return LotteryActivity::where('status', LotteryEnum::STATUS_ENDED)
  90. ->orWhere('end_time', '<', $now)
  91. ->select();
  92. }
  93. /**
  94. * 获取可显示的活动(排除逻辑状态)
  95. */
  96. public static function getDisplayableActivities()
  97. {
  98. $displayableStatuses = array_keys(LotteryEnum::getActivityStatusMap());
  99. return LotteryActivity::whereIn('status', $displayableStatuses)->select();
  100. }
  101. /**
  102. * 验证活动状态是否有效
  103. */
  104. public static function isValidStatus(LotteryActivity $activity)
  105. {
  106. return LotteryEnum::isValidActivityStatus($activity->status);
  107. }
  108. /**
  109. * 验证开奖方式是否有效
  110. */
  111. public static function isValidLotteryType(LotteryActivity $activity)
  112. {
  113. return LotteryEnum::isValidLotteryType($activity->lottery_type);
  114. }
  115. /**
  116. * 获取活动状态描述
  117. */
  118. public static function getActivityStatusDescription(LotteryActivity $activity)
  119. {
  120. $now = time();
  121. if (self::isCancelled($activity)) {
  122. return '活动已取消';
  123. }
  124. if (self::isSuspended($activity)) {
  125. return '活动已暂停';
  126. }
  127. if (self::isEnded($activity)) {
  128. return '活动已结束';
  129. }
  130. if (self::isRunning($activity)) {
  131. return '活动进行中';
  132. }
  133. if (self::isNotStarted($activity)) {
  134. return '活动未开始';
  135. }
  136. return '未知状态';
  137. }
  138. /**
  139. * 检查用户是否可以参与活动
  140. */
  141. public static function canUserParticipate(LotteryActivity $activity, $userId)
  142. {
  143. // 检查活动状态
  144. if (!self::isRunning($activity)) {
  145. return false;
  146. }
  147. // 检查抽奖时间
  148. if (!self::isInDrawTime($activity)) {
  149. return false;
  150. }
  151. // TODO: 检查用户群体限制
  152. // TODO: 检查用户参与次数限制
  153. return true;
  154. }
  155. /**
  156. * 获取活动统计信息
  157. */
  158. public static function getActivityStatistics(LotteryActivity $activity)
  159. {
  160. return [
  161. 'total_participants' => $activity->drawRecords()->count(),
  162. 'total_winners' => $activity->winRecords()->count(),
  163. 'total_prizes' => $activity->prizes()->count(),
  164. 'total_conditions' => $activity->conditions()->count(),
  165. ];
  166. }
  167. /**
  168. * 批量更新活动状态
  169. */
  170. public static function batchUpdateStatus()
  171. {
  172. $now = time();
  173. // 将已过期的活动标记为已结束
  174. LotteryActivity::where('status', LotteryEnum::STATUS_ONGOING)
  175. ->where('end_time', '<', $now)
  176. ->update(['status' => LotteryEnum::STATUS_ENDED]);
  177. // 将到期的未开始活动标记为进行中
  178. LotteryActivity::where('status', LotteryEnum::STATUS_NOT_STARTED)
  179. ->where('start_time', '<=', $now)
  180. ->where('end_time', '>=', $now)
  181. ->update(['status' => LotteryEnum::STATUS_ONGOING]);
  182. }
  183. /**
  184. * 创建新活动
  185. */
  186. public static function createActivity($data)
  187. {
  188. // 验证数据
  189. if (!LotteryEnum::isValidActivityStatus($data['status'])) {
  190. throw new \Exception('无效的活动状态');
  191. }
  192. if (!LotteryEnum::isValidLotteryType($data['lottery_type'])) {
  193. throw new \Exception('无效的开奖方式');
  194. }
  195. return LotteryActivity::create($data);
  196. }
  197. /**
  198. * 更新活动
  199. */
  200. public static function updateActivity(LotteryActivity $activity, $data)
  201. {
  202. // 验证数据
  203. if (isset($data['status']) && !LotteryEnum::isValidActivityStatus($data['status'])) {
  204. throw new \Exception('无效的活动状态');
  205. }
  206. if (isset($data['lottery_type']) && !LotteryEnum::isValidLotteryType($data['lottery_type'])) {
  207. throw new \Exception('无效的开奖方式');
  208. }
  209. return $activity->save($data);
  210. }
  211. /**
  212. * 删除活动
  213. */
  214. public static function deleteActivity(LotteryActivity $activity)
  215. {
  216. // 检查是否可以删除
  217. if (self::isRunning($activity)) {
  218. throw new \Exception('进行中的活动不能删除');
  219. }
  220. return $activity->delete();
  221. }
  222. /**
  223. * 获取奖品列表
  224. * @param int|null $activityId 活动ID,不传则查询进行中活动的奖品
  225. * @param int $page 页码
  226. * @param int $pageSize 每页数量
  227. * @param array|null $type 奖品类型筛选数组
  228. * @return \think\Paginator
  229. */
  230. public static function getPrizes($activityId = null, $page = 1, $pageSize = 10, $type = null)
  231. {
  232. $where = [];
  233. // 如果指定了活动ID,查询该活动的奖品
  234. if ($activityId) {
  235. $where['activity_id'] = $activityId;
  236. } else {
  237. // 否则查询进行中活动的奖品
  238. $now = time();
  239. $runningActivityIds = LotteryActivity::where('status', LotteryEnum::STATUS_ONGOING)
  240. ->where('start_time', '<=', $now)
  241. ->where('end_time', '>=', $now)
  242. ->column('id');
  243. if (empty($runningActivityIds)) {
  244. // 返回空的分页对象
  245. $prizeModel = new \app\common\model\lottery\LotteryPrize();
  246. return $prizeModel->where('id', 0)->paginate($pageSize, false, ['page' => $page]);
  247. }
  248. $where['activity_id'] = ['in', $runningActivityIds];
  249. }
  250. // 奖品状态筛选(只查询有效的奖品)
  251. $where['status'] = 1;
  252. // 奖品类型筛选
  253. if ($type && is_array($type) && !empty($type)) {
  254. $where['type'] = ['in', $type];
  255. }
  256. // 使用模型的分页查询器
  257. $prizeModel = new \app\common\model\lottery\LotteryPrize();
  258. $paginate = $prizeModel->where($where)
  259. ->field('id,activity_id,name,type,image,description,probability,total_stock,remain_stock,win_prompt,sort_order,unlock_people_num,createtime')
  260. ->order('sort_order asc, createtime desc')
  261. ->paginate($pageSize, false, ['page' => $page]);
  262. // 处理分页数据,添加额外信息
  263. foreach ($paginate->items() as $prize) {
  264. // 获取活动信息
  265. $activity = LotteryActivity::find($prize->activity_id);
  266. $prize->activity_name = $activity->name ?? '';
  267. $prize->activity_status = $activity->status ?? 0;
  268. // 奖品类型文本
  269. $prize->type_text = LotteryEnum::getPrizeTypeText($prize->type);
  270. // 检查奖品是否已解锁(如果活动开启按人数解锁)
  271. if ($activity && $activity->unlock_by_people && $prize->unlock_people_num) {
  272. $currentPeopleCount = \app\common\Service\lottery\LotteryChanceService::getActivityParticipants($prize->activity_id);
  273. $prize->is_unlocked = $currentPeopleCount >= $prize->unlock_people_num;
  274. } else {
  275. $prize->is_unlocked = true;
  276. }
  277. // 计算中奖率百分比
  278. $prize->probability_percent = round($prize->probability * 100, 2);
  279. // 计算库存百分比
  280. if ($prize->total_stock > 0) {
  281. $prize->stock_percent = round(($prize->remain_stock / $prize->total_stock) * 100, 2);
  282. } else {
  283. $prize->stock_percent = 0;
  284. }
  285. }
  286. return $paginate;
  287. }
  288. /**
  289. * 获取活动奖品统计信息
  290. * @param int $activityId 活动ID
  291. * @return array
  292. */
  293. public static function getActivityPrizeStats($activityId)
  294. {
  295. $prizes = \app\common\model\lottery\LotteryPrize::where('activity_id', $activityId)
  296. ->where('status', 1)
  297. ->select();
  298. $stats = [
  299. 'total_prizes' => 0,
  300. 'total_stock' => 0,
  301. 'remain_stock' => 0,
  302. 'used_stock' => 0,
  303. 'prize_types' => [],
  304. 'unlocked_prizes' => 0,
  305. 'locked_prizes' => 0
  306. ];
  307. foreach ($prizes as $prize) {
  308. $stats['total_prizes']++;
  309. $stats['total_stock'] += $prize->total_stock;
  310. $stats['remain_stock'] += $prize->remain_stock;
  311. $stats['used_stock'] += ($prize->total_stock - $prize->remain_stock);
  312. // 统计奖品类型
  313. $typeText = LotteryEnum::getPrizeTypeText($prize->type);
  314. if (!isset($stats['prize_types'][$prize->type])) {
  315. $stats['prize_types'][$prize->type] = [
  316. 'type' => $prize->type,
  317. 'type_text' => $typeText,
  318. 'count' => 0,
  319. 'total_stock' => 0,
  320. 'remain_stock' => 0
  321. ];
  322. }
  323. $stats['prize_types'][$prize->type]['count']++;
  324. $stats['prize_types'][$prize->type]['total_stock'] += $prize->total_stock;
  325. $stats['prize_types'][$prize->type]['remain_stock'] += $prize->remain_stock;
  326. // 检查解锁状态
  327. $activity = LotteryActivity::find($activityId);
  328. if ($activity && $activity->unlock_by_people && $prize->unlock_people_num) {
  329. $currentPeopleCount = \app\common\Service\lottery\LotteryChanceService::getActivityParticipants($activityId);
  330. if ($currentPeopleCount >= $prize->unlock_people_num) {
  331. $stats['unlocked_prizes']++;
  332. } else {
  333. $stats['locked_prizes']++;
  334. }
  335. } else {
  336. $stats['unlocked_prizes']++;
  337. }
  338. }
  339. return $stats;
  340. }
  341. /**
  342. * 获取进行中活动的奖品列表
  343. * @param int $page 页码
  344. * @param int $pageSize 每页数量
  345. * @param int|array|null $type 奖品类型筛选,支持单个类型或类型数组
  346. * @return \think\Paginator
  347. */
  348. public static function getRunningActivityPrizes($page = 1, $pageSize = 20, $type = null)
  349. {
  350. return self::getPrizes(null, $page, $pageSize, $type);
  351. }
  352. /**
  353. * 获取指定活动的奖品列表
  354. * @param int $activityId 活动ID
  355. * @param int $page 页码
  356. * @param int $pageSize 每页数量
  357. * @param int|array|null $type 奖品类型筛选,支持单个类型或类型数组
  358. * @return \think\Paginator
  359. */
  360. public static function getActivityPrizes($activityId, $page = 1, $pageSize = 20, $type = null)
  361. {
  362. return self::getPrizes($activityId, $page, $pageSize, $type);
  363. }
  364. }