| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?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\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;
- }
- }
- }
|