// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\MemberModel; use App\Models\MemberPaymentModel; use App\Services\BaseService; /** * 会员收款方式-服务类 * Class MemberPaymentService * @package App\Services\Api */ class MemberPaymentService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @since 2020/11/10 * MemberPaymentService constructor. */ public function __construct() { $this->model = new MemberPaymentModel(); $this->memberModel = new MemberModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取选项列表 * @param $userId * @return mixed */ public function getOptionList($userId, $type = 1, $pageSize = 15) { $pageSize = $type==1? 0 : $pageSize; $list = $this->model->where(function ($query) use ($userId, $type) { if ($type == 1) { $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))'); } else { $query->where(['user_id' => $userId,'mark'=>1])->where('status','>',0); } }) ->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') ->paginate($pageSize>0? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : ''; $item['realname_text'] = $item['real_name'] ? format_realname($item['real_name']) : ''; $item['account_text'] = $item['account'] ? format_account($item['account']) : ''; $item['status'] = $item['status'] ==1? true : false; $item['bank_card_text'] = $item['bank_card'] ? '*' . substr($item['bank_card'], -4, 4) : ''; if ($item['type'] == 1) { $item['show_text'] = $item['bank_name'] . ' ' . $item['realname_text'] . ' ' . $item['bank_card_text']; } else { $item['show_text'] = $item['realname_text'] . ' ' . $item['account_text']; } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 是否已经设置了收款方式 * @param $userId * @return mixed */ public function checkHasByUser($userId) { return $this->model->where(['user_id' => $userId, 'status' => 1,'mark'=>1])->count('id'); } /** * 更新状态 * @param $id * @param $status * @param $userId * @return mixed */ public function setPayment($id, $status, $userId){ $status = $status? 1 : 2; return $this->model->where(['user_id' => $userId, 'id' => $id,'mark'=>1])->update(['status'=> $status]); } /** * 删除 * @param $id * @param $userId * @return mixed */ public function delPayment($id, $userId){ // 永久删除 $this->model->where(['user_id' => $userId, 'id' => $id,'status'=> 0,'mark'=>1]) ->where('update_time','<', time() - 2 * 86400) ->delete(); return $this->model->where(['user_id' => $userId, 'id' => $id])->update(['status'=> 0,'mark'=>1]); } /** * 详情 * @param $id * @return mixed */ public function getInfo($id) { $info = $this->model->where(['id' => $id,'mark'=>1])->where('status','>', 0)->first(); if($info){ $info['logo'] = $info['logo']? get_image_url($info['logo']) : ''; $info['status'] = $info['status']==1? true : false; } return $info; } /** * 获取默认的收款方式,若没有则返回其他一个 * @param $userId * @return array */ public function getPayment($userId) { $info = $this->model->where(['user_id' => $userId,'mark'=>1,'status'=> 1]) ->whereRaw('(trade_num<=0 or trade_quota <= 0 or (trade_num-used_num>0 and trade_quota-used_quota>0))') ->select(['id','type','logo','real_name','bank_name','branch_name','bank_card','qrcode','account']) ->orderBy('is_default','asc') ->orderBy('id','desc') ->first(); $info = $info? $info->toArray() : []; if($info){ $info['logo'] = $info['logo']? get_image_url($info['logo']) : ''; } return $info; } /** * 编辑或新增 * @param $id * @param $params * @return mixed */ public function saveData($id, $params) { $data = [ 'user_id'=> isset($params['user_id'])? $params['user_id'] : 0, 'real_name'=> isset($params['real_name'])? $params['real_name'] : '', 'bank_name'=> isset($params['bank_name'])? $params['bank_name'] : '', 'branch_name'=> isset($params['branch_name'])? $params['branch_name'] : '', 'bank_card'=> isset($params['bank_card'])? $params['bank_card'] : '', 'trade_quota'=> isset($params['trade_quota'])? intval($params['trade_quota']) : 0, 'trade_num'=> isset($params['trade_num'])? intval($params['trade_num']) : 0, 'status'=> isset($params['status']) && $params['status'] == 1? intval($params['status']) : 2, 'mark'=> 1, ]; if(isset($params['logo']) && strpos($params['logo'],'http')===false){ $data['logo'] = $params['logo']; } if($id>0 && $this->model->where(['id'=> $id,'mark'=>1])->value('id')){ return $this->model->where(['id'=> $id])->update($data); }else{ return $this->model->insert($data); } } }