<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Request;
use mysql_xdevapi\Exception;
class Users extends Model
{
protected $table = "users";
public function getAll(){
return DB::table($this->table)->get();
}
public function getUserById($openid){
return DB::table($this->table)->where('id',$openid)->first();
}
public function getUserBySpecialId($special_id){
return DB::table($this->table)->where('special_id', $special_id)->first();
}
public function userRegistration($openid,$invite_id){
return DB::table($this->table)
->insert([
'id' => $openid,
'invite_id'=>$invite_id,
'invitation_reward'=>$invite_id!=null?1:0,
'rebate_ratio' => config('config.default_rebate_ratio')
]);
}
public function userUpdate(){
$openid = Request::post("openid");
$nickname = Request::post("nickname");
$username = Request::post("username");
$alipay_id = Request::post("alipay_id");
return DB::table($this->table)
->where('id', $openid)
->update([
'nickname' => $nickname,
'username' => $username,
'alipay_id' => $alipay_id
]);
}
public function updateNickname($openid,$nickname){
return DB::table($this->table)
->where('id', $openid)
->update([
'nickname' => $nickname
]);
}
public function updateSpecial_id($openid,$special_id){
try {
return DB::table($this->table)
->where('id', $openid)
->update([
'special_id' => $special_id
]);
}catch (Exception $e){
return 0;
}
}
public function updateUnsettled_balance($openid,$unsettled_balance){
return DB::table($this->table)
->where('id', $openid)
->update([
'unsettled_balance' => $unsettled_balance
]);
}
public function updateAvailable_balance($openid,$available_balance){
return DB::table($this->table)
->where('id', $openid)
->update([
'available_balance' => $available_balance
]);
}
public function getAllByPaginate($openid=null){
if($openid==null){
return DB::table('users')->paginate(10);
}else{
return DB::table('users')
->where('id',$openid)
->orWhere('nickname','like',"%".$openid."%")
->paginate(5);
}
}
public function modifyUserById($id,$rebate_ratio,$special_id){
return DB::table($this->table)
->where('id', $id)
->update([
'rebate_ratio' => $rebate_ratio,
'special_id' => $special_id,
]);
}
public function modifyRebateRatioById($id,$rebate_ratio){
return DB::table($this->table)
->where('id', $id)
->update([
'rebate_ratio' => $rebate_ratio
]);
}
public function getAlipayTraversalInReceive($receives){
$alipays = array();
foreach ($receives as $receive){
$openid=$receive->openid;
$user = $this->getUserById($openid);
$alipay = array();
$alipay["username"] = $user->username;
$alipay["alipay_id"] = $user->alipay_id;
array_push($alipays,$alipay);
}
return $alipays;
}
public function getUserCountByInviteId($invite_id){
return DB::table($this->table)
->where('invite_id',$invite_id)
->count();
}
public function getUserCountByInviteIdAndInvitationReward($invite_id){
return DB::table($this->table)
->where('invite_id',$invite_id)
->where('invitation_reward',0)
->count();
}
public function updateInvitationReward($openid){
return DB::table($this->table)
->where('id',$openid)
->update(['invitation_reward'=>0]);
}
}