FloatTipsService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/3/3
  6. * Time: 下午6:42
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Redis;
  10. use app\common\model\ChannelSpecialManage;
  11. use app\common\model\Floattips;
  12. use app\main\constants\BookConstants;
  13. use app\main\constants\CacheConstants;
  14. use think\db\Query;
  15. /**
  16. * Class FloatTipsService
  17. * @package app\main\service
  18. */
  19. class FloatTipsService extends BaseService
  20. {
  21. /**
  22. * @var FloatTipsService
  23. */
  24. public static $self = null;
  25. /**
  26. * @return BaseService|FloatTipsService
  27. */
  28. public static function instance()
  29. {
  30. if (self::$self == NULL) {
  31. self::$self = new self();
  32. }
  33. return self::$self;
  34. }
  35. /**
  36. * @return Floattips
  37. */
  38. public function getFloatTipsModel()
  39. {
  40. return model('Floattips');
  41. }
  42. public function getSysActivity()
  43. {
  44. $time = time();
  45. $maps = [
  46. 'activity.admin_id' => 0,
  47. 'activity.status' => ['eq', '1'],
  48. 'activity.starttime' => ['<=', $time],
  49. 'activity.endtime' => ['>=', $time],
  50. 'activity.is_system' => 1,
  51. ];
  52. $activityRows = model("Activity")
  53. ->field("activity.id")
  54. ->where($maps)
  55. ->order('activity.id', 'asc')
  56. ->field('id,name')
  57. ->select();
  58. return $this->setData($activityRows)->getReturn();
  59. }
  60. public function getFloatTips($id)
  61. {
  62. $cache = CacheConstants::getFloatTipsInfo($id);
  63. if (!$data = Redis::instance()->hGetAll($cache)) {
  64. $oFloatTips = model('floattips')
  65. ->join('book', 'book.id=float_tips.book_id', 'LEFT')
  66. ->where(function(Query $query){
  67. $query->whereNull('book.state')
  68. ->whereOr('book.state', BookConstants::BOOK_STATE_ON_SALE);
  69. })
  70. ->where('float_tips.status', 'normal')
  71. ->field('float_tips.*')
  72. ->where('float_tips.id', $id)
  73. ->find();
  74. if ($oFloatTips) {
  75. $data = $oFloatTips->getData();
  76. Redis::instance()->hMSet($cache, $data);
  77. } else {
  78. $data = ['id' => 0];
  79. Redis::instance()->hMSet($cache, $data);
  80. }
  81. Redis::instance()->expire($cache, 300);
  82. }
  83. if (!$data['id']) {
  84. $data = false;
  85. }
  86. return $this->setData($data)->getReturn();
  87. }
  88. public function getFloatTipsForUser($user_id, $chapter_idx, $sellVal)
  89. {
  90. $data = ['id' => 0];
  91. if ($sellVal >= 2) {
  92. return $this->setData($data)->getReturn();
  93. }
  94. $uCache = CacheConstants::getFloatTipsIdsByUserIds($user_id);
  95. $list = Redis::instance()->sMembers($uCache);
  96. if (!$list) {
  97. return $this->setData($data)->getReturn();
  98. }
  99. $now = time();
  100. rsort($list);
  101. foreach ($list as $id) {
  102. $aFloat = $this->getFloatTips($id)->data;
  103. if ($aFloat && !empty($aFloat['id']) && $aFloat['starttime'] < $now && $aFloat['endtime'] > $now && $chapter_idx >= $aFloat['chapter_show'] ) {
  104. if (strstr($aFloat['link'], '?')) {
  105. $aFloat['link'] .= '&float_id=' . $aFloat['id'];
  106. } else {
  107. $aFloat['link'] .= '?float_id=' . $aFloat['id'];
  108. }
  109. $data = $aFloat;
  110. break;
  111. }
  112. }
  113. return $this->setData($data)->getReturn();
  114. }
  115. /**
  116. * 更新浮动条充值信息
  117. * @param $float_id
  118. * @param $user_id
  119. * @param $money
  120. */
  121. public function updateFloatOrder($float_id, $user_id, $money)
  122. {
  123. $cacheUser = CacheConstants::getFloatTipsOrderUser($float_id);
  124. $cacheUserDay = CacheConstants::getFloatTipsOrderUserDay($float_id);
  125. $cacheMoney = CacheConstants::getFloatTipsOrderMoney($float_id);
  126. $cacheMoneyDay = CacheConstants::getFloatTipsOrderMoneyDay($float_id);
  127. Redis::instance()->incrByFloat($cacheMoney, $money);
  128. Redis::instance()->incrByFloat($cacheMoneyDay, $money);
  129. Redis::instance()->pfAdd($cacheUser, $user_id);
  130. Redis::instance()->pfAdd($cacheUserDay, $user_id);
  131. $this->getFloatTipsModel()->update([
  132. 'recharge_user' => Redis::instance()->pfCount($cacheUser),
  133. 'recharge_money' => Redis::instance()->get($cacheMoney),
  134. ], ['id' => $float_id]);
  135. }
  136. }