// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountLogModel; use App\Models\DriverModel; use App\Models\MemberModel; use App\Services\BaseService; use Illuminate\Support\Facades\DB; /** * 交易管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AccountService * @package App\Services\Common */ class AccountService extends BaseService { public static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AccountService constructor. */ public function __construct() { $this->model = new AccountLogModel(); } /** * 静态入口 * @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) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } $year = isset($params['year']) && $params['year']? $params['year'] : ''; $year = $year? $year : date('Y'); if(!$table = $this->model->getTable($year)){ return false; } // 账号 $list = $this->model->with(['member','driver'])->from("{$table} as a") ->leftJoin('member as b','b.id','=','a.source_id') ->leftJoin('member as c',function($join){ $join->on('c.id','=','a.user_id')->whereIn('a.user_type',[1,3]); }) ->leftJoin('driver as d',function($join){ $join->on('d.id','=','a.user_id')->where('a.user_type', 2); }) ->where($where) ->where(function ($query) use($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; $userType = isset($params['user_type'])? $params['user_type'] : 0; if($userType >= 1){ $query->where(['a.user_type'=> $userType]); } $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('c.nickname','like',"%{$keyword}%") ->orWhere('c.mobile','like',"%{$keyword}%") ->orWhere('d.realname','like',"%{$keyword}%") ->orWhere('d.mobile','like',"%{$keyword}%"); }); } $orderNo = isset($params['order_no'])? trim($params['order_no']) : ''; if($orderNo){ $query->where(function($query) use($orderNo){ $query->where('a.source_order_no','like',"%{$orderNo}%"); }); } }) ->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)); } $coinType = isset($params['coin_type'])? $params['coin_type'] : 0; if($coinType && is_array($coinType)){ $query->whereIn('a.coin_type',$coinType); }else if ($coinType){ $query->where('a.coin_type',$coinType); } }) ->select(['a.*','b.mobile as source_mobile','b.nickname as source_username']) ->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['source_username'] = $item['source_mobile']? $item['source_username']."({$item['source_mobile']})" : $item['source_username']; $item['user_type_name'] = $item['user_type'] == 2? '司机': ($item['user_type'] == 4? '平台':'用户'); } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * @param $params * @param int $pageSize * @return array */ public function getCount($params) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } $year = isset($params['year']) && $params['year']? $params['year'] : ''; $year = $year? $year : date('Y'); if(!$table = $this->model->getTable($year)){ return false; } // 数据 $model = $this->model->with(['member','driver'])->from("{$table} as a") ->leftJoin('member as b','b.id','=','a.source_id') ->leftJoin('member as c',function($join){ $join->on('c.id','=','a.user_id')->whereIn('a.user_type',[1,3]); }) ->leftJoin('driver as d',function($join){ $join->on('d.id','=','a.user_id')->where('a.user_type', 2); }) ->where($where) ->where(function ($query) use($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; $userType = isset($params['user_type'])? $params['user_type'] : 0; if($userType >= 1){ $query->where(['a.user_type'=> $userType]); } $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('c.nickname','like',"%{$keyword}%") ->orWhere('c.mobile','like',"%{$keyword}%") ->orWhere('d.realname','like',"%{$keyword}%") ->orWhere('d.mobile','like',"%{$keyword}%"); }); } $orderNo = isset($params['order_no'])? trim($params['order_no']) : ''; if($orderNo){ $query->where(function($query) use($orderNo){ $query->where('a.source_order_no','like',"%{$orderNo}%"); }); } }) ->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)); } $coinType = isset($params['coin_type'])? $params['coin_type'] : 0; if($coinType && is_array($coinType)){ $query->whereIn('a.coin_type',$coinType); }else if ($coinType){ $query->where('a.coin_type',$coinType); } }); $totalModel = clone $model; $counts = [ 'count'=> $model->count('a.id'), 'total'=> $totalModel->sum(DB::raw("abs(lev_a.money)")), 'income'=> $totalModel->sum(DB::raw("lev_a.money")), ]; return $counts; } /** * 按日期统计流水金额 * @param string $beginAt 开始时间 * @param string $endAt 结束时间 * @param int[] $status 状态:数组或数值 * @return mixed */ public function getTotalByTime($beginAt='', $endAt='', $status=1, $type=0) { $where = ['mark' => 1]; return $this->model->where($where)->where(function($query) use($beginAt,$endAt,$status,$type){ if($beginAt && $endAt){ $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]); }else if($beginAt){ $query->where('create_time','>=', strtotime($beginAt)); } if($status && is_array($status)){ $query->whereIn('status',$status); }else if($status){ $query->where('status',$status); } // 类型 if($type && is_array($type)){ $query->whereIn('type',$type); }else if($type){ $query->where('type',$type); } })->sum(DB::raw("abs(money)")); } /** * 按日期统计营收金额 * @param string $beginAt 开始时间 * @param string $endAt 结束时间 * @return mixed */ public function getIncomeTotalByTime($beginAt='', $endAt='') { $where = ['mark' => 1,'status'=>1]; return $this->model->where($where)->where(function($query) use($beginAt,$endAt){ if($beginAt && $endAt){ $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]); }else if($beginAt){ $query->where('create_time','>=', strtotime($beginAt)); } // 营收 $query->where(function($query){ $query->where(['type'=>2,'coin_type'=>1])->orWhere(['type'=>1,'user_type'=>4]); }); })->sum(DB::raw("abs(money)")); } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 调整用户余额 * @param $params * @return bool */ public function changeBalance($params) { $userId = isset($params['user_id'])? $params['user_id']:0; $userType = isset($params['user_type'])? $params['user_type']:0; $money = isset($params['money'])? $params['money']:0; $type = isset($params['type'])? $params['type']:0; // 司机 if($userType == 2){ $userInfo = DriverModel::where(['id'=> $userId,'mark'=>1])->select(['id','balance'])->first(); $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0; if(empty($userInfo)){ $this->error = '2206'; return false; } // 扣除验证 if($type == 2 && $money > $balance){ $this->error = '2202'; return false; } // 处理 DB::beginTransaction(); $money = $type==1? $money : -$money; $data = ['balance'=> DB::raw("balance + {$money}"),'update_time'=>time()]; if(!DriverModel::where(['id'=> $userId,'mark'=>1])->update($data)){ DB::rollBack(); $this->error = '2203'; return false; } $logData = [ 'user_id'=> $userId, 'source_id'=> 0, 'type'=> 4, 'coin_type'=> 4, 'user_type'=> 2, 'money'=> $money, 'balance'=> $balance, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '余额调整', 'status'=> 1, 'mark'=>1 ]; if (!AccountLogModel::insertGetId($logData)) { $this->error = 2204; DB::rollBack(); return true; } DB::commit(); // 结算统计 \App\Services\Api\FinanceService::make()->saveLog(0, $money,2); $this->error = 2205; return true; }else{ $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','balance'])->first(); $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0; if(empty($userInfo)){ $this->error = '2201'; return false; } // 扣除验证 if($type == 2 && $money > $balance){ $this->error = '2202'; return false; } // 处理 DB::beginTransaction(); $money = $type==1? $money : -$money; $data = ['balance'=> DB::raw("balance + {$money}"),'update_time'=>time()]; if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){ DB::rollBack(); $this->error = '2203'; return false; } $logData = [ 'user_id'=> $userId, 'source_id'=> 0, 'type'=> 4, 'coin_type'=> 4, 'user_type'=> $userType, 'money'=> $money, 'balance'=> $balance, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '余额调整', 'status'=> 1, 'mark'=>1 ]; if (!AccountLogModel::insertGetId($logData)) { $this->error = 2204; DB::rollBack(); return true; } DB::commit(); // 结算统计 \App\Services\Api\FinanceService::make()->saveLog(0, $money,2); $this->error = 2205; return true; } } /** * 调整用户积分 * @param $params * @return bool */ public function changeScore($params) { $userId = isset($params['user_id'])? $params['user_id']:0; $userType = isset($params['user_type'])? $params['user_type']:0; $money = isset($params['money'])? $params['money']:0; $type = isset($params['type'])? $params['type']:0; // 司机 if($userType == 2){ $userInfo = DriverModel::where(['id'=> $userId,'mark'=>1])->select(['id','tz_score'])->first(); $score = isset($userInfo['tz_score'])? $userInfo['tz_score'] : 0; if(empty($userInfo)){ $this->error = '2206'; return false; } // 扣除验证 if($type == 2 && $money > $score){ $this->error = '2212'; return false; } // 处理 DB::beginTransaction(); $money = $type==1? $money : -$money; $data = ['tz_score'=> DB::raw("tz_score + {$money}"),'update_time'=>time()]; if(!DriverModel::where(['id'=> $userId,'mark'=>1])->update($data)){ DB::rollBack(); $this->error = '2213'; return false; } $logData = [ 'user_id'=> $userId, 'source_id'=> 0, 'type'=> 4, 'coin_type'=> 3, 'user_type'=> 2, 'money'=> $money, 'balance'=> $score, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '通证积分调整', 'status'=> 1, 'mark'=>1 ]; if (!AccountLogModel::insertGetId($logData)) { $this->error = 2214; DB::rollBack(); return true; } DB::commit(); $this->error = 2215; return true; }else{ $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->select(['id','tz_score'])->first(); $score = isset($userInfo['tz_score'])? $userInfo['tz_score'] : 0; if(empty($userInfo)){ $this->error = '2201'; return false; } // 扣除验证 if($type == 2 && $money > $score){ $this->error = '2212'; return false; } // 处理 DB::beginTransaction(); $money = $type==1? $money : -$money; $data = ['tz_score'=> DB::raw("tz_score + {$money}"),'update_time'=>time()]; if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($data)){ DB::rollBack(); $this->error = '2213'; return false; } $logData = [ 'user_id'=> $userId, 'source_id'=> 0, 'type'=> 4, 'coin_type'=> 3, 'user_type'=> $userType, 'money'=> $money, 'balance'=> $score, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> isset($params['remark']) && $params['remark']? trim($params['remark']) : '通证积分调整', 'status'=> 1, 'mark'=>1 ]; if (!AccountLogModel::insertGetId($logData)) { $this->error = 2214; DB::rollBack(); return true; } DB::commit(); $this->error = 2215; return true; } } }