GroupDataService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\services\system\config\SystemGroupDataServices;
  13. /**
  14. * 获取组合数据配置
  15. * Class GroupDataService
  16. * @package crmeb\services
  17. */
  18. class GroupDataService
  19. {
  20. /**
  21. * 获取单个值
  22. * @param string $config_name 配置名称
  23. * @param int $limit 截取多少条
  24. * @param bool $isCaChe 是否读取缓存
  25. * @return array
  26. */
  27. public static function getData(string $config_name, int $limit = 0, bool $isCaChe = false): array
  28. {
  29. $callable = function () use ($config_name, $limit) {
  30. event('get.config');
  31. try {
  32. /** @var SystemGroupDataServices $service */
  33. $service = app()->make(SystemGroupDataServices::class);
  34. return $service->getConfigNameValue($config_name, $limit);
  35. } catch (\Exception $e) {
  36. return [];
  37. }
  38. };
  39. try {
  40. $cacheName = $limit ? "data_{$config_name}_{$limit}" : "data_{$config_name}";
  41. if ($isCaChe)
  42. return $callable();
  43. return CacheService::get($cacheName, $callable);
  44. } catch (\Throwable $e) {
  45. return $callable();
  46. }
  47. }
  48. /**
  49. * 根据id 获取单个值
  50. * @param int $id
  51. * @param bool $isCaChe 是否读取缓存
  52. * @return array
  53. */
  54. public static function getDataNumber(int $id, bool $isCaChe = false): array
  55. {
  56. $callable = function () use ($id) {
  57. try {
  58. /** @var SystemGroupDataServices $service */
  59. $service = app()->make(SystemGroupDataServices::class);
  60. $data = $service->getDateValue($id);
  61. if (is_object($data))
  62. $data = $data->toArray();
  63. return $data;
  64. } catch (\Exception $e) {
  65. return [];
  66. }
  67. };
  68. try {
  69. $cacheName = "data_number_{$id}";
  70. if ($isCaChe)
  71. return $callable();
  72. return CacheService::get($cacheName, $callable);
  73. } catch (\Throwable $e) {
  74. return $callable();
  75. }
  76. }
  77. }