add 用户推广分润

This commit is contained in:
chenbo 2024-01-19 14:35:51 +08:00
parent 2eb739607d
commit 957bdf7f7f
2 changed files with 168 additions and 0 deletions

View File

@ -122,6 +122,7 @@ class UserLogic extends BaseLogic
'register_num' => $params['register_num'], 'register_num' => $params['register_num'],
'merchant_num' => $params['merchant_num'], 'merchant_num' => $params['merchant_num'],
'trade_amount' => $params['trade_amount'], 'trade_amount' => $params['trade_amount'],
'group_id' => $params['group_id'],
'update_time' => time() 'update_time' => time()
]); ]);
} }
@ -247,4 +248,49 @@ class UserLogic extends BaseLogic
return true; return true;
} }
public static function getUserByInviteCode($inviteCode)
{
return User::where('invite_code', $inviteCode)->findOrEmpty();
}
public static function getUser($where)
{
return User::where($where)->findOrEmpty();
}
public static function shareProfit($bridgeUser, $villageUser, $streetUser, $profit)
{
try {
Db::startTrans();
// 小队
$proportion = 0.03; // 分润比例
self::userProfit($bridgeUser, $profit, $proportion);
// 村
self::userProfit($villageUser, $profit, $proportion);
// 镇
$proportion = 0.01; // 分润比例
self::userProfit($streetUser, $profit, $proportion);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
public static function userProfit($user, $profit, $proportion)
{
$money = bcmul($profit, $proportion, 2);
$arr = [$user['id'], AccountLogEnum::UM_INC_TASK, AccountLogEnum::INC, $money, $user['sn'], '平台推广' . '获得劳务费' . $money . '元', ['company_id' => $data['company_id'], 'proportion' => $proportion], $data['status']];
self::addUserAccountLog($arr);
}
/**成员分润
* @param $data
*/
private static function addUserAccountLog($data)
{
User::where('id', $data[0])->update(['user_money'=>Db::raw('user_money+' . $data[3])]);
return AccountLogLogic::add($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
}
} }

View File

@ -2,6 +2,7 @@
namespace app\api\controller; namespace app\api\controller;
use app\adminapi\logic\user\UserLogic;
use app\common\logic\task\TaskLogic; use app\common\logic\task\TaskLogic;
use app\common\model\Company; use app\common\model\Company;
use app\common\model\dict\DictData; use app\common\model\dict\DictData;
@ -16,5 +17,126 @@ use think\facade\Log;
*/ */
class ShopCallController extends BaseApiController class ShopCallController extends BaseApiController
{ {
public array $notNeedLogin = ['user_first_order_share_profit', 'user_order_share_profit'];
/**
* 用户首单分润
* 镇三级分润
*/
public function user_first_order_share_profit()
{
$inviteCode = $this->request->get('promotion_code'); // 推广码
$profit = $this->request->get('profit'); // 订单金额
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
if (empty($userSelf)) {
return $this->fail('推广人不存在');
}
// 推广人用户角色
// 小队/小区队长
if (in_array($userSelf['group_id'] , [2, 18])) {
//村/社区合伙人
$where = ['group_id' => 3, 'village'=>$userSelf['village']];
$villageUser = UserLogic::getUser($where);
//镇/街道合伙人
$where1 = ['group_id' => 15, 'street'=>$userSelf['street']];
$streetUser = UserLogic::getUser($where1);
// 计算分润
UserLogic::shareProfit($userSelf, $villageUser, $streetUser, $profit);
return $this->success('成功', [
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $villageUser['id'], 'mobile' => $villageUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $streetUser['id'], 'mobile' => $streetUser['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
]);
}
// 村/社区合伙人
if ($userSelf['group_id'] == 3) {
// 查下级 小队
$where = ['group_id' => [2, 18], 'village'=>$userSelf['village']];
$bridgeUser = UserLogic::getUser($where);
//镇/街道合伙人
$where = ['group_id' => 15, 'village'=>$userSelf['village']];
$streetUser = UserLogic::getUser($where);
// 计算分润
UserLogic::shareProfit($bridgeUser, $userSelf, $streetUser, $profit);
return $this->success('成功', [
['user_id' => $bridgeUser['id'], 'mobile' => $bridgeUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $streetUser['id'], 'mobile' => $streetUser['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
]);
}
// 镇/街道合伙人
if ($userSelf['group_id'] == 15) {
// 查下级村
$where = ['group_id' => 3, 'village'=>$userSelf['village']];
$villageUser = UserLogic::getUser($where);
//和小队
$where = ['group_id' => [2, 18], 'village'=>$userSelf['village']];
$bridgeUser = UserLogic::getUser($where);
// 计算分润
UserLogic::shareProfit($bridgeUser, $villageUser, $userSelf, $profit);
return $this->success('成功', [
['user_id' => $bridgeUser['id'], 'mobile' => $bridgeUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $villageUser['id'], 'mobile' => $villageUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
]);
}
}
/**
* 用户订单分润
* 镇合伙人
*/
public function user_order_share_profit()
{
$inviteCode = $this->request->get('promotion_code'); // 推广码
$profit = $this->request->get('profit'); // 订单金额
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
if (empty($userSelf)) {
return $this->fail('推广人不存在');
}
$proportion = 0.01; // 比例
// 镇/街道合伙人
if ($userSelf['group_id'] == 15) {
// 计算分润
UserLogic::userProfit($userSelf, $profit, $proportion);
}
// 小队/小区队长
if (in_array($userSelf['group_id'] , [2, 18])) {
//镇/街道合伙人
$where1 = ['group_id' => 15, 'street'=>$userSelf['street']];
$streetUser = UserLogic::getUser($where1);
// 计算分润
UserLogic::userProfit($streetUser, $profit, 0.01);
}
// 村/社区合伙人
if ($userSelf['group_id'] == 3) {
//镇/街道合伙人
$where = ['group_id' => 15, 'village'=>$userSelf['village']];
$streetUser = UserLogic::getUser($where);
// 计算分润
UserLogic::userProfit($streetUser, $profit, 0.01);
}
return $this->success('成功', ['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, $proportion, 2)]);
}
} }