123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/10/11
- * Time: 下午7:38
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\main\constants\BookConstants;
- use app\main\constants\CacheConstants;
- use app\main\constants\PayConstants;
- use app\main\constants\RechargeConstants;
- class BuyMoreService extends BaseService
- {
- protected static $self = null;
- public static function instance()
- {
- if (self::$self == null) {
- self::$self = new self();
- }
- return self::$self;
- }
- public function checkOrderBuyMore($user_id, $out_trade_no, $order_id)
- {
- $cacheOrder = CacheConstants::getBuyMoreOrderCache($out_trade_no);
- $cache = Redis::instance()->get($cacheOrder);
- if ($cache) {
- Redis::instance()->del($cacheOrder);
- LogService::info('PAYMORECACHE:' . $cache);
- switch ($cache) {
- case '1':
- $freeBook = Redis::instance()->get(CacheConstants::getBuyMoreFreeBookCache($user_id, 0));
- $this->addKandian($user_id, 500, $order_id);
- $this->addFreeKandian($user_id, 100, $order_id);
- $this->addFreeLimitBook($user_id, 5, 5);
- $this->addFreeBook($user_id, $freeBook);
- Redis::instance()->del(CacheConstants::getBuyMoreFreeBookCache($user_id, 0));
- break;
- case '2':
- $freeBook = Redis::instance()->get(CacheConstants::getBuyMoreFreeBookCache($user_id, 1));
- $this->addKandian($user_id, 1000, $order_id);
- $this->addFreeKandian($user_id, 500, $order_id);
- $this->addFreeLimitBook($user_id, 5, 5);
- $this->addFreeBook($user_id, $freeBook);
- $this->addPayDiscount($user_id, 0.8, 3);
- Redis::instance()->del(CacheConstants::getBuyMoreFreeBookCache($user_id, 1));
- break;
- case '3':
- $freeBook = Redis::instance()->get(CacheConstants::getBuyMoreFreeBookCache($user_id, 2));
- $this->addKandian($user_id, 3000, $order_id);
- $this->addFreeKandian($user_id, 1000, $order_id);
- $this->addFreeLimitBook($user_id, 10, 5);
- $this->addFreeBook($user_id, $freeBook);
- $this->addPayDiscount($user_id, 0.7, 3);
- Redis::instance()->del(CacheConstants::getBuyMoreFreeBookCache($user_id, 2));
- break;
- case '4':
- $freeBook = Redis::instance()->get(CacheConstants::getBuyMoreFreeBookCache($user_id, 3));
- $this->addKandian($user_id, 4900, $order_id);
- $this->addFreeKandian($user_id, 1500, $order_id);
- $this->addFreeLimitBook($user_id, 10, 5);
- $this->addFreeBook($user_id, $freeBook);
- $this->addPayDiscount($user_id, 0.7, 3);
- Redis::instance()->del(CacheConstants::getBuyMoreFreeBookCache($user_id, 3));
- break;
- }
- }
- }
- /**
- * 添加免费书币
- * @param $user_id
- * @param $kandian
- * @param $time
- * @return \app\main\model\object\ReturnObject
- */
- public function addFreeKandian($user_id, $kandian, $order_id, $time = 3, $type = RechargeConstants::RECHARGE_TYPE_KANDIAN)
- {
- $recharge = [
- 'user_id' => $user_id,
- 'type' => $type,
- 'kandian' => 0,
- 'remain_kandian' => 0,
- 'free_kandian' => $kandian,
- 'remain_free_kandian' => $kandian,
- 'free_endtime' => time() + $time * 86400,
- 'day' => 0,
- 'orders_id' => $order_id,
- 'book_id' => '',
- 'createtime' => time(),
- 'updatetime' => time(),
- 'business_line' => PayConstants::BUSINESS_WECHAT,
- ];
- $result = FinancialService::instance()->getRechargeModel()->setConnect($user_id)->insertGetId($recharge);
- Redis::instance()->del(CacheConstants::getFreeKandianUserRechargeListCacheKey($user_id));
- return $this->setData($result)->getReturn();
- }
- /**
- * 添加免费书币
- * @param $user_id
- * @param $kandian
- * @return \app\main\model\object\ReturnObject
- */
- public function addKandian($user_id, $kandian, $order_id)
- {
- $recharge = [
- 'user_id' => $user_id,
- 'type' => RechargeConstants::RECHARGE_TYPE_KANDIAN,
- 'kandian' => $kandian,
- 'remain_kandian' => $kandian,
- 'free_kandian' => 0,
- 'remain_free_kandian' => 0,
- 'day' => 0,
- 'orders_id' => $order_id,
- 'book_id' => '',
- 'createtime' => time(),
- 'updatetime' => time(),
- 'business_line' => PayConstants::BUSINESS_WECHAT,
- ];
- $result = FinancialService::instance()->getRechargeModel()->setConnect($user_id)->insertGetId($recharge);
- Redis::instance()->del(CacheConstants::getKandianUserRechargeListCacheKey($user_id));
- return $this->setData($result)->getReturn();
- }
- /**
- * 获取用户限免忽略书籍
- * @param $user_id
- * @return \app\main\model\object\ReturnObject
- */
- public function getIgnoreBooks($user_id)
- {
- //最近阅读
- $readBookIds = UserService::instance()->getUserRecentlyReadModel()
- ->setConnect($user_id)
- ->where('user_id', $user_id)
- ->group('book_id')
- ->column('book_id');
- $return = array_unique($readBookIds);
- return $this->setData($return)->getReturn();
- }
- /**
- * 添加限免书籍
- * @param $user_id
- * @param $count
- * @param $time
- * @return \app\main\model\object\ReturnObject
- */
- public function addFreeLimitBook($user_id, $count, $time)
- {
- $now = time();
- $ignore = $this->getIgnoreBooks($user_id)->data;
- //限免书
- $free = $this->getFreeLimitCacheBook($user_id)->data;
- $ignore = array_merge($ignore, array_keys($free));
- $bookIds = array_diff(PayConstants::FREE_LIMIT_BOOKS, $ignore);
- if (count($bookIds) < $count) {
- $diff = array_diff(PayConstants::FREE_LIMIT_BOOKS, $bookIds);
- $currentCount = count($bookIds);
- for ($i = 0; $i < $count - $currentCount; $i++) {
- if ($diff) {
- shuffle($diff);
- array_push($bookIds, array_pop($diff));
- }
- }
- } else {
- $bookIds = array_slice($bookIds, 0, $count);
- }
- foreach ($bookIds as $bookId) {
- if (array_key_exists($bookId, $free)) {
- $free[$bookId] += $time * 86400 + 0;
- } else {
- $free[$bookId] = $time * 86400 + $now;
- }
- BookService::instance()->setBookShelf($bookId, $user_id);
- }
- $cache = CacheConstants::getBuyMoreFreeLimitBookCache($user_id);
- Redis::instance()->hMset($cache, $free);
- Redis::instance()->expire($cache, max($free) - $now);
- return $this->setData(true)->getReturn();
- }
- /**
- * 获取缓存的限免书籍
- * @param $user_id
- * @return \app\main\model\object\ReturnObject
- */
- public function getFreeLimitCacheBook($user_id)
- {
- //限免书
- $cache = CacheConstants::getBuyMoreFreeLimitBookCache($user_id);
- $free = Redis::instance()->hGetAll($cache);
- $data = [];
- if ($free) {
- foreach ($free as $bookId => $time) {
- if ($time > time()) {
- $data[$bookId] = $time;
- } else {
- Redis::instance()->hDel($cache, $bookId);
- }
- }
- if (!$data) {
- Redis::instance()->del($cache);
- }
- }
- return $this->setData($data)->getReturn();
- }
- /**
- * 添加免费书籍
- * @param $user_id
- * @param $book_id
- * @return \app\main\model\object\ReturnObject
- */
- public function addFreeBook($user_id, $book_id)
- {
- $book = BookService::instance()->getBookModel()->getBooksInfo([$book_id]);
- $exists = FinancialService::instance()->getConsumeModel()->setConnect($user_id)
- ->where('user_id', $user_id)
- ->where('book_id', $book_id)
- ->where('type', BookConstants::BOOK_BILLING_MODEL_BOOK)
- ->find();
- if (!$book) {
- LogService::error('书籍不存在');
- return $this->setData(true)->getReturn();
- }
- if (!$exists) {
- $consume = [
- 'user_id' => $user_id,
- 'book_id' => $book_id,
- 'book_name' => $book[$book_id]['name'],
- 'type' => BookConstants::BOOK_BILLING_MODEL_BOOK,
- 'chapter_id' => '',
- 'chapter_name' => '',
- 'createtime' => time(),
- 'updatetime' => time(),
- 'extend1' => $book_id,
- 'extend2' => '',
- ];
- FinancialService::instance()->getConsumeModel()->setConnect($user_id)->insertGetId($consume);
- } else {
- LogService::info('书籍已经存在');
- }
- BookService::instance()->setBookShelf($book_id, $user_id);
- return $this->setData(true)->getReturn();
- }
- /**
- * 缓存永久免费书籍,回调使用
- * @param $user_id
- * @return \app\main\model\object\ReturnObject
- */
- public function setFreeBook($user_id)
- {
- $books = [];
- for ($i = 0; $i < 4; $i++) {
- $cache = CacheConstants::getBuyMoreFreeBookCache($user_id, $i);
- $id = Redis::instance()->get($cache);
- if (!$id) {
- $readBooks = $this->getIgnoreBooks($user_id)->data;
- $ids = array_diff(PayConstants::FREE_PERMANENT_BOOKS, $readBooks, $books);
- if (!$ids) {
- $ids = PayConstants::FREE_PERMANENT_BOOKS;
- }
- shuffle($ids);
- $id = array_pop($ids);
- Redis::instance()->set($cache, $id, 86400);
- }
- $books[] = $id;
- }
- return $this->setData($books)->getReturn();
- }
- /**
- * 用户阅读折扣
- * @param $user_id
- * @param $rate
- * @param $time
- * @return \app\main\model\object\ReturnObject
- */
- public function addPayDiscount($user_id, $rate, $time)
- {
- $cache = CacheConstants::getBuyMoreDiscountCache($user_id);
- $now = time();
- if ($cacheList = Redis::instance()->zRangeByScore($cache, time(), '+inf', ['withscores' => TRUE])) {
- $time = max($cacheList) + $time * 86400;
- } else {
- $time = $time * 86400 + $now;
- }
- Redis::instance()->zAdd($cache, $time, $time . ":" . $rate);
- Redis::instance()->expire($cache, $time - $now);
- // Redis::instance()->set($cache, $rate, $time * 86400);
- return $this->setData(true)->getReturn();
- }
- }
|