CoinService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Coin;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. /**
  9. * 平台支持的虚拟币
  10. */
  11. class CoinService extends BaseService
  12. {
  13. /**
  14. * @description: 模型
  15. * @return {string}
  16. */
  17. public static function model() :string
  18. {
  19. return Coin::class;
  20. }
  21. /**
  22. * @description: 枚举
  23. * @return {*}
  24. */
  25. public static function enum() :string
  26. {
  27. return '';
  28. }
  29. /**
  30. * @description: 获取查询条件
  31. * @param {array} $search 查询内容
  32. * @return {array}
  33. */
  34. public static function getWhere(array $search = []) :array
  35. {
  36. $where = [];
  37. if(isset($search['coin']) && !empty($search['coin'])){
  38. $where[] = ['coin', '=', $search['coin']];
  39. }
  40. if(isset($search['net']) && !empty($search['net'])){
  41. $where[] = ['net', '=', $search['net']];
  42. }
  43. if(isset($search['address']) && !empty($search['address'])){
  44. $where[] = ['address', '=', $search['address']];
  45. }
  46. if(isset($search['id']) && !empty($search['id'])){
  47. $where[] = ['id', '=', $search['id']];
  48. }
  49. return $where;
  50. }
  51. /**
  52. * @description: 查询单条数据
  53. * @param array $search
  54. * @return \App\Models\Coin|null
  55. */
  56. public static function findOne(array $search): ?Coin
  57. {
  58. return self::model()::where(self::getWhere($search))->first();
  59. }
  60. /**
  61. * @description: 查询所有数据
  62. * @param array $search
  63. * @return \Illuminate\Database\Eloquent\Collection
  64. */
  65. public static function findAll(array $search = [])
  66. {
  67. return self::model()::where(self::getWhere($search))->get();
  68. }
  69. /**
  70. * @description: 分页查询
  71. * @param array $search
  72. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  73. */
  74. public static function paginate(array $search = [])
  75. {
  76. $limit = isset($search['limit'])?$search['limit']:15;
  77. $paginator = self::model()::where(self::getWhere($search))->paginate($limit);
  78. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  79. }
  80. }