// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountLogModel; use App\Models\ActionLogModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\RedisService; 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'); if($item['user_type'] == 1){ $item['uid'] = $item['user_id']; $item['account'] = isset($item['nickname'])&& $item['nickname']?$item['nickname']: $item['user_id']; } } } 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('b.wallet_url', '=', trim($params['keyword'])) ->orWhere('b.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}%"); } $hash = isset($params['hash'])? trim($params['hash']) : ''; if($hash){ $query->where('a.hash', '=', trim($hash)); } $walletUrl = isset($params['wallet_url']) ? trim($params['wallet_url']) : ''; if ($walletUrl) { $query->where('b.wallet_url', $walletUrl); } $userId = isset($params['user_id'])? $params['user_id'] : 0; if ($userId) { $query->where('a.user_id', $userId); } $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'); $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(abs($total1),2), // 进 'total2' => round(abs($total2),2), // 出 'total' => round(abs($total1)+abs($total2),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=>'会员']; $fields = [1=>'usdt',2=>'sbt',3=>'profit']; $coinNames = [1=>'USDT余额',2=>'SBT',3=>'收益/奖励']; $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(); $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])){ $this->error = 4003; return false; } // 账户余额 $userAmount = isset($userInfo[$field])? $userInfo[$field] : 0; if($type == 2 && $userAmount < $amount){ $this->error = lang(2035,['name'=>$coinName]); return false; } DB::beginTransaction(); $amount = $type == 1? $amount : -$amount; $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('PC'); $log = [ 'user_id' => $accountId, 'source_uid' => 0, 'order_no' => $orderNo, 'type' => $type==1?5:6, 'coin_type' => $coinType, 'user_type'=> 1, 'money' => $amount, 'before_money' => isset($userInfo[$field])? $userInfo[$field] : 0, 'create_time' => time(), 'admin_id' => $adminId, 'action_ip' => get_client_ip(), 'remark' => $type==1?"链上充值":"链上扣除", 'status' => 1, 'mark' => 1, ]; if(!AccountLogModel::insertGetId($log)){ DB::rollBack(); $this->error = 2029; return false; } DB::commit(); ActionLogModel::setTitle("{$coinName}账户上分/下分"); ActionLogModel::record(); $this->error = 1002; return true; } }