Dict.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 function detail()
  21. {
  22. return $this->hasMany(DictDetail::class, 'dict_id', 'id');
  23. }
  24. public static function checkCodeIsUnique($code, $ignore_id = 0)
  25. {
  26. return self::query()->where('code', $code)->where('id', '<>', $ignore_id)->exists();
  27. }
  28. /**
  29. * 通过code获取配置项
  30. * @param $code
  31. * @return array|mixed
  32. */
  33. public static function byCodeGetDicts($code)
  34. {
  35. return Cache::tags('model')->remember('model:Dict:byCodeGetDicts:' . $code, Carbon::now()->addDays(1), function () use ($code) {
  36. $id = self::query()->where('code', $code)->value('id');
  37. if (!$id) {
  38. return [];
  39. }
  40. return DictDetail::query()->where('dict_id', $id)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['name', 'value', 'id'])->get()->toArray();
  41. });
  42. }
  43. /**
  44. * 根据code获取相应的选项
  45. * @param $code
  46. * @param array $ids
  47. * @return array|mixed
  48. */
  49. public static function byCodeAndIdsGetDicts($code, array $ids)
  50. {
  51. return Cache::tags('model')->remember('model:Dict:byCodeAndIdsGetDicts:' . $code . ':' . arr2str($ids, '_'), Carbon::now()->addDays(1), function () use ($code, $ids) {
  52. $id = self::query()->where('code', $code)->value('id');
  53. if (!$id) {
  54. return [];
  55. }
  56. return DictDetail::query()->where('dict_id', $id)->whereIn('value', $ids)->where('status', ModelStatusEnum::OK)->orderByDesc('sort')->select(['name', 'value'])->get();
  57. });
  58. }
  59. /**
  60. * 根据code获取相应的选项名
  61. * @param $code
  62. * @param $id
  63. * @return array|mixed
  64. */
  65. public static function byCodeAndIdGetDict($code, $id)
  66. {
  67. return Cache::tags('model')->remember('model:Dict:byCodeAndIdGetDict:' . $code . ':' . $id, Carbon::now()->addDays(1), function () use ($code, $id) {
  68. $did = self::query()->where('code', $code)->value('id');
  69. if (!$did) {
  70. return '';
  71. }
  72. return DictDetail::query()->where('dict_id', $did)->where('value', $id)->where('status', ModelStatusEnum::OK)->value('name');
  73. });
  74. }
  75. /**
  76. * 根据name获取value
  77. * @param $code
  78. * @param $name
  79. * @return array|mixed
  80. */
  81. public static function byCodeAndNameGetValue($code, $name)
  82. {
  83. return Cache::tags('model')->remember('model:Dict:byCodeAndNameGetValue:' . $code . ':' . $name, Carbon::now()->addDays(1), function () use ($code, $name) {
  84. $did = self::query()->where('code', $code)->value('id');
  85. if (!$did) {
  86. return '';
  87. }
  88. return DictDetail::query()->where('dict_id', $did)->where('name', $name)->where('status', ModelStatusEnum::OK)->value('value');
  89. });
  90. }
  91. /**
  92. * 查找并创建
  93. * @param $code
  94. * @param $name
  95. * @return
  96. */
  97. public static function firstOrCreateItem($code, $name)
  98. {
  99. $did = self::query()->where('code', $code)->value('id') ?? 0;
  100. if (!$did) return false;
  101. $re = DictDetail::query()->where('dict_id', $did)->where('name', $name)->where('status', ModelStatusEnum::OK)->first();
  102. if ($re) {
  103. return $re;
  104. }
  105. return DictDetail::query()->create([
  106. 'name' => $name,
  107. 'dict_id' => $did,
  108. 'value' => $name,
  109. ]);
  110. }
  111. }