MemberBankService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $list = $this->model->from('member_banks as a')
  64. ->where($where)
  65. ->select(['a.*'])
  66. ->orderBy('a.create_time', 'desc')
  67. ->orderBy('a.id', 'desc')
  68. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  69. $list = $list ? $list->toArray() : [];
  70. return [
  71. 'pageSize' => $pageSize,
  72. 'total' => isset($list['total']) ? $list['total'] : 0,
  73. 'list' => isset($list['data']) ? $list['data'] : []
  74. ];
  75. }
  76. /**
  77. * 选项列表
  78. * @param $userId
  79. * @return array|mixed
  80. */
  81. public function options($userId, $userType=1)
  82. {
  83. $cacheKey = "caches:members:banks:{$userId}_{$userType}";
  84. $datas = RedisService::get($cacheKey);
  85. if($datas){
  86. return $datas;
  87. }
  88. $datas = $this->model->where(['user_id'=> $userId,'user_type'=>$userType,'status'=> 1,'mark'=>1])
  89. ->select(['id','realname','bank_name','bank_card','status'])
  90. ->get();
  91. $datas = $datas? $datas->toArrayu() :[];
  92. if($datas){
  93. RedisService::set($cacheKey, $datas, 7200);
  94. }
  95. return $datas;
  96. }
  97. /**
  98. * 保存数据
  99. * @param $userId
  100. * @param $params
  101. * @return mixed
  102. */
  103. public function saveData($userId, $params)
  104. {
  105. $id = isset($params['id']) ? $params['id'] : 0;
  106. $type = isset($params['type']) && $params['type']? $params['type'] : 2;
  107. $data = [
  108. 'user_id'=> $userId,
  109. 'type'=> $userId,
  110. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  111. 'account'=> isset($params['account'])? $params['account'] : '',
  112. 'account_name'=> isset($params['account_name'])? $params['account_name'] : '',
  113. 'account_remark'=> isset($params['account_remark'])? $params['account_remark'] : '',
  114. 'status'=> isset($params['status']) && $params['status']? $params['status'] : 1,
  115. 'update_time'=> time(),
  116. 'mark'=> 1,
  117. ];
  118. if($id && $this->model->where(['id'=> $id])->value('id')){
  119. $this->model->where(['id'=> $id])->update($data);
  120. $this->error = $id? 1008 : 1027;
  121. return true;
  122. }else{
  123. $data['create_time'] = time();
  124. $this->error = 1027;
  125. return $this->model->insertGetId($data);
  126. }
  127. }
  128. /**
  129. * @return array|false
  130. */
  131. public function delete()
  132. {
  133. // 参数
  134. $id = request()->post('id');
  135. if (empty($id)) {
  136. $this->error = 2014;
  137. return false;
  138. }
  139. $this->error = 1002;
  140. $this->model->where(['mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  141. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  142. }
  143. }