276 行
10 KiB
PHP
可执行文件
276 行
10 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Distribution\Model;
|
|
|
|
use Common\Model\RetailPlugModel;
|
|
|
|
/**
|
|
* 分销员表
|
|
* Class DistributorModel
|
|
* @package Distribution\Model
|
|
*/
|
|
class DistributorModel extends \Common\Model\BaseModel
|
|
{
|
|
//分销员信息表
|
|
protected $tableName = 'distributor';
|
|
protected $fields = array(
|
|
'id', //int(11) NOT NULL AUTO_INCREMENT,
|
|
'merchant_id', //int(11) NOT NULL DEFAULT '0' COMMENT '商户id',
|
|
'phone_number', //varchar(50) DEFAULT '' COMMENT '手机号',
|
|
'uid', //int(11) DEFAULT '0' COMMENT '小程序用户ID',
|
|
'pid', //int(11) DEFAULT '0' COMMENT '上级分销员ID',
|
|
'grade_level_id', //int(11) DEFAULT NULL COMMENT 'distribution_grade 的id',
|
|
'add_time', //int(11) DEFAULT '0' NULL COMMENT '加入时间',
|
|
'join_type', //tinyint(1) DEFAULT NULL COMMENT '加入方式 1后台加入 2分销员推广 3小程序申请',
|
|
'status', //tinyint(1) DEFAULT '0' COMMENT '状态 0正常 1删除',
|
|
'total_indent', //int(11) DEFAULT '0' COMMENT '分销订单数',
|
|
'total_invite', //int(11) DEFAULT '0' COMMENT '累计邀请人数',
|
|
'total_commission', //decimal(10,2) DEFAULT '0.00' COMMENT '累计佣金',
|
|
'total_myself_money', //decimal(10,2) DEFAULT '0.00' COMMENT '自购商品金额',
|
|
'total_client', //int(11) DEFAULT '0' COMMENT '客户数',
|
|
'alipay_msg', //varchar(255) DEFAULT '' COMMENT '支付宝信息',
|
|
'bank_msg', //varchar(255) DEFAULT '' COMMENT '银行卡信息',
|
|
'settable_amount', //decimal(10,2) DEFAULT 0.00 NULL COMMENT '可结算金额',
|
|
'invite_code', //varchar(21) DEFAULT '' NULL COMMENT '邀请码',
|
|
);
|
|
|
|
|
|
/* ******************************************* 这里是小程序的方法Start ******************************************** */
|
|
// 获取用户基本信息--头像/佣金等级
|
|
public function getUserBasicInfo($where, $field)
|
|
{
|
|
$info = $this->alias('dor')
|
|
->join('ls_retail_xcxvip as rx on rx.id = dor.uid', 'LEFT')
|
|
->join('ls_distribution_grade as dg on dg.id = dor.grade_level_id', 'LEFT')
|
|
->field($field)
|
|
->where($where)
|
|
->find();
|
|
return $info;
|
|
}
|
|
|
|
// 获取我的分销员
|
|
public function getMyDistributor($where, $field, $page = 1, $limit = 10, $order)
|
|
{
|
|
$list = $this->alias('dor')
|
|
->join('ls_retail_xcxvip as rx on rx.id = dor.uid', 'LEFT')
|
|
->field($field)
|
|
->where($where)
|
|
->order($order)
|
|
->group('dor.uid')
|
|
->page($page, $limit)
|
|
->select();
|
|
|
|
$total = count($list);
|
|
|
|
$result['list'] = $list;
|
|
$result['count'] = $total;
|
|
return $result;
|
|
}
|
|
|
|
// 获取用户的上级
|
|
public function getUserLeader($merchant_id, $distributor_id)
|
|
{
|
|
$user = $this
|
|
->field(['pid'])
|
|
->where(['merchant_id' => $merchant_id, 'id' => $distributor_id, 'status' => 0])
|
|
->find();
|
|
|
|
return $user['pid'] ? $user['pid'] : 0;
|
|
}
|
|
|
|
// 获取当前用户的佣金比例
|
|
public function getUserPresent($distributor_id)
|
|
{
|
|
$info = $this->alias('dor')
|
|
->join('ls_distribution_grade as dg on dg.id = dor.grade_level_id', 'LEFT')
|
|
->field(['dg.proportion'])
|
|
->where(['dor.id' => $distributor_id, 'dor.status' => 0])
|
|
->find();
|
|
|
|
return $info['proportion'] ? $info['proportion'] : 0;
|
|
}
|
|
|
|
// 获取当前用户的佣金比例/等级
|
|
public function getUserGradeInfo($distributor_id)
|
|
{
|
|
$gradeInfo = $this->alias('dor')
|
|
->join('ls_distribution_grade as dg on dg.id = dor.grade_level_id', 'LEFT')
|
|
->field(['dg.id, dg.proportion, dg.grade_name'])
|
|
->where(['dor.id' => $distributor_id])
|
|
->find();
|
|
|
|
$gradeInfo['proportion'] = $gradeInfo['proportion'] ? $gradeInfo['proportion'] : 0;
|
|
return $gradeInfo;
|
|
}
|
|
|
|
// 对用户进行升级操作
|
|
public function userUpgrade()
|
|
{
|
|
$where['status'] = 0;
|
|
$distributorList = $this->where($where)->select();
|
|
if ($distributorList) {
|
|
foreach ($distributorList as $value) {
|
|
$this->userWhetherUpgrade($value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 判断当前用户是否满足升级
|
|
public function userWhetherUpgrade($distributor)
|
|
{
|
|
//获取当前等级信息
|
|
$GradeModel = new GradeModel();
|
|
$grade = $GradeModel->getFind(['id'=>$distributor['grade_level_id']]);
|
|
|
|
// 获取用户下个等级
|
|
$nextGrade = $GradeModel->getUpgradeConditions($grade['proportion'], $distributor['merchant_id']);
|
|
if (!$nextGrade) {
|
|
//已经没有下个级别
|
|
return false;
|
|
}
|
|
|
|
$count = 0;
|
|
$need_count = 0;
|
|
if ($nextGrade['is_invite'] == 1) {
|
|
if ($distributor['total_invite'] >= $nextGrade['invite']) {
|
|
$count++;
|
|
}
|
|
$need_count++;
|
|
}
|
|
if ($nextGrade['is_commission'] == 1) {
|
|
if ($distributor['total_commission'] >= $nextGrade['commission']) {
|
|
$count++;
|
|
}
|
|
$need_count++;
|
|
}
|
|
if ($nextGrade['is_myself_money'] == 1) {
|
|
if ($distributor['total_myself_money'] >= $nextGrade['myself_money']) {
|
|
$count++;
|
|
}
|
|
$need_count++;
|
|
}
|
|
if ($nextGrade['is_client'] == 1) {
|
|
if ($distributor['total_client'] >= $nextGrade['client']) {
|
|
$count++;
|
|
}
|
|
$need_count++;
|
|
}
|
|
if ($nextGrade['upgrade_type'] == 1 && $count == $need_count || $nextGrade['upgrade_type'] == 0 && $count) {
|
|
// 用户进行升级操作
|
|
$this->updateOrder(['id' => $distributor['id']], ['grade_level_id' => $nextGrade['id']]);
|
|
$distributor['grade_level_id'] = $nextGrade['id'];
|
|
$this->userWhetherUpgrade($distributor);
|
|
echo '用户' . $distributor['id'] . '升级完成<br>';
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 获取预估佣金?-- 根据价格获取佣金--用户是否开启分佣
|
|
* @param distirbutor_id 分销员id
|
|
* @param price 商品价格
|
|
* @param goodsId 商品id
|
|
*/
|
|
public function getSelfProportion($distributor_id, $price, $goodsId)
|
|
{
|
|
$ConfigModel = new ConfigModel();
|
|
$PlugModel = new RetailPlugModel();
|
|
$StoreModel = new StoreModel();
|
|
|
|
$plug = $PlugModel->getFind(['identification' => 'malldistribution', 'status' => 1, 'is_install' => 1], 'id');
|
|
if (!$plug) {
|
|
return false;
|
|
}
|
|
|
|
$distributor = $this->getFind(['id' => $distributor_id, 'status' => 0], 'merchant_id');
|
|
if (!$distributor) {
|
|
return false;
|
|
}
|
|
|
|
//获取商品一级佣金比
|
|
$firstProportion = $StoreModel->getFindField(['goods_id' => $goodsId, 'goods_status' => ['neq', 2]], 'first_proportion');
|
|
|
|
// 获取配置 - 是否可以自购 // 分销自购 0不可以获得佣金 1可以获得佣金
|
|
$config = $ConfigModel->getFind(['merchant_id' => $distributor['merchant_id']], 'is_self_buying');
|
|
$res['is_self_buying'] = $config['is_self_buying'];
|
|
|
|
$proportion = $this->getUserPresent($distributor_id);
|
|
if ($firstProportion) {
|
|
$res['commission'] = bcmul($firstProportion, $price, 2);
|
|
} else {
|
|
$res['commission'] = bcmul($proportion, $price, 2);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/* ******************************************* 这里是小程序的方法END ******************************************** */
|
|
|
|
public function verifyUserDistributor($uid, $merchant_id)
|
|
{
|
|
$DistributorModel = new DistributorModel();
|
|
$isDistributor = $DistributorModel->getOrderFind(['merchant_id' => $merchant_id, 'uid' => $uid, 'status' => 0], 'id', 'id desc');
|
|
if ($isDistributor) {
|
|
return $isDistributor['id'];
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 通过id获取用户的openin
|
|
public function getOpenidByDistributor($distributor_id)
|
|
{
|
|
$where['dor.id'] = $distributor_id;
|
|
$field = 'rx.openid';
|
|
|
|
$info = $this->alias('dor')
|
|
->join('ls_retail_xcxvip as rx on rx.id = dor.uid', 'LEFT')
|
|
->field($field)
|
|
->where($where)
|
|
->find();
|
|
return $info['openid'];
|
|
}
|
|
|
|
/**
|
|
* 生成不重复的随机数字(不能超过10位数,否则while循环陷入死循环)
|
|
* @param int $start 需要生成的数字开始范围
|
|
* @param int $end 结束范围
|
|
* @param int $length 需要生成的随机数个数
|
|
* @return number 生成的随机数
|
|
*/
|
|
public function getRandNumber($start = 0, $end = 9, $length = 6)
|
|
{
|
|
//初始化变量为0
|
|
$count = 0;
|
|
//建一个新数组
|
|
$temp = array();
|
|
while ($count < $length) {
|
|
//在一定范围内随机生成一个数放入数组中
|
|
$temp[] = mt_rand($start, $end);
|
|
//去除数组中的重复值用了“翻翻法”,就是用array_flip()把数组的key和value交换两次。这种做法比用 array_unique() 快得多。
|
|
$data = array_flip(array_flip($temp));
|
|
//将数组的数量存入变量count中
|
|
$count = count($data);
|
|
}
|
|
//为数组赋予新的键名
|
|
shuffle($data);
|
|
//数组转字符串
|
|
$str = implode(",", $data);
|
|
//替换掉逗号
|
|
$number = str_replace(',', '', $str);
|
|
return $number;
|
|
}
|
|
|
|
// 生成邀请码
|
|
public function createInviteCode($merchant_id)
|
|
{
|
|
$number = $this->getRandNumber(0, 9, 6);
|
|
$is_use = $this->getFind(['merchant_id' => $merchant_id, 'invite_code' => $number], 'invite_code');
|
|
if ($is_use) {
|
|
return $this->createInviteCode($merchant_id);
|
|
} else {
|
|
return $number;
|
|
}
|
|
}
|
|
}
|