MemberBankService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Common;
  12. use App\Models\MemberAddressModel;
  13. use App\Models\MemberBankModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 用户银行卡管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class MemberBankService
  21. * @package App\Services\Common
  22. */
  23. class MemberBankService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * MemberBankService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new MemberBankModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 列表数据
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15)
  55. {
  56. $where = ['a.mark' => 1];
  57. $status = isset($params['status']) ? $params['status'] : 0;
  58. if ($status > 0) {
  59. $where['a.status'] = $status;
  60. }
  61. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  62. if ($userId > 0) {
  63. $where['a.user_id'] = $userId;
  64. }
  65. $list = $this->model->from('member_bank as a')
  66. ->where($where)
  67. ->select(['a.*'])
  68. ->orderBy('a.is_default', 'asc')
  69. ->orderBy('a.id', 'desc')
  70. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  71. $list = $list ? $list->toArray() : [];
  72. if ($list) {
  73. foreach ($list['data'] as &$item) {
  74. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  75. }
  76. }
  77. return [
  78. 'pageSize' => $pageSize,
  79. 'total' => isset($list['total']) ? $list['total'] : 0,
  80. 'list' => isset($list['data']) ? $list['data'] : []
  81. ];
  82. }
  83. /**
  84. * 添加编辑
  85. * @return array
  86. * @since 2020/11/11
  87. * @author laravel开发员
  88. */
  89. public function edit()
  90. {
  91. // 请求参数
  92. $data = request()->all();
  93. return parent::edit($data); // TODO: Change the autogenerated stub
  94. }
  95. /**
  96. * 保存数据
  97. * @param $userId
  98. * @return mixed
  99. */
  100. public function saveData($userId)
  101. {
  102. $params = request()->all();
  103. $id = isset($params['id']) ? $params['id'] : 0;
  104. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  105. $data = [
  106. 'user_id' => $userId,
  107. 'realname' => isset($params['realname']) ? $params['realname'] : '',
  108. 'bank_name' => isset($params['bank_name']) ? $params['bank_name'] : '',
  109. 'bank_num' => isset($params['bank_num']) ? $params['bank_num'] : '',
  110. 'remark' => isset($params['remark']) ? $params['remark'] : '',
  111. 'is_default' => $isDefault,
  112. 'status' => isset($params['status']) ? $params['status'] : 1,
  113. 'update_time' => time(),
  114. 'mark' => 1,
  115. ];
  116. if($isDefault==1){
  117. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  118. }
  119. RedisService::keyDel("caches:banks:{$userId}");
  120. if(!$id){
  121. $data['create_time'] = time();
  122. return $this->model->insertGetId($data);
  123. }else{
  124. return $this->model->where(['id'=> $id])->update($data);
  125. }
  126. }
  127. /**
  128. * @param $userId
  129. * @return array|mixed
  130. */
  131. public function getBindInfo($userId)
  132. {
  133. $cacheKey = "caches:banks:{$userId}";
  134. $info = RedisService::get($cacheKey);
  135. if($info){
  136. return $info;
  137. }
  138. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  139. ->orderBy('is_default','asc')
  140. ->orderBy('id','desc')
  141. ->first();
  142. $info = $info? $info->toArray() : [];
  143. if($info){
  144. RedisService::set($cacheKey, $info, rand(5, 10));
  145. }
  146. return $info;
  147. }
  148. }