GroupDataService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/15
  6. */
  7. namespace crmeb\services;
  8. use app\admin\model\system\SystemGroupData;
  9. use think\facade\Cache;
  10. class GroupDataService
  11. {
  12. /**
  13. * 获取单个组数据
  14. * @param string $config_name 配置名称
  15. * @param int $limit 获取条数
  16. * @param bool $isCaChe 是否读取缓存
  17. * @return array
  18. */
  19. public static function getGroupData(string $config_name, $limit = 0, bool $isCaChe = false): array
  20. {
  21. $callable = function () use ($config_name, $limit) {
  22. try {
  23. $data = SystemGroupData::getGroupData($config_name, $limit);
  24. if (is_object($data))
  25. $data = $data->toArray();
  26. return $data;
  27. } catch (\Exception $e) {
  28. return [];
  29. }
  30. };
  31. try {
  32. $cacheName = $limit ? "group_data_{$config_name}_{$limit}" : "data_{$config_name}";
  33. if ($isCaChe)
  34. return $callable();
  35. return CacheService::get($cacheName, $callable);
  36. } catch (\Throwable $e) {
  37. return $callable();
  38. }
  39. }
  40. /**
  41. * 获取单个值
  42. * @param string $config_name 配置名称
  43. * @param int $limit 截取多少条
  44. * @param bool $isCaChe 是否读取缓存
  45. * @return array
  46. */
  47. public static function getData(string $config_name, int $limit = 0, bool $isCaChe = false): array
  48. {
  49. $callable = function () use ($config_name, $limit) {
  50. try {
  51. $data = SystemGroupData::getAllValue($config_name, $limit);
  52. if (is_object($data))
  53. $data = $data->toArray();
  54. return $data;
  55. } catch (\Exception $e) {
  56. return [];
  57. }
  58. };
  59. try {
  60. $cacheName = $limit ? "data_{$config_name}_{$limit}" : "data_{$config_name}";
  61. if ($isCaChe)
  62. return $callable();
  63. return CacheService::get($cacheName, $callable);
  64. } catch (\Throwable $e) {
  65. return $callable();
  66. }
  67. }
  68. /**
  69. * 根据id 获取单个值
  70. * @param int $id
  71. * @param bool $isCaChe 是否读取缓存
  72. * @return array
  73. */
  74. public static function getDataNumber(int $id, bool $isCaChe = false): array
  75. {
  76. $callable = function () use ($id) {
  77. try {
  78. $data = SystemGroupData::getDateValue($id);
  79. if (is_object($data))
  80. $data = $data->toArray();
  81. return $data;
  82. } catch (\Exception $e) {
  83. return [];
  84. }
  85. };
  86. try {
  87. $cacheName = "data_number_{$id}";
  88. if ($isCaChe)
  89. return $callable();
  90. return CacheService::get($cacheName, $callable);
  91. } catch (\Throwable $e) {
  92. return $callable();
  93. }
  94. }
  95. }