AddressService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Address;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. /**
  9. * 菜单
  10. */
  11. class AddressService extends BaseService
  12. {
  13. /**
  14. * @description: 模型
  15. * @return {string}
  16. */
  17. public static function model(): string
  18. {
  19. return Address::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['id']) && !empty($search['id'])) {
  38. $where[] = ['id', '=', $search['id']];
  39. }
  40. if (isset($search['member_id']) && !empty($search['member_id'])) {
  41. $where[] = ['member_id', '=', $search['member_id']];
  42. }
  43. return $where;
  44. }
  45. /**
  46. * @description: 查询单条数据
  47. * @param array $search
  48. * @return \App\Models\Coin|null
  49. */
  50. public static function findOne(array $search): ?Address
  51. {
  52. return self::model()::where(self::getWhere($search))->first();
  53. }
  54. /**
  55. * @description: 查询所有数据
  56. * @param array $search
  57. * @return \Illuminate\Database\Eloquent\Collection
  58. */
  59. public static function findAll(array $search = [])
  60. {
  61. return self::model()::where(self::getWhere($search))->get();
  62. }
  63. /**
  64. * @description: 分页查询
  65. * @param array $search
  66. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  67. */
  68. public static function paginate(array $search = [])
  69. {
  70. $limit = isset($search['limit']) ? $search['limit'] : 15;
  71. $paginator = self::model()::where(self::getWhere($search))
  72. ->orderBy("sort", 'asc')
  73. ->paginate($limit);
  74. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  75. }
  76. /**
  77. * @description:
  78. * @param {*} $params
  79. * @return {*}
  80. */
  81. public static function submit($params = [])
  82. {
  83. $result = false;
  84. $msg['code'] = self::NOT;
  85. $msg['msg'] = '';
  86. // 2. 判断是否是更新
  87. if (!empty($params['id'])) {
  88. // 更新
  89. $info = self::findOne(['id'=>$params['id']] );
  90. if (!$info) {
  91. $msg['msg'] = '菜单不存在!';
  92. }else{
  93. $result = $info->update($params);
  94. }
  95. } else {
  96. // 创建
  97. $result = $info = self::model()::create($params);
  98. }
  99. if($result){
  100. $msg['code'] = self::YES;
  101. $msg['msg'] = '设置成功';
  102. }else{
  103. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  104. }
  105. return $msg;
  106. }
  107. }