123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2020/3/3
- * Time: 下午6:42
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\common\model\ChannelSpecialManage;
- use app\common\model\Floattips;
- use app\main\constants\BookConstants;
- use app\main\constants\CacheConstants;
- use think\db\Query;
- /**
- * Class FloatTipsService
- * @package app\main\service
- */
- class FloatTipsService extends BaseService
- {
- /**
- * @var FloatTipsService
- */
- public static $self = null;
- /**
- * @return BaseService|FloatTipsService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return Floattips
- */
- public function getFloatTipsModel()
- {
- return model('Floattips');
- }
- public function getSysActivity()
- {
- $time = time();
- $maps = [
- 'activity.admin_id' => 0,
- 'activity.status' => ['eq', '1'],
- 'activity.starttime' => ['<=', $time],
- 'activity.endtime' => ['>=', $time],
- 'activity.is_system' => 1,
- ];
- $activityRows = model("Activity")
- ->field("activity.id")
- ->where($maps)
- ->order('activity.id', 'asc')
- ->field('id,name')
- ->select();
- return $this->setData($activityRows)->getReturn();
- }
- public function getFloatTips($id)
- {
- $cache = CacheConstants::getFloatTipsInfo($id);
- if (!$data = Redis::instance()->hGetAll($cache)) {
- $oFloatTips = model('floattips')
- ->join('book', 'book.id=float_tips.book_id', 'LEFT')
- ->where(function(Query $query){
- $query->whereNull('book.state')
- ->whereOr('book.state', BookConstants::BOOK_STATE_ON_SALE);
- })
- ->where('float_tips.status', 'normal')
- ->field('float_tips.*')
- ->where('float_tips.id', $id)
- ->find();
- if ($oFloatTips) {
- $data = $oFloatTips->getData();
- Redis::instance()->hMSet($cache, $data);
- } else {
- $data = ['id' => 0];
- Redis::instance()->hMSet($cache, $data);
- }
- Redis::instance()->expire($cache, 300);
- }
- if (!$data['id']) {
- $data = false;
- }
- return $this->setData($data)->getReturn();
- }
- public function getFloatTipsForUser($user_id, $chapter_idx, $sellVal)
- {
- $data = ['id' => 0];
- if ($sellVal >= 2) {
- return $this->setData($data)->getReturn();
- }
- $uCache = CacheConstants::getFloatTipsIdsByUserIds($user_id);
- $list = Redis::instance()->sMembers($uCache);
- if (!$list) {
- return $this->setData($data)->getReturn();
- }
- $now = time();
- rsort($list);
- foreach ($list as $id) {
- $aFloat = $this->getFloatTips($id)->data;
- if ($aFloat && !empty($aFloat['id']) && $aFloat['starttime'] < $now && $aFloat['endtime'] > $now && $chapter_idx >= $aFloat['chapter_show'] ) {
- if (strstr($aFloat['link'], '?')) {
- $aFloat['link'] .= '&float_id=' . $aFloat['id'];
- } else {
- $aFloat['link'] .= '?float_id=' . $aFloat['id'];
- }
- $data = $aFloat;
- break;
- }
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 更新浮动条充值信息
- * @param $float_id
- * @param $user_id
- * @param $money
- */
- public function updateFloatOrder($float_id, $user_id, $money)
- {
- $cacheUser = CacheConstants::getFloatTipsOrderUser($float_id);
- $cacheUserDay = CacheConstants::getFloatTipsOrderUserDay($float_id);
- $cacheMoney = CacheConstants::getFloatTipsOrderMoney($float_id);
- $cacheMoneyDay = CacheConstants::getFloatTipsOrderMoneyDay($float_id);
- Redis::instance()->incrByFloat($cacheMoney, $money);
- Redis::instance()->incrByFloat($cacheMoneyDay, $money);
- Redis::instance()->pfAdd($cacheUser, $user_id);
- Redis::instance()->pfAdd($cacheUserDay, $user_id);
- $this->getFloatTipsModel()->update([
- 'recharge_user' => Redis::instance()->pfCount($cacheUser),
- 'recharge_money' => Redis::instance()->get($cacheMoney),
- ], ['id' => $float_id]);
- }
- }
|