订单贡献值队列

main
1748153932 1 year ago
parent 139b74fb38
commit 11802839fc
  1. 57
      app/command/brand.php
  2. 7
      app/controller/api/Auth.php
  3. 54
      app/controller/api/user/User.php
  4. 80
      crmeb/jobs/OrderPartnerJob.php
  5. 4
      route/api.php

@ -25,6 +25,7 @@ namespace app\command;
use Swoole\Coroutine\MySQL\Exception;
use think\console\Command;
use app\common\model\store\order\StoreOrderContribute;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
@ -61,38 +62,64 @@ class brand extends Command
*/
protected function execute(Input $input, Output $output)
{
echo systemConfig('beian_sn');die;
//获取所有用户
$userList = userModel::getDB()->where('status', 1)->field('uid,brand_integral,super_days')->select()->toArray();
$userList = userModel::getDB()->where('status', 1)->select()->toArray();
$start = date('Y-m-d', strtotime("-1 day")).' 00:00:00';
$end = date('Y-m-d', strtotime("-1 day")).' 23:59:59';
//全网报单区流水昨日24小时的
$baodanall = StoreOrder::getDB()
->alias('a')
->leftJoin('StoreStoreProduct b', 'a.order_id = B.order_id')
->leftJoin('StoreSpu c', 'a.order_id = B.order_id')
->where('a.paid', 1)
->where('a.pay_time', '>=',$start)
->where('a.pay_time', '<=',$end)
->sum('a.cost');
var_dump($baodanall);die;
$baodanall = StoreOrderContribute::getDB()
->where('bao_price','>',0)
->where('created_time', '>=',$start)
->where('created_time', '<=',$end)
->sum('baodan_liushui');
//全网品宣贡献值之和
$allBrandValue = userModel::getDB()->where('status', 1)->sum('brand_integral');
Db::startTrans();
foreach ($userList as $k => $v) {
try {
//用户的品宣贡献值=本人当前品宣积分之和÷全网品宣积分之和×全网报单流水×1%
//用户的品宣贡献值=本人当前品宣积分÷全网品宣积分之和×全网报单流水×1%
$brandGongxian = round($v['brand_integral']/$allBrandValue*$baodanall/100,2);
//加到log日志
ValueContributionLog::getDB()->insert([
'type' => 7,
'types_of' => 1,
'num' => $brandGongxian,
'ctime' => date('Y-m-d H:i:s'),
'memo' => '品宣积分'.$v['brand_integral'].'自动转换为品宣贡献值'.$brandGongxian,
'user_id' => $v['uid'],
]);
//用户总贡献值 = 当前总贡献值+昨日的品宣贡献值
$all_value = $brandGongxian+$v['all_value'];
//贡献值自动转换为超级积分 暂定1:1
$superBrand = $all_value;
//超级积分自动转换为增值积分 按照每天的比例
$brandAdd = $superBrand;
$user = userModel::getDB()->where('uid', $v['uid'])->find();
//品宣积分清0
$user->brand_integral = 0;
//总贡献清0
$user->all_value = 0;
//超级积分清0
$user->super_brand = 0;
//更新增值积分
$user->brand_add += $brandAdd;
//天数+1
$user->super_days += 1;
$user->save();
Db::commit();
} catch (\Exception $exception) {
echo $exception->getMessage();
Db::rollback();
}
}
die;
echo env('brand.brand_integral', '100');
die;
echo date('Y-m-d H:i:s').'完成';
}

