149 lines
6.7 KiB
PHP
149 lines
6.7 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
// +----------------------------------------------------------------------
|
||
// | Author: CRMEB Team <admin@crmeb.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
|
||
namespace crmeb\jobs;
|
||
|
||
|
||
use app\common\repositories\user\UserBrokerageRepository;
|
||
use app\common\repositories\user\UserRepository;
|
||
use crmeb\interfaces\JobInterface;
|
||
use think\facade\Log;
|
||
use app\common\model\system\supplychain\SupplyChainBorkerage;
|
||
use app\common\model\system\supplychain\SupplyChainTeam;
|
||
use app\common\model\system\supplychain\SupplyChainLevel;
|
||
use app\common\model\system\supplychain\SupplyChainLinkMerchant;
|
||
use app\common\repositories\user\UserBillRepository;
|
||
use app\common\model\user\User;
|
||
use app\common\model\store\GeoArea;
|
||
|
||
/**
|
||
*
|
||
* 供应链分佣队列
|
||
*
|
||
*/
|
||
class SupplyChainOrderBrokerAgeJob implements JobInterface
|
||
{
|
||
public function fire($job, $data)
|
||
{
|
||
try {
|
||
return false;
|
||
// 获取当前用户
|
||
$user = app()->make(UserRepository::class)->get($data['uid']);
|
||
$supplyTeam = SupplyChainTeam::find($user['fa_supply_team_id']);
|
||
$supplyTeamId = SupplyChainLinkMerchant::where('eb_merchant_id', $data['mer_id'])->value('fa_supply_chain_id'); // 获取供应链团队ID
|
||
$supplyLevel = SupplyChainLevel::find($supplyTeam['supply_level_id']); // 获取供应链等级
|
||
$price = $data['brokerage_price']; // 待分润金额 == 订单佣金金额
|
||
|
||
// 判断 存在数据的话,进行分润
|
||
if($supplyTeam)
|
||
{
|
||
// 可获得金额
|
||
$usrPrice = floatval(($price/100)*$supplyLevel['rate']);
|
||
|
||
// 分润数据
|
||
$dataArr = [
|
||
'user_id' => $data['uid'], // 用户ID
|
||
'pay_price' => $data['inc'], // 订单金额
|
||
'price' => $price, // 订单分佣金额
|
||
'order_sn' => $data['order_sn'], // 订单编号
|
||
'supply_sn' => $data['order_sn'], // 订单编号
|
||
'order_id' => $data['order_id'], // 订单ID
|
||
'mer_id' => $data['mer_id'], // 商户ID
|
||
'status' => 1,
|
||
'supply_level_id' => $supplyLevel['id'], // 分佣等级ID
|
||
'brokerage_price' => $usrPrice, // 获取的佣金金额
|
||
'user_info' => $user['nickname'], // 用户名
|
||
'fa_supply_chain_id' => $supplyTeamId, // 供应链团队ID
|
||
'brokerage_rate' => $supplyLevel['rate'] // 分佣比例
|
||
];
|
||
|
||
// 储存小组服务分佣记录
|
||
SupplyChainBorkerage::create($dataArr);
|
||
// 写入冻结佣金
|
||
$userBillRepository = app()->make(UserBillRepository::class);
|
||
|
||
// 加入账单记录 佣金明细
|
||
$userBillRepository->incBill($data['uid'], 'brokerage', 'order_one', [
|
||
'link_id' => $data['order_id'],
|
||
'status' => 0,
|
||
'title' => '获得采购佣金',
|
||
'number' => $usrPrice, // 增加金额
|
||
'mark' => $data['order_sn'] . '采购:' . $data['inc'] . '元,获得采购佣金' . $usrPrice,
|
||
'balance' => 0,
|
||
'source' => 1 // 1 小组采购 2 普通商品
|
||
]);
|
||
|
||
$userRepository = app()->make(UserRepository::class);
|
||
|
||
// 增加小组服务佣金
|
||
$userRepository->incSupplyBrokerage($data['uid'], $price);
|
||
|
||
// 后台分组用户开始
|
||
$userInfo = User::with('nkUserMsg')->find($data['uid']); // 当前用户信息
|
||
$nkUserMsg = $userInfo['nkUserMsg']; // 当前用户关联上级街道、区县等信息
|
||
|
||
$brigade_id = $nkUserMsg['brigade_id']; // 大队
|
||
$village_id = $nkUserMsg['village_id']; // 村
|
||
$street_id = $nkUserMsg['street_id']; //镇
|
||
$area_id = $nkUserMsg['area_id']; // 区域
|
||
$city_code = GeoArea::where('area_code', $area_id)->value('city_code'); // 市
|
||
|
||
// 获取供应链团队
|
||
$supplyChainTeam = SupplyChainTeam::with('level')
|
||
->where('parent_code', 'in', [$brigade_id, $village_id, $street_id, $area_id, $city_code])
|
||
->select(); // 获取关联上级团队
|
||
|
||
if($supplyChainTeam)
|
||
{
|
||
// 进行分佣
|
||
foreach ($supplyChainTeam as $v) {
|
||
|
||
// 可获得金额
|
||
$usrPrice = floatval(($price/100)*$v['level']['rate']);
|
||
|
||
// 分润数据
|
||
$dataArr = [
|
||
'supply_team_id' => $v['id'], // 团队ID
|
||
'pay_price' => $data['inc'], // 订单金额
|
||
'price' => $price, // 订单分佣金额
|
||
'order_sn' => $data['order_sn'], // 订单编号
|
||
'supply_sn' => $data['order_sn'], // 订单编号
|
||
'order_id' => $data['order_id'], // 订单ID
|
||
'mer_id' => $data['mer_id'], // 商户ID
|
||
'supply_level_id' => $v['supply_level_id'], // 分佣等级ID
|
||
'brokerage_price' => $usrPrice, // 获取的佣金金额
|
||
'fa_supply_chain_id' => $supplyTeamId, // 供应链团队ID
|
||
'brokerage_rate' => $v['level']['rate'] // 分佣比例
|
||
];
|
||
|
||
SupplyChainBorkerage::create($dataArr); // 储存小组服务分佣记录
|
||
SupplyChainTeam::where('id', $v['id'])->inc('free_brokerage', $usrPrice)->update(); // 增加冻结佣金
|
||
}
|
||
}
|
||
}
|
||
|
||
\think\facade\Log::record('供应链佣金分布执行完毕');
|
||
|
||
} catch (\Exception $e) {
|
||
Log::info('小组服务佣金同步失败: ' . var_export($data, 1) . $e->getMessage());
|
||
}
|
||
|
||
$job->delete();
|
||
|
||
}
|
||
|
||
public function failed($data)
|
||
{
|
||
}
|
||
}
|