$val) { foreach ($sellDataArray_new as $c_key => $c_val) { if ($val['id'] == $c_val['id']) { $sellDataArray[$key] = $c_val; } } } return $sellDataArray; } /** * 收银端登录会员获取优惠券列表 * @param $param * [ * merchant_id * store_id 门店id * vip_id 会员id * m_coupon_id_redis 正在使用的优惠券id集合 * ] * @return mixed */ public function cashierGetCouponList($param) { $m_coupon_id_redis = $param['m_coupon_id_redis']; $time = $_SERVER['REQUEST_TIME']; $where = [ 'r.isuse' => 1, 'r.merchantid' => $param['merchant_id'], 'r.vid' => $param['vip_id'], ]; if (!empty($m_coupon_id_redis)) { $where['c.id'] = ['not in', $m_coupon_id_redis]; } $where[] = [ [ 'c.coupontype' => 1, 'c.starttime' => ['lt', $time], 'c.endtime' => ['gt', $time], ], [ 'c.coupontype' => 2, 'r.addtime + (c.daynum*86400)' => ['gt', $time] ], [ 'c.coupontype' => 3, 'r.addtime+86400' => ['lt', $time], 'r.addtime +86400+(c.daynum*86400)' => ['gt', $time] ], '_logic' => 'OR' ]; $fields = [ 'r.id', 'c.coupon_name', 'c.storeids', 'c.appoint_type', 'c.appoint_id_str', 'c.needmoney as consumption_threshold', 'c.reducemoney as discount_amount', 'c.coupontype as coupon_type', 'c.daynum as day_num', 'c.starttime as start_time', 'c.endtime as end_time', 'r.addtime as receive_time' ]; try { $VipMCouponModel = new RetailVipMCouponModel(); $coupon_list = $VipMCouponModel->getVipCouponList($where, $fields); $list = []; if (!empty($coupon_list)) { foreach ($coupon_list as $val) { $result = $this->appointCouponVerify([ 'verify_goods_type' => 2, 'store_id' => $param['store_id'], 'coupon_info' => $val ]); if ($result['status'] == 1) { $valResult = $this->couponFieldsConvert($val, 1); $list[] = $valResult; } } } return callBack(statusCode()['success'], '查询成功', $list); } catch (\Exception $e) { return callBack(statusCode()['success'], "查询成功", []); } } /** * 下单普通优惠券使用 * @param $param * [ * merchant_id * store_id 门店id * vip_id 会员id * coupon_id 优惠券id * order_money 订单金额 * sellDataArray 购买的商品信息 * ] * @return mixed * [ * status * message * shop_coupon * goods 使用商家券的商品信息 * goods_money 参与优惠的商品总金额 * ] */ public function PlaceOrderCoupon($param) { if (empty($param['merchant_id']) || empty($param['store_id']) || empty($param['coupon_id']) || empty($param['order_money']) || empty($param['sellDataArray'])) { return callBack(statusCode()['error'], "未找到该优惠券"); } $time = $_SERVER['REQUEST_TIME']; $where = [ 'r.id' => $param['coupon_id'], 'r.isuse' => 1, 'r.merchantid' => $param['merchant_id'], 'r.vid' => $param['vip_id'], ]; $where[] = [ [ 'c.coupontype' => 1, 'c.starttime' => ['lt', $time], 'c.endtime' => ['gt', $time], ], [ 'c.coupontype' => 2, 'r.addtime + (c.daynum*86400)' => ['gt', $time] ], [ 'c.coupontype' => 3, 'r.addtime+86400' => ['lt', $time], 'r.addtime+86400+(c.daynum*86400)' => ['gt', $time] ], '_logic' => 'OR' ]; $fields = [ 'r.id', 'c.storeids', 'c.appoint_type', 'c.appoint_id_str', 'c.needmoney as consumption_threshold', 'c.reducemoney as discount_amount', ]; try { $CouponModel = new RetailVipMCouponModel(); $coupon = $CouponModel->getSingleCoupon($where, $fields); if (empty($coupon)) { return callBack(statusCode()['error'], "该优惠券已使用"); } $result = $this->appointCouponVerify([ 'coupon_info' => $coupon, 'store_id' => $param['store_id'], 'order_money' => $param['order_money'], 'sellDataArray' => $param['sellDataArray'] ]); $result['data']['coupon'] = $coupon; return $result; } catch (\Exception $e) { return callBack(statusCode()['success'], '暂无可用优惠券', []); } } /** * 获取会员可用的优惠券列表 * @param $param * [ * merchant_id * store_id 门店id * scene_type 1普通下单场景 2预售下单场景 * vip_info 会员信息 * m_coupon_id_redis 正在使用的优惠券id集合 * shopping_ids 购物车id集合 普通订单使用 * goods_id 预售订单使用 * sku_id 预售规格id * goods_class_id 预售分类id * xcx_id 小程序用户id * is_integral_cash 是否启用积分抵现1启用 0不启用 * order_money 订单金额 * ] * @return mixed */ public function getUsableCouponList($param) { if (empty($param['merchant_id']) || empty($param['store_id']) || empty($param['vip_info'])) { return callBack(statusCode()['error'], "参数有误"); } if (!in_array($param['scene_type'], ['1', '2'])) { return callBack(statusCode()['error'], "场景有误"); } if (empty($param['order_money'])) { return callBack(statusCode()['error'], "订单金额有误"); } switch ($param['scene_type']) { case 1: //普通下单 $ShopClass = new ShopCoupon(); $result = $ShopClass->getShoppingGoods($param); $sellDataArray = $result['data']['sellDataArray']; //购买的商品信息 break; case 2: //预售下单 $sku_json_info = ''; if (!empty($param['sku_id'])) { $sku_json_info = json_encode(['id' => $param['sku_id']]); } $sellDataArray[] = [ 'goods_class_id' => $param['goods_class_id'], 'goods_id' => $param['goods_id'], 'sku_json_info' => $sku_json_info, 'all_money' => $param['order_money'], ]; break; } if (empty($sellDataArray)) { return callBack(statusCode()['error'], '暂无优惠券'); } $m_coupon_id_redis = $param['m_coupon_id_redis']; $time = $_SERVER['REQUEST_TIME']; $where = [ 'r.isuse' => 1, 'r.merchantid' => $param['merchant_id'], 'r.vid' => $param['vip_info']['vid'], ]; if (!empty($m_coupon_id_redis)) { $where['c.id'] = ['not in', $m_coupon_id_redis]; } $where[] = [ [ 'c.coupontype' => 1, 'c.starttime' => ['lt', $time], 'c.endtime' => ['gt', $time], ], [ 'c.coupontype' => 2, 'r.addtime + (c.daynum*86400)' => ['gt', $time] ], [ 'c.coupontype' => 3, 'r.addtime+86400' => ['lt', $time], 'r.addtime +86400+(c.daynum*86400)' => ['gt', $time] ], '_logic' => 'OR' ]; $fields = [ 'r.id', 'c.coupon_name', 'c.storeids', 'c.appoint_type', 'c.appoint_id_str', 'c.needmoney as consumption_threshold', 'c.reducemoney as discount_amount', 'c.coupontype as coupon_type', 'c.daynum as day_num', 'c.starttime as start_time', 'c.endtime as end_time', 'r.addtime as receive_time' ]; try { $VipMCouponModel = new RetailVipMCouponModel(); $coupon_list = $VipMCouponModel->getVipCouponList($where, $fields, 'r.id desc'); $list = []; if (!empty($coupon_list)) { foreach ($coupon_list as &$val) { $timeResult = $this->appointCouponVerify([ 'coupon_info' => $val, 'store_id' => $param['store_id'], 'order_money' => $param['order_money'], 'sellDataArray' => $sellDataArray, ]); //验证 if ($timeResult['status'] == 1) { $valResult = $this->couponFieldsConvert($val, 2); $list[] = $valResult; } else { continue; } } } return callBack(statusCode()['success'], "查询成功", $list); } catch (\Exception $e) { return callBack(statusCode()['success'], "查询成功", []); } } /** * 券字段转换 * @param $val * @param $type 1收银端 2小程序 * @return mixed */ public function couponFieldsConvert($val, $type = 2) { $val['coupon_choice_type'] = 2; //会员普通优惠券 switch ($val['coupon_type']) { case 1: $start_time = date('Y-m-d H:i:s', $val['start_time']); $end_time = date('Y-m-d', $val['end_time']); break; case 2: $start_time = date('Y-m-d H:i:s', $val['receive_time']); $end_time = date('Y-m-d H:i:s', $val['receive_time'] + ($val['day_num'] * 86400)); break; case 3: $start_time = date('Y-m-d H:i:s', ($val['receive_time'] + 86400)); $end_time = date('Y-m-d H:i:s', ($val['receive_time'] + 86400) + ($val['day_num'] * 86400)); break; } $val['begin_time'] = $start_time; $val['end_time'] = $end_time; $val['validity_time'] = $start_time . '至' . $end_time; $val['reduce_money'] = round($val['discount_amount'], 2); $val['need_money'] = round($val['consumption_threshold'], 2); //以下字段收银端转换字段与商家券一致 if ($type == 1) { switch ($val['appoint_type']) { case 1: //全部商品 $val['goods_type'] = 1; $val['goods_id_gather'] = $val['appoint_id_str']; break; case 2: //指定分类 $val['goods_type'] = 3; $GoodsClassify = new GoodsClassify(); $class_arr = $GoodsClassify->basisIdGetLowerId($val['appoint_id_str']); $val['goods_id_gather'] = implode(',', $class_arr); $list = $GoodsClassify->basisIdGetNameList($val['appoint_id_str']); $val['appoint_type_name'] = '指定分类:' . implode(';', $list); break; case 3: //指定商品 $val['goods_type'] = 2; $val['goods_id_gather'] = $val['appoint_id_str']; $SaleGoodsClass = new SaleGoods(); $list = $SaleGoodsClass->appointGoodsList($val['appoint_id_str']); $str = ''; foreach ($list as $v) { $str .= ';' . $v['goods_name']; if (!empty($v['sku_name'])) { $str .= '-' . $v['sku_name']; } } $val['appoint_type_name'] = '部分商品:' . trim($str, ';'); break; } } return $val; } /** * 优惠券使用验证 * @param $param * [ * coupon_info 优惠券信息=>[ * consumption_threshold 消费门槛 * discount_amount 优惠金额 * appoint_type 指定类型 * appoint_id_str 指定商品或分类 * storeids 门店id string * ] * store_id 核销的门店 * order_money 订单的金额 * sellDataArray 商品信息集合[['goods_class_id'=>1,'goods_id'=>1,'sku_json_info'=>'','all_money'=>1]] * verify_goods_type 2 当收银机登录会员或者小程序点击立即使用时获取商家券时不需要验证金额 * ] * @return mixed * [ * activity_goods 参与活动的商品信息 * activity_goods_money 参与活动的商品总金额 * ] */ public function appointCouponVerify($param) { $coupon_info = $param['coupon_info']; $order_money = $param['order_money']; if (empty($coupon_info)) { return callBack(statusCode()['error'], '该券信息有误'); } //门店验证 if ($coupon_info['storeids'] != '0' && !in_array($param['store_id'], explode(',', $coupon_info['storeids']))) { return callBack(statusCode()['error'], "该券不支持该门店"); } if (empty($param['verify_goods_type']) || $param['verify_goods_type'] != 2) { //总金额验证 if ($order_money < $coupon_info['consumption_threshold']) { return callBack(statusCode()['error'], '未满足消费门槛'); } if (empty($order_money)) { return callBack(statusCode()['error'], "订单金额有误"); } $sellDataArray = $param['sellDataArray']; //购买的商品信息 $activity_goods = []; //参与优惠的商品信息 switch ($coupon_info['appoint_type']) { case 1: $appoint_money = $param['order_money']; break; case 2: //指定分类 $GoodsClassify = new GoodsClassify(); $appoint_id_arr = $GoodsClassify->basisIdGetLowerId($coupon_info['appoint_id_str']); //指定的分类集合 $appoint_money = 0; //指定分类的总金额 foreach ($sellDataArray as $value) { if (in_array($value['goods_class_id'], $appoint_id_arr)) { $appoint_money += $value['all_money']; $activity_goods[] = $value; } } if ($appoint_money < $coupon_info['consumption_threshold']) { return callBack(statusCode()['error'], '指定分类的金额未达到优惠条件'); } break; case 3: //指定商品 $appoint_id_arr = explode(',', $coupon_info['appoint_id_str']); $appoint_money = 0; //指定商品的总金额 foreach ($sellDataArray as $value) { $goods_id = ''; $goods_id = $value['goods_id']; if (!empty($value['sku_json_info'])) { $goods_id .= '_' . json_decode($value['sku_json_info'], true)['id']; } if (in_array($goods_id, $appoint_id_arr)) { $appoint_money += $value['all_money']; $activity_goods[] = $value; } } if ($appoint_money < $coupon_info['consumption_threshold']) { return callBack(statusCode()['error'], '指定商品的金额未达到优惠条件'); } break; } } return callBack(statusCode()['success'], '验证成功', [ 'activity_goods' => $activity_goods ? $activity_goods : [], 'activity_goods_money' => $appoint_money ? $appoint_money : 0 ]); } /** * 获取某会员的优惠券列表 * @param $param * [ * merchant_id * vip_id 会员id * type 1可用 2已使用 3已过期 * page 页码 * ] * @return mixed */ public function getVipCouponList($param) { if (!in_array($param['type'], ['1', '2', '3']) || empty($param['merchant_id'])) { return callBack(statusCode()['error'], '请求有误'); } if (empty($param['vip_id'])) { return callBack(statusCode()['success'], "查询成功", []); } $time = $_SERVER['REQUEST_TIME']; $where = [ 'r.vid' => $param['vip_id'], 'r.status' => ['in', ['1', '2']], 'r.merchantid' => $param['merchant_id'], ]; switch ($param['type']) { case 1: //可用 $where['r.isuse'] = 1; $where[] = [ [ 'c.coupontype' => 1, 'c.endtime' => ['gt', $time] ], [ 'c.coupontype' => 2, 'r.addtime + (c.daynum*86400)' => ['gt', $time] ], [ 'c.coupontype' => 3, 'r.addtime+86400' => ['lt', $time], 'r.addtime+86400+(c.daynum*86400)' => ['gt', $time] ], '_logic' => 'OR' ]; break; case 2: //已使用 $where['r.isuse'] = 2; break; case 3: //已过期 $where['r.isuse'] = ['neq', 2]; $where[] = [ [ 'c.coupontype' => 1, 'c.endtime' => ['elt', $time] ], [ 'c.coupontype' => 2, 'r.addtime + (c.daynum*86400)' => ['lt', $time] ], [ 'c.coupontype' => 3, 'r.addtime+(c.daynum*86400)' => ['lt', $time], ], '_logic' => 'OR' ]; break; } $fields = [ 'c.id', 'c.coupon_name', 'c.needmoney', 'c.reducemoney', 'c.storeids', 'c.coupontype', 'c.starttime', 'c.endtime', 'c.daynum as day_num', 'c.appoint_type', 'c.appoint_id_str', 'r.addtime as receive_time' ]; $VipMCouponModel = new RetailVipMCouponModel(); $list = $VipMCouponModel->getVipCouponList($where, $fields, 'r.id desc', $param['page'], 10); if (!empty($list)) { foreach ($list as &$val) { $val['consumption_threshold'] = round($val['needmoney'], 2); $val['discount_amount'] = round($val['reducemoney'], 2); switch ($val['coupontype']) { case 1: $start_time = date('Y-m-d H:i:s', $val['starttime']); $end_time = date('Y-m-d H:i:s', $val['endtime']); break; case 2: $start_time = date('Y-m-d H:i:s', $val['receive_time']); $end_time = date('Y-m-d H:i:s', $val['receive_time'] + ($val['day_num'] * 86400)); break; case 3: $start_time = date('Y-m-d H:i:s', ($val['receive_time'] + 86400)); $end_time = date('Y-m-d H:i:s', ($val['receive_time'] + 86400) + ($val['day_num'] * 86400)); break; } $val['validity_time'] = $start_time . '至' . $end_time; $val['discount_amount'] = round($val['reducemoney'], 2); if ($val['storeids'] == '0') { //所有门店 $val['store_type'] = 1; } else { //指定门店 $val['store_type'] = 2; } switch ($val['appoint_type']) { case 1: $val['appoint_type_name'] = '所有商品'; break; case 2: $val['appoint_type_name'] = '部分分类'; $val['appoint_id_gather'] = $val['appoint_id_str']; break; case 3: $val['appoint_type_name'] = '部分商品'; $goods_id_arr = explode(',', $val['appoint_id_str']); $goods_id_gather = []; foreach ($goods_id_arr as $key => $value) { if (strpos($value, '_') !== false) { $id_arr = explode('_', $value); $goods_id_gather[] = $id_arr[0]; } else { $goods_id_gather[] = $value; } } $goods_id_gather = array_flip($goods_id_gather); $goods_id_gather = array_keys($goods_id_gather); $val['appoint_id_gather'] = implode(',', $goods_id_gather); break; } } } return callBack(statusCode()['success'], "查询成功", $list); } /** * 优惠券详情 * @param $param * [ * merchant_id * id 优惠券id * ] * @return mixed */ public function couponInfo($param) { if (empty($param['merchant_id']) || empty($param['id'])) { return callBack(statusCode()['error'], "参数缺失"); } $where = [ 'id' => $param['id'], 'merchantid' => $param['merchant_id'] ]; try { $fields = [ 'coupon_name', 'needmoney', 'reducemoney', 'storeids', 'coupontype', 'appoint_type', 'appoint_id_str', 'detailmsg', 'starttime', 'endtime', 'daynum' ]; $CouponModel = new RetailMCouponModel(); $info = $CouponModel->getFind($where, $fields); if (empty($info)) { return callBack(statusCode()['error'], "未找到该优惠券"); } $info['consumption_threshold'] = round($info['needmoney'], 2); $info['discount_amount'] = round($info['reducemoney'], 2); if ($info['storeids'] == '0') { $info['store_type'] = '所有门店'; } else { $Store = new Store(); $storeResult = $Store->storeNameList([ 'store_id_gather' => $info['storeids'] ]); if ($storeResult['status'] == 1) { $store_list = $storeResult['data']['list']; $info['store_type'] = '部分门店:' . implode(',', $store_list); } } switch ($info['coupontype']) { case 1: $info['validity_time'] = date('Y-m-d H:i:s', $info['starttime']) . '-' . date('Y-m-d H:i:s', $info['endtime']); break; case 2: $info['validity_time'] = '领券当日开始' . $info['daynum'] . '天内有效'; break; case 3: $info['validity_time'] = '领券次日开始' . $info['daynum'] . '天内有效'; break; } switch ($info['appoint_type']) { case 1: $info['appoint_type_name'] = '所有商品'; break; case 2: $GoodsClassify = new GoodsClassify(); $list = $GoodsClassify->basisIdGetNameList($info['appoint_id_str']); $info['appoint_type_name'] = '指定分类:' . implode(',', $list); break; case 3: $SaleGoodsClass = new SaleGoods(); $list = $SaleGoodsClass->appointGoodsList($info['appoint_id_str']); $str = ''; foreach ($list as $val) { $str .= ',' . $val['goods_name']; if (!empty($val['sku_name'])) { $str .= '-' . $val['sku_name']; } } $info['appoint_type_name'] = '部分商品:' . trim($str, ','); break; } return callBack(statusCode()['success'], "查询成功", $info); } catch (\Exception $e) { return callBack(statusCode()['success'], "查询成功", []); } } }