// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountLogModel; use App\Models\ActionLogModel; use App\Models\MemberModel; use App\Services\BaseService; use Illuminate\Support\Facades\DB; /** * 承兑商管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class AccountLogService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new AccountLogModel(); } /** * 获取列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 10, $field = []) { $query = $this->getQuery($params); $list = $query->select($field ? $field : ['a.*', 'b.nickname','b.wallet_url']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s'); } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 查询构造 * @param $params * @return \Illuminate\Database\Eloquent\Builder */ public function getQuery($params) { $where = ['a.mark' => 1]; return $this->model->with(['member']) ->from('account_log as a') ->leftJoin('member as b', function($join) { $join->on('b.id','=', 'a.user_id')->where('a.user_type',1); }) ->where($where) ->where(function ($query) use ($params) { $kw = isset($params['keyword']) ? trim($params['keyword']) : ''; if ($kw) { $query->where('b.nickname', 'like', "%{$params['keyword']}%") ->orWhere('c.id', '=', "{$params['keyword']}"); } }) ->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)); } $orderNo = isset($params['order_no'])? trim($params['order_no']) : ''; if($orderNo){ $query->where('a.order_no', 'like', "%{$orderNo}%"); } $walletUrl = isset($params['wallet_url']) ? trim($params['wallet_url']) : ''; if ($walletUrl) { $query->where('b.wallet_url', $walletUrl); } $userType = isset($params['user_type'])? $params['user_type'] : 0; if ($userType) { $query->where('a.user_type', $userType); } $type = isset($params['type'])? $params['type'] : 0; if (is_array($type)) { $query->whereIn('a.type', $type); } else if($type){ $query->where('a.type', $type); } $coinType = isset($params['coin_type'])? $params['coin_type'] : 0; if (is_array($coinType)) { $query->whereIn('a.coin_type', $coinType); } else if($coinType){ $query->where('a.coin_type', $coinType); } $status = isset($params['status'])? $params['status'] : 0; if (is_array($status)) { $query->whereIn('a.status', $status); } else if($status){ $query->where('a.status', $status); } }); } /** * 统计 * @param $params * @return array */ public function count($params) { $query = $this->getQuery($params); $count = $query->count('a.id'); $total = $query->sum('a.money'); $query1 = clone $query; $query2 = clone $query; $total1 = $query1->where('a.money','>',0)->sum('a.money'); $total2 = $query2->where('a.money','<=',0)->sum('a.money'); return [ 'count' => $count, 'total1' => round($total1,2), // 进 'total2' => round($total2,2), // 出 'total' => round($total,2) ]; } /** * 平台充兑 * @param $adminId * @param $params * @return bool */ public function changeAccount($adminId,$params) { $userType = isset($params['user_type'])? intval($params['user_type']) : 1; $coinType = isset($params['coin_type'])? intval($params['coin_type']) : 1; $type = isset($params['type'])? intval($params['type']) : 1; $accountId = isset($params['user_id'])? intval($params['user_id']) : 0; $amount = isset($params['amount'])? floatval($params['amount']) : 0; if($amount<=0){ $this->error = 4100; return false; } if(!in_array($type,[1,2])){ $this->error = 4003; return false; } $userTypes = [1=>'会员',2=>'商家',3=>'承兑商']; $fields = [1=>'usdt',2=>'balance',3=>'power_num',4=>'score',5=>'wait_score',6=>'quota']; $coinNames = [1=>'USDT余额',2=>'星豆余额',3=>'算力',4=>'积分',5=>'待返积分',6=>'交易额度']; $field = isset($fields[$coinType])? $fields[$coinType] : 'usdt'; $coinName = isset($coinNames[$coinType])? $coinNames[$coinType] : 'USDT 余额'; $accountName = isset($userTypes[$userType])? $userTypes[$userType] : '会员'; ActionLogModel::setTitle("{$accountName}[ID:{$accountId}]{$coinName}账户调整"); ActionLogModel::record(); $amount = $type == 1? $amount : -$amount; if($userType == 3){ $userInfo = AcceptorModel::where(['id'=> $accountId,'mark'=>1])->first(); $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0; if(empty($userInfo) || $userId<=0){ $this->error = 4004; return false; } if(!in_array($coinType,[1,6])){ $this->error = 4003; return false; } DB::beginTransaction(); $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()]; if(!AcceptorModel::where(['id'=> $accountId])->update($updateData)){ DB::rollBack(); $this->error = 1003; return false; } // 平台明细 $orderNo = get_order_num('PS'); $log = [ 'user_id' => $accountId, 'source_id' => $adminId, 'source_order_no' => $orderNo, 'type' => 8, 'coin_type' => $coinType, 'user_type'=> $userType, 'money' => $amount, 'actual_money' => $amount, 'balance' => isset($userInfo[$field])? $userInfo[$field] : 0, 'create_time' => time(), 'update_time' => time(), 'remark' => $type==1?"平台上分":"平台下分", 'status' => 1, 'mark' => 1, ]; if(!AccountLogModel::insertGetId($log)){ DB::rollBack(); $this->error = 2029; return false; } DB::commit(); $dateTime = date('Y-m-d H:i:s'); $actionName = $type==1?"平台上分":"平台下分"; MessageService::make()->pushMessage($userId,$type==1?"平台上分成功通知":"平台下分成功通知","您在{$dateTime}(UTC+8)收到{$actionName}[{$amount}]{$coinName}账户调整成功,请及时查看对应账户进行查收",3); $this->error = 1002; return true; }else{ $userInfo = MemberModel::where(['id'=> $accountId,'mark'=>1])->first(); $userId = isset($userInfo['id'])? $userInfo['id'] : 0; if(empty($userInfo) || $userId<=0){ $this->error = 4004; return false; } if(!in_array($coinType,[1,2,3,4])){ $this->error = 4003; return false; } DB::beginTransaction(); $updateData = [$field=>DB::raw("{$field} + {$amount}"),'update_time'=>time()]; if(!MemberModel::where(['id'=> $accountId])->update($updateData)){ DB::rollBack(); $this->error = 1003; return false; } // 平台明细 $orderNo = get_order_num('PS'); $log = [ 'user_id' => $accountId, 'source_id' => $adminId, 'source_order_no' => $orderNo, 'type' => 8, 'coin_type' => $coinType, 'user_type'=> $userType, 'money' => $amount, 'actual_money' => $amount, 'balance' => isset($userInfo[$field])? $userInfo[$field] : 0, 'create_time' => time(), 'update_time' => time(), 'remark' => $type==1?"平台上分":"平台下分", 'status' => 1, 'mark' => 1, ]; if(!AccountLogModel::insertGetId($log)){ DB::rollBack(); $this->error = 2029; return false; } DB::commit(); $dateTime = date('Y-m-d H:i:s'); $actionName = $type==1?"平台上分":"平台下分"; MessageService::make()->pushMessage($userId,$type==1?"平台上分成功通知":"平台下分成功通知","您在{$dateTime}(UTC+8)收到{$actionName}[{$amount}]{$coinName}账户调整成功,请及时查看对应账户进行查收",3); $this->error = 1002; return true; } $this->error = 1003; return false; } }