// +---------------------------------------------------------------------- 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; use app\common\model\store\order\StoreOrderLevel; class OrderPartnerJob implements JobInterface { public function fire($job, $data) { file_put_contents('/tmp/debug_partner', '开始处理贡献值订单:' . var_export($data, 1), 8); try { //初始化贡献值 $bao_price = $mian_price = $ping_price = $total_price = 0; $bao_flowing = $mian_flowing = $ping_flowing = 0; $brand_ids = $trade_ids = $user_log_data = []; $admin_id = config('partner.gongxian_admin_id') ?? ''; //查询商品类型 -报单区 100% 免单区66.7% 平价区66.7% $order_product = StoreOrderProduct::getDB() ->where('order_id', $data['order_id']) ->select()->toArray(); $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('StoreSpu b', 'a.product_id = b.product_id') ->field('a.*,b.mer_labels') ->whereIn('a.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']; $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']; //计算免单区 订单毛利的66.7% 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); $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_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); $total_price = bcadd($total_price, $ping_price, 2); } } } } //如果商品未包含指定的三种类型,则不计入贡献值记录 if ($has_contribute == false) { 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'); 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'), 'baodan_liushui' => $bao_flowing, 'miandan_liushui' => $mian_flowing, 'ping_liushui' => $ping_flowing, ]; StoreOrderContribute::getDB()->insert($contribute_insert); //读取消费等级增加生命值 $user_hb = 0; $order_level = StoreOrderLevel::getDB()->order('level asc')->select(); if(!empty($order_level)){ foreach($order_level as $k=>$v){ if($total_price >= $v->min_price){ $user_hb = bcmul($total_price,$v->pay_hp,2); break; } } } //订单角色贡献值-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); //增加生命值 if(!empty($user_hb)){ $user->hp_value = bcadd($user->hp_value, $user_hb, 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, 'id'); $brand_brand_data = array_column($brand_roles, null, 'id'); $role_list = UserPartner::getDB() ->whereIn('partner_id', $role_ids) ->where('uid', '<>', $data['uid']) ->select()->toArray(); foreach ($role_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); if (empty($buv['uid'])) { $user = User::getDB()->where('uid', $admin_id)->find();; } else { $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, 'id'); $trade_trade_data = array_column($trade_roles, null, 'id'); $role_list = UserPartner::getDB() ->whereIn('partner_id', $role_ids) ->where('uid', '<>', $data['uid']) ->select()->toArray(); foreach ($role_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); if (empty($tuv['uid'])) { $user = User::getDB()->where('uid', $admin_id)->find();; } else { $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, 'id'); $trade_trade_data = array_column($area_roles, null, 'id'); $role_list = UserPartner::getDB() ->whereIn('partner_id', $role_ids) ->where('uid', '<>', $data['uid']) ->select()->toArray(); foreach ($role_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); if (empty($auv['uid'])) { $user = User::getDB()->where('uid', $admin_id)->find();; } else { $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'], ]; } } } //查询大区角色 $region_list = config('partner.region_type'); $region_ids = []; if (!empty($province_id) && !empty($region_list)) { foreach ($region_list as $rv) { if (in_array($province_id, $rv['province_ids'])) { $region_ids[] = $rv['id']; } } if(!empty($region_ids)){ $region_roles = Partner::getDB() ->where('status', 1) ->whereNotIn('type', 2) ->whereIn('region_id', $region_ids) ->select()->toArray(); $role_ids = array_column($region_roles, 'id'); $regin_trade_data = array_column($region_roles, null, 'id'); $role_list = UserPartner::getDB() ->whereIn('partner_id', $role_ids) ->where('uid', '<>', $data['uid']) ->select()->toArray(); foreach ($role_list as $ruk => $ruv) { $role_data = $regin_trade_data[$ruv['partner_id']] ?? []; if (!empty($role_data)) { $role_value = bcmul($total_price, $role_data['ratio'] / 100, 2); if (empty($ruv['uid'])) { $user = User::getDB()->where('uid', $admin_id)->find();; } else { $user = User::getDB()->where('uid', $ruv['uid'])->find(); } $user->all_value = bcadd($user->all_value, $role_value, 2); $user->save(); $user_log_data[] = [ 'type' => 6, 'types_of' => 1, 'num' => $role_value, 'ctime' => date('Y-m-d H:i:s'), 'memo' => '订单消费大区贡献值', 'user_id' => $ruv['uid'], ]; } } } } //查询通用角色 $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'); $role_list = UserPartner::getDB() ->whereIn('partner_id', $role_ids) ->where('uid', '<>', $data['uid']) ->select(); if (!empty($role_list)) { $role_list = $role_list->toArray(); foreach ($role_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); if (empty($cuv['uid'])) { $user = User::getDB()->where('uid', $admin_id)->find();; } else { $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) { // TODO: Implement failed() method. } //贡献值转增值积分 public function addValue($uid,$num,$user_start) { //读取用户当前贡献值增长率 } }