| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountLogModel;
- use App\Models\ActionLogModel;
- use App\Models\BalanceLogModel;
- use App\Models\MemberModel;
- use App\Models\MessageModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 余额管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class BalanceLogService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AccountService constructor.
- */
- public function __construct()
- {
- $this->model = new BalanceLogModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $query = $this->getQuery($params);
- $model = clone $query;
- $counts = [
- 'count'=> $model->count('a.id'),
- 'total'=> $model->sum('a.money'),
- ];
- $list = $query->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->orderBy('a.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- $item['pay_img'] = $item['pay_img']? get_image_url($item['pay_img']) : '';
- $item['member'] = isset($item['member'])? $item['member'] : [];
- }
- // 消息已读
- MessageModel::where(['is_read'=>2,'type'=>6,'mark'=>1])->update(['is_read'=>1,'update_time'=>time()]);
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'counts'=>$counts,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $type = isset($params['type'])? $params['type'] : 0;
- if($type>0){
- $where['a.type'] = $type;
- }
- return $this->model->with(['member'])->from("balance_logs as a")
- ->leftJoin('member as b','b.id','=','a.user_id')
- ->where($where)
- ->where(function ($query) use($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- $userId = isset($params['user_id'])? $params['user_id'] : 0;
- if($userId){
- $query->where('a.user_id',$userId);
- }
- if ($keyword) {
- $query->where(function($query) use($keyword){
- $query->where('b.nickname','like',"%{$keyword}%")
- ->orWhere('b.mobile','like',"%{$keyword}%");
- });
- }
- $orderNo = isset($params['order_no'])? trim($params['order_no']) : '';
- if($orderNo){
- $query->where(function($query) use($orderNo){
- $query->where('a.order_no','like',"%{$orderNo}%");
- });
- }
- $status = isset($params['status'])? $params['status'] : -1;
- if($status==0){
- $query->whereIn('a.status',[2,3,4]);
- }else if($status>0){
- $query->where('a.status', $status);
- }
- })
- ->where(function ($query) use($params){
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0])? $date[0] : '';
- $end = isset($date[1])? $date[1] : '';
- $end = $start>=$end? '' : $end;
- if ($start) {
- $query->where('a.create_time','>=', strtotime($start));
- }
- if($end){
- $query->where('a.create_time','<=', strtotime($end));
- }
- });
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 审核
- * @param $adminId
- * @param $params
- * @return array|false
- */
- public function confirm($adminId, $params)
- {
- $id = isset($params['id'])? intval($params['id']) : 0;
- $status = isset($params['status'])? intval($params['status']) : 0;
- $payStatus = isset($params['pay_status'])? intval($params['pay_status']) : 0;
- $confirmRemark = isset($params['confirm_remark'])? trim($params['confirm_remark']) : '';
- $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
- $payImg = $payImg? get_image_path($payImg) : '';
- $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
- $userInfo = isset($info['member'])? $info['member'] : [];
- $orderStatus = isset($info['status'])? $info['status'] : 0;
- $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
- $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
- $money = isset($info['money'])? $info['money'] : 0;
- if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
- $this->error = '提现信息不存在或参数错误';
- return false;
- }
- if($orderStatus != 1){
- $this->error = '提现订单状态不可操作';
- return false;
- }
- if(!in_array($status,[2,3])){
- $this->error = '审核状态错误';
- return false;
- }
- if($status == 3 && empty($confirmRemark)){
- $this->error = '请填写审核驳回备注';
- return false;
- }
- DB::beginTransaction();
- $updateData = ['status'=>$status,'pay_status'=> $payStatus,'pay_img'=> $payImg,'confirm_admin_id'=>$adminId,'update_time'=>time(),'confirm_remark'=>$confirmRemark];
- if(!$this->model->where(['id'=> $id])->update($updateData)){
- DB::rollBack();
- $this->error = '提现审核失败';
- return false;
- }
- // 如果驳回
- if($status == 3){
- $updateData = ['balance'=>DB::raw("balance + {$money}"),'update_time'=>time()];
- if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => $orderUserId,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'type' => 5,
- 'money' => $money,
- 'before_money' => $balance,
- 'date'=> date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '提现驳回',
- 'status' => 1,
- 'mark' => 1,
- ];
- if(!AccountLogModel::insertGetId($log)){
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- }else if($status == 2){
- // 线上直接打款逻辑
- }
- DB::commit();
- $this->error = '提现审核成功';
- return ['id'=>$id,'money'=>$money,'status'=>$status];
- }
- /**
- * 打款
- * @param $adminId
- * @param $params
- * @return array|false
- */
- public function payment($adminId, $params)
- {
- $id = isset($params['id'])? intval($params['id']) : 0;
- $payStatus = isset($params['pay_status'])? intval($params['pay_status']) : 0;
- $payImg = isset($params['pay_img'])? trim($params['pay_img']) : '';
- $payImg = $payImg? get_image_path($payImg) : '';
- $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
- $userInfo = isset($info['member'])? $info['member'] : [];
- $orderStatus = isset($info['status'])? $info['status'] : 0;
- $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
- $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
- $money = isset($info['money'])? $info['money'] : 0;
- if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
- $this->error = '提现信息不存在或参数错误';
- return false;
- }
- if($orderStatus != 2){
- $this->error = '提现订单状态不可操作';
- return false;
- }
- if($payStatus != 2){
- $this->error = '请选择已打款';
- return false;
- }
- DB::beginTransaction();
- $updateData = ['pay_status'=> $payStatus,'pay_img'=> $payImg,'update_time'=>time()];
- if(!$this->model->where(['id'=> $id])->update($updateData)){
- DB::rollBack();
- $this->error = '提现打款处理失败';
- return false;
- }
- DB::commit();
- $this->error = '提现打款状态更新成功';
- return ['id'=>$id,'money'=>$money,'status'=>$payStatus];
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete(); // TODO: Change the autogenerated stub
- }
- /*\提现统计
- * @param int $type
- * @return array|mixed
- */
- public function getTotal($type=1)
- {
- $cacheKey = "caches:balances:total_{$type}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['mark'=>1])
- ->where(function($query) use($type){
- if($type== 1){
- $query->where(['type'=>2])->whereIn('status',[2,4]);
- }else {
- $query->where(['type'=>2]);
- }
- })->sum('money');
- if($data){
- RedisService::set($cacheKey, $data, rand(300, 600));
- }
- return $data;
- }
- }
|