Games.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\games;
  11. use addon\coupon\model\Coupon;
  12. use app\model\BaseModel;
  13. use app\model\member\MemberAccount;
  14. use app\model\system\Cron;
  15. use app\model\system\Config as ConfigModel;
  16. use app\model\upload\Upload;
  17. /**
  18. * 游戏
  19. */
  20. class Games extends BaseModel
  21. {
  22. public $status = [
  23. 0 => '未开始',
  24. 1 => '进行中',
  25. 2 => '已结束',
  26. 3 => '已关闭',
  27. ];
  28. /**
  29. * 添加游戏活动
  30. * @param $game_data
  31. * @param $award_json
  32. * @return array
  33. */
  34. public function addGames($game_data, $award_json)
  35. {
  36. $time = time();
  37. $game_data[ 'create_time' ] = $time;
  38. if ($time > $game_data[ 'start_time' ] && $time < $game_data[ 'end_time' ]) {
  39. $game_data[ 'status' ] = 1;
  40. } else {
  41. $game_data[ 'status' ] = 0;
  42. }
  43. model('promotion_games')->startTrans();
  44. try {
  45. $game_id = model('promotion_games')->add($game_data);
  46. //添加奖品
  47. $award_json_data = json_decode($award_json, true);
  48. $award_data = [];
  49. $award_winning_rate = 0;
  50. foreach ($award_json_data as $k => $v) {
  51. $award_img = '';
  52. if($v[ 'award_type' ] == 3){
  53. $award_img = isset($v[ 'award_img' ]) ? $v[ 'award_img' ] : img("/public/static/img/game_coupon.png");
  54. }elseif ($v[ 'award_type' ] == 2){
  55. $award_img = isset($v[ 'award_img' ]) ? $v[ 'award_img' ] : img("/public/static/img/game_hongbao.png");
  56. }elseif ($v[ 'award_type' ] == 1){
  57. $award_img = isset($v[ 'award_img' ]) ? $v[ 'award_img' ] : img("/public/static/img/game_jifen.png");
  58. }
  59. $item = [
  60. 'site_id' => $game_data[ 'site_id' ],
  61. 'game_id' => $game_id,
  62. 'award_name' => $v[ 'award_name' ],
  63. 'award_img' => $award_img,
  64. 'award_type' => $v[ 'award_type' ],
  65. 'relate_id' => $v[ 'relate_id' ],
  66. 'relate_name' => isset($v[ 'relate_name' ]) ? $v[ 'relate_name' ] : '',
  67. 'point' => $v[ 'point' ],
  68. 'balance' => $v[ 'balance' ],
  69. 'award_num' => $v[ 'award_num' ],
  70. 'award_winning_rate' => $v[ 'award_winning_rate' ],
  71. 'remaining_num' => $v[ 'award_num' ],
  72. 'no_winning_img' => isset($v[ 'no_winning_img' ]) ? $v[ 'no_winning_img' ] : '',
  73. ];
  74. $award_data[ $k ] = $item;
  75. }
  76. model('promotion_games_award')->addList($award_data);
  77. $cron = new Cron();
  78. if ($game_data[ 'status' ] == 1) {//进行中
  79. $cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
  80. } else {//未进行
  81. $cron->addCron(1, 0, "游戏活动开启", "OpenGame", $game_data[ 'start_time' ], $game_id);
  82. $cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
  83. }
  84. model('promotion_games')->commit();
  85. return $this->success();
  86. } catch (\Exception $e) {
  87. model('promotion_games')->rollback();
  88. return $this->error('', $e->getMessage());
  89. }
  90. }
  91. /**
  92. * 编辑游戏活动
  93. * @param $condition
  94. * @param $game_data
  95. * @param $award_json
  96. * @return array
  97. */
  98. public function editGames($condition, $game_data, $award_json, $delete_award_ids)
  99. {
  100. $game_info = model('promotion_games')->getInfo($condition, 'game_id,status');
  101. if (in_array($game_info[ 'status' ], [ 2, 3 ])) {
  102. return $this->error('', '已关闭或已结束的活动不能编辑');
  103. }
  104. $game_id = $game_info[ 'game_id' ];
  105. $time = time();
  106. $game_data[ 'update_time' ] = $time;
  107. if ($time > $game_data[ 'start_time' ] && $time < $game_data[ 'end_time' ]) {
  108. $game_data[ 'status' ] = 1;
  109. } else {
  110. $game_data[ 'status' ] = 0;
  111. }
  112. model('promotion_games')->startTrans();
  113. try {
  114. $game_info = model('promotion_games')->getInfo($condition);
  115. if($game_info['no_winning_img'] && $game_data['no_winning_img'] && $game_info['no_winning_img'] != $game_data['no_winning_img']){
  116. $upload_model = new Upload();
  117. $upload_model->deletePic($game_info['no_winning_img'], $game_data[ 'site_id' ]);
  118. }
  119. model('promotion_games')->update($game_data, $condition);
  120. if (!empty($delete_award_ids)) {
  121. model('promotion_games_award')->delete([ [ 'award_id', 'in', $delete_award_ids ] ]);
  122. }
  123. //添加奖品
  124. $award_json_data = json_decode($award_json, true);
  125. foreach ($award_json_data as $k => $v) {
  126. $item = [
  127. 'site_id' => $game_data[ 'site_id' ],
  128. 'game_id' => $game_id,
  129. 'award_name' => $v[ 'award_name' ],
  130. 'award_img' => isset($v[ 'award_img' ]) ? $v[ 'award_img' ] : '',
  131. 'award_type' => $v[ 'award_type' ],
  132. 'relate_id' => $v[ 'relate_id' ],
  133. 'relate_name' => isset($v[ 'relate_name' ]) ? $v[ 'relate_name' ] : '',
  134. 'point' => $v[ 'point' ],
  135. 'balance' => $v[ 'balance' ],
  136. 'award_num' => $v[ 'award_num' ],
  137. 'award_winning_rate' => $v[ 'award_winning_rate' ],
  138. 'remaining_num' => $v[ 'award_num' ],
  139. 'no_winning_img' => isset($v[ 'no_winning_img' ]) ? $v[ 'no_winning_img' ] : '',
  140. ];
  141. if (isset($v[ 'award_id' ]) && $v[ 'award_id' ] > 0) {
  142. model('promotion_games_award')->update($item, [ [ 'award_id', '=', $v[ 'award_id' ] ] ]);
  143. } else {
  144. model('promotion_games_award')->add($item);
  145. }
  146. }
  147. $cron = new Cron();
  148. $cron->deleteCron([ [ 'event', '=', 'OpenGame' ], [ 'relate_id', '=', $game_id ] ]);
  149. $cron->deleteCron([ [ 'event', '=', 'CloseGame' ], [ 'relate_id', '=', $game_id ] ]);
  150. if ($game_data[ 'status' ] == 1) {//进行中
  151. $cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
  152. } else {//未进行
  153. $cron->addCron(1, 0, "游戏活动开启", "OpenGame", $game_data[ 'start_time' ], $game_id);
  154. $cron->addCron(1, 0, "游戏活动关闭", "CloseGame", $game_data[ 'end_time' ], $game_id);
  155. }
  156. model('promotion_games')->commit();
  157. return $this->success();
  158. } catch (\Exception $e) {
  159. model('promotion_games')->rollback();
  160. return $this->error('', $e->getMessage());
  161. }
  162. }
  163. /**
  164. * 获取游戏奖项
  165. * @param array $condition
  166. * @param string $field
  167. */
  168. public function getGameAward($condition = [], $field = '*')
  169. {
  170. $data = model('promotion_games_award')->getList($condition, $field);
  171. return $this->success($data);
  172. }
  173. /**
  174. * 获取游戏信息
  175. * @param array $condition
  176. * @param string $field
  177. */
  178. public function getGamesInfo($condition, $field = '*')
  179. {
  180. $res = model('promotion_games')->getInfo($condition, $field);
  181. return $this->success($res);
  182. }
  183. /**
  184. * 获取游戏详情
  185. * @param int $site_id
  186. */
  187. public function getGamesDetail($site_id, $game_id)
  188. {
  189. $res = model('promotion_games')->getInfo([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ], '*');
  190. if (!empty($res)) {
  191. $game_award = model('promotion_games_award')->getList([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ], '*');
  192. $res[ 'game_award' ] = $game_award;
  193. }
  194. return $this->success($res);
  195. }
  196. /**
  197. * 删除游戏
  198. * @param $site_id
  199. * @param $game_id
  200. * @return array
  201. */
  202. public function deleteGames($site_id, $game_id)
  203. {
  204. model('promotion_games')->startTrans();
  205. try {
  206. model('promotion_games')->delete([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ]);
  207. model('promotion_games_award')->delete([ [ 'site_id', '=', $site_id ], [ 'game_id', '=', $game_id ] ]);
  208. model('promotion_games')->commit();
  209. return $this->success();
  210. } catch (\Exception $e) {
  211. model('promotion_games')->rollback();
  212. return $this->error('', $e->getMessage());
  213. }
  214. }
  215. /**
  216. * 获取游戏列表
  217. * @param array $condition
  218. * @param string $field
  219. * @param string $order
  220. * @param null $limit
  221. * @return array
  222. */
  223. public function getGamesList($condition = [], $field = '*', $order = '', $limit = null)
  224. {
  225. $list = model('promotion_games')->getList($condition, $field, $order, '', '', '', $limit);
  226. return $this->success($list);
  227. }
  228. /**
  229. * 获取游戏分页列表
  230. * @param array $condition
  231. * @param int $page
  232. * @param int $page_size
  233. * @param string $order
  234. * @param string $field
  235. * @return array
  236. */
  237. public function getGamesPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  238. {
  239. $list = model('promotion_games')->pageList($condition, $field, $order, $page, $page_size);
  240. return $this->success($list);
  241. }
  242. /**
  243. * 关闭游戏
  244. * @param $site_id
  245. * @param $game_id
  246. * @return array
  247. */
  248. public function finishGames($site_id, $game_id)
  249. {
  250. //团购信息
  251. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ], 'status');
  252. if (!empty($game_info)) {
  253. if ($game_info[ 'status' ] != 3) {
  254. $res = model('promotion_games')->update([ 'status' => 3 ], [ [ 'game_id', '=', $game_id ] ]);
  255. // if ($res) {
  256. // $cron = new Cron();
  257. // $cron->deleteCron([ [ 'event', '=', 'OpenGame' ], [ 'relate_id', '=', $game_id ] ]);
  258. // $cron->deleteCron([ [ 'event', '=', 'CloseGame' ], [ 'relate_id', '=', $game_id ] ]);
  259. // }
  260. return $this->success($res);
  261. } else {
  262. $this->error('', '该游戏已关闭');
  263. }
  264. } else {
  265. $this->error('', '该游戏不存在');
  266. }
  267. }
  268. /**
  269. * 开启游戏
  270. * @param $game_id
  271. * @return array|\multitype
  272. */
  273. public function cronOpenGames($game_id)
  274. {
  275. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'start_time,status');
  276. if (!empty($game_info)) {
  277. if ($game_info[ 'start_time' ] <= time() && $game_info[ 'status' ] == 0) {
  278. $res = model('promotion_games')->update([ 'status' => 1 ], [ [ 'game_id', '=', $game_id ] ]);
  279. return $this->success($res);
  280. } else {
  281. return $this->error("", "游戏已开启或者关闭");
  282. }
  283. } else {
  284. return $this->error("", "游戏不存在");
  285. }
  286. }
  287. /**
  288. * 开启游戏
  289. * @param $game_id
  290. * @return array|\multitype
  291. */
  292. public function startGames($game_id)
  293. {
  294. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'end_time,status');
  295. if (!empty($game_info)) {
  296. if ($game_info[ 'end_time' ] >= time()) {
  297. $res = model('promotion_games')->update([ 'status' => 1 ], [ [ 'game_id', '=', $game_id ] ]);
  298. return $this->success($res);
  299. } else {
  300. return $this->error("", "游戏已结束");
  301. }
  302. } else {
  303. return $this->error("", "游戏不存在");
  304. }
  305. }
  306. /**
  307. * 关闭游戏
  308. * @param $game_id
  309. * @return array|\multitype
  310. */
  311. public function cronCloseGames($game_id)
  312. {
  313. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ] ], 'start_time,status');
  314. if (!empty($game_info)) {
  315. if ($game_info[ 'status' ] != 2) {
  316. $res = model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $game_id ] ]);
  317. return $this->success($res);
  318. } else {
  319. return $this->error("", "该游戏已结束");
  320. }
  321. } else {
  322. return $this->error("", "游戏不存在");
  323. }
  324. }
  325. /**
  326. * 抽奖
  327. * @param $game_id
  328. * @param $member_id
  329. * @param $site_id
  330. */
  331. public function lottery($game_id, $member_id, $site_id)
  332. {
  333. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ]);
  334. if (empty($game_info)) return $this->error("", "未获取到游戏信息");
  335. if ($game_info[ 'status' ] == 0 && $game_info['game_type'] != 'scenefestival') return $this->error("", "游戏尚未开始");
  336. if ($game_info[ 'status' ] == 2 || $game_info[ 'status' ] == 3) return $this->error("", "游戏已经结束");
  337. $member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'nickname,member_level,point');
  338. if (empty($member_info)) return $this->error("", "未获取到会员信息");
  339. if (!empty($game_info[ 'level_id' ])) {
  340. $level = explode(',', $game_info[ 'level_id' ]);
  341. if (!in_array($member_info[ 'member_level' ], $level)) {
  342. return $this->error("", "只有{$game_info['level_name']}等级的会员可参与该活动");
  343. }
  344. }
  345. if ($game_info[ 'join_type' ]) {
  346. // 每天
  347. $tody_start = strtotime(date("Y-m-d"), time());
  348. $luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ], [ 'create_time', 'between', [ $tody_start, time() ] ] ]);
  349. if ($luck_draw_num > $game_info[ 'join_frequency' ]) return $this->error("", "您今日的抽奖次数已用完");
  350. } else {
  351. // 全程
  352. $luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ] ]);
  353. if ($luck_draw_num > $game_info[ 'join_frequency' ]) return $this->error("", "您的抽奖次数已用完");
  354. }
  355. if ($game_info[ 'points' ] > 0 && $member_info[ 'point' ] < $game_info[ 'points' ]) return $this->error("", "积分不足");
  356. $lottery_result = $this->lotteryCalculate($game_info);
  357. model('promotion_games')->startTrans();
  358. try {
  359. $meber_account = new MemberAccount();
  360. if ($game_info[ 'points' ] > 0) {
  361. // 扣除积分
  362. $meber_account->addMemberAccount($site_id, $member_id, 'point', -$game_info[ 'points' ], $game_info[ 'game_type' ], $game_id, "参与活动扣除");
  363. }
  364. model('promotion_games')->setInc([ [ 'game_id', '=', $game_id ] ], 'join_num'); // 抽奖人数
  365. $record = [
  366. 'site_id' => $site_id,
  367. 'game_id' => $game_id,
  368. 'game_type' => $game_info[ 'game_type' ],
  369. 'member_id' => $member_id,
  370. 'member_nick_name' => $member_info[ 'nickname' ],
  371. 'points' => $game_info[ 'points' ],
  372. 'create_time' => time()
  373. ];
  374. if ($lottery_result[ 'is_winning' ]) {
  375. switch ( $lottery_result[ 'award_type' ] ) {
  376. case 1:
  377. // 积分
  378. $meber_account->addMemberAccount($site_id, $member_id, 'point', $lottery_result[ 'point' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
  379. break;
  380. case 2:
  381. // 余额
  382. $meber_account->addMemberAccount($site_id, $member_id, 'balance', $lottery_result[ 'balance' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
  383. break;
  384. case 3:
  385. // 优惠券
  386. $coupon = new Coupon();
  387. $receive_res = $coupon->giveCoupon([ ['coupon_type_id' => $lottery_result[ 'relate_id' ], 'num' => 1 ] ], $site_id, $member_id, 6);
  388. // 如果优惠券发放失败则本次抽奖为未中奖
  389. if ($receive_res[ 'code' ] < 0) {
  390. $lottery_result = [ 'is_winning' => 0 ];
  391. }
  392. break;
  393. case 4:
  394. // 赠品
  395. break;
  396. }
  397. if ($lottery_result[ 'is_winning' ]) {
  398. $record[ 'is_winning' ] = 1;
  399. $record[ 'award_id' ] = $lottery_result[ 'award_id' ];
  400. $record[ 'award_name' ] = $lottery_result[ 'award_name' ];
  401. $record[ 'award_type' ] = $lottery_result[ 'award_type' ];
  402. $record[ 'relate_id' ] = $lottery_result[ 'relate_id' ];
  403. $record[ 'relate_name' ] = $lottery_result[ 'relate_name' ];
  404. $record[ 'point' ] = $lottery_result[ 'point' ];
  405. $record[ 'balance' ] = $lottery_result[ 'balance' ];
  406. model('promotion_games')->setInc([ [ 'game_id', '=', $game_id ] ], 'winning_num'); // 中奖人数
  407. model('promotion_games_award')->setDec([ [ 'award_id', '=', $lottery_result[ 'award_id' ] ] ], 'remaining_num'); // 剩余数量
  408. model('promotion_games_award')->setInc([ [ 'award_id', '=', $lottery_result[ 'award_id' ] ] ], 'receive_num'); // 已领取数量
  409. }
  410. }
  411. model('promotion_games_draw_record')->add($record);
  412. model('promotion_games')->commit();
  413. return $this->success($lottery_result);
  414. } catch (\Exception $e) {
  415. model('promotion_games')->rollback();
  416. return $this->error("", $e->getMessage());
  417. }
  418. }
  419. /**
  420. * 节日有礼领取
  421. * @param $game_id
  422. * @param $member_id
  423. * @param $site_id
  424. */
  425. public function receive($game_id, $member_id, $site_id)
  426. {
  427. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ] ]);
  428. if (empty($game_info)) return $this->error("", "未获取到游戏信息");
  429. if ($game_info[ 'status' ] == 2 || $game_info[ 'status' ] == 3) return $this->error("", "游戏已经结束");
  430. $member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'nickname,member_level,point');
  431. if (empty($member_info)) return $this->error("", "未获取到会员信息");
  432. if (!empty($game_info[ 'level_id' ])) {
  433. $level = explode(',', $game_info[ 'level_id' ]);
  434. if (!in_array($member_info[ 'member_level' ], $level)) {
  435. return $this->error("", "只有{$game_info['level_name']}等级的会员可参与该活动");
  436. }
  437. }
  438. model('promotion_games')->startTrans();
  439. try {
  440. $meber_account = new MemberAccount();
  441. $award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $game_id ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance');
  442. $record = [
  443. 'site_id' => $site_id,
  444. 'game_id' => $game_id,
  445. 'game_type' => $game_info[ 'game_type' ],
  446. 'member_id' => $member_id,
  447. 'member_nick_name' => $member_info[ 'nickname' ],
  448. 'points' => $game_info[ 'points' ],
  449. 'create_time' => time()
  450. ];
  451. if(!empty($award_list)){
  452. foreach($award_list as $v){
  453. switch ( $v[ 'award_type' ] ) {
  454. case 1:
  455. // 积分
  456. $meber_account->addMemberAccount($site_id, $member_id, 'point', $v[ 'point' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
  457. break;
  458. case 2:
  459. // 余额
  460. $meber_account->addMemberAccount($site_id, $member_id, 'balance', $v[ 'balance' ], $game_info[ 'game_type' ], $game_id, "活动奖励发放");
  461. break;
  462. case 3:
  463. // 优惠券
  464. $coupon = new Coupon();
  465. $receive_res = $coupon->receiveCoupon($v[ 'relate_id' ], $site_id, $member_id, '3', 0, 0);
  466. break;
  467. case 4:
  468. // 赠品
  469. break;
  470. }
  471. $record[ 'is_winning' ] = 1;
  472. $record[ 'award_id' ] = $v[ 'award_id' ];
  473. $record[ 'award_name' ] = $v[ 'award_name' ];
  474. $record[ 'award_type' ] = $v[ 'award_type' ];
  475. $record[ 'relate_id' ] = $v[ 'relate_id' ];
  476. $record[ 'relate_name' ] = $v[ 'relate_name' ];
  477. $record[ 'point' ] = $v[ 'point' ];
  478. $record[ 'balance' ] = $v[ 'balance' ];
  479. model('promotion_games_award')->setDec([ [ 'award_id', '=', $v[ 'award_id' ] ] ], 'remaining_num'); // 剩余数量
  480. model('promotion_games_award')->setInc([ [ 'award_id', '=', $v[ 'award_id' ] ] ], 'receive_num'); // 已领取数量
  481. model('promotion_games_draw_record')->add($record);
  482. }
  483. }else{
  484. model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $game_id ] ]);
  485. }
  486. model('promotion_games')->commit();
  487. return $this->success(['status'=>1]);
  488. } catch (\Exception $e) {
  489. model('promotion_games')->rollback();
  490. return $this->error("", $e->getMessage());
  491. }
  492. }
  493. /**
  494. * 中奖计算
  495. */
  496. private function lotteryCalculate($data)
  497. {
  498. $rand = mt_rand(1, 10000);
  499. $result = [ 'is_winning' => 0 ];
  500. if ($data[ 'winning_rate' ] > 0 && $rand <= ( $data[ 'winning_rate' ] * 100 )) {
  501. $award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $data[ 'game_id' ] ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance,award_winning_rate', 'award_winning_rate asc');
  502. if (!empty($award_list)) {
  503. $rate_arr = [];
  504. foreach ($award_list as $item) {
  505. $rate_arr[] = $item[ 'award_winning_rate' ];
  506. }
  507. $key = $this->get_rand($rate_arr);
  508. if (isset($award_list[ $key ])) {
  509. $result = $award_list[ $key ];
  510. $result[ 'is_winning' ] = 1;
  511. unset($result[ 'award_winning_rate' ]);
  512. }
  513. } else {
  514. model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $data[ 'game_id' ] ] ]);
  515. }
  516. }
  517. return $result;
  518. }
  519. /**
  520. * 奖品计算
  521. */
  522. private function awardCalculate($data)
  523. {
  524. $award_list = model('promotion_games_award')->getList([ [ 'game_id', '=', $data[ 'game_id' ] ], [ 'remaining_num', '>', 0 ] ], 'award_id,award_name,award_type,relate_id,relate_name,point,balance,award_winning_rate', 'award_winning_rate asc');
  525. if (!empty($award_list)) {
  526. $rate_arr = [];
  527. foreach ($award_list as $item) {
  528. $rate_arr[] = $item[ 'award_winning_rate' ];
  529. }
  530. $key = $this->get_rand($rate_arr);
  531. if (isset($award_list[ $key ])) {
  532. $result = $award_list[ $key ];
  533. $result[ 'is_winning' ] = 1;
  534. unset($result[ 'award_winning_rate' ]);
  535. }
  536. } else {
  537. model('promotion_games')->update([ 'status' => 2 ], [ [ 'game_id', '=', $data[ 'game_id' ] ] ]);
  538. }
  539. return $result;
  540. }
  541. /**
  542. * 获取随机奖励
  543. * @param $proArr
  544. * @return int|string
  545. */
  546. private function get_rand($proArr)
  547. {
  548. $result = '';
  549. //总权重
  550. $proSum = array_sum($proArr);
  551. //概率数组循环
  552. foreach ($proArr as $key => $proCur) {
  553. $randNum = mt_rand(1, $proSum);
  554. if ($randNum <= $proCur) {
  555. $result = $key;
  556. break;
  557. } else {
  558. $proSum -= $proCur;
  559. }
  560. }
  561. unset ($proArr);
  562. return $result;
  563. }
  564. /**
  565. * 获取会员剩余可抽奖次数
  566. * @param $game_id
  567. * @param $member_id
  568. * @param $site_id
  569. */
  570. public function getMemberSurplusNum($game_id, $member_id, $site_id)
  571. {
  572. $num = 0;
  573. $game_info = model('promotion_games')->getInfo([ [ 'game_id', '=', $game_id ], [ 'site_id', '=', $site_id ], [ 'status', '=', 1 ] ], 'join_type,join_frequency');
  574. if (!empty($game_info)) {
  575. if ($game_info[ 'join_type' ]) {
  576. // 每天
  577. $tody_start = strtotime(date("Y-m-d"), time());
  578. $luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ], [ 'create_time', 'between', [ $tody_start, time() ] ] ]);
  579. if ($luck_draw_num < $game_info[ 'join_frequency' ]) {
  580. $num = $game_info[ 'join_frequency' ] - $luck_draw_num;
  581. }
  582. } else {
  583. // 全程
  584. $luck_draw_num = model('promotion_games_draw_record')->getCount([ [ 'game_id', '=', $game_id ], [ 'member_id', '=', $member_id ] ]);
  585. if ($luck_draw_num < $game_info[ 'join_frequency' ]) {
  586. $num = $game_info[ 'join_frequency' ] - $luck_draw_num;
  587. }
  588. }
  589. }
  590. return $this->success($num);
  591. }
  592. /**
  593. * 游戏推广二维码
  594. * @param $game_id
  595. * @param $game_name
  596. * @param $url
  597. * @param $site_id
  598. * @param string $type
  599. * @return array
  600. */
  601. public function qrcode($game_id, $game_name, $url, $site_id, $type = "create")
  602. {
  603. $data = [
  604. 'site_id' => $site_id,
  605. 'app_type' => "all", // all为全部
  606. 'type' => $type, // 类型 create创建 get获取
  607. 'data' => [
  608. "id" => $game_id
  609. ],
  610. 'page' => $url,
  611. 'qrcode_path' => 'upload/qrcode/games',
  612. 'qrcode_name' => "games_qrcode_" . $game_id
  613. ];
  614. event('Qrcode', $data, true);
  615. $app_type_list = config('app_type');
  616. $path = [];
  617. foreach ($app_type_list as $k => $v) {
  618. switch ( $k ) {
  619. case 'h5':
  620. $wap_domain = getH5Domain();
  621. $path[ $k ][ 'status' ] = 1;
  622. $path[ $k ][ 'url' ] = $wap_domain . $data[ 'page' ] . '?id=' . $game_id;
  623. $path[ $k ][ 'img' ] = "upload/qrcode/games/games_qrcode_" . $game_id . "_" . $k . ".png";
  624. break;
  625. case 'weapp' :
  626. $config = new ConfigModel();
  627. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WEAPP_CONFIG' ] ]);
  628. if (!empty($res[ 'data' ])) {
  629. if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
  630. $path[ $k ][ 'status' ] = 2;
  631. $path[ $k ][ 'message' ] = '未配置微信小程序';
  632. } else {
  633. $path[ $k ][ 'status' ] = 1;
  634. $path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
  635. }
  636. } else {
  637. $path[ $k ][ 'status' ] = 2;
  638. $path[ $k ][ 'message' ] = '未配置微信小程序';
  639. }
  640. break;
  641. case 'wechat' :
  642. $config = new ConfigModel();
  643. $res = $config->getConfig([ [ 'site_id', '=', $site_id ], [ 'app_module', '=', 'shop' ], [ 'config_key', '=', 'WECHAT_CONFIG' ] ]);
  644. if (!empty($res[ 'data' ])) {
  645. if (empty($res[ 'data' ][ 'value' ][ 'qrcode' ])) {
  646. $path[ $k ][ 'status' ] = 2;
  647. $path[ $k ][ 'message' ] = '未配置微信公众号';
  648. } else {
  649. $path[ $k ][ 'status' ] = 1;
  650. $path[ $k ][ 'img' ] = $res[ 'data' ][ 'value' ][ 'qrcode' ];
  651. }
  652. } else {
  653. $path[ $k ][ 'status' ] = 2;
  654. $path[ $k ][ 'message' ] = '未配置微信公众号';
  655. }
  656. break;
  657. }
  658. }
  659. $return = [
  660. 'path' => $path,
  661. 'game_name' => $game_name,
  662. ];
  663. return $this->success($return);
  664. }
  665. public function gameUrlQrcode($page, $qrcode_param, $promotion_type, $site_id){
  666. $params = [
  667. 'site_id' => $site_id,
  668. 'data' => $qrcode_param,
  669. 'page' => $page,
  670. 'promotion_type' => $promotion_type,
  671. 'h5_path' => $page.'?id='.$qrcode_param['id'],
  672. 'qrcode_path' => 'upload/qrcode/games',
  673. 'qrcode_name' => 'games_qrcode_'. $promotion_type .'_' .$qrcode_param['id'] . '_' . $site_id,
  674. ];
  675. $solitaire = event('ExtensionInformation', $params, true);
  676. return $this->success($solitaire);
  677. }
  678. /**
  679. * 获取首条数据
  680. * @param $condition
  681. * @param string $field
  682. * @param string $order
  683. * @return array
  684. */
  685. public function getFirstInfo($condition, $field = '*', $order = "")
  686. {
  687. $data = model('promotion_games')->getFirstData($condition, $field, $order);
  688. return $this->success($data);
  689. }
  690. }