PriceService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\NoticeModel;
  13. use App\Models\PriceModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 价格服务-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Api
  21. */
  22. class PriceService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * NoticeService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new PriceModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 当前价格
  48. * @param int $type 1-数字资产,2-绿色积分
  49. * @param false $refresh
  50. * @return array|mixed|string
  51. */
  52. public function getTodayPrice($type=1, $refresh=false)
  53. {
  54. $cacheKey = "caches:prices:today_price_{$type}";
  55. $data = RedisService::get($cacheKey);
  56. $price = isset($data['price'])?$data['price']: '0.00';
  57. if($data && $price && !$refresh){
  58. return $price;
  59. }
  60. $data = $this->model->where(['type'=>$type,'status'=>1,'mark'=>1])
  61. ->orderBy('date','desc')
  62. ->first();
  63. $data = $data?$data->toArray() : [];
  64. $price = isset($data['price'])?$data['price']: '0.00';
  65. if($data && $price){
  66. RedisService::set($cacheKey, $data, rand(300,600));
  67. }
  68. return $price;
  69. }
  70. public function updatePrice($type=1)
  71. {
  72. }
  73. }