UserMembershipLevelService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\UserMembershipLevel;
  5. class UserMembershipLevelService extends BaseService
  6. {
  7. public static $MODEL= UserMembershipLevel::class;
  8. /**
  9. * @description: 获取查询条件
  10. * @param {array} $search 查询内容
  11. * @return {array}
  12. */
  13. public static function getWhere(array $search = []): array
  14. {
  15. $where = [];
  16. if (isset($search['id']) && $search['id'] !== '') {
  17. $where[] = ['id', '=', $search['id']];
  18. }
  19. if (isset($search['code']) && $search['code'] !== '') {
  20. $where[] = ['code', '=', $search['code']];
  21. }
  22. if (isset($search['name']) && $search['name'] !== '') {
  23. $where[] = ['name', 'like', '%' . $search['name'] . '%'];
  24. }
  25. return $where;
  26. }
  27. /**
  28. * @description: 查询单条数据
  29. * @param array $search
  30. * @return
  31. */
  32. public static function findOne(array $search)
  33. {
  34. return static::model()::where(static::getWhere($search))->first();
  35. }
  36. /**
  37. * @description: 查询所有数据
  38. * @param array $search
  39. * @return \Illuminate\Database\Eloquent\Collection
  40. */
  41. public static function findAll(array $search = [])
  42. {
  43. return static::model()::where(static::getWhere($search))->get();
  44. }
  45. /**
  46. * @description: 分页查询
  47. * @param array $search
  48. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  49. */
  50. public static function paginate(array $search = [])
  51. {
  52. $limit = isset($search['limit']) ? $search['limit'] : 15;
  53. $paginator = static::model()::where(static::getWhere($search))
  54. ->orderBy('updated_at', 'desc')
  55. ->paginate($limit);
  56. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  57. }
  58. /**
  59. * @description:
  60. * @param {*} $params
  61. * @return {*}
  62. */
  63. public static function submit($params = [])
  64. {
  65. $result = false;
  66. $msg['code'] = self::NOT;
  67. $msg['msg'] = '';
  68. // 2. 判断是否是更新
  69. if (!empty($params['id'])) {
  70. // 更新
  71. $info = self::findOne(['id'=>$params['id']] );
  72. if (!$info) {
  73. $msg['msg'] = '数据不存在!';
  74. }else{
  75. $result = $info->update($params);
  76. $id = $params['id'];
  77. }
  78. } else {
  79. // 创建
  80. $result = $info = self::model()::create($params);
  81. $id = $result->id;
  82. }
  83. if($result){
  84. $msg['code'] = self::YES;
  85. $msg['msg'] = '设置成功';
  86. }else{
  87. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  88. }
  89. return $msg;
  90. }
  91. }