UserBankCardService.php 3.1 KB

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