Dict.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Cache;
  7. class Dict extends Model
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $table = 'base_dicts';
  13. protected $guarded = [];
  14. /**
  15. * The attributes excluded from the model's JSON form.
  16. *
  17. * @var array
  18. */
  19. protected $hidden = [];
  20. public static function checkCodeIsUnique($code, $ignore_id = 0)
  21. {
  22. return self::query()->where('code', $code)->where('id', '<>', $ignore_id)->exists();
  23. }
  24. /**
  25. * 通过code获取配置项
  26. * @param $code
  27. * @return array|mixed
  28. */
  29. public static function byCodeGetDicts($code)
  30. {
  31. return Cache::tags('model')->remember('model:Dict:byCodeGetDicts:' . $code, Carbon::now()->addDays(1), function () use ($code) {
  32. $id = self::query()->where('code', $code)->value('id');
  33. if (!$id) {
  34. return [];
  35. }
  36. return DictDetail::query()->where('dict_id', $id)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['name', 'value', 'id'])->get()->toArray();
  37. });
  38. }
  39. /**
  40. * 根据code获取相应的选项
  41. * @param $code
  42. * @param array $ids
  43. * @return array|mixed
  44. */
  45. public static function byCodeAndIdsGetDicts($code, array $ids)
  46. {
  47. return Cache::tags('model')->remember('model:Dict:byCodeAndIdsGetDicts:' . $code . ':' . arr2str($ids, '_'), Carbon::now()->addDays(1), function () use ($code, $ids) {
  48. $id = self::query()->where('code', $code)->value('id');
  49. if (!$id) {
  50. return [];
  51. }
  52. return DictDetail::query()->where('dict_id', $id)->whereIn('value', $ids)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['name', 'value'])->get();
  53. });
  54. }
  55. /**
  56. * 根据code获取相应的选项名
  57. * @param $code
  58. * @param $id
  59. * @return array|mixed
  60. */
  61. public static function byCodeAndIdGetDict($code, $id)
  62. {
  63. return Cache::tags('model')->remember('model:Dict:byCodeAndIdGetDict:' . $code . ':' . $id, Carbon::now()->addDays(1), function () use ($code, $id) {
  64. $did = self::query()->where('code', $code)->value('id');
  65. if (!$did) {
  66. return '';
  67. }
  68. return DictDetail::query()->where('dict_id', $did)->where('value', $id)->where('status', ModelStatusEnum::OK)->value('name');
  69. });
  70. }
  71. /**
  72. * 根据name获取value
  73. * @param $code
  74. * @param $name
  75. * @return array|mixed
  76. */
  77. public static function byCodeAndNameGetValue($code, $name)
  78. {
  79. return Cache::tags('model')->remember('model:Dict:byCodeAndNameGetValue:' . $code . ':' . $name, Carbon::now()->addDays(1), function () use ($code, $name) {
  80. $did = self::query()->where('code', $code)->value('id');
  81. if (!$did) {
  82. return '';
  83. }
  84. return DictDetail::query()->where('dict_id', $did)->where('name', $name)->where('status', ModelStatusEnum::OK)->value('value');
  85. });
  86. }
  87. /**
  88. * 查找并创建
  89. * @param $code
  90. * @param $name
  91. * @return
  92. */
  93. public static function firstOrCreateItem($code, $name)
  94. {
  95. $did = self::query()->where('code', $code)->value('id') ?? 0;
  96. if (!$did) return false;
  97. $re = DictDetail::query()->where('dict_id', $did)->where('name', $name)->where('status', ModelStatusEnum::OK)->first();
  98. if ($re) {
  99. return $re;
  100. }
  101. return DictDetail::query()->create([
  102. 'name' => $name,
  103. 'dict_id' => $did,
  104. 'value' => $name,
  105. ]);
  106. }
  107. public function detail()
  108. {
  109. return $this->hasMany(DictDetail::class, 'dict_id', 'id');
  110. }
  111. }