You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
crmeb_php/crmeb/jobs/OrderPartnerJob.php

271 lines
13 KiB

<?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 crmeb\interfaces\JobInterface;
use think\Exception;
use think\facade\Log;
use think\queue\Job;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\product\Product;
use app\common\model\store\StoreBrand;
use app\common\model\store\StoreBrandCategory;
use app\common\model\system\admin\Partner;
use app\common\model\user\User;
use app\common\model\user\UserPartner;
use app\common\model\store\product\Spu;
use app\common\model\user\ValueContributionLog;
use app\common\model\store\order\StoreOrderContribute;
use app\common\repositories\user\UserAddressRepository;
class OrderPartnerJob implements JobInterface
{
public function fire($job, $data)
{
try {
//初始化贡献值
$bao_price = $mian_price = $ping_price = $total_price = 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) {
$prodcut_temp[$pv['product_id']] = $pv;
}
$product_list = Product::getDB()->alias('a')
->leftJoin('Spu b', 'a.product_id = b.product_id')
->field('a.*,b.mer_labels')
->whereIn('product_id', $product_ids)
->select()->toArray();
$has_contribute = false;
if (!empty($product_list)) {
foreach ($product_list as $k => $v) {
if ($v['mer_labels'] == ',1,') {
$brand_ids[] = $v['brand_id'];
$has_contribute = true;
//计算报单区 100%金额
$bao_price = $prodcut_temp[$v['product_id']]['product_price'];
$total_price = bcadd($total_price, $bao_price, 2);
} elseif ($v['mer_labels'] == ',2,') {
$brand_ids[] = $v['brand_id'];
//计算免单区 订单毛利的66.7%
if ($prodcut_temp[$v['product_id']]['product_price'] > 0 && $data['commission_rate'] > 0) {
//计算当前商品的毛利占比
$has_contribute = true;
$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);
$total_price = bcadd($total_price, $mian_price, 2);
}
} elseif ($v['mer_labels'] == ',3,') {
$brand_ids[] = $v['brand_id'];
if ($prodcut_temp[$v['product_id']]['product_price'] > 0 && $data['commission_rate'] > 0) {
$has_contribute = true;
$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);
$total_price = bcadd($total_price, $ping_price, 2);
}
}
}
}
//如果商品未包含指定的三种类型,则不计入贡献值记录
if ($has_contribute == false) {
throw new Exception('订单没有指定三种类型商品,不计入贡献值统计');
}
//读取品牌所属分类
if (!empty($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');
foreach ($all_cat_ids as $ck => $cv) {
$temp_ids = explode('/', $cv);
if (!empty($temp_ids)) {
foreach ($temp_ids as $tk => $tv) {
$trade_ids[] = $tv;
}
}
}
$trade_ids = array_unique($trade_ids);
}
//读取订单所属地区
$addressRepository = app()->make(UserAddressRepository::class);
$address = $addressRepository->getWhere(['uid' => $data['uid'], 'address_id' => $data['user_address_id']]);
//生成订单贡献值记录
$contribute_insert = [
'order_id' => $data['order_id'],
'order_sn' => $data['order_sn'],
'order_profit' => $total_price,
'order_price' => $data['pay_price'],
'bao_price' => $bao_price,
'mian_price' => $mian_price,
'ping_price' => $ping_price,
'uid' => $data['uid'],
'brand_ids' => implode(',', $brand_ids),
'trade_ids' => implode(',', $trade_ids),
'province_id' => $address['province_id'] ?? '',
'city_id' => $address['city_id'] ?? '',
'district_id' => $address['district_id'] ?? '',
'street_id' => $address['street_id'] ?? '',
'created_time' => date('Y-m-d H:i:s'),
];
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();
$user_log_data[] = [
'type' => 6,
'types_of' => 1,
'num' => $user_value,
'ctime' => date('Y-m-d H:i:s'),
'memo' => '订单消费贡献值',
'user_id' => $data['uid'],
];
//处理合作人角色是否包含此商品品牌
if (!empty($brand_ids)) {
$brand_roles = Partner::getDB()
->where('type', 3)
->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');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('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']] ?? [];
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', $buv['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' => $buv['uid'],
];
}
}
}
//处理合作人角色是否包含此商品行业
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');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('uid', '<>', $data['uid'])
->field('u.*,p.partner_id')
->select()->toArray();
$user_count = count($user_list);
foreach ($user_list as $tuk => $tuv) {
$role_data = $trade_trade_data[$tuv['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', $tuv['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' => $tuv['uid'],
];
}
}
}
//查询所属区域角色
if (!empty($address)) {
$province_id = $address['province_id'] ?? '';
$city_id = $address['city_id'] ?? '';
$district_id = $address['district_id'] ?? '';
$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');
$user_list = User::getDB()->alias('u')
->leftJoin('UserPartner p', 'u.uid = p.uid')
->whereIn('p.partner_id', $role_ids)
->where('uid', '<>', $data['uid'])
->field('u.*,p.partner_id')
->select()->toArray();
$user_count = count($user_list);
foreach ($user_list as $auk => $auv) {
$role_data = $trade_trade_data[$auv['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', $auv['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' => $auv['uid'],
];
}
}
}
//查询通用角色
//批量增加用户贡献值日志
ValueContributionLog::getDB()->insertAll($user_log_data);
} catch (\Exception $e) {
Log::info('订单角色贡献值处理失败; error : ' . $e->getMessage());
}
$job->delete();
}
public function failed($data)
{
// TODO: Implement failed() method.
}
}