MemberPaymentService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\MemberModel;
  13. use App\Models\MemberPaymentModel;
  14. use App\Services\BaseService;
  15. /**
  16. * 会员收款方式-服务类
  17. * Class MemberPaymentService
  18. * @package App\Services\Api
  19. */
  20. class MemberPaymentService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @since 2020/11/10
  27. * MemberPaymentService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new MemberPaymentModel();
  32. $this->memberModel = new MemberModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 获取选项列表
  47. * @param $userId
  48. * @return mixed
  49. */
  50. public function getOptionList($userId, $type = 1, $pageSize = 15)
  51. {
  52. $pageSize = $type==1? 0 : $pageSize;
  53. $list = $this->model->where(function ($query) use ($userId, $type) {
  54. if ($type == 1) {
  55. $query->where(['user_id' => $userId, 'status' => 1,'mark'=>1])->whereRaw('(trade_num<=0 or trade_quota <= 0 or (trade_num-used_num>0 and trade_quota-used_quota>0))');
  56. } else {
  57. $query->where(['user_id' => $userId,'mark'=>1])->where('status','>',0);
  58. }
  59. })
  60. ->selectRaw('id,type,real_name,logo,bank_name,branch_name,account,is_default,bank_card,trade_num,used_num,trade_quota,used_quota,(trade_quota - used_quota) as credit,status')
  61. ->paginate($pageSize>0? $pageSize : 9999999);
  62. $list = $list? $list->toArray() :[];
  63. if($list){
  64. foreach($list['data'] as &$item){
  65. $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
  66. $item['realname_text'] = $item['real_name'] ? format_realname($item['real_name']) : '';
  67. $item['account_text'] = $item['account'] ? format_account($item['account']) : '';
  68. $item['status'] = $item['status'] ==1? true : false;
  69. $item['bank_card_text'] = $item['bank_card'] ? '*' . substr($item['bank_card'], -4, 4) : '';
  70. if ($item['type'] == 1) {
  71. $item['show_text'] = $item['bank_name'] . ' ' . $item['realname_text'] . ' ' . $item['bank_card_text'];
  72. } else {
  73. $item['show_text'] = $item['realname_text'] . ' ' . $item['account_text'];
  74. }
  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. * @param $userId
  86. * @return mixed
  87. */
  88. public function checkHasByUser($userId)
  89. {
  90. return $this->model->where(['user_id' => $userId, 'status' => 1,'mark'=>1])->count('id');
  91. }
  92. /**
  93. * 更新状态
  94. * @param $id
  95. * @param $status
  96. * @param $userId
  97. * @return mixed
  98. */
  99. public function setPayment($id, $status, $userId){
  100. $status = $status? 1 : 2;
  101. return $this->model->where(['user_id' => $userId, 'id' => $id,'mark'=>1])->update(['status'=> $status]);
  102. }
  103. /**
  104. * 删除
  105. * @param $id
  106. * @param $userId
  107. * @return mixed
  108. */
  109. public function delPayment($id, $userId){
  110. // 永久删除
  111. $this->model->where(['user_id' => $userId, 'id' => $id,'status'=> 0,'mark'=>1])
  112. ->where('update_time','<', time() - 2 * 86400)
  113. ->delete();
  114. return $this->model->where(['user_id' => $userId, 'id' => $id])->update(['status'=> 0,'mark'=>1]);
  115. }
  116. /**
  117. * 详情
  118. * @param $id
  119. * @return mixed
  120. */
  121. public function getInfo($id)
  122. {
  123. $info = $this->model->where(['id' => $id,'mark'=>1])->where('status','>', 0)->first();
  124. if($info){
  125. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  126. $info['status'] = $info['status']==1? true : false;
  127. }
  128. return $info;
  129. }
  130. /**
  131. * 获取默认的收款方式,若没有则返回其他一个
  132. * @param $userId
  133. * @return array
  134. */
  135. public function getPayment($userId)
  136. {
  137. $info = $this->model->where(['user_id' => $userId,'mark'=>1,'status'=> 1])
  138. ->whereRaw('(trade_num<=0 or trade_quota <= 0 or (trade_num-used_num>0 and trade_quota-used_quota>0))')
  139. ->select(['id','type','logo','real_name','bank_name','branch_name','bank_card','qrcode','account'])
  140. ->orderBy('is_default','asc')
  141. ->orderBy('id','desc')
  142. ->first();
  143. $info = $info? $info->toArray() : [];
  144. if($info){
  145. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  146. }
  147. return $info;
  148. }
  149. /**
  150. * 编辑或新增
  151. * @param $id
  152. * @param $params
  153. * @return mixed
  154. */
  155. public function saveData($id, $params)
  156. {
  157. $data = [
  158. 'user_id'=> isset($params['user_id'])? $params['user_id'] : 0,
  159. 'real_name'=> isset($params['real_name'])? $params['real_name'] : '',
  160. 'bank_name'=> isset($params['bank_name'])? $params['bank_name'] : '',
  161. 'branch_name'=> isset($params['branch_name'])? $params['branch_name'] : '',
  162. 'bank_card'=> isset($params['bank_card'])? $params['bank_card'] : '',
  163. 'trade_quota'=> isset($params['trade_quota'])? intval($params['trade_quota']) : 0,
  164. 'trade_num'=> isset($params['trade_num'])? intval($params['trade_num']) : 0,
  165. 'status'=> isset($params['status']) && $params['status'] == 1? intval($params['status']) : 2,
  166. 'mark'=> 1,
  167. ];
  168. if(isset($params['logo']) && strpos($params['logo'],'http')===false){
  169. $data['logo'] = $params['logo'];
  170. }
  171. if($id>0 && $this->model->where(['id'=> $id,'mark'=>1])->value('id')){
  172. return $this->model->where(['id'=> $id])->update($data);
  173. }else{
  174. return $this->model->insert($data);
  175. }
  176. }
  177. }