MemberBankService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberBankModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 用户银行卡管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Common
  20. */
  21. class MemberBankService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * MemberBankService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new MemberBankModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表数据
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1,'a.status'=>1];
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. if ($status > 0) {
  57. $where['a.status'] = $status;
  58. }
  59. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  60. if ($userId > 0) {
  61. $where['a.user_id'] = $userId;
  62. }
  63. $type = isset($params['type']) ? $params['type'] : 0;
  64. if ($type > 0) {
  65. $where['a.type'] = $type;
  66. }
  67. $list = $this->model->from('member_banks as a')
  68. ->where($where)
  69. ->select(['a.*'])
  70. ->orderBy('a.create_time', 'desc')
  71. ->orderBy('a.id', 'desc')
  72. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  73. $list = $list ? $list->toArray() : [];
  74. return [
  75. 'pageSize' => $pageSize,
  76. 'total' => isset($list['total']) ? $list['total'] : 0,
  77. 'list' => isset($list['data']) ? $list['data'] : []
  78. ];
  79. }
  80. /**
  81. * 选项列表
  82. * @param $userId
  83. * @return array|mixed
  84. */
  85. public function options($userId, $userType=1)
  86. {
  87. $cacheKey = "caches:members:banks:{$userId}_{$userType}";
  88. $datas = RedisService::get($cacheKey);
  89. if($datas){
  90. return $datas;
  91. }
  92. $datas = $this->model->where(['user_id'=> $userId,'user_type'=>$userType,'status'=> 1,'mark'=>1])
  93. ->select(['id','realname','bank_name','bank_card','status'])
  94. ->get();
  95. $datas = $datas? $datas->toArrayu() :[];
  96. if($datas){
  97. RedisService::set($cacheKey, $datas, 7200);
  98. }
  99. return $datas;
  100. }
  101. /**
  102. * 保存数据
  103. * @param $userId
  104. * @param $params
  105. * @return mixed
  106. */
  107. public function saveData($userId, $params)
  108. {
  109. $id = isset($params['id']) ? $params['id'] : 0;
  110. $type = isset($params['type']) && $params['type']? $params['type'] : 2;
  111. $data = [
  112. 'user_id'=> $userId,
  113. 'type'=> $type,
  114. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  115. 'account'=> isset($params['account'])? $params['account'] : '',
  116. 'account_name'=> isset($params['account_name'])? $params['account_name'] : '',
  117. 'account_remark'=> isset($params['account_remark'])? $params['account_remark'] : '',
  118. 'status'=> isset($params['status']) && $params['status']? $params['status'] : 1,
  119. 'update_time'=> time(),
  120. 'mark'=> 1,
  121. ];
  122. if($id && $this->model->where(['id'=> $id])->value('id')){
  123. $this->model->where(['id'=> $id])->update($data);
  124. $this->error = $id? 1008 : 1027;
  125. return true;
  126. }else{
  127. $data['create_time'] = time();
  128. $this->error = 1027;
  129. return $this->model->insertGetId($data);
  130. }
  131. }
  132. /**
  133. * @return array|false
  134. */
  135. public function delete()
  136. {
  137. // 参数
  138. $id = request()->post('id');
  139. if (empty($id)) {
  140. $this->error = 2014;
  141. return false;
  142. }
  143. $this->error = 1002;
  144. $this->model->where(['mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  145. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  146. }
  147. }