Activity.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use app\common\controller\Backend;
  4. use app\common\Enum\LotteryEnum;
  5. use app\common\Enum\ChannelEnum;
  6. use think\Db;
  7. use think\exception\ValidateException;
  8. use think\exception\PDOException;
  9. use Exception;
  10. /**
  11. * 抽奖活动管理
  12. *
  13. * @icon fa fa-gift
  14. */
  15. class Activity extends Backend
  16. {
  17. /**
  18. * Activity模型对象
  19. * @var \app\admin\model\lottery\Activity
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\lottery\Activity;
  26. // 获取枚举值列表
  27. $this->view->assign("typeList", LotteryEnum::getLotteryTypeMap());
  28. $this->assignconfig("typeList", json_encode(LotteryEnum::getLotteryTypeMap()));
  29. $this->view->assign("statusList", LotteryEnum::getActivityStatusMap());
  30. $this->assignconfig("statusList", json_encode(LotteryEnum::getActivityStatusMap()));
  31. $this->view->assign("lotteryTypeList", LotteryEnum::getLotteryTypeMap());
  32. $this->assignconfig("lotteryTypeList", json_encode(LotteryEnum::getLotteryTypeMap()));
  33. $this->view->assign("userLimitTypeList", LotteryEnum::getUserLimitTypeMap());
  34. $this->assignconfig("userLimitTypeList", json_encode(LotteryEnum::getUserLimitTypeMap()));
  35. $this->view->assign("guideStyleList", LotteryEnum::getGuideStyleMap());
  36. $this->assignconfig("guideStyleList", json_encode(LotteryEnum::getGuideStyleMap()));
  37. $this->view->assign("conditionTypeList", LotteryEnum::getConditionTypeMap());
  38. $this->assignconfig("conditionTypeList", json_encode(LotteryEnum::getConditionTypeMap()));
  39. // 奖品类型配置
  40. $this->view->assign("prizeTypeList", LotteryEnum::getPrizeTypeMap());
  41. $this->assignconfig("prizeTypeList", json_encode(LotteryEnum::getPrizeTypeMap()));
  42. // 奖品默认图片配置
  43. $this->assignconfig("prizeDefaultImages", json_encode(LotteryEnum::getPrizeDefaultImageMap()));
  44. // 渠道配置
  45. $this->view->assign("channelList", ChannelEnum::getChannelMap());
  46. $this->assignconfig("channelList", json_encode(ChannelEnum::getChannelMap()));
  47. // $this->view->assign("channelGroupList", ChannelEnum::getChannelGroupMap());
  48. // $this->assignconfig("channelGroupList", json_encode(ChannelEnum::getChannelGroupMap()));
  49. // $this->view->assign("channelsByGroup", ChannelEnum::getChannelsByGroup());
  50. // $this->assignconfig("channelsByGroup", json_encode(ChannelEnum::getChannelsByGroup()));
  51. $config = config('site');
  52. $lottery_guide = $config['lottery_guide'] ?? '';
  53. $prize_losing_lottery = $config['prize_losing_lottery'] ?? '';
  54. $this->view->assign("prize_losing_lottery", $prize_losing_lottery);
  55. $this->view->assign("lottery_guide", $lottery_guide);
  56. }
  57. /**
  58. * 查看
  59. */
  60. public function index()
  61. {
  62. //当前是否为关联查询
  63. $this->relationSearch = true;
  64. //设置过滤方法
  65. $this->request->filter(['strip_tags', 'trim']);
  66. if ($this->request->isAjax()) {
  67. //如果发送的来源是Selectpage,则转发到Selectpage
  68. if ($this->request->request('keyField')) {
  69. return $this->selectpage();
  70. }
  71. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  72. $list = $this->model
  73. ->with(['prize'])
  74. ->where($where)
  75. ->order($sort, $order)
  76. ->paginate($limit);
  77. foreach ($list as $row) {
  78. $row->visible(['id','name','description','cover_image','type','status','start_time','end_time','lottery_type','total_draw_count','total_people_count','total_win_count']);
  79. $row->visible(['prize']);
  80. // 处理奖品关联数据(hasMany关系)
  81. if (isset($row->prize) && $row->prize) {
  82. foreach ($row->prize as $prize) {
  83. if (is_object($prize) && method_exists($prize, 'visible')) {
  84. $prize->visible(['name','type','total_stock','remain_stock']);
  85. }
  86. }
  87. }
  88. }
  89. $result = array("total" => $list->total(), "rows" => $list->items());
  90. return json($result);
  91. }
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 添加
  96. */
  97. public function add()
  98. {
  99. if ($this->request->isPost()) {
  100. $params = $this->request->post("row/a");
  101. if ($params) {
  102. $params = $this->preExcludeFields($params);
  103. // 获取奖品和条件数据
  104. $prizesJson = $this->request->post("prizes_json", "");
  105. $prizesData = [];
  106. if (!empty($prizesJson)) {
  107. $prizesData = json_decode($prizesJson, true);
  108. if (json_last_error() !== JSON_ERROR_NONE) {
  109. $this->error('奖品数据格式错误');
  110. }
  111. }
  112. $conditionData = $this->request->post("condition/a", []);
  113. $taskTypes = isset($params['task_type']) ? $params['task_type'] : [];
  114. // 处理活动基础数据
  115. $activityData = $this->processActivityData($params);
  116. // 验证奖品数据
  117. $this->validatePrizesData($prizesData);
  118. // 验证条件数据
  119. $this->validateConditionData($conditionData, $taskTypes);
  120. $result = false;
  121. Db::startTrans();
  122. try {
  123. // 保存活动主表数据
  124. //是否采用模型验证
  125. if ($this->modelValidate) {
  126. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  127. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  128. $this->model->validateFailException(true)->validate($validate);
  129. }
  130. $result = $this->model->allowField(true)->save($activityData);
  131. if ($result) {
  132. $activityId = $this->model->id;
  133. // 保存奖品数据
  134. $this->savePrizesData($activityId, $prizesData);
  135. // 保存参与条件数据
  136. $this->saveConditionData($activityId, $conditionData, $taskTypes);
  137. }
  138. Db::commit();
  139. } catch (ValidateException $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. } catch (PDOException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if ($result !== false) {
  150. $this->success();
  151. } else {
  152. $this->error(__('No rows were inserted'));
  153. }
  154. }
  155. $this->error(__('Parameter %s can not be empty', ''));
  156. }
  157. return $this->view->fetch();
  158. }
  159. /**
  160. * 处理活动基础数据
  161. */
  162. private function processActivityData($params)
  163. {
  164. // 处理时间字段
  165. if (isset($params['start_time']) && $params['start_time']) {
  166. $params['start_time'] = strtotime($params['start_time']);
  167. }
  168. if (isset($params['end_time']) && $params['end_time']) {
  169. $params['end_time'] = strtotime($params['end_time']);
  170. }
  171. if (isset($params['lottery_time']) && $params['lottery_time']) {
  172. $params['lottery_time'] = strtotime($params['lottery_time']);
  173. }
  174. // 处理JSON字段
  175. if (isset($params['user_limit_value']) && $params['user_limit_value']) {
  176. $params['user_limit_value'] = json_encode($params['user_limit_value']);
  177. }
  178. // 处理渠道字段(如果存在的话)
  179. if (isset($params['channels']) && is_array($params['channels'])) {
  180. $params['channels'] = json_encode($params['channels']);
  181. }
  182. // 处理兑奖期限设置
  183. if (isset($params['redeem_expire_type'])) {
  184. $params['redeem_expire_type'] = intval($params['redeem_expire_type']);
  185. if ($params['redeem_expire_type'] == ActivityEnum::REDEEM_EXPIRE_FIXED) {
  186. $params['redeem_expire_days'] = isset($params['redeem_expire_days']) ? intval($params['redeem_expire_days']) : 7;
  187. } else {
  188. $params['redeem_expire_days'] = null;
  189. }
  190. }
  191. // 字段名映射 - 将前端字段名映射到实际数据表字段名
  192. if (isset($params['title'])) {
  193. $params['name'] = $params['title']; // 活动标题映射到name字段
  194. unset($params['title']);
  195. }
  196. if (isset($params['lottery_people'])) {
  197. $params['lottery_people_num'] = $params['lottery_people']; // 开奖人数字段名映射
  198. unset($params['lottery_people']);
  199. }
  200. if (isset($params['per_person_limit'])) {
  201. $params['person_limit_num'] = $params['per_person_limit']; // 单人限制次数字段名映射
  202. unset($params['per_person_limit']);
  203. }
  204. // 移除不属于活动表的字段
  205. unset($params['task_type']);
  206. // 设置默认值
  207. $params['type'] = ActivityEnum::ACTIVITY_TYPE_CONSUMPTION_LOTTERY; // 消费抽奖
  208. // 初始化统计字段
  209. if (!isset($params['total_draw_count'])) {
  210. $params['total_draw_count'] = 0;
  211. }
  212. if (!isset($params['total_people_count'])) {
  213. $params['total_people_count'] = 0;
  214. }
  215. if (!isset($params['total_win_count'])) {
  216. $params['total_win_count'] = 0;
  217. }
  218. return $params;
  219. }
  220. /**
  221. * 验证奖品数据
  222. */
  223. private function validatePrizesData($prizesData)
  224. {
  225. if (empty($prizesData) || count($prizesData) < 8) {
  226. throw new Exception('奖品数量不足,必须配置8个奖品(包含未中奖项)');
  227. }
  228. $totalRate = 0;
  229. foreach ($prizesData as $prize) {
  230. if (!isset($prize['name']) || !isset($prize['type']) || !isset($prize['rate'])) {
  231. throw new Exception('奖品配置不完整');
  232. }
  233. $totalRate += floatval($prize['rate']);
  234. // 验证特定类型奖品的必需字段
  235. switch (intval($prize['type'])) {
  236. case 5: // 优惠券
  237. if (empty($prize['coupon_id'])) {
  238. throw new Exception('优惠券奖品必须选择优惠券');
  239. }
  240. break;
  241. case 8: // 商城奖品
  242. if (empty($prize['goods_id'])) {
  243. throw new Exception('商城奖品必须选择商品');
  244. }
  245. break;
  246. }
  247. }
  248. if (abs($totalRate - 100) > 0.01) {
  249. throw new Exception('奖品总概率必须等于100%,当前为' . $totalRate . '%');
  250. }
  251. }
  252. /**
  253. * 验证条件数据
  254. */
  255. private function validateConditionData($conditionData, $taskTypes)
  256. {
  257. if (empty($taskTypes)) {
  258. throw new Exception('请至少选择一种任务类型');
  259. }
  260. foreach ($taskTypes as $taskType) {
  261. $type = intval($taskType);
  262. switch ($type) {
  263. case 1: // 购买指定商品
  264. if (!isset($conditionData[$type]) || empty($conditionData[$type]['goods_ids'])) {
  265. throw new Exception('购买指定商品任务必须選擇商品');
  266. }
  267. break;
  268. case 2: // 单笔订单消费满额
  269. case 3: // 单次充值满额
  270. case 4: // 活动期间累计消费满额
  271. if (!isset($conditionData[$type]) || empty($conditionData[$type]['condition_value'])) {
  272. throw new Exception('消费/充值任务必须设置金额');
  273. }
  274. if (floatval($conditionData[$type]['condition_value']) <= 0) {
  275. throw new Exception('消费/充值任务金额必须大于0');
  276. }
  277. break;
  278. }
  279. }
  280. }
  281. /**
  282. * 保存奖品数据
  283. */
  284. private function savePrizesData($activityId, $prizesData)
  285. {
  286. $prizeModel = new \app\admin\model\lottery\LotteryPrize;
  287. foreach ($prizesData as $index => $prize) {
  288. $prizeData = [
  289. 'activity_id' => $activityId,
  290. 'name' => $prize['name'],
  291. 'type' => intval($prize['type']),
  292. 'image' => isset($prize['image']) ? $prize['image'] : '',
  293. 'probability' => floatval($prize['rate']),
  294. 'total_stock' => isset($prize['quantity']) ? intval($prize['quantity']) : 0,
  295. 'remain_stock' => isset($prize['quantity']) ? intval($prize['quantity']) : 0,
  296. 'sort_order' => $index,
  297. 'status' => 1,
  298. 'createtime' => time()
  299. ];
  300. // 根据奖品类型设置特定字段
  301. switch (intval($prize['type'])) {
  302. case ActivityEnum::PRIZE_TYPE_POINTS: // 积分 - 使用amount字段存储积分数量
  303. $prizeData['amount'] = isset($prize['points']) ? intval($prize['points']) : 0;
  304. $prizeData['description'] = '积分奖品';
  305. break;
  306. case ActivityEnum::PRIZE_TYPE_BALANCE: // 余额 - 使用amount字段存储余额金额
  307. $prizeData['amount'] = isset($prize['balance']) ? floatval($prize['balance']) : 0;
  308. $prizeData['description'] = '余额奖品';
  309. break;
  310. case ActivityEnum::PRIZE_TYPE_COUPON: // 优惠券
  311. $prizeData['coupon_id'] = isset($prize['coupon_id']) ? intval($prize['coupon_id']) : 0;
  312. $prizeData['description'] = '优惠券奖品';
  313. break;
  314. case ActivityEnum::PRIZE_TYPE_REDPACK: // 红包 - 使用amount字段存储红包金额
  315. $prizeData['amount'] = isset($prize['redpack_amount']) ? floatval($prize['redpack_amount']) : 0;
  316. $prizeData['description'] = '红包奖品';
  317. break;
  318. case ActivityEnum::PRIZE_TYPE_CODE: // 兑换码
  319. if (isset($prize['manual_codes']) && !empty($prize['manual_codes'])) {
  320. $codes = array_filter(array_map('trim', explode("\n", $prize['manual_codes'])));
  321. $prizeData['exchange_codes'] = json_encode($codes);
  322. $prizeData['used_codes'] = json_encode([]); // 初始化已使用列表
  323. $prizeData['total_stock'] = count($codes);
  324. $prizeData['remain_stock'] = count($codes);
  325. }
  326. $prizeData['description'] = '兑换码奖品';
  327. break;
  328. case ActivityEnum::PRIZE_TYPE_SHOP_GOODS: // 商城奖品
  329. $prizeData['goods_id'] = isset($prize['goods_id']) ? intval($prize['goods_id']) : 0;
  330. $prizeData['goods_sku_id'] = isset($prize['goods_sku_id']) ? intval($prize['goods_sku_id']) : null;
  331. $prizeData['description'] = '商城奖品';
  332. break;
  333. case ActivityEnum::PRIZE_TYPE_GOODS: // 实物奖品
  334. $prizeData['description'] = isset($prize['description']) ? $prize['description'] : '实物奖品';
  335. break;
  336. case ActivityEnum::PRIZE_TYPE_NO_PRIZE: // 未中奖
  337. default:
  338. $prizeData['description'] = '未中奖';
  339. break;
  340. }
  341. $prizeModel->create($prizeData);
  342. }
  343. }
  344. /**
  345. * 保存参与条件数据
  346. */
  347. private function saveConditionData($activityId, $conditionData, $taskTypes)
  348. {
  349. $conditionModel = new \app\admin\model\lottery\Condition;
  350. foreach ($taskTypes as $taskType) {
  351. $type = intval($taskType);
  352. $condition = isset($conditionData[$type]) ? $conditionData[$type] : [];
  353. $conditionRecord = [
  354. 'activity_id' => $activityId,
  355. 'type' => $type,
  356. 'reward_times' => 1,
  357. 'is_repeatable' => 0,
  358. 'status' => $conditionModel::STATUS_ENABLED,
  359. 'createtime' => time()
  360. ];
  361. switch ($type) {
  362. case ActivityEnum::CONDITION_TYPE_BUY_GOODS: // 购买指定商品
  363. $goodsIds = isset($condition['goods_ids']) ? $condition['goods_ids'] : '[]';
  364. if (is_string($goodsIds)) {
  365. $goodsIds = json_decode($goodsIds, true) ?: [];
  366. }
  367. $conditionRecord['goods_ids'] = json_encode($goodsIds);
  368. $conditionRecord['goods_rule'] = isset($condition['goods_rule']) ? intval($condition['goods_rule']) : $conditionModel::GOODS_RULE_INCLUDE;
  369. break;
  370. case ActivityEnum::CONDITION_TYPE_ORDER_AMOUNT: // 单笔订单消费满额
  371. case ActivityEnum::CONDITION_TYPE_RECHARGE_AMOUNT: // 单次充值满额
  372. case ActivityEnum::CONDITION_TYPE_TOTAL_AMOUNT: // 活动期间累计消费满额
  373. $conditionRecord['condition_value'] = isset($condition['condition_value']) ? floatval($condition['condition_value']) : 0;
  374. if ($type == ActivityEnum::CONDITION_TYPE_TOTAL_AMOUNT) {
  375. $conditionRecord['is_repeatable'] = 0; // 累计消费不可重复
  376. } else {
  377. $conditionRecord['is_repeatable'] = 1; // 其他类型可重复
  378. }
  379. break;
  380. }
  381. $conditionModel->create($conditionRecord);
  382. }
  383. }
  384. /**
  385. * 编辑
  386. */
  387. public function edit($ids = null)
  388. {
  389. $row = $this->model->get($ids);
  390. if (!$row) {
  391. $this->error(__('No Results were found'));
  392. }
  393. $adminIds = $this->getDataLimitAdminIds();
  394. if (is_array($adminIds)) {
  395. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  396. $this->error(__('You have no permission'));
  397. }
  398. }
  399. if ($this->request->isPost()) {
  400. $params = $this->request->post("row/a");
  401. if ($params) {
  402. $params = $this->preExcludeFields($params);
  403. // 获取奖品和条件数据
  404. $prizesJson = $this->request->post("prizes_json", "");
  405. $prizesData = [];
  406. if (!empty($prizesJson)) {
  407. $prizesData = json_decode($prizesJson, true);
  408. if (json_last_error() !== JSON_ERROR_NONE) {
  409. $this->error('奖品数据格式错误');
  410. }
  411. }
  412. $conditionData = $this->request->post("condition/a", []);
  413. $taskTypes = isset($params['task_type']) ? $params['task_type'] : [];
  414. // 处理活动基础数据
  415. $activityData = $this->processActivityData($params);
  416. // 验证奖品数据
  417. $this->validatePrizesData($prizesData);
  418. // 验证条件数据
  419. $this->validateConditionData($conditionData, $taskTypes);
  420. $result = false;
  421. Db::startTrans();
  422. try {
  423. // 更新活动主表数据
  424. //是否采用模型验证
  425. if ($this->modelValidate) {
  426. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  427. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  428. $row->validateFailException(true)->validate($validate);
  429. }
  430. $result = $row->allowField(true)->save($activityData);
  431. if ($result !== false) {
  432. $activityId = $ids;
  433. // 删除原有奖品数据
  434. Db::name('shop_lottery_prize')->where('activity_id', $activityId)->delete();
  435. // 删除原有条件数据
  436. Db::name('shop_lottery_condition')->where('activity_id', $activityId)->delete();
  437. // 保存新的奖品数据
  438. $this->savePrizesData($activityId, $prizesData);
  439. // 保存新的参与条件数据
  440. $this->saveConditionData($activityId, $conditionData, $taskTypes);
  441. }
  442. Db::commit();
  443. } catch (ValidateException $e) {
  444. Db::rollback();
  445. $this->error($e->getMessage());
  446. } catch (PDOException $e) {
  447. Db::rollback();
  448. $this->error($e->getMessage());
  449. } catch (Exception $e) {
  450. Db::rollback();
  451. $this->error($e->getMessage());
  452. }
  453. if ($result !== false) {
  454. $this->success();
  455. } else {
  456. $this->error(__('No rows were updated'));
  457. }
  458. }
  459. $this->error(__('Parameter %s can not be empty', ''));
  460. }
  461. // 处理时间显示
  462. if ($row->start_time) {
  463. $row->start_time = date('Y-m-d H:i:s', $row->start_time);
  464. }
  465. if ($row->end_time) {
  466. $row->end_time = date('Y-m-d H:i:s', $row->end_time);
  467. }
  468. if ($row->lottery_time) {
  469. $row->lottery_time = date('Y-m-d H:i:s', $row->lottery_time);
  470. }
  471. // 处理JSON字段
  472. if ($row->user_limit_value) {
  473. $row->user_limit_value = json_decode($row->user_limit_value, true);
  474. }
  475. // 处理渠道字段
  476. if ($row->channels) {
  477. $row->channels = json_decode($row->channels, true);
  478. }
  479. // 字段名映射 - 将数据表字段名映射回前端字段名
  480. if (isset($row->name)) {
  481. $row->title = $row->name; // 将name映射回title供前端使用
  482. }
  483. if (isset($row->lottery_people_num)) {
  484. $row->lottery_people = $row->lottery_people_num; // 映射回前端字段名
  485. }
  486. if (isset($row->person_limit_num)) {
  487. $row->per_person_limit = $row->person_limit_num; // 映射回前端字段名
  488. }
  489. // 获取奖品数据
  490. $prizes = Db::name('shop_lottery_prize')
  491. ->where('activity_id', $ids)
  492. ->order('sort_order', 'asc')
  493. ->select();
  494. // 处理奖品数据的字段映射
  495. foreach ($prizes as &$prize) {
  496. // 将数据表字段映射回前端字段
  497. $prize['quantity'] = $prize['total_stock']; // 前端使用quantity字段
  498. $prize['rate'] = $prize['probability']; // 前端使用rate字段
  499. // 根据奖品类型设置特定字段
  500. switch (intval($prize['type'])) {
  501. case ActivityEnum::PRIZE_TYPE_POINTS:
  502. $prize['points'] = $prize['amount'] ?? 0;
  503. break;
  504. case ActivityEnum::PRIZE_TYPE_BALANCE:
  505. $prize['balance'] = $prize['amount'] ?? 0;
  506. break;
  507. case ActivityEnum::PRIZE_TYPE_REDPACK:
  508. $prize['redpack_amount'] = $prize['amount'] ?? 0;
  509. break;
  510. case ActivityEnum::PRIZE_TYPE_CODE:
  511. if (!empty($prize['exchange_codes'])) {
  512. $codes = json_decode($prize['exchange_codes'], true) ?: [];
  513. $prize['manual_codes'] = implode("\n", $codes);
  514. }
  515. break;
  516. }
  517. }
  518. // 获取条件数据
  519. $conditions = Db::name('shop_lottery_condition')
  520. ->where('activity_id', $ids)
  521. ->select();
  522. // 处理条件数据的字段映射
  523. $taskTypes = [];
  524. $conditionsData = [];
  525. foreach ($conditions as $condition) {
  526. $taskTypes[] = $condition['type'];
  527. $conditionsData[$condition['type']] = $condition;
  528. // 处理商品IDs JSON
  529. if (!empty($condition['goods_ids'])) {
  530. $conditionsData[$condition['type']]['goods_ids_array'] = json_decode($condition['goods_ids'], true) ?: [];
  531. }
  532. }
  533. $this->view->assign("row", $row);
  534. $this->view->assign("prizes", $prizes);
  535. $this->view->assign("prizesJson", json_encode($prizes)); // 为前端JavaScript提供JSON格式的奖品数据
  536. $this->view->assign("conditions", $conditions);
  537. $this->view->assign("conditionsData", $conditionsData);
  538. $this->view->assign("taskTypes", $taskTypes);
  539. return $this->view->fetch();
  540. }
  541. /**
  542. * 奖品管理
  543. */
  544. public function prize($ids = null)
  545. {
  546. $activity = $this->model->get($ids);
  547. if (!$activity) {
  548. $this->error(__('No Results were found'));
  549. }
  550. $this->view->assign("activity", $activity);
  551. return $this->view->fetch();
  552. }
  553. /**
  554. * 参与条件管理
  555. */
  556. public function condition($ids = null)
  557. {
  558. $activity = $this->model->get($ids);
  559. if (!$activity) {
  560. $this->error(__('No Results were found'));
  561. }
  562. $this->view->assign("activity", $activity);
  563. return $this->view->fetch();
  564. }
  565. /**
  566. * 抽奖记录
  567. */
  568. public function records($ids = null)
  569. {
  570. $activity = $this->model->get($ids);
  571. if (!$activity) {
  572. $this->error(__('No Results were found'));
  573. }
  574. $this->view->assign("activity", $activity);
  575. return $this->view->fetch();
  576. }
  577. /**
  578. * 统计数据
  579. */
  580. public function statistics($ids = null)
  581. {
  582. $activity = $this->model->get($ids);
  583. if (!$activity) {
  584. $this->error(__('No Results were found'));
  585. }
  586. // 获取统计数据
  587. $statisticsModel = new \app\admin\model\lottery\Statistics;
  588. $statistics = $statisticsModel->where('activity_id', $ids)->order('stat_date', 'desc')->paginate(15);
  589. $this->view->assign("activity", $activity);
  590. $this->view->assign("statistics", $statistics);
  591. return $this->view->fetch();
  592. }
  593. }