@ -14,6 +14,7 @@
namespace app\controller\api;
use app\common\dao\store\order\StoreOrderDao;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\order\StoreRefundOrderRepository;
use app\common\repositories\system\notice\SystemNoticeConfigRepository;
@ -44,6 +45,8 @@ use think\facade\Cache;
use think\facade\Log;
use think\facade\Queue;
use crmeb\jobs\SendSmsJob;
use crmeb\jobs\OrderPartnerJob;
use app\common\model\store\order\StoreOrder;
/**
* Class Auth
@ -55,6 +58,10 @@ class Auth extends BaseController
{
public function test()
{
$order = StoreOrder::getDB()->where('order_id', 9)->find()->toArray();
$job = new OrderPartnerJob();
$res = $job->fire($order);
// $data = [
// 'tempId' => '',
// 'id' => '',

@ -21,6 +21,8 @@ use app\common\repositories\user\MemberinterestsRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserBrokerageRepository;
use app\common\model\user\ValueContributionLog;
use app\common\model\store\product\Product;
use app\common\model\user\CashLog;
use app\common\repositories\user\UserRepository;
use app\common\repositories\user\UserVisitRepository;
use app\validate\api\UserBaseInfoValidate;
@ -391,9 +393,61 @@ class User extends BaseController
//提现申请
public function cashWithdrawal()
{
$price = (float)$this->request->param('price');
//判断用户有无这么多贡献值
$user = userModel::getDB()
->where('uid', $this->user->uid)
->find();
if (bccomp($user->all_value, $price) == -1) {
return app('json')->fail('余额不够提现');
}
//扣钱
$user->all_value -= $price;
$user->save();
//加到log日志
CashLog::getDB()->insert([
'status' => 0,
'image' => $this->request->param('image'),
'price' => $price,
'ctime' => date('Y-m-d H:i:s'),
'memo' => '发起提现申请' .$price.'贡献值',
'user_id' => $this->user->uid,
]);
return app('json')->success('申请成功');
}
//获取提现记录
public function cashLog()
{
$list = CashLog::getDB()
->where('user_id', $this->user->uid)
->page((int)$this->request->param('page'), $this->request->param('limit'))->select();
return app('json')->success($list);
}
//获取提现记录
public function getAreaList()
{
$type = ','.$this->request->param('type').',';
$product_list = Product::getDB()->alias('a')
->leftJoin('StoreSpu b', 'a.product_id = b.product_id')
->field('a.*,b.mer_labels')
->where('b.is_del',0)
->where('a.is_del',0)
->where('a.is_show',1)
->where('a.status',1)
->where('a.is_used',1)
->where('b.mer_labels',$type)
->page((int)$this->request->param('page'), $this->request->param('limit'))->select()->toArray();
return app('json')->success($product_list);
}

@ -37,20 +37,21 @@ class OrderPartnerJob implements JobInterface
try {
//初始化贡献值
$bao_price = $mian_price = $ping_price = $total_price = 0;
$bao_flowing = $mian_flowing = $ping_flowing = 0;
$brand_ids = $trade_ids = $user_log_data = [];
//查询商品类型 -报单区 100% 免单区66.7% 平价区66.7%
$order_product = StoreOrderProduct::getDB()
->where('order_id', $data['order_id'])
->select()->toArray();
$product_ids = array_column($order_product, null, $order_product['product_id']);
$prodcut_temp = [];
foreach ($order_product as $pk => $pv) {
$product_ids[] = $pv['product_id'];
$prodcut_temp[$pv['product_id']] = $pv;
}
$product_list = Product::getDB()->alias('a')
->leftJoin('Spu b', 'a.product_id = b.product_id')
->leftJoin('StoreSpu b', 'a.product_id = b.product_id')
->field('a.*,b.mer_labels')
->whereIn('product_id', $product_ids)
->whereIn('a.product_id', $product_ids)
->select()->toArray();
$has_contribute = false;
if (!empty($product_list)) {
@ -60,6 +61,7 @@ class OrderPartnerJob implements JobInterface
$has_contribute = true;
//计算报单区 100%金额
$bao_price = $prodcut_temp[$v['product_id']]['product_price'];
$bao_flowing = bcadd($bao_flowing, $bao_price, 2);
$total_price = bcadd($total_price, $bao_price, 2);
} elseif ($v['mer_labels'] == ',2,') {
$brand_ids[] = $v['brand_id'];
@ -67,6 +69,7 @@ class OrderPartnerJob implements JobInterface
if ($prodcut_temp[$v['product_id']]['product_price'] > 0 && $data['commission_rate'] > 0) {
//计算当前商品的毛利占比
$has_contribute = true;
$mian_flowing = bcadd($mian_flowing, $prodcut_temp[$v['product_id']]['product_price'], 2);
$mian_rate = bcdiv($prodcut_temp[$v['product_id']]['product_price'], $data['pay_price'], 3);
$mian_price = bcmul($data['commission_rate'], $mian_rate, 2);
$mian_price = bcmul($mian_price, 0.667, 2);
@ -76,6 +79,7 @@ class OrderPartnerJob implements JobInterface
$brand_ids[] = $v['brand_id'];
if ($prodcut_temp[$v['product_id']]['product_price'] > 0 && $data['commission_rate'] > 0) {
$has_contribute = true;
$ping_flowing = bcadd($ping_flowing, $prodcut_temp[$v['product_id']]['product_price'], 2);
$ping_rate = bcdiv($prodcut_temp[$v['product_id']]['product_price'], $data['pay_price'], 3);
$ping_price = bcmul($data['commission_rate'], $ping_rate, 2);
$ping_price = bcmul($ping_price, 0.667, 2);
@ -90,7 +94,9 @@ class OrderPartnerJob implements JobInterface
throw new Exception('订单没有指定三种类型商品,不计入贡献值统计');
}
//读取品牌所属分类
if (!empty($brand_ids)) {
$brand_ids = array_unique($brand_ids);
$cat_ids = StoreBrand::getDB()->whereIn('brand_id', $brand_ids)->column('brand_category_id');
$trade_ids = $cat_ids;
$all_cat_ids = StoreBrandCategory::getDB()->whereIn('store_brand_category_id', $cat_ids)->column('path');
@ -125,10 +131,12 @@ class OrderPartnerJob implements JobInterface
'district_id' => $address['district_id'] ?? '',
'street_id' => $address['street_id'] ?? '',
'created_time' => date('Y-m-d H:i:s'),
'baodan_liushui' => $bao_flowing,
];
StoreOrderContribute::getDB()->insert($contribute_insert);
//订单角色贡献值-30%
$user_value = bcmul($total_price, 0.3, 2);
$user = User::getDB()->where('uid', $data['uid'])->find();
$user->all_value = bcadd($user->all_value, $user_value, 2);
$user->save();
@ -148,14 +156,17 @@ class OrderPartnerJob implements JobInterface
->where('status', 1)
->whereIn('brand_id', $brand_ids)
->select()->toArray();
$role_ids = array_column($brand_roles, null, 'id');
$brand_brand_data = array_column($brand_roles, 'id');
$role_ids = array_column($brand_roles, 'id');
$brand_brand_data = array_column($brand_roles, null, 'id');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('uid', '<>', $data['uid'])
->where('u.uid', '<>', $data['uid'])
->field('u.*,p.partner_id')
->select()->toArray();
$user_count = count($user_list);
foreach ($user_list as $buk => $buv) {
$role_data = $brand_brand_data[$buv['partner_id']] ?? [];
@ -178,17 +189,19 @@ class OrderPartnerJob implements JobInterface
}
//处理合作人角色是否包含此商品行业
if (!empty($trade_ids)) {
$trade_roles = Partner::getDB()
->where('type', 4)
->where('status', 1)
->whereIn('trade_id', $trade_ids)
->select()->toArray();
$role_ids = array_column($trade_roles, null, 'id');
$trade_trade_data = array_column($trade_roles, 'id');
$role_ids = array_column($trade_roles, 'id');
$trade_trade_data = array_column($trade_roles,null, 'id');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('uid', '<>', $data['uid'])
->where('u.uid', '<>', $data['uid'])
->field('u.*,p.partner_id')
->select()->toArray();
$user_count = count($user_list);
@ -219,18 +232,20 @@ class OrderPartnerJob implements JobInterface
$street_id = $address['street_id'] ?? '';
$area_ids = [$province_id, $city_id, $district_id, $street_id];
$area_ids = array_values(array_filter($area_ids));
$area_roles = Partner::getDB()
->where('is_area', 1)
->where('status', 1)
->whereNotIn('type', [3, 4])
->whereIn('area_id', $area_ids)
->select()->toArray();
$role_ids = array_column($area_roles, null, 'id');
$trade_trade_data = array_column($area_roles, 'id');
$role_ids = array_column($area_roles, 'id');
$trade_trade_data = array_column($area_roles, null,'id');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('uid', '<>', $data['uid'])
->where('u.uid', '<>', $data['uid'])
->field('u.*,p.partner_id')
->select()->toArray();
$user_count = count($user_list);
@ -247,7 +262,7 @@ class OrderPartnerJob implements JobInterface
'types_of' => 1,
'num' => $role_value,
'ctime' => date('Y-m-d H:i:s'),
'memo' => '订单消费行业贡献值',
'memo' => '订单消费区域贡献值',
'user_id' => $auv['uid'],
];
}
@ -255,13 +270,52 @@ class OrderPartnerJob implements JobInterface
}
//查询通用角色
$common_roles = Partner::getDB()
->where('type', 1)
->where('is_area', 0)
->where('status', 1)
->select()->toArray();
$role_ids = array_column($common_roles, 'id', );
$common_trade_data = array_column($common_roles, null,'id');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->field('u.*,p.partner_id')
->whereIn('p.partner_id', $role_ids)
->where('u.uid', '<>', $data['uid'])
->select();
if(!empty($user_list)){
$user_list = $user_list->toArray();
$user_count = count($user_list);
foreach ($user_list as $cuk => $cuv) {
$role_data = $common_trade_data[$cuv['partner_id']] ?? [];
if (!empty($role_data)) {
$role_value = bcmul($total_price, $role_data['ratio'] / 100, 2);
$role_value = bcdiv($role_value, $user_count, 2);
$user = User::getDB()->where('uid', $cuv['uid'])->find();
$user->all_value = bcadd($user->all_value, $role_value, 2);
$user->save();
$user_log_data[] = [
'type' => 5,
'types_of' => 1,
'num' => $role_value,
'ctime' => date('Y-m-d H:i:s'),
'memo' => '订单消费通用贡献值',
'user_id' => $cuv['uid'],
];
}
}
}
//批量增加用户贡献值日志
ValueContributionLog::getDB()->insertAll($user_log_data);
echo 'ok';
} catch (\Exception $e) {
Log::info('订单角色贡献值处理失败; error : ' . $e->getMessage());
}
$job->delete();
}
public function failed($data)

@ -113,8 +113,8 @@ Route::group('api/', function () {
Route::get('getBrandList', 'User/getBrandList');
Route::get('getPayConfig', 'User/getPayConfig');
Route::get('cashWithdrawal', 'User/cashWithdrawal');
Route::get('cashLog', 'User/cashLog');
Route::get('getAreaList', 'User/getAreaList');

Loading…
Cancel
Save