diff --git a/app/common/model/store/order/StoreOrderContribute.php b/app/common/model/store/order/StoreOrderContribute.php new file mode 100644 index 0000000..29361b9 --- /dev/null +++ b/app/common/model/store/order/StoreOrderContribute.php @@ -0,0 +1,47 @@ + +// +---------------------------------------------------------------------- + + +namespace app\common\model\store\order; + + +use app\common\model\BaseModel; + +/** + * Class StoreOrderStatus + * @package app\common\model\store\order + * @author xaboy + * @day 2020/6/12 + */ +class StoreOrderContribute extends BaseModel +{ + + /** + * @return string|null + * @author xaboy + * @day 2020/6/12 + */ + public static function tablePk(): ?string + { + return 'id'; + } + + /** + * @return string + * @author xaboy + * @day 2020/6/12 + */ + public static function tableName(): string + { + return 'store_order_contribute'; + } +} diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index d8ec8c4..c70250f 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -49,11 +49,11 @@ class StoreOrderCreateRepository extends StoreOrderRepository //检查购物车失效数据 if (count($fail)) { if ($fail[0]['is_fail']) - throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'],0,10).'...'); + throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); if (in_array($fail[0]['product_type'], [1, 2, 3]) && !$fail[0]['userPayCount']) { - throw new ValidateException('[超出限购数]' . mb_substr($fail[0]['product']['store_name'],0,10).'...'); + throw new ValidateException('[超出限购数]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); } - throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'],0,10).'...'); + throw new ValidateException('[已失效]' . mb_substr($fail[0]['product']['store_name'], 0, 10) . '...'); } $svip_status = $user->is_svip > 0 && systemConfig('svip_switch_status') == '1'; @@ -67,17 +67,17 @@ class StoreOrderCreateRepository extends StoreOrderRepository //检查商品类型, 活动商品只能单独购买 foreach ($merchantCartList as $merchantCart) { foreach ($merchantCart['list'] as $cart) { - if ($cart['product_type']==0) { - if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) - throw new ValidateException('[低于起购数:'.$cart['product']['once_min_count'].']'.mb_substr($cart['product']['store_name'],0,10).'...'); + if ($cart['product_type'] == 0) { + if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) + throw new ValidateException('[低于起购数:' . $cart['product']['once_min_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); if ($cart['product']['pay_limit'] == 1 && $cart['product']['once_max_count'] < $cart['cart_num']) - throw new ValidateException('[超出单次限购数:'.$cart['product']['once_max_count'].']'.mb_substr($cart['product']['store_name'],0,10).'...'); - if ($cart['product']['pay_limit'] == 2){ + throw new ValidateException('[超出单次限购数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); + if ($cart['product']['pay_limit'] == 2) { //如果长期限购 //已购买数量 - $count = app()->make(StoreOrderRepository::class)->getMaxCountNumber($cart['uid'],$cart['product_id']); + $count = app()->make(StoreOrderRepository::class)->getMaxCountNumber($cart['uid'], $cart['product_id']); if (($cart['cart_num'] + $count) > $cart['product']['once_max_count']) - throw new ValidateException('[超出限购总数:'. $cart['product']['once_max_count'].']'.mb_substr($cart['product']['store_name'],0,10).'...'); + throw new ValidateException('[超出限购总数:' . $cart['product']['once_max_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); } } @@ -385,7 +385,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $coupon['disabled'] = true; continue; } - if($svip_coupon_merge != '1' && $use_svip){ + if ($svip_coupon_merge != '1' && $use_svip) { $coupon['disabled'] = true; continue; } @@ -440,7 +440,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $coupon['checked'] = false; $coupon['disabled'] = $pay_price <= 0; if ($use_store_coupon || $pay_price <= 0) continue; - if($svip_coupon_merge != '1' && $use_svip){ + if ($svip_coupon_merge != '1' && $use_svip) { $coupon['disabled'] = true; continue; } @@ -527,7 +527,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository } unset($cart, $_k); $total_true_price = bcadd($_pay_price, $total_true_price, 2); - if(count($merchantCartList) > 1 || count($merchantCart['list']) > 1){ + if (count($merchantCartList) > 1 || count($merchantCart['list']) > 1) { $orderDeliveryStatus = $orderDeliveryStatus && $deliveryStatus; } $merchantCart['order'] = [ @@ -762,7 +762,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository $integralRate = $cart['product']['integral_rate']; if ($integralRate < 0) { $integralRate = $merIntegralConfig['mer_integral_rate']; - } else if($integralRate > 0){ + } else if ($integralRate > 0) { $integralRate = min(bcdiv($integralRate, 100, 4), 1); } if ($integralRate > 0) { @@ -1092,7 +1092,8 @@ class StoreOrderCreateRepository extends StoreOrderRepository 'mark' => $mark[$merchantCart['mer_id']] ?? '', 'coupon_price' => bcadd($merchantCart['order']['coupon_price'], $merchantCart['order']['platform_coupon_price'], 2), 'platform_coupon_price' => $merchantCart['order']['platform_coupon_price'], - 'pay_type' => $pay_type + 'pay_type' => $pay_type, + 'user_address_id' => $user_address['address_id'] ?? 0, ]; $allUseCoupon = array_merge($allUseCoupon, $merchantCart['order']['useCouponIds']); $orderList[] = $_order; diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index c825be5..a847aa9 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -744,9 +744,11 @@ class StoreOrderRepository extends BaseRepository $order->save(); $storeOrderStatusRepository->{$func}($orderStatus); }); - event('order.take', compact('order')); //推送到贡献值结算队列 Queue::push(OrderPartnerJob::class, compact('order')); + + event('order.take', compact('order')); + } diff --git a/crmeb/jobs/OrderPartnerJob.php b/crmeb/jobs/OrderPartnerJob.php index 89df5f6..c1e3a62 100644 --- a/crmeb/jobs/OrderPartnerJob.php +++ b/crmeb/jobs/OrderPartnerJob.php @@ -14,22 +14,251 @@ namespace crmeb\jobs; use crmeb\interfaces\JobInterface; +use think\Exception; use think\facade\Log; use think\queue\Job; -use crmeb\services\ExcelService; +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{ - //查询商品类型 -报单区 100% 其他 66.7% - //查询合作人角色是否包含此商品品牌 - //查询合作人橘色是否包含此商品行业 + 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'], + ]; + } + } + } + //查询通用角色 - }catch (\Exception $e){ + + //批量增加用户贡献值日志 + ValueContributionLog::getDB()->insertAll($user_log_data); + } catch (\Exception $e) { Log::info('订单角色贡献值处理失败; error : ' . $e->getMessage()); } $job->delete();