ActivityService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2018/12/3
  6. * Time: 下午3:38
  7. */
  8. namespace app\main\service;
  9. use app\common\model\Activity;
  10. use app\common\model\Goods;
  11. use app\common\model\Resource;
  12. use app\main\constants\ActivityConstants;
  13. use app\main\constants\AdminConstants;
  14. use app\main\constants\PayConstants;
  15. use app\main\constants\UserConstants;
  16. use app\main\helper\ArrayHelper;
  17. use app\main\model\object\UserObject;
  18. use think\Config;
  19. use think\Exception;
  20. use think\Request;
  21. use app\common\library\Redis;
  22. /**
  23. * 处理活动
  24. * Class ActivityService
  25. * @package app\main\service
  26. */
  27. class ActivityService extends BaseService
  28. {
  29. /**
  30. * @var ActivityService
  31. */
  32. protected static $self = NULL;
  33. /**
  34. * @return ActivityService
  35. */
  36. public static function instance()
  37. {
  38. if (self::$self == NULL) {
  39. self::$self = new self();
  40. }
  41. return self::$self;
  42. }
  43. /**
  44. * @return Activity
  45. */
  46. public function getActivityModel()
  47. {
  48. return model('Activity');
  49. }
  50. /**
  51. * @return Resource
  52. */
  53. public function getResourceModel()
  54. {
  55. return model('Resource');
  56. }
  57. /**
  58. * @return Goods
  59. */
  60. public function getGoodsModel()
  61. {
  62. return model('Goods');
  63. }
  64. /**
  65. * 前台登录的活动获取
  66. * @param $controllerName
  67. * @param $actionName
  68. * @param $channel_id
  69. * @param $user_id
  70. * @return null
  71. */
  72. public function getActivityFront($controllerName, $actionName,$channel_id,$user_id)
  73. {
  74. $relation = [
  75. 'user_recent' => ActivityConstants::RESOURCE_POP_RANGE_RECENT,
  76. 'index_index' => ActivityConstants::RESOURCE_POP_RANGE_MALE,
  77. 'index_index_girl' => ActivityConstants::RESOURCE_POP_RANGE_FEMALE,
  78. 'index_index_boy' => ActivityConstants::RESOURCE_POP_RANGE_MALE,
  79. 'user_index' => ActivityConstants::RESOURCE_POP_RANGE_PERSONAL,
  80. 'category_index' => ActivityConstants::RESOURCE_POP_RANGE_CATEGORY,
  81. 'book_list_free' => ActivityConstants::RESOURCE_POP_RANGE_FREE,
  82. 'book_list_finish' => ActivityConstants::RESOURCE_POP_RANGE_COMPLETE,
  83. ];
  84. $type = ArrayHelper::array_get($_GET, 'type', '');
  85. if($type){
  86. $key = implode('_', [$controllerName, $actionName, $type]);
  87. }else{
  88. $key = implode('_', [$controllerName, $actionName]);
  89. }
  90. $pop_range = ArrayHelper::array_get($relation, $key, ActivityConstants::RESOURCE_POP_RANGE_RECENT);
  91. $time = Request::instance()->server('REQUEST_TIME');
  92. $user = UserService::instance();
  93. $show_type = [];
  94. //商品资源展示对象
  95. if ($user->is_pay == UserConstants::USER_IS_PAY_NO) {
  96. $show_type['goods']['pay_type'] = ActivityConstants::GOODS_SHOW_TYPE_PAY_NO; //未付费
  97. } else {
  98. $show_type['goods']['pay_type'] = ActivityConstants::GOODS_SHOW_TYPE_PAY_YES; //普通付费
  99. }
  100. //用户类别
  101. if ($user->createtime < strtotime(date("Ymd"))) {
  102. //老用户
  103. $show_type['goods']['register_type'] = ActivityConstants::GOODS_SHOW_TYPE_USER_OLD;
  104. } else {
  105. //新用户
  106. $show_type['goods']['register_type'] = ActivityConstants::GOODS_SHOW_TYPE_USER_NEW;
  107. }
  108. //活动资源展示对象
  109. if ($user->is_pay == UserConstants::USER_IS_PAY_NO) {
  110. $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_NOT; //未付费
  111. } else {
  112. $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_NORMAL; //普通付费
  113. }
  114. //年费会员
  115. if ($user->vip_endtime > $time) {
  116. $show_type['resource']['pay_type'] = ActivityConstants::RESOURCE_PAY_YEAR; //年费会员
  117. }
  118. $activityRes = $this->getActivity($pop_range, $show_type, $channel_id, $user_id);
  119. if ($activityRes) {
  120. return $activityRes;
  121. }
  122. return null;
  123. }
  124. /**
  125. * 获取活动的资源,如果能关联到活动商品,以商品的展示对象为准;如果不能,则以资源的展示对象为准
  126. * @param $aid 活动id
  127. * @return array 活动对象+是否超出活动范围(1超出;0正常展示)
  128. */
  129. public function getActivityResource($aid)
  130. {
  131. $goodsShowTypes = $this->_makeGoodsShowType();//通过当前用户登录信息确定"展示对象"
  132. $arRedisKey = 'ARK:1:'.md5(json_encode($goodsShowTypes)).":".$aid;
  133. $arRedisData = Redis::instance()->get($arRedisKey);
  134. if ($arRedisData === false) {
  135. $resource = $this->getResourceModel()->join(['goods' => 'g'], 'g.id=resource.goods_id')
  136. ->field(['resource.id'])
  137. ->whereIn('g.show_type', $goodsShowTypes)
  138. ->where('activity_id', $aid)
  139. ->order('resource.weigh', 'desc')
  140. ->select();
  141. if (!empty($resource)) {
  142. $result = [current($resource), 0];
  143. Redis::instance()->set($arRedisKey, json_encode($result), 60);
  144. } else {
  145. $resourceShowTypes = $this->_makeResourceShowType();
  146. $arRedisKey2 = 'ARK:2:'.md5(json_encode($resourceShowTypes)).":".$aid;
  147. $arRedisData2 = Redis::instance()->get($arRedisKey2);
  148. if ($arRedisData2 === false) {
  149. $resource = $this->getResourceModel()->field(['resource.id'])
  150. ->whereIn('show_type', $resourceShowTypes)
  151. ->where('activity_id', $aid)
  152. ->order('weigh', 'desc')
  153. ->select();
  154. if (!empty($resource)) {
  155. $result = [current($resource), 0];
  156. Redis::instance()->set($arRedisKey2, json_encode($result), 60);
  157. } else {
  158. $arRedisKey3 = 'ARK:3:'.":".$aid;
  159. $arRedisData3 = Redis::instance()->get($arRedisKey3);
  160. if ($arRedisData3 === false) {
  161. $resource = $this->getResourceModel()->where('activity_id', $aid)
  162. ->find();
  163. $result = [$resource, 1];
  164. Redis::instance()->set($arRedisKey3, json_encode($result), 60);
  165. } else {
  166. $result = json_decode($arRedisData3, true);
  167. }
  168. }
  169. } else {
  170. $result = json_decode($arRedisData2, true);
  171. }
  172. }
  173. } else {
  174. $result = json_decode($arRedisData, true);
  175. }
  176. return $result;
  177. }
  178. /**
  179. * 构造商品的显示范围
  180. * @return array
  181. */
  182. private function _makeGoodsShowType()
  183. {
  184. $user = UserService::instance()->getUserInfo();
  185. $showType = $user->is_pay == UserConstants::USER_IS_PAY_NO ?
  186. PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED : PayConstants::GOODS_SHOW_TYPE_RECHARGED;
  187. return [PayConstants::GOODS_SHOW_TYPE_ALL, $showType];
  188. }
  189. /**
  190. * 构造资源的显示范围
  191. * @return array
  192. */
  193. private function _makeResourceShowType()
  194. {
  195. $time = Request::instance()->server('REQUEST_TIME');
  196. $user = UserService::instance()->getUserInfo();
  197. if ($user->is_pay == UserConstants::USER_IS_PAY_NO) {
  198. $showType = ActivityConstants::RESOURCE_PAY_NOT; //未付费
  199. } else {
  200. $showType = ActivityConstants::RESOURCE_PAY_NORMAL; //普通付费
  201. }
  202. if ($user->vip_endtime > $time) {
  203. $showType = ActivityConstants::RESOURCE_PAY_YEAR; //年费会员
  204. }
  205. return [ActivityConstants::RESOURCE_ALL, $showType];
  206. }
  207. public function getActivity($pop_range, $show_type, $channel_id, $user_id)
  208. {
  209. if ($channel_id) {
  210. $group_id = model('AuthGroupAccess')->getGroupId($channel_id);
  211. $ex_info = model('AdminExtend')->getInfo($channel_id);
  212. //-------------------渠道自定义活动
  213. //平台没有活动时,检查渠道的活动
  214. if (AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id) {
  215. if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $channel_id, $user_id)) {
  216. return $res;
  217. }
  218. }
  219. //检查配号代理商
  220. if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && intval($ex_info['distribute'])) {
  221. //渠道有活动返回渠道的
  222. if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $ex_info['create_by'], $user_id)) {
  223. return $res;
  224. }
  225. //检查配号代理商
  226. if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $channel_id, $user_id)) {
  227. return $res;
  228. }
  229. }
  230. //渠道没有时检查普通代理商
  231. if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && !intval($ex_info['distribute'])) {
  232. if ($res = $this->getChannelActivity($pop_range, $show_type['goods'], $ex_info['create_by'], $user_id)) {
  233. return $res;
  234. }
  235. }
  236. //平台有活动时显示平台的
  237. if ($res = $this->getPlatformActivity($pop_range, $show_type['goods'])) {
  238. return $res;
  239. }
  240. //-------------------赠币活动
  241. //平台没有活动时,检查渠道的活动
  242. //判断是否关闭了赠币
  243. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
  244. if ($adminConfig && $adminConfig['give_kandian'] == 0) {
  245. return null;
  246. }
  247. if (AdminConstants::ADMIN_GROUP_ID_CHANNEL == $group_id) {
  248. if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $channel_id, $user_id)) {
  249. return $res;
  250. }
  251. }
  252. //检查配号代理商
  253. if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && intval($ex_info['distribute'])) {
  254. //渠道有活动返回渠道的
  255. if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $ex_info['create_by'], $user_id)) {
  256. return $res;
  257. }
  258. //检查配号代理商
  259. if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $channel_id, $user_id)) {
  260. return $res;
  261. }
  262. }
  263. //渠道没有时检查普通代理商
  264. if (AdminConstants::ADMIN_GROUP_ID_AGENT == $group_id && !intval($ex_info['distribute'])) {
  265. if ($res = $this->getGiveActivity($pop_range, $show_type['resource'], $ex_info['create_by'], $user_id)) {
  266. return $res;
  267. }
  268. }
  269. }
  270. return null;
  271. }
  272. /**
  273. * 获取平台活动
  274. * @param $pop_range
  275. * @param $show_type
  276. * @return mixed
  277. * @throws \think\db\exception\BindParamException
  278. * @throws \think\exception\PDOException
  279. */
  280. private function getPlatformActivity($pop_range, $show_type)
  281. {
  282. if (!is_array($show_type)) {
  283. $show_type = [$show_type];
  284. }
  285. $res = null;
  286. $key = "S-P-T-P:N:";
  287. $redis = Redis::instance();
  288. foreach ($show_type as $user_type) {
  289. $positionKey[] = "{$pop_range}:{$user_type}";
  290. }
  291. $redisVals = $redis->hMGet($key, $positionKey);
  292. foreach ($redisVals as $k => $redisVal) {
  293. if ($redisVal != false) {
  294. $data = json_decode($redisVal, true);
  295. //判断活动结束时间
  296. if ($data && isset($data['endtime'])) {
  297. if ($data['endtime'] <= time()) {
  298. //结束啦
  299. continue;
  300. }
  301. }
  302. $res = $data;
  303. break;
  304. }
  305. }
  306. return $res;
  307. }
  308. /**
  309. * @param $resourceShowType
  310. * @return array
  311. */
  312. private function _makeGoodShowTypeFromResource($resourceShowType)
  313. {
  314. $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL];
  315. switch ($resourceShowType) {
  316. case ActivityConstants::RESOURCE_PAY_NOT:
  317. $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED;
  318. break;
  319. case ActivityConstants::RESOURCE_PAY_NORMAL:
  320. $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_RECHARGED;
  321. break;
  322. case ActivityConstants::RESOURCE_PAY_YEAR:
  323. $goodsShowTypes[] = PayConstants::GOODS_SHOW_TYPE_RECHARGED;
  324. break;
  325. default:
  326. break;
  327. }
  328. return $goodsShowTypes;
  329. }
  330. /**
  331. * 获取渠道商活动
  332. * @param $pop_range
  333. * @param $show_type
  334. * @param $channel_id
  335. * @param $user_id
  336. * @return mixed
  337. */
  338. private function getChannelActivity($pop_range, $show_type, $channel_id, $user_id)
  339. {
  340. if (!is_array($show_type)) {
  341. $show_type = [$show_type];
  342. }
  343. $res = null;
  344. $key = "S-P-T-C:N:{$channel_id}";
  345. $redis = Redis::instance();
  346. foreach ($show_type as $user_type) {
  347. $positionKey[] = "{$pop_range}:{$user_type}";
  348. }
  349. $redisVals = $redis->hMGet($key, $positionKey);
  350. foreach ($redisVals as $k => $redisVal) {
  351. if ($redisVal != false) {
  352. $data = json_decode($redisVal, true);
  353. //判断活动结束时间
  354. if ($data && isset($data['endtime'])) {
  355. if ($data['endtime'] <= time()) {
  356. //结束啦
  357. continue;
  358. }
  359. }
  360. //检查限制
  361. if ($data && array_key_exists('limited', $data)) {
  362. if (intval($data['limited'])) {
  363. //用户在渠道自定义的活动充值次数统计
  364. $pay_count = intval($redis->hGet("ATPU:{$data['aid']}", $user_id));
  365. if (intval($data['limited']) <= $pay_count) {
  366. continue;
  367. }
  368. }
  369. }
  370. $res = $data;
  371. break;
  372. }
  373. }
  374. if(isset($show_type['pay_type']) && isset($res['pop_ispay']) && $res['pop_ispay']!=1){
  375. if($res['pop_ispay'] == 3 && $show_type['pay_type'] == 1){
  376. $res = null;
  377. }
  378. if($res['pop_ispay'] ==2 && $show_type['pay_type'] == 2 ){
  379. $res = null;
  380. }
  381. }
  382. return $res;
  383. }
  384. /**
  385. * 获取渠道商自定义活动列表,供充值页面使用
  386. * @param UserObject $userInfo
  387. * @return array|mixed
  388. */
  389. public function getRechargePageChannelActivity($userInfo)
  390. {
  391. $channelId = $userInfo->channel_id;
  392. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channelId);
  393. if ($adminConfig['activity_show_recharge_page'] == 0) {
  394. return [];
  395. }
  396. $activityConfig = Config::get('site.activity_config');
  397. $redis = Redis::instance();
  398. $userId = $userInfo->id;
  399. $key = "CIEN:".$channelId;
  400. if ($redis->exists($key)) {
  401. $res = json_decode($redis->get($key), true);
  402. } else {
  403. $time = time();
  404. $res = [];
  405. $activitySimpleList = [];
  406. $activityList = $this->getActivityModel()
  407. ->field(['id', 'config_id', 'limited', 'pop_status'])
  408. ->where('starttime', '<', $time)
  409. ->where('endtime', '>', $time)
  410. ->where('status', '1')
  411. ->where('type', '1')
  412. ->where('show_for_recharge_page', 1)
  413. ->where('admin_id', $channelId)
  414. ->order('createtime desc')
  415. ->select();
  416. if ($activityList) {
  417. $resourceIds = [];
  418. foreach ($activityList as $activity) {
  419. //获取活动的ConfigID
  420. $config_id = $activity['config_id'] ?? null;
  421. if (!$config_id) {
  422. LogService::error("Activity Error activity_id:{$activity['id']} ConfigID is Null");
  423. continue;
  424. }
  425. $config = $activityConfig['config'][$config_id] ?? null;
  426. if (!$config) {
  427. LogService::error("Activity Error activity_id:{$activity['id']} config_id:{$config_id} Config Not Find");
  428. continue;
  429. }
  430. if ($config && intval($config['status']) && (isset($config['resource_id']) && $config['resource_id'])) {
  431. $item = [
  432. 'activity_id' => $activity['id'],
  433. 'config_id' => $activity['config_id'],
  434. 'limited' => $activity['limited'],
  435. 'resource_id' => $config['resource_id'],
  436. ];
  437. $activitySimpleList[] = $item;
  438. $resourceIds[] = $config['resource_id'];
  439. } else {
  440. continue;
  441. }
  442. }
  443. $actCategoryId = GoodsService::instance()->getActivityGoodsCategoryId();
  444. $resourceList = $this->getResourceModel()->alias('r')->join(['goods' => 'g'], 'r.goods_id = g.id')
  445. ->field([
  446. 'r.id' => 'rid',
  447. 'g.id',
  448. 'category_id',
  449. 'title',
  450. 'g.money',
  451. 'g.first_description',
  452. 'g.second_description',
  453. 'g.kandian',
  454. 'g.free_kandian',
  455. 'g.icon',
  456. 'g.show_type',
  457. 'g.type'
  458. ])
  459. ->whereIn('r.id', $resourceIds)
  460. ->where('resource_type', ActivityConstants::ACTIVITY_TYPE_CUSTOM)
  461. ->where('r.status', '1')
  462. ->where("find_in_set($actCategoryId,g.category_id)")
  463. ->order('r.weigh', 'desc')
  464. ->select();
  465. foreach ($activitySimpleList as $activitySimpleItem) {
  466. foreach ($resourceList as $resItem) {
  467. if ($activitySimpleItem['resource_id'] == $resItem['rid']) {
  468. $item = $activitySimpleItem;
  469. $item['money'] = $resItem['money'];
  470. $item['show_type'] = $resItem['show_type'];
  471. $item['id'] = $resItem['id'];
  472. $item['category_id'] = $resItem['category_id'];
  473. $item['kandian'] = $resItem['kandian'];
  474. $item['free_kandian'] = $resItem['free_kandian'];
  475. $item['title'] = $resItem['title'];
  476. $item['icon'] = $resItem['icon'];
  477. $item['first_description'] = $resItem['first_description'];
  478. $item['second_description'] = $resItem['second_description'];
  479. $res[] = $item;
  480. }
  481. }
  482. }
  483. }
  484. $redis->set($key, json_encode($res, JSON_UNESCAPED_UNICODE), 60);
  485. }
  486. if (intval($userInfo->is_pay) == 0) {
  487. $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL, PayConstants::GOODS_SHOW_TYPE_UN_RECHARGED];
  488. } else {
  489. $goodsShowTypes = [PayConstants::GOODS_SHOW_TYPE_ALL, PayConstants::GOODS_SHOW_TYPE_RECHARGED];
  490. }
  491. //检查次数限制
  492. foreach ($res as $item) {
  493. if (intval($item['limited'])) {
  494. //用户在渠道自定义的活动充值次数统计
  495. $pay_count = intval($redis->hGet("ATPU:{$item['activity_id']}", $userId));
  496. if (intval($item['limited']) <= $pay_count) {
  497. unset($item);
  498. continue;
  499. }
  500. if (in_array($item['show_type'], $goodsShowTypes)) {
  501. unset($item);
  502. continue;
  503. }
  504. }
  505. }
  506. return $res;
  507. }
  508. /**
  509. * 获取赠币活动
  510. * @param $pop_range
  511. * @param $show_type
  512. * @param $channel_id
  513. * @param $user_id
  514. * @return mixed
  515. */
  516. private function getGiveActivity($pop_range, $show_type, $channel_id, $user_id)
  517. {
  518. if (!is_array($show_type)) {
  519. $show_type = [$show_type];
  520. }
  521. $res = null;
  522. $key = "S-P-T-G:N:{$channel_id}";
  523. $redis = Redis::instance();
  524. foreach ($show_type as $user_type) {
  525. $positionKey[] = "{$pop_range}:{$user_type}";
  526. }
  527. $redisVals = $redis->hMGet($key, $positionKey);
  528. foreach ($redisVals as $k => $redisVal) {
  529. if ($redisVal != false) {
  530. $data = json_decode($redisVal, true);
  531. //判断活动结束时间
  532. if ($data && isset($data['endtime'])) {
  533. if ($data['endtime'] <= time()) {
  534. //结束啦
  535. continue;
  536. }
  537. }
  538. //检查限制
  539. if ($data && array_key_exists('limited', $data)) {
  540. if (intval($data['limited'])) {
  541. //用户在渠道自定义的活动充值次数统计
  542. $pay_count = intval($redis->hGet("ATGP:{$data['aid']}", $user_id));
  543. if (intval($data['limited']) <= $pay_count) {
  544. continue;
  545. }
  546. }
  547. }
  548. $res = $data;
  549. break;
  550. }
  551. }
  552. if(isset($show_type['pay_type']) && isset($res['pop_ispay']) && $res['pop_ispay']!=1){
  553. if($res['pop_ispay'] == 3 && $show_type['pay_type'] == 2){
  554. $res = null;
  555. }
  556. if($res['pop_ispay'] ==2 && $show_type['pay_type'] == 1 ){
  557. $res = null;
  558. }
  559. }
  560. return $res;
  561. }
  562. /**
  563. * 获取活动资源信息,其中type属性以goods表为准(1=看点充值,2=VIP充值)
  564. * @param $rid 资源id
  565. * @return array|false|\PDOStatement|string|\think\Model|null
  566. */
  567. public function getResourceForFront($rid)
  568. {
  569. //加缓存
  570. $key = 'A-R-R:'.$rid;
  571. if (Redis::instance()->exists($key)) {
  572. $res = json_decode(Redis::instance()->get($key), true);
  573. } else {
  574. $resource = $this->getResourceModel()->getInfo($rid);
  575. if (empty($resource)) {
  576. $res = [];
  577. } else {
  578. if ($resource['goods_id'] == 0) {
  579. $resource['type'] = $resource['type'] == 1 ? 2 : 1;
  580. $res = $resource;
  581. } else {
  582. $goods = $this->getGoodsModel()->find($resource['goods_id']);
  583. $res = [
  584. 'day' => $goods['day'],
  585. 'type' => $goods['type'],
  586. 'money' => $goods['money'],
  587. 'kandian' => $goods['kandian'],
  588. 'free_kandian' => $goods['free_kandian'],
  589. 'free_day' => $goods['free_day'],
  590. 'activity_id' => $resource['activity_id'],
  591. 'name' => $resource['name'],
  592. ];
  593. }
  594. }
  595. Redis::instance()->set($key, json_encode($res, JSON_UNESCAPED_UNICODE), 60);
  596. }
  597. return $res;
  598. }
  599. public function getActivityInfo($aid)
  600. {
  601. $resourceInfo = $this->getResourceModel()->where('activity_id', $aid)->find();
  602. $res = [];
  603. try {
  604. if (empty($resourceInfo)) {//渠道商自定义活动
  605. $aInfo = $this->getActivityModel()->where('id', $aid)->find();
  606. if (empty($aInfo)) {
  607. throw new Exception('没有找到活动,aId:' . $aid);
  608. }
  609. $configId = $aInfo['config_id'];
  610. $activity_config = Config::get('site.activity_config');
  611. $config = $activity_config['config'][$configId] ?? null;
  612. if (empty($config)) {
  613. throw new Exception('没有找到config,config_id:' . $configId);
  614. }
  615. if ($config && (isset($config['resource_id']) && $config['resource_id'])
  616. ) {
  617. $resourceInfo = $this->getResourceModel()->where('id', $config['resource_id'])->find();
  618. if (empty($resourceInfo)) {
  619. throw new Exception('渠道商自定义活动没有找到资源,rid:' . $config['resource_id']);
  620. }
  621. $res = $this->makeActivityInfo($resourceInfo);
  622. }
  623. return $res;
  624. } else {//平台活动
  625. $res = $this->makeActivityInfo($resourceInfo);
  626. return $res;
  627. }
  628. } catch (Exception $e) {
  629. LogService::error($e->getMessage());
  630. $res['free_day'] = Config::get('site.kandian_free_day');
  631. return $res;
  632. }
  633. }
  634. private function makeActivityInfo($resourceInfo)
  635. {
  636. if (empty($resourceInfo['goods_id'])) {
  637. $res['free_day'] = $resourceInfo['free_day'];
  638. } else {
  639. $goodsInfo = $this->getGoodsModel()->getGoodsInfoById($resourceInfo['goods_id']);
  640. if (empty($goodsInfo)) {
  641. throw new Exception('没有找到商品,goodsId:' . $resourceInfo['goods_id']);
  642. }
  643. $res['free_day'] = $goodsInfo['free_day'];
  644. }
  645. return $res;
  646. }
  647. /**
  648. * 获取有效的活动
  649. * @return false|\PDOStatement|string|\think\Collection
  650. * @throws \think\db\exception\DataNotFoundException
  651. * @throws \think\db\exception\ModelNotFoundException
  652. * @throws \think\exception\DbException
  653. */
  654. public function getValidActivities($type = 0)
  655. {
  656. $map['type'] = ['in', [0, 1, 2]];
  657. if ($type) {
  658. $map['type'] = ['eq', $type];
  659. }
  660. //$activity = new Activity();
  661. $data = $this->getActivityModel()
  662. ->where($map)
  663. ->where('status', 'eq', 1)
  664. ->where('starttime', '<', time())
  665. ->where('endtime', '>', time())
  666. ->select();
  667. $aActivity = [];
  668. if($data){
  669. foreach ($data as $activity) {
  670. $aActivity[$activity->id] = $activity->name;
  671. }
  672. }
  673. return $aActivity;
  674. }
  675. }