AddressService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ->paginate($limit);
  73. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  74. }
  75. /**
  76. * @description:
  77. * @param {*} $params
  78. * @return {*}
  79. */
  80. public static function submit($params = [])
  81. {
  82. $result = false;
  83. $msg['code'] = self::NOT;
  84. $msg['msg'] = '';
  85. // 2. 判断是否是更新
  86. if (!empty($params['id'])) {
  87. // 更新
  88. $info = self::findOne(['id'=>$params['id']] );
  89. if (!$info) {
  90. $msg['msg'] = '菜单不存在!';
  91. }else{
  92. $result = $info->update($params);
  93. }
  94. } else {
  95. // 创建
  96. $result = $info = self::model()::create($params);
  97. }
  98. if($result){
  99. $msg['code'] = self::YES;
  100. $msg['msg'] = '设置成功';
  101. }else{
  102. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  103. }
  104. return $msg;
  105. }
  106. }