首先我們要知道什么是分銷系統(tǒng)?
分銷系統(tǒng)指通過互聯(lián)網(wǎng)將供應(yīng)商與經(jīng)銷商有機(jī)地聯(lián)系在一起,為企業(yè)的業(yè)務(wù)經(jīng)營及與貿(mào)易伙伴的合作提供了一種全新的模式。供應(yīng)商、分支機(jī)構(gòu)和經(jīng)銷商之間可以實(shí)現(xiàn)實(shí)時(shí)地提交業(yè)務(wù)單據(jù)、查詢產(chǎn)品供應(yīng)和庫存狀況、并獲得市場、銷售信息及客戶支持,實(shí)現(xiàn)了供應(yīng)商、分支機(jī)構(gòu)與經(jīng)銷商之間端到端的供應(yīng)鏈管理,有效地縮短了供銷鏈。
知識付費(fèi)的分銷功能主要體現(xiàn)在專題課程及會(huì)員模塊;用戶購買專題課程、會(huì)員后會(huì)給用戶的上級和上上級返傭。
那么分銷系統(tǒng)是如何實(shí)現(xiàn)的呢?
知識付費(fèi)系統(tǒng)中在用戶進(jìn)入系統(tǒng)時(shí)若是帶有上級的信息,那么這個(gè)用戶就是別的用戶發(fā)展的下級,我們就需要記錄該用戶的上級信息。數(shù)據(jù)庫用戶表eb_user表中有字段spread_uid(推廣員id),該字段記錄的是用戶的上級id。
首先確定什么時(shí)候執(zhí)行分銷功能?分銷功能執(zhí)行肯定是需要訂單結(jié)束后,這樣可以避免大多數(shù)用戶退款后的傭金返還問題。
然后就是分銷代碼的書寫了,知識付費(fèi)分銷分為人人分銷和指定分銷;人人分銷是指系統(tǒng)中的所有用戶均可參與分銷活動(dòng),并且在下級用戶購買后可以獲得傭金。指定分銷是指只有系統(tǒng)設(shè)置推廣權(quán)限的用戶才能在下級用戶購買后獲得傭金。因?yàn)橹R付費(fèi)的分銷是二級分銷,所有我們是直接寫了一級返傭和二級返傭的方法,在執(zhí)行完一級返傭后執(zhí)行二級返傭,這是分銷的最簡單邏輯了。
以專題課程為例,在專題課程購買成功后執(zhí)行返傭。
/**
* //TODO 專題支付成功后
* @param $orderId
* @param $notify
* @return bool
*/
public static function paySuccess($orderId)
{
$order = self::where('order_id', $orderId)->where('type', 0)->find();
if (!$order) return false;
$resPink = true;
$res2 = true;
$res3 = true;
User::bcInc($order['uid'], 'pay_count', 1, 'uid');
$res1 = self::where('order_id', $orderId)->where('type', 0)->update(['paid' => 1, 'pay_time' => time()]);
if ($res1 && $order['pay_type'] != 'yue') {
$res2 = UserBill::expend('購買專題', $order['uid'], $order['pay_type'], 'pay_product', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元購買專題');
}
if ($res1) {
$res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 0);
}
if ($order['combination_id'] && $res1 && !$order['refund_status']) {
$resPink = StorePink::createPink($order);//創(chuàng)建拼團(tuán)
} else {
if (!$order['is_gift']) {
//如果是專欄,記錄專欄下所有專題購買。
SpecialBuy::setAllBuySpecial($orderId, $order['uid'], $order['cart_id']);
TestPaperObtain::setTestPaper($orderId, $order['uid'], $order['cart_id'], 2);
DataDownloadBuy::setDataDownload($orderId, $order['uid'], $order['cart_id'], 0);
try {
//專題返傭
User::backOrderBrokerage($order);
} catch (\Throwable $e) {
}
}
}
StoreOrderStatus::status($order->id, 'pay_success', '用戶付款成功');
$site_url = SystemConfigService::get('site_url');
try {
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if ($wechat_notification_message == 1) {
$title = Special::getName($order['cart_id']);
WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
'character_string2' => $orderId,
'thing3' => mb_substr($title, 0, 20, 'utf-8'),
'amount5' => $order['pay_price'],
'time4' => date('Y-m-d H:i:s', time())
], $site_url . Url::build('wap/special/grade_special'));
WechatTemplateService::sendAdminNoticeTemplate($order['mer_id'], [
'character_string2' => $orderId,
'thing3' => mb_substr($title, 0, 20, 'utf-8'),
'amount5' => $order['pay_price'],
'time4' => date('Y-m-d H:i:s', time())
]);
} else {
$data['character_string1']['value'] = $orderId;
$data['amount3']['value'] = $order['pay_price'];
$data['time2']['value'] = date('Y-m-d H:i:s', time());
$data['thing6']['value'] = '您購買的專題已支付成功!';
RoutineTemplate::sendOrderSuccess($data, $order['uid'], $site_url . Url::build('wap/special/grade_special'));
$dataAdmin['character_string1']['value'] = $orderId;
$dataAdmin['amount3']['value'] = $order['pay_price'];
$dataAdmin['time2']['value'] = date('Y-m-d H:i:s', time());
$dataAdmin['thing6']['value'] = '您有一個(gè)新的課程訂單!';
RoutineTemplate::sendAdminNoticeTemplate($dataAdmin);
}
} catch (\Throwable $e) {
}
$res = $res1 && $resPink && $res2 && $res3;
return false !== $res;
}
/**
* 一級推廣 專題
* @param $orderInfo
* @return bool
*/
public static function backOrderBrokerage($orderInfo)
{
$userInfo = self::getUserData($orderInfo['uid']);
if (!$userInfo || !$userInfo['spread_uid']) return true;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//獲取后臺分銷類型
if ($storeBrokerageStatu == 1) {
if (!self::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
}
$data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
if (isset($data['is_alone']) && $data['is_alone']) {
if (!isset($data['brokerage_ratio']) || !$data['brokerage_ratio']) return true;
$brokerageRatio = bcdiv($data['brokerage_ratio'], 100, 2);
} else {
$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//課程分銷開關(guān)
if ($course_distribution_switch == 0) return true;
$brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_ratio'), 100, 2);
}
if ($brokerageRatio <= 0) return true;
$brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
if ($brokeragePrice <= 0) return true;
$mark = '一級推廣人' . $userInfo['nickname'] . '消費(fèi)' . floatval($orderInfo['pay_price']) . '元購買專題,獎(jiǎng)勵(lì)推廣傭金' . floatval($brokeragePrice);
$bill = UserBill::billDoesDataExist($userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id']);
if ($bill) return true;
self::beginTrans();
$res1 = UserBill::income('購買專題返傭', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
$res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
$User = self::getUserData($userInfo['spread_uid']);
if (!$User) {
self::checkTrans(false);
return true;
}
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if (!$wechat_notification_message) {
$dat['thing8']['value'] = '返傭金額';
$dat['date4']['value'] = date('Y-m-d H:i:s', time());
$dat['amount1']['value'] = $brokeragePrice;
$dat['amount2']['value'] = $User['brokerage_price'];
$dat['thing5']['value'] = '您收到一筆專題返傭!';
RoutineTemplate::sendAccountChanges($dat, $userInfo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
}
$dats['brokerage_price'] = $brokeragePrice;
SmsTemplate::sendSms($userInfo['spread_uid'], $dats, 'ORDER_BROKERAGE');
$res = $res1 && $res2;
self::checkTrans($res);
if ($res) self::backOrderBrokerageTwo($orderInfo);
return $res;
}
/**
* 二級推廣 專題
* @param $orderInfo
* @return bool
*/
public static function backOrderBrokerageTwo($orderInfo)
{
$userInfo = self::getUserData($orderInfo['uid']);
$userInfoTwo = self::getUserData($userInfo['spread_uid']);
if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
$storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//獲取后臺分銷類型
if ($storeBrokerageStatu == 1) {
if (!self::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
}
$data = Special::getIndividualDistributionSettings($orderInfo['cart_id']);
if (isset($data['is_alone']) && $data['is_alone']) {
if (!isset($data['brokerage_two']) || !$data['brokerage_two']) return true;
$brokerageRatio = bcdiv($data['brokerage_two'], 100, 2);
} else {
$course_distribution_switch = SystemConfigService::get('course_distribution_switch');//課程分銷開關(guān)
if ($course_distribution_switch == 0) return true;
$brokerageRatio = bcdiv(SystemConfigService::get('store_brokerage_two'), 100, 2);
}
if ($brokerageRatio <= 0) return true;
$brokeragePrice = bcmul($orderInfo['pay_price'], $brokerageRatio, 2);
if ($brokeragePrice <= 0) return true;
$mark = '二級推廣人' . $userInfo['nickname'] . '消費(fèi)' . floatval($orderInfo['pay_price']) . '元購買專題,獎(jiǎng)勵(lì)推廣傭金' . floatval($brokeragePrice);
$bill = UserBill::billDoesDataExist($userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id']);
if ($bill) return true;
self::beginTrans();
$res1 = UserBill::income('購買專題返傭', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], 0, $mark);
$res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
$User = self::getUserData($userInfoTwo['spread_uid']);
if (!$User) {
self::checkTrans(false);
return true;
}
$wechat_notification_message = SystemConfigService::get('wechat_notification_message');
if (!$wechat_notification_message) {
$dat['thing8']['value'] = '返傭金額';
$dat['date4']['value'] = date('Y-m-d H:i:s', time());
$dat['amount1']['value'] = $brokeragePrice;
$dat['amount2']['value'] = $User['brokerage_price'];
$dat['thing5']['value'] = '您收到一筆專題返傭!';
RoutineTemplate::sendAccountChanges($dat, $userInfoTwo['spread_uid'], Url::build('wap/spread/commission', [], true, true));
}
$dats['brokerage_price'] = $brokeragePrice;
SmsTemplate::sendSms($userInfoTwo['spread_uid'], $dats, 'ORDER_BROKERAGE');
$res = $res1 && $res2;
self::checkTrans($res);
return $res;
}
這樣專題課程的二級分銷就完成了,若是想再加第三級分銷,我們一樣的邏輯,獲取二級的上級,然后判斷返傭即可。