DayAward.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\game;
  9. use app\admin\model\system\SystemConfig;
  10. use app\models\store\StoreOrder;
  11. use app\models\user\User;
  12. use app\models\user\UserBill;
  13. use crmeb\traits\ModelTrait;
  14. use crmeb\basic\BaseModel;
  15. /**
  16. * TODO 用户消费新增金额明细 model
  17. * Class UserBill
  18. * @package app\models\user
  19. */
  20. class DayAward extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'id';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'day_award';
  32. use ModelTrait;
  33. public static function getAward($uid, $id)
  34. {
  35. $info = self::get($id);
  36. if (!$info || $info['uid'] != $uid) return self::setErrorInfo('领取失败');
  37. self::beginTrans();
  38. try {
  39. $get = bcmul($info['award'], 0.9, 2);
  40. $left = bcsub($info['award'], $get, 2);
  41. $res = self::where('id', $id)->update(['status' => 1]) &&
  42. User::bcInc($uid, 'brokerage_price', $get, 'uid') &&
  43. UserBill::income('天天领红包', $uid, 'now_money', 'brokerage', $get, $id, User::where('uid', $uid)->value('brokerage_price'), '今日领红包' . $info['award'] . '扣除公益池实际到账' . $get) &&
  44. StoreOrder::addPublicLake($left, $id, 'award');
  45. if ($res) {
  46. self::commitTrans();
  47. return true;
  48. } else {
  49. self::rollbackTrans();
  50. return self::setErrorInfo('领取失败');
  51. }
  52. } catch (\Exception $e) {
  53. self::rollbackTrans();
  54. return self::setErrorInfo();
  55. }
  56. }
  57. public static function dayAward()
  58. {
  59. if (self::whereTime('add_time', 'today')->find()) {
  60. return true;
  61. }
  62. if (!Lottery::whereTime('add_time', 'yesterday')->where('status', 1)->find()) {
  63. return true;
  64. }
  65. $all_point = Lottery::where('status', 1)->whereTime('open_time', 'yesterday')->sum('ticket');
  66. $award_lake = sys_config('award_lake', 0, true);
  67. $single_award = bcdiv($award_lake, bcmul($all_point, 2), 2);
  68. if ($single_award < 0) {
  69. return true;
  70. }
  71. $list = Lottery::whereTime('add_time', 'yesterday')->where('status', 1)
  72. ->group('uid')->field('uid,SUM(ticket) as sum_ticket')->select();
  73. self::beginTrans();
  74. try {
  75. $res = true;
  76. foreach ($list as $v) {
  77. $award = bcmul($single_award, $v['sum_ticket'], 2);
  78. $res1 = self::create([
  79. 'uid' => $v['uid'],
  80. 'point' => $v['sum_ticket'],
  81. 'add_time' => time(),
  82. 'award' => $award
  83. ]);
  84. $res = $res && $res1 && self::expendAwardLake($award, $res1->id);
  85. }
  86. if ($res) {
  87. self::commitTrans();
  88. return true;
  89. } else {
  90. self::rollbackTrans();
  91. return self::setErrorInfo('发放失败');
  92. }
  93. } catch (\Exception $e) {
  94. self::rollbackTrans();
  95. return self::setErrorInfo();
  96. }
  97. }
  98. public static function dayAwardOut()
  99. {
  100. $all_award = self::whereTime('add_time', 'yesterday')->where('status', 0)->sum('award');
  101. if ($all_award <= 0) {
  102. return true;
  103. }
  104. self::beginTrans();
  105. try {
  106. $res = self::whereTime('add_time', 'yesterday')->where('status', 0)->update(['status' => 2]) &&
  107. StoreOrder::addPublicLake($all_award, 0, 'award_out');
  108. if ($res) {
  109. self::commitTrans();
  110. return true;
  111. } else {
  112. self::rollbackTrans();
  113. return self::setErrorInfo('过期失败');
  114. }
  115. } catch (\Exception $e) {
  116. self::rollbackTrans();
  117. return self::setErrorInfo();
  118. }
  119. }
  120. public static function expendAwardLake($num, $link_id, $type = 'award')
  121. {
  122. $old = sys_config('award_lake', 0, true);
  123. $res = SystemConfig::where('menu_name', 'award_lake')->update(['value' => (float)$old - $num]);
  124. $str = '';
  125. switch ($type) {
  126. case 'award':
  127. $str = '每日奖金';
  128. break;
  129. }
  130. return $res && UserBill::expend('奖池支出', 0, 'award_lake', 'expend', $num, $link_id, (float)$old - $num, $str ?: '奖池支出');
  131. }
  132. }