企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 查询订单商品满足的最优满减满折活动信息(结算用) **位置:** Common\Lib\EventLib.class.php **参数:** * @param array $order_goods 订单商品列表数组 * @param int $use 用途,2表示门店结算, 3表示小程序结算 * @param int $customer_id 客户ID **调用:** ~~~ $event = new EventLib(); $order_goods = array( array( "sku_id"=>20467, // 购买的SKUID "ac_price"=>3000, // SKU限时折扣活动折后单价 "num"=>1 // 购买数量 ), array( "sku_id"=>20468, "ac_price"=>1000, "num"=>1 ) ); $total_event = $event->orderTotalEvent($order_goods, 3, 14983); // 14983董先生的顾客ID ~~~ **返回:** 返回最优满减满折活动信息$active数组 ,数组各项含义如下 : $active['event_id'] // 活动ID $active['coupon'] // 满减满折优惠金额 $active['after_coupon'] // 活动优惠后的订单总金额 $active['event_name'] // 活动名称 $active['url'] // POS中活动详情链接 **完整代码:** ~~~ /** * 查询订单商品满足的最优满减满折活动信息 * QJM 2018-07-18 * @param array $order_goods 订单商品列表数组 * $order_goods = array( * array( * "sku_id"=>20467, // 购买的SKUID * "ac_price"=>3000, // SKU限时折扣活动折后单价 * "num"=>1 // 购买数量 * ), * array( * "sku_id"=>20468, * "ac_price"=>1000, * "num"=>1 * ) * ) * @param int $use 用途,2表示门店结算, 3表示商城结算 * @param int $customer_id 客户ID * @return array 活动优惠信息 */ public function orderTotalEvent($order_goods, $use, $customer_id) { // 1、根据customer_id获取会员级别ID if (empty($customer_id) || $customer_id == 0) { $vip_type = 0; // 0表示普通顾客 } else { $important = $this->getVipType($customer_id); if ($important == -1) { return -1; // '异常2001:未找到该顾客信息,请新增顾客信息' } $vip_type = $important['ber_id']; // 会员级别ID } // 2、根据用途和会员类型,获取符合的满减、满折活动基本信息 $event_reduce = $this->getEvent(2, $use, $vip_type); // 调用封装的函数获取满减活动 $event_discount = $this->getEvent(3, $use, $vip_type); // 调用封装的函数获取满折活动 $event_result = array_merge($event_reduce, $event_discount); // 合并满减满折活动列表 // 3、判断订单商品列表中的SKU是否在营销活动的范围内 $event_goods = array(); // 记录符合营销活动的商品和活动ID foreach ($event_result as $key_a => $value_a) { $this_goods = 0; // 记录每个活动符合范围的商品信息 0代表没有商品符合活动范围 1代表有商品符合活动范围 foreach ($order_goods as $key_b => $value_b) { // 根据传入的商品ID获取商品的SPUID和SKUID及商品品类信息 $sku_id = $value_b['sku_id']; // 获取sku_id $spu_id = M('goods_sku')->where('id=' . $sku_id)->getField('spu_id'); // 获取spu_id $goods = M('goods_sku as sku') ->field('sku.original_price as price, sku.goods_sku, skc.goods_skc, spu.brand_id as brand, spu.spu_no') ->join('coscia_goods_skc as skc on skc.id = sku.skc_id') ->join('coscia_goods_spu as spu on spu.id = sku.spu_id') ->where('sku.id ='.$sku_id) ->find(); $goods['sex'] = M('goods_spu_category')->where('level=1 and spu_id=' . $spu_id)->getField('cat_id'); // 获取性别ID $goods['big_class'] = M('goods_spu_category')->where('level=2 and spu_id=' . $spu_id)->getField('cat_id'); // 获取大类ID // 满减满折范围 1为品类,2为SPU,3为SKU if ($value_a['order_reduce'] == 1 || $value_a['order_discount'] == 1) { $where_a['event_id'] = array('eq', $value_a['id']); $where_a['is_delete'] = array('eq', 0); $event_class = M('event_total_goods_class')->where($where_a)->select(); foreach ($event_class as $key_c => $value_c) { // 判断品牌ID是否相等 if (!empty($value_c['brand_id'])) { if ($value_c['brand_id'] != $goods['brand'] && $value_c['cat_big_id'] != 1) { unset($event_class[$key_c]); continue; } } // 判断性别ID是否相等 if (!empty($value_c['cat_sex_id'])) { if ($value_c['cat_sex_id'] != $goods['sex'] && $value_c['cat_big_id'] != 1) { unset($event_class[$key_c]); continue; } } // 判断大类ID是否相等 if (!empty($value_c['cat_big_id'])) { if ($value_c['cat_big_id'] != $goods['big_class'] && $value_c['cat_big_id'] != 1) { unset($event_class[$key_c]); continue; } } } // 只要满足其中一个活动范围,说明这商品满足活动范围 if (!empty($event_class)) { $this_goods = 1; $event_goods[] = array('goods' => $value_b ,'event_id' => $value_a['id']); } } elseif ($value_a['order_reduce'] == 2 || $value_a['order_discount'] == 2) { $where_b['event_id'] = array('eq', $value_a['id']); $where_b['mark'] = array('eq', 1); // 1是款号;2是SKU $where_b['is_delete'] = array('eq', 0); $where_b['menu_id'] = array('eq', $spu_id); $event_no = M('event_total_goods_no')->where($where_b)->select(); // 待测试商品SKU对应款号 $spu_no = $goods['spu_no']; foreach ($event_no as $key_d => $value_d) { if ($value_d['system_style_no'] == $spu_no) { $this_goods = 1; $event_goods[] = array('goods' => $value_b ,'event_id' => $value_a['id']); break; } } } elseif ($value_a['order_reduce'] == 3 || $value_a['order_discount'] == 3) { $where_c['event_id'] = array('eq', $value_a['id']); $where_c['is_delete'] = array('eq', 0); $event_no = M('event_total_goods_no')->where($where_c)->select(); foreach ($event_no as $key_e => $value_e) { if ($value_e['system_style_no'] == $goods['goods_sku']) { $this_goods = 1; $event_goods[] = array('goods' => $value_b, 'event_id' => $value_a['id']); break; } } } elseif ($value_a['order_reduce'] == 4 || $value_a['order_discount'] == 4) { $where_d['event_id'] = array('eq', $value_a['id']); $where_d['is_delete'] = array('eq', 0); $event_no = M('event_total_goods_no')->where($where_d)->select(); foreach ($event_no as $key_f => $value_f) { if ($value_f['system_style_no'] == $goods['goods_skc']) { $this_goods = 1; $event_goods[] = array('goods' => $value_b, 'event_id' => $value_a['id']); break; } } } } // 判断没有活动商品符合条件,筛选掉这个活动信息 if ($this_goods == 0) { unset($event_result[$key_a]); } } if (empty($event_result)) { return -2; // 无符合的活动 } // 4、判断每个活动范围的商品符合的满减满折规则,找出每个活动的最优规则 foreach ($event_result as $key_a => $value_a) { // 满减规则的逻辑判断 if (!empty($value_a['order_reduce'])) { $where_d['event_id'] = array('eq', $value_a['id']); $where_d['type'] = array('eq', 2); $rules = M('event_rules')->where($where_d)->select(); $price_sum = 0; foreach ($event_goods as $key_b => $value_b) { if ($value_b['event_id'] == $value_a['id']) { // 得出活动商品的消费总金额 $price_sum += $value_b['goods']['ac_price'] * $value_b['goods']['num']; } } foreach ($rules as $key_c => $value_c) { if ($value_c['full'] > $price_sum) { unset($rules[$key_c]); } } // 如果本活动的活动规则都不符合订单总价,删除掉活动 if (empty($rules)) { unset($event_result[$key_a]); } else { // 最优满减规则判断 $rules_full = 0; $k = 0; foreach ($rules as $key_d => $value_d) { if (empty($rules_full)) { $rules_full = $value_d['full']; $k = $key_d; continue; } if ($value_d['full'] < $rules_full) { unset($rules[$key_d]); } else { unset($rules[$k]); $k = $key_d; $rules_full = $value_d['full']; } } } } // 满折规则的逻辑判断 else { // 查询满折规则的数组,需要符合最优原则 $where_e['event_id'] = array('eq',$value_a['id']); $where_e['type'] = array('eq',3); $rules = M('event_rules')->where($where_e)->select(); $count = 0; foreach ($event_goods as $key_h => $value_h) { if ($value_h['event_id'] == $value_a['id']) { // 得出活动商品的总数量 $count += $value_h['goods']['num']; } } foreach ($rules as $key_c => $value_c) { if ($value_c['full'] > $count) { unset($rules[$key_c]); } } if (empty($rules)) { unset($event_result[$key_a]); } else { // 最优满折规则判断 $rules_full = 0; $k = 0; foreach ($rules as $key_d => $value_d) { if (empty($rules_full)) { $rules_full = $value_d['full']; $k = $key_d; continue; } if ($value_d['full'] < $rules_full) { unset($rules[$key_d]); } else { unset($rules[$k]); $k = $key_d; $rules_full = $value_d['full']; } } } } // 将每个活动中订单满足的最优的规则作为活动规则项保存到活动数组 if (!empty($rules)) { $r_a = array(); foreach ($rules as $k => $v) { $r_a = $v; } $event_result[$key_a]['rules'] = $r_a; } } if (empty($event_result)) { return -2; // 无符合的活动 } // 5、最优活动判断,分别计算整单的优惠金多少,哪个优惠金额多,就是最优营销活动 $all_price = 0; // 初始化当前整单总金额为0 foreach($order_goods as $key_z => $value_z) { $all_price += $value_z['ac_price'] * $value_z['num']; } $coupon = 0; // 设置优惠金初始值 $k_a = 0; foreach ($event_result as $key_a => $value_a) { // 初始参数设定 if (empty($coupon)) { // 满减规则 if ($value_a['rules']['type'] == 2) { $coupon = $value_a['rules']['event_reduce']; $k_a = $key_a; continue; } // 满折规则 elseif ($value_a['rules']['type'] == 3) { $rules_goods = []; // 符合当前满折活动的商品 $event_goods是所有活动商品 foreach ($event_goods as $key_c => $value) { if($value['event_id'] == $event_result[$key_a]['id']) { $rules_goods[] = $event_goods[$key_c]; } } // 这里不能整单进行打折,需要只对活动商品打折 $ac_goods = array_column($rules_goods,'goods'); // 参与该满折活动的商品 // 初始化参与本活动的商品的总价格为0 $total_price = 0; foreach ($ac_goods as $key_f => $value_f) { $total_price += ($value_f['ac_price'] * $value_f['num']); } $coupon = $total_price * (1-$value_a['rules']['event_reduce']); // 减多少元或者打几折 $k_a = $key_a; continue; } } // 满减规则 if ($value_a['rules']['type'] == 2) { if ($value_a['rules']['reduce'] < $coupon) { unset($event_result[$key_a]); } else { unset($event_result[$k_a]); $k_a = $key_a; $coupon = $value_a['rules']['reduce']; } } // 满折规则 elseif ($value_a['rules']['type'] == 3) { $rules_goods = []; // 符合当前满折活动的商品 $event_goods是所有活动商品 foreach ($event_goods as $key_c => $value) { if($value['event_id'] == $event_result[$key_a]['id']) { $rules_goods[] = $event_goods[$key_c]; } } // 这里不能整单进行打折,需要只对活动商品打折 $ac_goods = array_column($rules_goods,'goods'); // 参与该满折活动的商品 // 初始化参与本活动的商品的总价格为0 $total_price = 0; foreach ($ac_goods as $key_f => $value_f) { $total_price += ($value_f['ac_price'] * $value_f['num']); } $coupon_a = $total_price * (1-$value_a['rules']['event_reduce']); if ($coupon_a < $coupon) { unset($event_result[$key_a]); } else { unset($event_result[$k_a]); $k_a = $key_a; $coupon = $coupon_a; } } } if (empty($event_result)) { return -2; // 无符合的活动 } $event_a = array(); foreach ($event_result as $key => $value) { $event_a = $value; } $coupon = round($coupon,2); // 满减满折优惠总金额 小数点后2位四舍五入 $after_coupon = $all_price - $coupon; // 最终活动优惠后的金额 $url = U("/Wpos/Member/Event/index",array('id' => $event_a['id'])); // 活动详情链接 $active = array( 'event_id' => $event_a['id'], 'coupon' => $coupon, 'after_coupon' => $after_coupon, 'event_name' => $event_a['event_name'] , 'rules' => $event_a['rules'], 'url' => $url ); return $active; } ~~~