164 lines
5.8 KiB
PHP
Executable File
164 lines
5.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace wstmart\common\model;
|
|
use think\Db;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class UserTrees extends Base
|
|
{
|
|
public function getShareNum($where)
|
|
{
|
|
return $this->where($where)->count();
|
|
}
|
|
|
|
public function getField($where, $field = 'pid')
|
|
{
|
|
return $this->where($where)->value($field);
|
|
}
|
|
|
|
public function getInfo($where, $field = 'pid')
|
|
{
|
|
return $this->where($where)->field($field)->find();
|
|
}
|
|
|
|
public static function getUpperId($userId){
|
|
$data = self::where(["uid"=>$userId])->find();
|
|
return (null == $data)?$data:$data->pid;
|
|
|
|
}
|
|
public static function realGetMyChildren($myUserId, $users, $level = 1, &$data = []){
|
|
$inUser = implode("','",$users);
|
|
$_childes = self::where("pid in('$inUser')")->select();
|
|
$nextUsers = [];
|
|
foreach ($_childes as $_child){$nextUsers[] = $_child["uid"];}
|
|
$data[$level-1]["count"]=sizeof($_childes);
|
|
$data[$level-1]["total"]=self::realGetUsersHelpSaleNum($myUserId, $level);
|
|
if($level <= 10){$data[$level-1]["level"]=self::$level[$level];}
|
|
else{$data[$level-1]["level"]="第".$level."层";}
|
|
$data[$level-1]["level_no"]=$level;
|
|
return $level < (int)dataConf("helpSaleMaxLevel") ? self::realGetMyChildren($myUserId, $nextUsers, ++$level, $data) : $data;
|
|
}
|
|
|
|
public static function checkUserCanDisplay($userId){
|
|
if($userId == 1)return true;
|
|
$user = (new \wstmart\app\model\Users())->getUserInfo($userId, 'dataFlag');
|
|
if($user===null)return false;
|
|
if($user['dataFlag']!='1')return false;
|
|
$data = Db::name("user_vouchers_summary")->where(compact('userId'))->find();
|
|
if($data===null){$userHelpGet=0;}
|
|
else{$userHelpGet = (float)$data['helpSaleNum'];}
|
|
return ($userHelpGet >= 0);
|
|
}
|
|
|
|
public static function realGetUsersHelpSaleNum($fromUsers, $level){
|
|
$count = DB::table("hyh_orders")->where("userId='$fromUsers' and helpUserLevel='$level' and orderStatus=2")->sum("helpGetCount");
|
|
$minus = DB::table("hyh_orders")->where("helpUserId='$fromUsers' and helpUserLevel='$level' and orderStatus=2")->sum("helpGetCount");
|
|
// $count -= DB::table("hyh_orders")->where("helpUserId='$fromUsers' and helpUserLevel='$level' and orderStatus=2")->sum("helpGetCount2");
|
|
return round($count-$minus, 2);
|
|
}
|
|
|
|
public static function realGetLevelsDownBetween($fromUser, $toUser, $level=0){
|
|
if($level === 0 && $fromUser == $toUser)return 0;
|
|
if(!is_array($fromUser))$fromUser=[$fromUser];
|
|
if(sizeof($fromUser)==0)return 0;
|
|
if(is_array($toUser))$toUser=$toUser[0];
|
|
$inUser = implode("','",$fromUser);
|
|
$_childes = self::where("pid in('$inUser')")->select();
|
|
$nextUsers = [];
|
|
foreach ($_childes as $_child){$nextUsers[] = $_child["uid"];}
|
|
return in_array($toUser, $nextUsers) ? ++$level : self::realGetLevelsDownBetween($nextUsers, $toUser, ++$level);
|
|
}
|
|
|
|
public static function realGetLevelsBetween($fromUser, $toUser, $level=0){
|
|
if($level === 0 && $fromUser == $toUser)return 0;
|
|
$parent = self::where("uid = '$fromUser'")->find();
|
|
$nextUsers = $parent->value("pid");
|
|
return $nextUsers != null ? ($nextUsers == $toUser ? ++$level : self::realGetLevelsBetween($nextUsers, $toUser, ++$level)) : $level;
|
|
}
|
|
/**
|
|
* getMyLevel
|
|
* @RequestMapping("getMyLevel")
|
|
* @param $userId
|
|
* @param int $level
|
|
* @param array $levelArr
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
* @author 倪苍华 <canghua.cc@gmail.com>
|
|
* Date 2019/9/10 10:35
|
|
*/
|
|
public static function getMyLevel($userId, $level = 1, $levelArr = [])
|
|
{
|
|
$myChildren = [];
|
|
if ($userId) {
|
|
$myChildren = self::where("pid in($userId)")->select();
|
|
}
|
|
$ids = [];
|
|
$key = 0;
|
|
foreach ($myChildren as $key => $v) {
|
|
$ids[] = $v['uid'];
|
|
}
|
|
$levelArr[$level]['count'] = $key + 1;
|
|
$levelArr[$level]['total'] = 0;
|
|
$level++;
|
|
|
|
if ($level <= (int)dataConf("helpSaleMaxLevel")) {
|
|
return self::getMyLevel(implode(',', $ids), $level, $levelArr);
|
|
} else {
|
|
return self::getMyProductNum($levelArr);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* getMyProductNum
|
|
* @RequestMapping("getMyProductNum")
|
|
* @param $levelArr
|
|
* @return mixed
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
* @author 倪苍华 <canghua.cc@gmail.com>
|
|
* Date 2019/9/10 11:02
|
|
*/
|
|
public static function getMyProductNum($levelArr)
|
|
{
|
|
foreach ($levelArr as $key => $v){
|
|
$levels = UserLevel::where(['uid'=>get_my_id(),'level' => $key])->select();
|
|
$ids = [];
|
|
foreach ($levels as $lvk => $lvv){
|
|
$ids[] = $lvv->pid;
|
|
}
|
|
$ids = implode(',',$ids);
|
|
if(!$ids){
|
|
$ids = 0;
|
|
}
|
|
// Fix Injection
|
|
$levelArr[$key]['total'] = DB::table("hyh_user_vouchers_summary")->where("userId in('$ids')")->sum("helpSaleNum");
|
|
if($key>10){
|
|
$levelArr[$key]['level'] = "第".$key."层";
|
|
}else{
|
|
$levelArr[$key]['level'] = self::$level[$key];
|
|
}
|
|
$levelArr[$key]['level_id'] = $key;
|
|
}
|
|
return $levelArr;
|
|
}
|
|
|
|
public static $level = [
|
|
1 => "第一层",
|
|
2 => "第二层",
|
|
3 => "第三层",
|
|
4 => "第四层",
|
|
5 => "第五层",
|
|
6 => "第六层",
|
|
7 => "第七层",
|
|
8 => "第八层",
|
|
9 => "第九层",
|
|
10 => "第十层",
|
|
];
|
|
|
|
}
|