$merchant_id, 'type' => array('in', array(4, 5)), 'card_type' => 1, '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 判断来源 $source 1.线下 2.线上 if ($source == 1) { $map['line_state'] = 1; } else { $map['online_state'] = 1; } // 判断门店id if ($store_id) { $map['storeids'] = $store_id; } // 搜索 if (trim($keyword)) { $map['name'] = array('like', '%' . trim($keyword) . '%'); } $field = array( 'id', 'name', 'surplus', 'use_type', 'type', 'frequency', 'limit', 'time_interval', 'money', 'introduce', 'effective_start', 'effective_end', 'gallery_id', 'image', 'end_time', 'card_name_color', 'btn_background', 'btn_text_color', 'storeids', 'effective_time_type', 'effective_day', ); // 实例化 $SubCard = new SubCardModel(); // 查询 $sub_card = $SubCard->toSelect($map, $page, $limit, $field); $ImageGallery = new ImageGalleryModel(); $storeList = array(); $StoreModel = new RetailStoreModel(); foreach ($sub_card as &$val) { // 金额 $val['money'] = floatval($val['money']) ? floatval($val['money']) : 0; // 截止日期 $val['end_time'] = date('Y/m/d H:i:s', $val['end_time']); // 类型 if ($val['use_type'] == 1) { $val['use_rule'] = '每天一次'; } else if ($val['use_type'] == 2) { $val['use_rule'] = '可用' . $val['frequency'] . '次,每日最多使用' . $val['limit'] . '次'; } else if ($val['use_type'] == 3) { $val['use_rule'] = '每隔' . $val['limit'] . '天可使用一次'; } $val['time_interval'] = str_replace(',', ' - ', $val['time_interval']); // 有效时间 if ($val['effective_time_type'] == 1) { if (!$val['effective_start'] || !$val['effective_end']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = date('Y-m-d', $val['effective_start']) . ' - ' . date('Y-m-d', $val['effective_end']); } } else if ($val['effective_time_type'] == 2) { if (!$val['effective_day']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = '会员领取当日起' . $val['effective_day'] . '天内有效'; } } else if ($val['effective_time_type'] == 3) { if (!$val['effective_day']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = '会员领取次日起' . $val['effective_day'] . '天内有效'; } } // 图片 if ($val['gallery_id'] && $val['gallery_id'] != 0) { $val['image_url'] = $ImageGallery->getFieldPath(array('id' => $val['gallery_id'])); } else { $val['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 颜色 $val['card_name_color'] = $val['card_name_color'] ? $val['card_name_color'] : '#FFFFFF'; $val['btn_background'] = $val['btn_background'] ? $val['btn_background'] : '#FFFFFF'; $val['btn_text_color'] = $val['btn_text_color'] ? $val['btn_text_color'] : '#36549a'; //门店图片 if ($storeList[$val['storeids']]) { $val['store_name'] = $storeList[$val['storeids']]['store_name']; $val['store_image'] = $storeList[$val['storeids']]['store_image']; } else { $store = $StoreModel->getFind(array('id' => $val['storeids']), 'storetag as store_name,shopimg_id'); $store['store_image'] = getImage($store['shopimg_id']); $val['store_name'] = $store['store_name']; $val['store_image'] = $store['store_image']; $storeList[$val['storeids']] = $store; } $val['store_id'] = $val['storeids']; unset($val['storeids']); } $res = array( 'status' => 1, 'sub_card' => !empty($sub_card) ? $sub_card : array() ); return $res; } /** * 获取次卡列表 type 4,5 * @param int $merchant_id 商户idz * @param int $source 来源 1.线下 2.线上 * @param int $store_id 门店id * @return $res */ public function cardBagList($merchant_id, $source, $store_id = '', $page = '', $limit = '', $keyword = '') { // 实例化模型 $time = time(); // 条件 $map = array( 'merchantid' => $merchant_id, 'type' => array('in', array(4, 5)), '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 判断类型 $source 来源 1.线下 2. 线上 if ($source == 1) { $map['line_state'] = 1; } else { $map['online_state'] = 1; } // 判断门店id if ($store_id) { $map['storeids'] = $store_id; } // 搜索 if (trim($keyword)) { $map['name'] = array('like', '%' . trim($keyword) . '%'); } $field = array( 'id', 'name', 'surplus', 'money', 'type', 'image', 'gallery_id', 'end_time', 'card_name_color', 'btn_background', 'btn_text_color', 'storeids' ); // 实例化 $CardBag = new CardBagModel(); // 查询 $card_bag = $CardBag->toSelect($map, $page, $limit, $field); $ImageGallery = new ImageGalleryModel(); $StoreModel = new RetailStoreModel(); $storeList = array(); foreach ($card_bag as &$val) { // 金额 $val['money'] = floatval($val['money']) ? floatval($val['money']) : 0; // 截止日期 $val['end_time'] = date('Y/m/d H:i:s', $val['end_time']); // 图片 if ($val['gallery_id'] && $val['gallery_id'] != 0) { $val['image_url'] = $ImageGallery->getFieldPath(array('id' => $val['gallery_id'])); } else { $val['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 颜色 $val['card_name_color'] = $val['card_name_color'] ? $val['card_name_color'] : '#FFFFFF'; $val['btn_background'] = $val['btn_background'] ? $val['btn_background'] : '#FFFFFF'; $val['btn_text_color'] = $val['btn_text_color'] ? $val['btn_text_color'] : '#36549a'; //门店图片 if ($storeList[$val['storeids']]) { $val['store_name'] = $storeList[$val['storeids']]['store_name']; $val['store_image'] = $storeList[$val['storeids']]['store_image']; } else { $store = $StoreModel->getFind(array('id' => $val['storeids']), 'storetag as store_name,shopimg_id'); $store['store_image'] = getImage($store['shopimg_id']); $val['store_name'] = $store['store_name']; $val['store_image'] = $store['store_image']; $storeList[$val['storeids']] = $store; } $val['store_id'] = $val['storeids']; unset($val['storeids']); } $res = array( 'status' => 1, 'card_bag' => !empty($card_bag) ? $card_bag : array() ); return $res; } /** * 次卡信息 * @param int $merchant_id * @param int $source 来源 1.线下 2.线上 * @param int $card_id * @param int $store_id 非必传参数 * @return array */ public function subCardInfo($merchant_id, $source, $card_id, $store_id = '') { $time = time(); // 条件 $map = array( 'id' => $card_id, 'merchantid' => $merchant_id, 'card_type' => 1, ); // 判断来源 $source 1.线下 2.线上 if ($source == 1) { $map['line_state'] = 1; } else { $map['online_state'] = 1; } // 判断门店 if ($store_id) { $map['storeids'] = $store_id; } $field = array( 'id', 'name', 'surplus', 'use_type', 'frequency', 'limit', 'time_interval', 'type', 'money', 'introduce', 'effective_start', 'effective_end', 'gallery_id', 'image', 'end_time', 'status', 'card_name_color', 'btn_background', 'btn_text_color', 'effective_time_type', 'effective_day' ); // 实例化 $SubCard = new SubCardModel(); // 查询 $sub_card = $SubCard->toFind($map, $field); // 判断 if (empty($sub_card)) { $res = array('status' => 0, 'msg' => '该次卡活动异常'); return $res; } // 判断结束时间,数量 $sub_card['surplus'] = intval($sub_card['surplus']); if ($sub_card['end_time'] < $time || $sub_card['surplus'] <= 0) { $sub_card['status'] = 1; } // 结束时间 $sub_card['end_time'] = date('Y/m/d H:i:s', $sub_card['end_time']); // 处理数据 if ($sub_card['use_type'] == 1) { $sub_card['use_rule'] = '每天一次'; } else if ($sub_card['use_type'] == 2) { $sub_card['use_rule'] = '可用' . $sub_card['frequency'] . '次,每日最多使用' . $sub_card['limit'] . '次'; } else if ($sub_card['use_type'] == 3) { $sub_card['use_rule'] = '每隔' . $sub_card['limit'] . '天可使用一次'; } $sub_card['time_interval'] = str_replace(',', ' - ', $sub_card['time_interval']); // 使用有效时间 if ($sub_card['effective_time_type'] == 1) { if (!$sub_card['effective_start'] || !$sub_card['effective_end']) { $sub_card['effective_time'] = '无效时间'; } else { $sub_card['effective_time'] = date('Y/m/d', $sub_card['effective_start']) . ' - ' . date('Y/m/d', $sub_card['effective_end']); } } else if ($sub_card['effective_time_type'] == 2) { if (!$sub_card['effective_day']) { $sub_card['effective_time'] = '无效时间'; } else { $sub_card['effective_time'] = '会员领取当日起' . $sub_card['effective_day'] . '天内有效'; } } else if ($sub_card['effective_time_type'] == 3) { if (!$sub_card['effective_day']) { $sub_card['effective_time'] = '无效时间'; } else { $sub_card['effective_time'] = '会员领取次日起' . $sub_card['effective_day'] . '天内有效'; } } // 图片 if ($sub_card['gallery_id'] && $sub_card['gallery_id'] != 0) { $ImageGallery = new ImageGalleryModel(); $sub_card['image_url'] = $ImageGallery->getFieldPath(array('id' => $sub_card['gallery_id'])); } else { $sub_card['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 颜色 $sub_card['card_name_color'] = $sub_card['card_name_color'] ? $sub_card['card_name_color'] : '#FFFFFF'; $sub_card['btn_background'] = $sub_card['btn_background'] ? $sub_card['btn_background'] : '#FFFFFF'; $sub_card['btn_text_color'] = $sub_card['btn_text_color'] ? $sub_card['btn_text_color'] : '#36549a'; // 判断类型 if ($sub_card['type'] == 5) { // 判断store_id if (!$store_id) { $res = array('status' => 0, 'msg' => '缺少必要的store_id'); return $res; } // 获取商品 $result = $this->cardGoods($merchant_id, 0, $card_id, $store_id); if ($result['status'] == 0) { return $result; } $res['card_goods'] = $result['card_goods']; } // 返回值 $res['status'] = 1; $res['sub_card'] = $sub_card; return $res; } /** * 卡包信息 * @param int $merchant_id * @param int $source 来源 1.线下 2.线上 * @param int $card_id * @param int $store_id 非必传参数 * @return array */ public function cardBagInfo($merchant_id, $source, $bag_id, $store_id = '') { // 实例化模型 $time = time(); // 条件 $map = array( 'id' => $bag_id, 'merchantid' => $merchant_id, ); // 判来源 $source 1.线下 2.线上 if ($source == 1) { $map['line_state'] = 1; } else { $map['online_state'] = 1; } // 判断门店id if ($store_id) { $map['storeids'] = $store_id; } $field = array( 'id', 'name', 'surplus', 'image', 'gallery_id', 'end_time', 'type', 'money', 'status', 'card_name_color', 'btn_background', 'btn_text_color' ); // 实例化 $CardBag = new CardBagModel(); // 查询 $card_bag = $CardBag->toFind($map, $field); if (empty($card_bag)) { $res = array('status' => 0, 'msg' => '该卡包活动异常'); return $res; } // 处理数据 $card_bag['surplus'] = intval($card_bag['surplus']); if ($card_bag['end_time'] < $time || $card_bag['surplus'] <= 0) { $card_bag['status'] = 1; } // 结束时间 $card_bag['end_time'] = date('Y-m-d H:i:s', $card_bag['end_time']); // 颜色 $card_bag['card_name_color'] = $card_bag['card_name_color'] ? $card_bag['card_name_color'] : '#FFFFFF'; $card_bag['btn_background'] = $card_bag['btn_background'] ? $card_bag['btn_background'] : '#FFFFFF'; $card_bag['btn_text_color'] = $card_bag['btn_text_color'] ? $card_bag['btn_text_color'] : '#36549a'; // 卡包详情 $result = $this->cardBagDetails($merchant_id, $card_bag); if ($result['status'] == 0) { return $result; } // 判断类型 if ($card_bag['type'] == 5) { // 判断store_id if (!$store_id) { $res = array('status' => 0, 'msg' => '缺少必要的store_id'); return $res; } // 获取商品 $result2 = $this->cardGoods($merchant_id, 1, $bag_id, $store_id); if ($result2['status'] == 0) { return $result2; } $res['card_goods'] = $result2['card_goods']; } $res['status'] = 1; $res['card_bag'] = $card_bag; $res['card_list'] = $result['card_list']; return $res; } /** * 卡包详情[次卡展示] * @param int $bag_id * @return array */ public function cardBagDetails($merchant_id, $card_bag) { // 查询条件 $map = array( 'merchantid' => $merchant_id, 'bag_id' => $card_bag['id'], 'card_type' => 2, ); $field = array( 'id', 'name', 'surplus', 'use_type', 'frequency', 'limit', 'time_interval', 'money', 'introduce', 'effective_start', 'effective_end', 'gallery_id', 'image', 'effective_time_type', 'effective_day' ); // 实例化模型 $SubCard = new SubCardModel(); // 查询 $card_list = $SubCard->toSelect($map, '', '', $field); if (empty($card_list)) { $res = array('status' => 0, 'msg' => '该卡包内暂无可用次卡'); return $res; } // 处理数据 $ImageGallery = new ImageGalleryModel(); foreach ($card_list as &$val) { if ($val['use_type'] == 1) { $val['use_rule'] = '每天一次'; } else if ($val['use_type'] == 2) { $val['use_rule'] = '可用' . $val['frequency'] . '次,每日最多使用' . $val['limit'] . '次'; } else if ($val['use_type'] == 3) { $val['use_rule'] = '每隔' . $val['limit'] . '天可使用一次'; } $val['time_interval'] = str_replace(',', ' - ', $val['time_interval']); // 使用有效时间 if ($val['effective_time_type'] == 1) { if (!$val['effective_start'] || !$val['effective_end']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = date('Y/m/d', $val['effective_start']) . ' - ' . date('Y/m/d', $val['effective_end']); } } else if ($val['effective_time_type'] == 2) { if (!$val['effective_day']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = '会员领取当日起' . $val['effective_day'] . '天内有效'; } } else if ($val['effective_time_type'] == 3) { if (!$val['effective_day']) { $val['effective_time'] = '无效时间'; } else { $val['effective_time'] = '会员领取次日起' . $val['effective_day'] . '天内有效'; } } // 图片 if ($val['gallery_id'] && $val['gallery_id'] != 0) { $val['image_url'] = $ImageGallery->getFieldPath(array('id' => $val['gallery_id'])); } else { $val['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 发放结束时间 $val['end_time'] = $card_bag['end_time']; // 颜色 $val['card_name_color'] = $card_bag['card_name_color']; $val['btn_background'] = $card_bag['btn_background']; $val['btn_text_color'] = $card_bag['btn_text_color']; // 类型 $val['type'] = $card_bag['type']; } $res = array('status' => 1, 'card_list' => $card_list); return $res; } /** * 次卡,卡包商品 * @param int $merchant_id * @param int $type * @param int $card_id * @param int $store_id * @return array */ public function cardGoods($merchant_id, $type, $card_id, $store_id) { // 次卡,卡包商品条件 $CardGoods = new RetailCardGoodsModel(); $c_map = array( 'c.' . $CardGoods::MERCHANT_ID => $merchant_id, 'c.' . $CardGoods::STORE_ID => $store_id, 'c.' . $CardGoods::TYPE => $type, 'c.' . $CardGoods::C_ID => $card_id, 'c.' . $CardGoods::STATUS => 1, ); $c_field = array( 'c.*', 'g.goods_name', 'g.retail_price', 'g.online_price', 'g.goods_num', 'g.plus_price', 'g.online_state', 'g.attribute_state', 'g.goods_img_id', 'g.shop_mem_status', 'g.shop_mem_price', 'g.store_mem_status', 'g.store_mem_price', 'g.plus_mem_status', 'g.pricing_method', 'g.vip_retail_state', 'g.vip_online_state', 'g.store_mem_integral', 'g.plus_mem_integral', 'g.company' ); // 次卡,卡包商品查询 $card_goods = $CardGoods->getCardGoods($c_map, $c_field); // 次卡,卡包商品判断 if (empty($card_goods)) { $res = array( 'status' => 0, 'msg' => $type == 1 ? '卡包商品查询失败' : '次卡商品查询失败' ); return $res; } // 规格公共条件 $SkuGoods = new RetailSkuGoodsModel(); $sku_map = array( 'k.' . $SkuGoods::STORE_ID => $store_id, 'k.' . $SkuGoods::STATUS => 1, 'k.' . $SkuGoods::STATE => 0, ); $sku_field = array( 'k.' . $SkuGoods::ID, 'k.' . $SkuGoods::SKU_ID_LIST, 'k.' . $SkuGoods::ONLINE_PRICE, 'k.' . $SkuGoods::RETAIL_PRICE, 'k.' . $SkuGoods::GOODS_IMG, 'k.' . $SkuGoods::SKU_CODE, 'k.' . $SkuGoods::SHOP_MEM_PRICE, 'k.' . $SkuGoods::STORE_MEM_PRICE, 'k.' . $SkuGoods::PLUS_PRICE, 'k.' . $SkuGoods::GOODS_IMG_ID, 's.goods_num', 'k.store_mem_integral', 'k.plus_mem_integral' ); // 商品库存公共条件 $SSaleMGoods = new SSaleMGoodsModel(); $sag_map = array( 'merchant_id' => $merchant_id, 'store_id' => $store_id, ); // 处理数据 $SkuList = new RetailSkuListModel(); foreach ($card_goods as &$value) { $value['number'] = floatval($value['number']); // 库存 $sg_map2 = $sag_map; $sg_map2['goods_id'] = $value[$CardGoods::GOODS_ID]; $value['goods_num'] = $SSaleMGoods->getFindField($sg_map2, 'goods_num'); $value['goods_num'] = floatval($value['goods_num']) > 0 ? floatval($value['goods_num']) : 0; // 金额 $value['retail_price'] = floatval($value['retail_price']); // 门店 $value['online_price'] = floatval($value['online_price']); // 商城 $value['plus_price'] = floatval($value['plus_price']); // plus会员 $value['shop_mem_price'] = floatval($value['shop_mem_price']); // 商城会员 $value['store_mem_price'] = floatval($value['store_mem_price']); // 门店会员 $value['goods_num'] = floatval($value['goods_num']); // 判断指定规格 if ($value[$CardGoods::SKU_ID]) { // 条件 $sku_map2 = $sku_map; $sku_map2['k.' . $SkuGoods::ID] = $value[$CardGoods::SKU_ID]; $sku_map2['k.' . $SkuGoods::GOODS_ID] = $value[$CardGoods::GOODS_ID]; // 查询 $sku_goods = $SkuGoods->getSkuGoodsInfo($sku_map2, $sku_field); // 判断 if (empty($sku_goods)) { // 清除指定规格 $value[$CardGoods::SKU_ID] = 0; } else { // 规格 $sku_name = $SkuList->getFindField(array($SkuList::ID => array('in', $sku_goods[$SkuGoods::SKU_ID_LIST])), $SkuList::NAME, true); $value['sku_id_list'] = $sku_goods['sku_id_list']; $value['sku_name'] = implode(',', $sku_name); $value['sku'] = array( array('id' => $value[$CardGoods::SKU_ID], 'name' => $value['sku_name']) ); // 库存 $value['goods_num'] = floatval($sku_goods['goods_num']) > 0 ? floatval($sku_goods['goods_num']) : 0; // 金额 $value['retail_price'] = floatval($sku_goods[$SkuGoods::RETAIL_PRICE]); $value['online_price'] = floatval($sku_goods[$SkuGoods::ONLINE_PRICE]); $value['plus_price'] = floatval($sku_goods[$SkuGoods::PLUS_PRICE]); $value['shop_mem_price'] = floatval($sku_goods[$SkuGoods::SHOP_MEM_PRICE]); $value['store_mem_price'] = floatval($sku_goods[$SkuGoods::STORE_MEM_PRICE]); $value['store_mem_integral'] = floatval($sku_goods['store_mem_integral']); $value['plus_mem_integral'] = floatval($sku_goods['plus_mem_integral']); // 图片 $value['goods_img_id'] = $sku_goods[$SkuGoods::GOODS_IMG_ID]; } unset($sku_map2); unset($sku_name); unset($sku_goods); } // 图片 $value['goods_image'] = getGoodsImage($value['goods_img_id'], $store_id); // 判断规格商品 if ($value['attribute_state'] == 2) { $sku_map2 = $sku_map; $sku_map2['k.' . $SkuGoods::GOODS_ID] = $value[$CardGoods::GOODS_ID]; $sku_goods = $SkuGoods->getSkuGoodsList($sku_map2, $sku_field); if (empty($sku_goods)) { // 关闭规格 $value['attribute_state'] = 1; } else { // 处理商品规格 $str = ''; foreach ($sku_goods as &$val) { $str .= ',' . $val[$SkuGoods::SKU_ID_LIST]; $val[$SkuGoods::GOODS_IMG] = getGoodsImage($val[$SkuGoods::GOODS_IMG_ID], $store_id); $name = $SkuList->getFindField(array($SkuList::ID => array('in', $val[$SkuGoods::SKU_ID_LIST])), $SkuList::NAME, true); $val['name'] = implode(',', $name); $val['store_mem_status'] = $value['store_mem_status']; $val['shop_mem_status'] = $value['shop_mem_status']; $val['plus_mem_status'] = $value['plus_mem_status']; } $str = ltrim($str, ','); $sku_id_list = explode(',', $sku_goods[0][$SkuGoods::SKU_ID_LIST]); $attr = array(); foreach ($sku_id_list as &$v) { $list_map['l.' . $SkuList::ID] = $v; $tempAttr = $SkuList->getCashierSkuList($list_map); $tempAttr['list'] = $SkuList->getSelect(array($SkuList::ID => array('in', $str), $SkuList::ATTR_ID => $tempAttr[$SkuList::ID], $SkuList::STATE => 0), array($SkuList::ID, $SkuList::NAME)); $attr[] = $tempAttr; } $value['skuList'] = $sku_goods; $value['skuAttr'] = $attr; } } $value['skuAttr'] = !empty($value['skuAttr']) ? $value['skuAttr'] : array(); $value['skuList'] = !empty($value['skuList']) ? $value['skuList'] : array(); } $res = array('status' => 1, 'card_goods' => $card_goods); return $res; } /** * 支付完成,发放次卡 * @param array $pay_order * @return array */ public function updataCardOrder($pay_order) { // 条件 $CardOrder = new RetailCardOrderModel(); // 次卡订单 $PayOrder = new RetailPayOrderModel(); // 流水 $co_map = array( $CardOrder::MERCHANT_ID => $pay_order[$PayOrder::MERCHANT_ID], $CardOrder::STORE_ID => $pay_order[$PayOrder::STORE_ID], $CardOrder::VIP_ID => $pay_order[$PayOrder::VID], $CardOrder::STATE => 0, $CardOrder::PAY_ORDER_ID => $pay_order[$PayOrder::ID], ); $card_order = $CardOrder->getFind($co_map); if (empty($card_order)) { $res = array('status' => 0, 'msg' => '订单查询失败'); return $res; } // 会员 $Vip = new RetailVipModel(); // 条件 $v_map = array( $Vip::ID => $pay_order[$PayOrder::VID], $Vip::MERCHANT_ID => $pay_order[$PayOrder::MERCHANT_ID], $Vip::STATUS => array('neq', 2) ); // 查询 $vip_info = $Vip->getFind($v_map); if (empty($vip_info)) { $res = array('status' => 0, 'msg' => '会员信息查询失败'); return $res; } // 判断发放类型 $time = time(); $VipCardApi = new VipCardController(); if ($card_order[$CardOrder::TYPE] == 1) { // 次卡 $SubCard = new SubCardModel(); // 条件 $sc_map = array( 'id' => $card_order[$CardOrder::CARD_ID], 'merchantid' => $pay_order[$PayOrder::MERCHANT_ID], 'type' => 4, 'card_type' => 1, 'storeids' => $pay_order[$PayOrder::STORE_ID], '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 查询 $sub_card = $SubCard->toSelect($sc_map); // 判断 if (empty($sub_card)) { $res = array('status' => 0, 'msg' => '次卡信息查询失败'); return $res; } // 发放 $res = $VipCardApi->vipGetSubCard($pay_order[$PayOrder::MERCHANT_ID], $vip_info, $sub_card, 0, 0, $pay_order[$PayOrder::ID]); } else { // 卡包 $CardBag = new CardBagModel(); // 条件 $cb_map = array( 'id' => $card_order[$CardOrder::CARD_ID], 'merchantid' => $pay_order[$PayOrder::MERCHANT_ID], 'type' => 4, 'storeids' => $pay_order[$PayOrder::STORE_ID], '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 查询 $card_bag = $CardBag->toSelect($cb_map); // 判断 if (empty($card_bag)) { $res = array('status' => 0, 'msg' => '次卡信息查询失败'); return $res; } // 发放 $res = $VipCardApi->vipGetCardBag($pay_order[$PayOrder::MERCHANT_ID], $vip_info, $card_bag, 0, 0, $pay_order[$PayOrder::ID]); } // 更新card_order if ($res['status'] == 1) { $CardOrder->updateOrder(array($CardOrder::ID => $card_order[$CardOrder::ID]), array($CardOrder::STATE => 1)); } return $res; } /** * 用户次卡列表 * @param $merchant_id * @param $vip_id * @param $page * @param $limit * @param string $card_id * @param bool $get_can_use * @return array */ public function userCardList($merchant_id, $vip_id, $page, $limit, $card_id = '', $get_can_use = true) { // 条件 $time = time(); $map = array( 'Vip.id' => $vip_id, 'VipCard.merchantid' => $merchant_id, 'VipCard.status' => 0, ); if ($get_can_use) { //仅获取在有效期内的卡 $map['VipCard.state'] = 0; $map['VipCard.start_time'] = ['elt', $time]; $map['VipCard.end_time'] = ['egt', $time]; } if ($card_id) { $map['VipCard.id'] = $card_id; } $field = array( 'name', 'use_type', 'frequency', 'limit_num', 'gallery_id', 'image', 'id', 'use_num', 'time_interval', 'effective_start', 'effective_end', 'card_type', 'sub_card_id', 'card_bag_id', 'use_num', 'addtime', 'state', 'effective_time_type', 'effective_day', 'start_time', 'end_time' ); $start_time = strtotime(date('Y-m-d')); // 今日开始时间 // 查询 $SubCardView = new SubCardViewModel(); $card_list = $SubCardView->toSelect($map, $page, $limit, $field, 'VipCard.state asc,VipCard.id desc,SubCard.effective_end desc'); // 处理数据 $RetailVipCardRecordModel = new RetailVipCardRecordModel(); $ImageGallery = new ImageGalleryModel(); $SubCard = new SubCardModel(); $CardBag = new CardBagModel(); $returnArr = []; foreach ($card_list as $key => &$val) { // 判断是否使用完毕 if ($val['state'] == 1) { $val['is_use'] = 2; } $useMap = [ 'vip_id' => $vip_id, 'merchantid' => $merchant_id, 'vip_card_id' => $val['id'], 'addtime' => ['between', [$start_time, time()]], ]; // 今日使用次数和剩余次数处理 //is_use 1可用 2用完 3过期 4异常 switch ($val['use_type']) { case '1': //每天一次 $val['day_limit_num'] = 1; $day_use_num = $RetailVipCardRecordModel->getCount($useMap); $val['canUseNum'] = bcsub($val['day_limit_num'], $day_use_num); break; case '2': //自定义总次数,且限制每天使用次数 $day_use_num = $RetailVipCardRecordModel->getCount($useMap); $day_less_num = bcsub($val['frequency'] + $day_use_num, $val['use_num']); $val['day_limit_num'] = $day_less_num >= $val['limit_num'] ? $val['limit_num'] : $day_less_num; $val['canUseNum'] = bcsub($val['day_limit_num'], $day_use_num); break; case '3': //几天可使用一次 unset($useMap['addtime']); $pre_use_time = $RetailVipCardRecordModel->getOrderFindField($useMap, 'addtime', 'id desc'); $pre_use_start_time = strtotime(date('Y-m-d', $pre_use_time));//15 00:00:00 $min_pre_use_start_time = $start_time - ($val['limit_num'] * 86400);//18 00:00:00 - 3*86400 = 15 00:00:00 if (empty($pre_use_time) || $pre_use_start_time <= $min_pre_use_start_time) { $val['day_limit_num'] = 1; $val['canUseNum'] = 1; } else if ($pre_use_start_time == $start_time) { $val['day_limit_num'] = 1; $val['canUseNum'] = 0; } else { $val['day_limit_num'] = 0; $val['canUseNum'] = 0; } break; } //判断是否可以使用 if ($val['state'] == 1) { //已用完 $val['is_use'] = 2; } else if ($val['start_time'] <= $time && $val['end_time'] >= $time) { //可以使用 $val['is_use'] = 1; } else if ($val['end_time'] < $time) { //已过期 $val['is_use'] = 3; } else if ($val['start_time'] > $time) { //未开始 $val['is_use'] = 5; } else { //异常 $val['is_use'] = 4; } $val['is_use'] = $val['end_time'] < $time ? 3 : ($val['canUseNum'] ? 1 : 2); //有效期 $val['effective_time'] = date('Y/m/d', $val['start_time']) . ' ~ ' . date('Y/m/d', $val['end_time']); // 领取时间 $val['addtime'] = date('Y/m/d H:i:s', $val['addtime']); // 图片 if ($val['gallery_id'] && $val['gallery_id'] != 0) { $val['image_url'] = $ImageGallery->getFieldPath(array('id' => $val['gallery_id'])); } else { $val['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 判断类型 if ($val['card_type'] == 1 && $val['sub_card_id']) { $info = $SubCard->toFind(array('id' => $val['sub_card_id']), 'card_name_color,btn_background,btn_text_color'); } else if ($val['card_type'] == 2 && $val['card_bag_id']) { $info = $CardBag->toFind(array('id' => $val['card_bag_id']), 'card_name_color,btn_background,btn_text_color'); } else { $info = array(); } // 判断$info if (!empty($info)) { $val['card_name_color'] = $info['card_name_color'] ? $info['card_name_color'] : '#FFFFFF'; $val['btn_background'] = $info['btn_background'] ? $info['btn_background'] : '#FFFFFF'; $val['btn_text_color'] = $info['btn_text_color'] ? $info['btn_text_color'] : '#36549a'; } else { $val['card_name_color'] = '#FFFFFF'; $val['btn_background'] = '#FFFFFF'; $val['btn_text_color'] = '#36549a'; } $returnArr[] = $val; } $res = array('status' => 1, 'card_list' => !empty($returnArr) ? $returnArr : array()); return $res; } /** * 删除次卡 * @param int $merchant_id * @param int $vip_id * @param int $vip_card_id * @return array */ public function deleteCard($merchant_id, $vip_id, $vip_card_id) { // 条件 $map = array( 'VipCard.id' => $vip_card_id, 'VipCard.merchantid' => $merchant_id, 'VipCard.status' => 0, 'Vip.id' => $vip_id, ); // 查询 $SubCardView = new SubCardViewModel(); $user_card = $SubCardView->toFind($map); // 判断 if (empty($user_card)) { $res = array('status' => 0, 'msg' => '查询失败'); return $res; } // 删除次卡 $VipCard = new RetailVipCardModel(); $save = $VipCard->updateOrder(array('id' => $vip_card_id), array('status' => 1)); if ($save) { $res = array('status' => 1, 'msg' => '删除成功'); } else { $res = array('status' => 0, 'msg' => '删除失败'); } return $res; } /** * 用户次卡详情 * @param int $merchant_id * @param int $vip_id * @param int $vip_card_id * @return array */ public function userCardInfo($merchant_id, $vip_id, $vip_card_id) { // 条件 $map = array( 'VipCard.id' => $vip_card_id, 'VipCard.merchantid' => $merchant_id, 'VipCard.status' => 0, 'Vip.id' => $vip_id, ); $field = array( 'name', 'use_type', 'frequency', 'limit_num', 'time_interval', 'gallery_id', 'image', 'introduce', 'effective_start', 'effective_end', 'card_type', 'sub_card_id', 'card_bag_id', 'use_num', 'addtime', 'state', 'effective_time_type', 'effective_day', 'start_time', 'end_time' ); // 查询 $SubCardView = new SubCardViewModel(); $user_card = $SubCardView->toFind($map, $field); // 判断 if (empty($user_card)) { $res = array('status' => 0, 'msg' => '查询失败'); return $res; } // 处理数据 $user_card['is_use'] = $user_card['end_time'] < time() ? 2 : 1; //有效期 $user_card['effective_time'] = date('Y/m/d', $user_card['start_time']) . ' ~ ' . date('Y/m/d', $user_card['end_time']); // 领取时间 $user_card['add_time'] = date('Y/m/d H:i:s', $user_card['addtime']); // 类型 总次数 switch ($user_card['use_type']){ case '1': //每天一次 $user_card['frequency'] = round(($user_card['end_time'] - $user_card['start_time']) / 86400); $user_card['use_rule'] = '每天一次'; break; case '2': //自定义总次数,且限制每天使用次数 $user_card['frequency'] = intval($user_card['frequency']); $user_card['limit_num'] = intval($user_card['limit_num']); $user_card['use_rule'] = '可用' . $user_card['frequency'] . '次,每日最多使用' . intval($user_card['limit_num']) . '次'; break; case '3': //每隔几天可用一次 $user_card['limit_num'] = intval($user_card['limit_num']); $user_card['frequency'] = ceil(($user_card['end_time'] - $user_card['start_time']) / 86400 / ($user_card['limit_num'])); $user_card['use_rule'] = '每隔' . intval($user_card['limit_num']) . '天可使用一次'; break; } // 使用次数 $user_card['use_num'] = intval($user_card['use_num']); // 剩余次数 $user_card['residue_num'] = $user_card['frequency'] - $user_card['use_num'] > 0 ? intval($user_card['frequency'] - $user_card['use_num']) : 0; // 判断剩余次数 if ($user_card['state'] == 1) { $user_card['is_use'] = 2; } // 时段 $user_card['time_interval'] = str_replace(',', ' - ', $user_card['time_interval']); // 图片 if ($user_card['gallery_id'] && $user_card['gallery_id'] != 0) { $ImageGallery = new ImageGalleryModel(); $user_card['image_url'] = $ImageGallery->getFieldPath(array('id' => $user_card['gallery_id'])); } else { $user_card['image_url'] = getServerUrl() . '/Public/paper/images/sub_card.png'; } // 使用说明 $user_card['introduce'] = trim($user_card['introduce']) ? trim($user_card['introduce']) : '暂无说明'; // 判断类型 查询指定门店 if ($user_card['card_type'] == 1 && $user_card['sub_card_id']) { $SubCard = new SubCardModel(); $info = $SubCard->toFind(array('id' => $user_card['sub_card_id']), 'card_name_color,storeids'); } else if ($user_card['card_type'] == 2 && $user_card['card_bag_id']) { $CardBag = new CardBagModel(); $info = $CardBag->toFind(array('id' => $user_card['card_bag_id']), 'card_name_color,storeids'); } else { $res = array('status' => 0, 'msg' => '未知的次卡类型'); return $res; } // 颜色 $user_card['card_name_color'] = $info['card_name_color'] ? $info['card_name_color'] : '#FFFFFF'; // 判断门店 if ($info['storeids'] && ($info['storeids'] != 0 || $info['storeids'] != '0')) { // 查询门店 $Store = new RetailStoreModel(); $user_card['store_data'] = $Store->getFindField(array($Store::ID => array('in', $info['storeids']), $Store::MERCHANT_ID => $merchant_id), $Store::STORE_NAME, true); } else { $user_card['store_data'] = array('所有门店'); } // 返回值 $res = array('status' => 1, 'user_card' => $user_card); return $res; } /** * 使用次卡 * @param int $merchant_id * @param int $vip_id * @param int $vip_card_id * @return array */ public function useCard($merchant_id, $vip_id, $vip_card_id) { // 条件 $map = array( 'VipCard.id' => $vip_card_id, 'VipCard.merchantid' => $merchant_id, 'VipCard.Vip_id' => $vip_id ); $field = array('name', 'state'); // 查询 $SubCardView = new SubCardViewModel(); $user_card = $SubCardView->toFind($map, $field); // 判断 if (empty($user_card)) { $res = array('status' => 0, 'msg' => '查询失败'); return $res; } // 判断是否使用完毕 if ($user_card['state'] == 1) { $res = array('status' => 0, 'msg' => '该次卡已使用完毕'); return $res; } // 使用码记录 $time = time(); $code_cr = array( 'merchantid' => $merchant_id, 'vip_id' => $vip_id, 'vip_card_id' => $vip_card_id, 'addtime' => $time, 'end_time' => $time + 180, 'status' => 0 ); // 生成使用码 $use_code = $this->generateUseCode(); if (!$use_code || $use_code == '') { $res = array('status' => 0, 'msg' => '使用码生成失败'); return $res; } $code_cr['use_code'] = $use_code; // 添加 $CardUseCode = new CardUseCodeModel(); $code_add = $CardUseCode->toAdd($code_cr); // 判断 if ($code_add) { $res = array('status' => 1, 'use_code' => $use_code, 'card_name' => $user_card['name']); } else { $res = array('status' => 0, '使用码生成失败'); } return $res; } /** * 生成使用码 * @return mixed */ protected function generateUseCode() { // 实例化模型 $CardUseCode = new CardUseCodeModel(); $i = 0; $use_code = ''; while ($i < 10) { // 生成11位使用码 $use_code = rand(100000, 999999) . rand(10000, 99999); // 查询使用码是否存在 $find = $CardUseCode->toFind(array('use_code' => $use_code)); if (!$find) { $i = 11; } else { $i++; } } return $use_code; } /** * 购买商品验证次卡是否可以购买 * */ public function checkCard($merchant_id, $store_id, $vip_id, $card_id, $card_type, $type, $source) { // 判断 $card_type $SubCard = new SubCardModel(); // 次卡 $CardBag = new CardBagModel(); // 卡包 $time = time(); if ($card_type == 1) { // 判断次卡 $sc_map = array( 'id' => $card_id, 'merchantid' => $merchant_id, 'type' => $type, 'card_type' => 1, 'storeids' => $store_id, '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 判断来源 if ($source == 1) { $sc_map['line_state'] = 1; } else { $sc_map['online_state'] = 1; } $data = $SubCard->tofind($sc_map); } else { // 判断卡包 $cb_map = array( 'id' => $card_id, 'merchantid' => $merchant_id, 'type' => $type, 'storeids' => $store_id, '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 判断来源 if ($source == 1) { $cb_map['line_state'] = 1; } else { $cb_map['online_state'] = 1; } // 查询 $data = $CardBag->tofind($cb_map); } // 判断 if (empty($data)) { $res = array( 'status' => 0, 'msg' => $card_type == 1 ? '次卡活动查询失败' : '卡包活动查询失败' ); return $res; } // 判断购买次数 $CardOrder = new RetailCardOrderModel(); // 购买记录 $co_map = array( $CardOrder::MERCHANT_ID => $merchant_id, $CardOrder::STORE_ID => $store_id, $CardOrder::VIP_ID => $vip_id, $CardOrder::TYPE => $card_type, $CardOrder::CARD_ID => $card_id, $CardOrder::STATE => 1, ); $co_count = $CardOrder->getCount($co_map); if ($co_count && intval($co_count) >= intval($data['give_num'])) { $res = array( 'status' => 0, 'msg' => $card_type == 1 ? '购买过该次卡活动次数已达上限' : '购买过该卡包活动次数已达上限' ); return $res; } // 验证通过 $res = array('status' => 1, 'data' => $data); return $res; } /** * 购买商品发放次卡,卡包 * @param array $billOrder * @return array */ public function grantCard($card_order, $vip_id) { $CardOrder = new RetailCardOrderModel(); // 次卡订单 $Vip = new RetailVipModel(); // 条件 $v_map = array( $Vip::ID => $vip_id, $Vip::MERCHANT_ID => $card_order[$CardOrder::MERCHANT_ID], $Vip::STATUS => array('neq', 2) ); // 查询会员 $vip_info = $Vip->getFind($v_map); if (empty($vip_info)) { $res = array('status' => 0, 'msg' => '会员信息查询失败'); return $res; } // 判断发放类型 $time = time(); $VipCardApi = new VipCardController(); if ($card_order[$CardOrder::TYPE] == 1) { // 次卡 $SubCard = new SubCardModel(); // 条件 $sc_map = array( 'id' => $card_order[$CardOrder::CARD_ID], 'merchantid' => $card_order[$CardOrder::MERCHANT_ID], 'type' => 5, 'card_type' => 1, 'storeids' => $card_order[$CardOrder::STORE_ID], '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 查询 $sub_card = $SubCard->toSelect($sc_map); // 判断 if (empty($sub_card)) { $res = array('status' => 0, 'msg' => '次卡信息查询失败'); return $res; } // 发放 $res = $VipCardApi->vipGetSubCard($card_order[$CardOrder::MERCHANT_ID], $vip_info, $sub_card, 0, 0, 0, $card_order[$CardOrder::BILL_ID]); } else { // 卡包 $CardBag = new CardBagModel(); // 条件 $cb_map = array( 'id' => $card_order[$CardOrder::CARD_ID], 'merchantid' => $card_order[$CardOrder::MERCHANT_ID], 'type' => 5, 'storeids' => $card_order[$CardOrder::STORE_ID], '_string' => "end_time > {$time} AND surplus > 0 AND status = 0", ); // 查询 $card_bag = $CardBag->toSelect($cb_map); // 判断 if (empty($card_bag)) { $res = array('status' => 0, 'msg' => '次卡信息查询失败'); return $res; } // 发放 $res = $VipCardApi->vipGetCardBag($card_order[$CardOrder::MERCHANT_ID], $vip_info, $card_bag, 0, 0, 0, $card_order[$CardOrder::BILL_ID]); } return $res; } }