'/index.php/Retail/SubCard/getStoreListAndVipGrade', 'subCardRecord' => '/index.php/Retail/SubCard/subCardRecord', 'getCardImage' => '/index.php/Retail/SubCard/getCardImage', 'getVips' => '/index.php/Retail/SubCard/getVips', 'getSendOutVipList' => '/index.php/Retail/SubCard/getSendOutVipList', 'invalidSubCard' => '/index.php/Retail/SubCard/invalidSubCard', ); $authArr = checkFunction($arr); $this->assign('authArr', $authArr); $this->display(); } /** * 获取次卡 * @param [array] $seek_data 搜索条件 * @param [int] $page 分页 * @param [int] $limit 数量 * @return [array] $res */ public function getSubCardList() { // 实例化模型 $subCardModel = new SubCardModel(); $time = time(); // 搜索条件 $seek_data = I('post.seek_data'); // 名称搜索 if (trim($seek_data['name'])) { $map['name'] = array('like', '%' . trim($seek_data['name']) . '%'); } // 类型搜索 if ($seek_data['type']) { $map['type'] = $seek_data['type']; } // 时间搜索 if ($seek_data['start_time'] && $seek_data['end_time']) { $start_time = strtotime($seek_data['start_time']); $end_time = strtotime($seek_data['end_time']); $map['addtime'] = array('between', array($start_time, $end_time)); } else if ($seek_data['start_time'] && !$seek_data['end_time']) { $start_time = strtotime($seek_data['start_time']); $map['addtime'] = array('gt', $start_time); } else if (!$seek_data['start_time'] && $seek_data['end_time']) { $end_time = strtotime($seek_data['end_time']); $map['addtime'] = array('lt', $end_time); } // 状态 $card_state = I('post.card_state'); if (intval($card_state == 2)) { $map['_string'] = "end_time > {$time} AND surplus > 0 AND status = 0"; } else if (intval($card_state == 3)) { $map['_string'] = "end_time < {$time} OR surplus <= 0 OR status = 1"; } // 查询条件 $map['merchantid'] = $this->merchant_id; $map['card_type'] = 1; $sub_card = $subCardModel->toSelect($map, (int)I('post.page'), (int)I('post.limit')); if (!$sub_card) { $res = array('status' => 0, 'status' => '暂无数据'); $this->ajaxReturn($res); } // 统计总数 $total = $subCardModel->total($map); foreach ($sub_card as &$val) { $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']); 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']); switch ($val['type']) { case 1: $val['type_name'] = '自主发放'; break; case 2: $val['type_name'] = '充值赠送'; break; case 3: $val['type_name'] = '消费赠送'; break; case 4: $val['type_name'] = '门店购买'; break; case 5: $val['type_name'] = '门店购买商品'; break; default: $val['type_name'] = '未知类型'; } if ($val['status'] == 1) { $val['status_name'] = '已失效'; } else { if ($val['surplus'] <= 0) { $val['status_name'] = '发放完毕'; $val['status'] = 1; } else if (!$val['end_time'] || $val['end_time'] <= time()) { $val['status_name'] = '已截止'; $val['status'] = 1; } else { $val['status_name'] = '正常'; } } // 发放截止时间 if (!$val['end_time']) { $val['end_time'] = '无效时间'; $val['status_name'] = '时间异常'; } else { $val['end_time'] = date('Y-m-d H:i:s', $val['end_time']); } // 使用有效时间 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'] . '天内有效'; } } } $res = array('status' => 1, 'sub_card' => $sub_card, 'total' => (int)$total); $this->ajaxReturn($res); } /** * 获取卡包 * @param [array] $seek_data 搜索条件 * @param [int] $page 分页 * @param [int] $limit 数量 * @return [array] $res */ public function getCardBagList() { // 实例化模型 $cardBagModel = new CardBagModel(); $time = time(); // 搜索条件 $seek_data = I('post.seek_data'); // 名称搜索 if (trim($seek_data['name'])) { $map['name'] = array('like', '%' . trim($seek_data['name']) . '%'); } // 类型搜索 if ($seek_data['type']) { $map['type'] = $seek_data['type']; } // 时间搜索 if ($seek_data['start_time'] && $seek_data['end_time']) { $start_time = strtotime($seek_data['start_time']); $end_time = strtotime($seek_data['end_time']); $map['addtime'] = array('between', array($start_time, $end_time)); } else if ($seek_data['start_time'] && !$seek_data['end_time']) { $start_time = strtotime($seek_data['start_time']); $map['addtime'] = array('gt', $start_time); } else if (!$seek_data['start_time'] && $seek_data['end_time']) { $end_time = strtotime($seek_data['end_time']); $map['addtime'] = array('lt', $end_time); } // 状态 $card_state = I('post.card_state'); if (intval($card_state == 2)) { $map['_string'] = "end_time > {$time} AND surplus > 0 AND status = 0"; } else if (intval($card_state == 3)) { $map['_string'] = "end_time < {$time} OR surplus <= 0 OR status = 1"; } // 查询条件 $map['merchantid'] = $this->merchant_id; $card_bag = $cardBagModel->toSelect($map, (int)I('post.page'), (int)I('post.limit')); if (!$card_bag) { $res = array('status' => 0, 'status' => '暂无数据'); $this->ajaxReturn($res); } // 统计总数 $total = $cardBagModel->total($map); foreach ($card_bag as &$val) { $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']); switch ($val['type']) { case 1: $val['type_name'] = '自主发放'; break; case 2: $val['type_name'] = '充值赠送'; break; case 3: $val['type_name'] = '消费赠送'; break; case 4: $val['type_name'] = '购买使用'; break; case 5: $val['type_name'] = '门店购买商品'; break; default: $val['type_name'] = '未知类型'; } if ($val['status'] == 1) { $val['status_name'] = '已失效'; } else { if ($val['surplus'] <= 0) { $val['status_name'] = '发放完毕'; $val['status'] = 1; } else if (!$val['end_time'] || $val['end_time'] <= time()) { $val['status_name'] = '已截止'; $val['status'] = 1; } else { $val['status_name'] = '正常'; } } if (!$val['end_time']) { $val['end_time'] = '无效时间'; $val['status_name'] = '时间异常'; } else { $val['end_time'] = date('Y-m-d H:i:s', $val['end_time']); } } $res = array('status' => 1, 'card_bag' => $card_bag, 'total' => (int)$total); $this->ajaxReturn($res); } /** * 获取门店、会员等级 * @return [array] $res */ public function getStoreListAndVipGrade() { // 实例化模型 $storeModel = new RetailStoreModel(); $vipIntegral = new VipIntegralModel(); // 查询出所有可用门店 $store_list = $storeModel->getSelect(['merchantid' => $this->merchant_id, 'store_status' => 0, 'state' => 0], 'id,storetag'); // 查询出所有可用会员等级 $vip_grade = $vipIntegral->getSelect(['merchantid' => $this->merchant_id], 'id,cardname'); $res = array( 'status' => 1, 'store_list' => $store_list ? $store_list : array(), 'vip_grade' => $vip_grade ? $vip_grade : array() ); $this->ajaxReturn($res); } /** * 获取门店商品 * @param [int] $store_id 门店id * @return [array] $res */ public function getStoreGoods() { // 判断登录 $merchant_id = I('session.merchant_userinfo')['id']; // 登录超时 if (!$merchant_id) { $res = array('status' => 0, 'msg' => '登录超时,请重新登录'); $this->ajaxReturn($res); } // 接受数据 $store_id = I('post.store_id'); $keyword = I('post.keyword'); $online_state = intval(I('post.online_state')); $page = intval(I('post.page')); // 判断 if (!$store_id || !in_array($online_state, array(0, 1))) { $res = array('status' => 0, 'msg' => '必要参数缺失'); $this->ajaxReturn($res); } // 条件 $Goods = new RetailGoodsModel(); $g_map = array( 'g.' . $Goods::MERCHANT_ID => $merchant_id, 'g.' . $Goods::STORE_ID => $store_id, 'g.' . $Goods::STATUS => 0 ); // 判断线上 if ($online_state == 1) { $g_map['g.' . $Goods::ONLINE_STATE] = 1; } // 搜索 if ($keyword) { $g_map['g.' . $Goods::GOODS_NAME . '|' . 'g.' . $Goods::GOODS_NUMBER . '|' . 'g.' . $Goods::GOODS_CODE] = $keyword; } $field = array( 'g.' . $Goods::ID, 'g.' . $Goods::GOODS_NAME, 'g.' . $Goods::GOODS_NUMBER, 'g.' . $Goods::GOODS_CODE, 'g.' . $Goods::ATTRIBUTE_STATE, 'g.' . $Goods::PRICING_METHOD, 'g.' . $Goods::ONLINE_STATE, 'g.' . $Goods::ATTRIBUTE_STATE, 's.goods_num' ); // 查询 $goods_list = $Goods->getSaleGoodsList($g_map, $field, $page, 10); if (empty($goods_list)) { $res = array('status' => 0, 'msg' => '商品查询失败'); $this->ajaxReturn($res); } // 统计 $total = $Goods->getSaleGoodsCount($g_map); // 处理数据 foreach ($goods_list as &$val) { // 判断规格 if ($val['attribute_state'] == 2) { $val['goods_num'] = '规格库存'; } else { $val['goods_num'] = floatval($val['goods_num']); } $val['number'] = 1; } // return $res = array( 'status' => 1, 'goods_list' => $goods_list, 'total' => intval($total) ); $this->ajaxReturn($res); } /** * 获取规格 * @param [int] $store_id * @param [int] $card_mold * @param [int] $goods_id * @return [array] $res */ public function getSkuList() { // 判断登录 $merchant_id = I('session.merchant_userinfo')['id']; // 登录超时 if (!$merchant_id) { $res = array('status' => 0, 'msg' => '登录超时,请重新登录'); $this->ajaxReturn($res); } // 接受数据 $store_id = I('post.store_id'); $goods_id = I('post.goods_id'); // 判断 if (!$store_id || !$goods_id) { $res = array('status' => 0, 'msg' => '必要参数缺失'); $this->ajaxReturn($res); } $SkuGoods = new RetailSkuGoodsModel(); // 条件 $map = array( 'k.' . $SkuGoods::STORE_ID => $store_id, 'k.' . $SkuGoods::GOODS_ID => $goods_id, 'k.' . $SkuGoods::STATUS => 1, 'k.' . $SkuGoods::STATE => 0, ); $field = array( 'k.' . $SkuGoods::ID, 'k.' . $SkuGoods::SKU_ID_LIST, 'k.' . $SkuGoods::SKU_GOODS_NUMBER, 'k.' . $SkuGoods::SKU_CODE, 's.goods_num' ); $sku_list = $SkuGoods->getSkuGoodsList($map, $field); if (empty($sku_list)) { $res = array('status' => 0, 'msg' => '请先添加规格商品'); $this->ajaxReturn($res); } // 处理数据 $SkuList = new RetailSkuListModel(); foreach ($sku_list as &$val) { // 处理规格名称 $sku_name = $SkuList->getFindField(array($SkuList::ID => array('in', $val['sku_id_list'])), $SkuList::NAME, true); $val['sku_name'] = implode(',', $sku_name); $val['goods_num'] = floatval($val['goods_num']); } $res = array('status' => 1, 'sku_list' => $sku_list); $this->ajaxReturn($res); } /** * 创建次卡 * @param [array] $sub_card 提交信息 * @return [array] $res */ public function createSubCard() { // 实例化模型 $subCardModel = new SubCardModel(); // 添加数据 $cr = array(); // 提交数据 $sub_card = I('post.sub_card'); if (!$sub_card) { $res = array('status' => 0, 'msg' => '创建失败,请刷新重试'); $this->ajaxReturn($res); } // 验证 if (!trim($sub_card['name']) || !trim($sub_card['card_name_color']) || !trim($sub_card['btn_background']) || !trim($sub_card['btn_text_color']) || !trim($sub_card['stock']) || !$sub_card['time_interval'] || !trim($sub_card['type']) || !trim($sub_card['end_time']) || empty($sub_card['effective_time_type'])) { $res = array('status' => 0, 'msg' => '请先完善表单信息'); $this->ajaxReturn($res); } // 使用类型 2.自定义 if ($sub_card['use_type'] == 2 || $sub_card['use_type'] == '2') { // 自定义次数 if (!$sub_card['frequency'] || intval($sub_card['frequency']) <= 0) { $res = array('status' => 0, 'msg' => '请设置正确的使用次数'); $this->ajaxReturn($res); } // 自定义每天使用次数 if (!$sub_card['limit'] || intval($sub_card['limit']) <= 0) { $res = array('status' => 0, 'msg' => '请设置正确的每天使用次数'); $this->ajaxReturn($res); } $cr['frequency'] = intval($sub_card['frequency']); $cr['limit'] = intval($sub_card['limit']); } // 使用类型 3.几天使用一次 if ($sub_card['use_type'] == 3) { // 几天使用一次 if (!$sub_card['limit'] || intval($sub_card['limit']) <= 0 || intval($sub_card['limit']) > 9999) { $res = array('status' => 0, 'msg' => '请设置正确的几天使用一次'); $this->ajaxReturn($res); } $cr['limit'] = intval($sub_card['limit']); } // 次卡类型 1.自主发放 2.充值赠送 3.消费赠送 4.门店购买 5.门店购买指定商品 $sub_card['type'] = intval($sub_card['type']); if ($sub_card['type'] == 1 || $sub_card['type'] == 2 || $sub_card['type'] == 3) { // 等级 $cr['gradeids'] = intval($sub_card['grade_type']) == 2 && !empty($sub_card['gradeids']) ? implode(',', $sub_card['gradeids']) : 0; // 指定门店 $cr['storeids'] = intval($sub_card['store_type']) == 2 && !empty($sub_card['storeids']) ? implode(',', $sub_card['storeids']) : 0; // 判断充值,消费 if ($sub_card['type'] == 2 || $sub_card['type'] == 3) { // 判断金额度 if (!$sub_card['money'] || floatval($sub_card['money']) <= 0) { $res = array('status' => 0, 'msg' => '请设置正确的金额度'); $this->ajaxReturn($res); } // 判断赠送次数 if (!$sub_card['give_num'] || intval($sub_card['give_num']) < 0) { $res = array('status' => 0, 'msg' => '请设置正确的赠送次数'); $this->ajaxReturn($res); } $cr['money'] = floatval($sub_card['money']); $cr['give_num'] = intval($sub_card['give_num']); } } else { // 判断门店id if (!$sub_card['store_id']) { $res = array('status' => 0, 'msg' => '请选择门店'); $this->ajaxReturn($res); } // 门店购买 if ($sub_card['type'] == 4 && (!$sub_card['money'] || floatval($sub_card['money']) <= 0)) { // 判断金额度 $res = array('status' => 0, 'msg' => '请设置正确的金额度'); $this->ajaxReturn($res); } // 门店购买商品 if ($sub_card['type'] == 5 && empty($sub_card['goods'])) { $res = array('status' => 0, 'msg' => '请选择商品'); $this->ajaxReturn($res); } // 判断次数 if (!$sub_card['give_num'] || intval($sub_card['give_num']) < 0) { $res = array( 'status' => 0, 'msg' => $sub_card['type'] == 4 ? '请设置正确的购买次数' : '请设置正确的赠送次数' ); $this->ajaxReturn($res); } // 判断商品 $cr['storeids'] = $sub_card['store_id']; $cr['money'] = floatval($sub_card['money']); $cr['give_num'] = intval($sub_card['give_num']); } if ($sub_card['effective_time_type'] == 1) { // 1固定时间 if (empty($sub_card['effective_time'])) { $res = array('status' => 0, 'msg' => '请设置正确的有效期时间'); $this->ajaxReturn($res); } $cr['effective_start'] = strtotime($sub_card['effective_time'][0] . '00:00:00'); $cr['effective_end'] = strtotime($sub_card['effective_time'][1] . '23:59:59'); } else if ($sub_card['effective_time_type'] == 2 || $sub_card['effective_time_type'] == 3) { if (!$sub_card['effective_day'] || intval($sub_card['effective_day']) <= 0 || intval($sub_card['effective_day']) > 9999) { $res = array('status' => 0, 'msg' => '请设置正确的有效期天数'); $this->ajaxReturn($res); } $cr['effective_day'] = $sub_card['effective_day']; } $cr['effective_time_type'] = $sub_card['effective_time_type']; $cr['merchantid'] = $this->merchant_id; $cr['name'] = $sub_card['name']; $cr['card_name_color'] = $sub_card['card_name_color']; $cr['btn_background'] = $sub_card['btn_background']; $cr['btn_text_color'] = $sub_card['btn_text_color']; $cr['stock'] = $sub_card['stock']; $cr['surplus'] = $sub_card['stock']; $cr['use_type'] = $sub_card['use_type']; $cr['time_interval'] = implode(',', $sub_card['time_interval']); $cr['type'] = $sub_card['type']; $cr['line_state'] = $sub_card['line_state']; $cr['online_state'] = $sub_card['online_state']; $cr['end_time'] = strtotime($sub_card['end_time']); $cr['introduce'] = $sub_card['introduce'] ? $sub_card['introduce'] : '暂无说明'; $cr['addtime'] = time(); $cr['status'] = 0; $cr['card_type'] = 1; $add = $subCardModel->toAdd($cr); // 判断add if (!$add) { $res = array('status' => 0, 'msg' => '创建失败'); $this->ajaxReturn($res); } $res = array('status' => 1, 'msg' => '创建成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-新增次卡';//操作行为描述 action_log($source, $type, $request_data, $desc); //end // 判断类型 5.门店购买商品 if ($sub_card['type'] == 5) { $g_res = $this->addCardGoods($sub_card['store_id'], $sub_card['goods'], 0, $add); $res = $g_res; } $this->ajaxReturn($res); } /** * 创建卡包 * @param [array] $card_bag 提交信息 * @return [array] $res */ public function createCardBag() { // 实例化模型 $cardBagModel = new CardBagModel(); // 添加数据 $cr = array(); // 提交数据 $card_bag = I('post.card_bag'); // 卡包 $wait_add = I('post.wait_add'); // 次卡(所有) if (!$card_bag || !$wait_add) { $res = array('status' => 0, 'msg' => '请完善表单信息'); $this->ajaxReturn($res); } // 验证 if (!trim($card_bag['name']) || !trim($card_bag['card_name_color']) || !trim($card_bag['btn_background']) || !trim($card_bag['btn_text_color']) || !trim($card_bag['stock']) || !trim($card_bag['type']) || !trim($card_bag['end_time'])) { $res = array('status' => 0, 'msg' => '请先完善表单信息'); $this->ajaxReturn($res); } // 次卡类型 1.自主发放 2.充值赠送 3.消费赠送 4.门店购买 5.门店购买商品 $card_bag['type'] = intval($card_bag['type']); if ($card_bag['type'] == 1 || $card_bag['type'] == 2 || $card_bag['type'] == 3) { // 等级 $cr['gradeids'] = intval($card_bag['grade_type']) == 2 && !empty($card_bag['gradeids']) ? implode(',', $card_bag['gradeids']) : 0; // 指定门店 $cr['storeids'] = intval($card_bag['store_type']) == 2 && empty($card_bag['storeids']) ? implode(',', $card_bag['storeids']) : 0; // 判断充值,消费赠送 if ($card_bag['type'] == 2 || $card_bag['type'] == 3) { // 判断金额度 if (!$card_bag['money'] || floatval($card_bag['money']) <= 0) { $res = array('status' => 0, 'msg' => '请设置正确的金额度'); $this->ajaxReturn($res); } // 判断赠送次数 if (!$card_bag['give_num'] || intval($card_bag['give_num']) < 0) { $res = array('status' => 0, 'msg' => '请设置正确的赠送次数'); $this->ajaxReturn($res); } $cr['money'] = floatval($card_bag['money']); $cr['give_num'] = intval($card_bag['give_num']); } } else { // 判断门店id if (!$card_bag['store_id']) { $res = array('status' => 0, 'msg' => '请选择门店'); $this->ajaxReturn($res); } // 门店购买 if ($card_bag['type'] == 4 && (!$card_bag['money'] || floatval($card_bag['money']) <= 0)) { // 判断金额度 $res = array('status' => 0, 'msg' => '请设置正确的金额度'); $this->ajaxReturn($res); } // 门店购买商品 if ($card_bag['type'] == 5 && empty($card_bag['goods'])) { $res = array('status' => 0, 'msg' => '请选择商品'); $this->ajaxReturn($res); } // 判断次数 if (!$card_bag['give_num'] || intval($card_bag['give_num']) < 0) { $res = array( 'status' => 0, 'msg' => $card_bag['type'] == 4 ? '请设置正确的购买次数' : '请设置正确的赠送次数' ); $this->ajaxReturn($res); } $cr['storeids'] = $card_bag['store_id']; $cr['money'] = floatval($card_bag['money']); $cr['give_num'] = intval($card_bag['give_num']); } $cr['merchantid'] = $this->merchant_id; $cr['name'] = $card_bag['name']; $cr['card_name_color'] = $card_bag['card_name_color']; $cr['btn_background'] = $card_bag['btn_background']; $cr['btn_text_color'] = $card_bag['btn_text_color']; $cr['stock'] = $card_bag['stock']; $cr['surplus'] = $card_bag['stock']; $cr['type'] = $card_bag['type']; $cr['line_state'] = $card_bag['line_state']; $cr['online_state'] = $card_bag['online_state']; $cr['end_time'] = strtotime($card_bag['end_time']); $cr['addtime'] = time(); $cr['status'] = 0; $add = $cardBagModel->toAdd($cr); if (!$add) { $res = array('status' => 0, 'msg' => '创建失败'); $this->ajaxReturn($res); } $cr1 = array( 'merchantid' => $this->merchant_id, 'stock' => $cr['stock'], 'surplus' => $cr['stock'], 'give_num' => $cr['give_num'] ? $cr['give_num'] : 1, 'storeids' => $cr['storeids'], 'gradeids' => $cr['gradeids'], 'end_time' => $cr['end_time'], 'addtime' => time(), 'status' => 0, 'card_type' => 2, 'bag_id' => $add ); // 添加次卡 $c_res = $this->addSubCard($cr1, $wait_add); if ($c_res['status'] == 0) { $this->ajaxReturn($c_res); } $res = $c_res; // 判断类型 5.门店购买商品 if ($card_bag['type'] == 5) { $g_res = $this->addCardGoods($card_bag['store_id'], $card_bag['goods'], 1, $add); $res = $g_res; } $this->ajaxReturn($res); } /** * 添加卡包次卡 * @param [array] $cr 提交数据 * @param [array] $sub_card_list 次卡列表 * @return [array] $res */ public function addSubCard($cr, $sub_card_list) { // 实例化模型 $subCardModel = new SubCardModel(); $record = 0; $log_data = []; foreach ($sub_card_list as &$val) { $cr1 = $cr; if (!trim($val['name']) || !trim($val['use_type']) || !$val['time_interval'] || empty($val['effective_time_type'])) { // 验证失败,跳过该条次卡 continue; } // 使用类型 2.自定义 if ($val['use_type'] == 2 || $val['use_type'] == '2') { // 自定义次数 if (!$val['frequency'] || intval($val['frequency']) <= 0) { // 验证失败,跳过该条次卡 continue; } // 自定义每天使用次数 if (!$val['limit'] || intval($val['limit']) <= 0) { // 验证失败,跳过该条次卡 continue; } $cr1['frequency'] = intval($val['frequency']); $cr1['limit'] = intval($val['limit']); } // 使用类型 3.几天使用一次 if ($val['use_type'] == 3) { // 几天使用一次 if (!$val['limit'] || intval($val['limit']) <= 0 || intval($val['limit']) > 9999) { // 验证失败,跳过该条次卡 continue; } $cr1['limit'] = intval($val['limit']); } if ($val['effective_time_type'] == 1) { // 1固定时间 if (empty($val['effective_time'])) { // 验证失败,跳过该条次卡 continue; } $cr1['effective_start'] = strtotime($val['effective_time'][0] . '00:00:00'); $cr1['effective_end'] = strtotime($val['effective_time'][1] . '23:59:59'); } else if ($val['effective_time_type'] == 2 || $val['effective_time_type'] == 3) { if (!$val['effective_day'] || intval($val['effective_day']) <= 0 || intval($val['effective_day']) > 9999) { // 验证失败,跳过该条次卡 continue; } $cr1['effective_day'] = $val['effective_day']; } $cr1['effective_time_type'] = $val['effective_time_type']; $cr1['name'] = $val['name']; $cr1['use_type'] = $val['use_type']; $cr1['time_interval'] = implode(',', $val['time_interval']); $cr1['introduce'] = $val['introduce'] ? $val['introduce'] : '暂无说明'; $add = $subCardModel->toAdd($cr1); if ($add) { $log_data[] = $cr1; $record++; } unset($cr1); unset($add); } if (!$record || $record == 0) { $CardBag = new CardBagModel(); $CardBag->toSave(array('id' => $cr['bag_id']), array('status' => 1)); $res = array('status' => 0, 'msg' => '卡包创建成功,次卡添加失败,请失效该卡包,重新创建'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($log_data);//请求参数 json数据 $desc = '插件-卡包创建成功,次卡添加失败,请失效该卡包,重新创建';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $res = array('status' => 1, 'msg' => '创建成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($log_data);//请求参数 json数据 $desc = '插件-卡包创建成功';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } return $res; } /** * 添加商品 * @param [array] $store_id 门店id * @param [array] $goods 选择的商品 * @param [int] $type 类型 1.次卡 2.卡包 * @param [int] $c_id 次卡,卡包id */ protected function addCardGoods($store_id, $goods, $type, $c_id) { $CardGoods = new RetailCardGoodsModel(); $cr = array( $CardGoods::MERCHANT_ID => I('session.merchant_userinfo')['id'], $CardGoods::STORE_ID => $store_id, $CardGoods::TYPE => $type, $CardGoods::C_ID => $c_id, $CardGoods::ADD_TIME => time(), $CardGoods::STATUS => 1 ); // 判断商品 $Goods = new RetailGoodsModel(); $map = array( $Goods::MERCHANT_ID => I('session.merchant_userinfo')['id'], $Goods::STORE_ID => $store_id, $Goods::STATUS => 0 ); // 判断规格 $SkuGoods = new RetailSkuGoodsModel(); $map2 = array( $SkuGoods::STORE_ID => $store_id, $SkuGoods::STATUS => 1, $SkuGoods::STATE => 0, ); $num = 0; foreach ($goods as $key => &$val) { // 判断商品 $g_map = $map; $g_cr = $cr; $g_map[$Goods::ID] = $val[$Goods::ID]; $g_find = $Goods->getFind($g_map); if (empty($g_find)) { continue; } // 判断规格 if ($val['sku_id']) { $s_map = $map2; $s_map[$SkuGoods::ID] = $val['sku_id']; $s_map[$SkuGoods::GOODS_ID] = $val[$Goods::ID]; $s_find = $SkuGoods->getFind($s_map); // 判断 if (empty($s_find)) { continue; } $g_cr[$CardGoods::SKU_ID] = $val['sku_id']; } $g_cr[$CardGoods::GOODS_ID] = $val[$Goods::ID]; $g_cr[$CardGoods::NUMBER] = floatval($val['number']) > 0 ? floatval($val['number']) : 1; $add = $CardGoods->addOrder($g_cr); if ($add) { $num++; } unset($g_find); unset($add); } // 判断添加 if ($num == 0) { // 添加失败 if ($type == 0) { $SubCard = new SubCardModel(); $SubCard->toSave(array('id' => $c_id), array('status' => 1)); } else { } $res = array('status' => 0, 'msg' => '创建失败'); } else { if (count($goods) == $num) { $res = array('status' => 1, 'msg' => '创建成功'); } else { $res = array('status' => 2, 'msg' => '创建成功,部分异常商品移除'); } } return $res; } /** * 获取卡包详情 * @param [int] $id 卡包id * @return [array] $res */ public function cardBagDetails() { // 实例化模型 $subCardModel = new SubCardModel(); $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失,请刷新重试'); $this->ajaxReturn($res); } // 查询条件 $map['merchantid'] = $this->merchant_id; $map['bag_id'] = $id; $map['card_type'] = 2; $card_bag_details = $subCardModel->toSelect($map); if (!$card_bag_details) { $res = array('status' => 0, 'msg' => '该卡包内暂无可用次卡'); $this->ajaxReturn($res); } foreach ($card_bag_details as &$val) { $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']); 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'] . '天内有效'; } } } $res = array('status' => 1, 'card_bag_details' => $card_bag_details); $this->ajaxReturn($res); } /** * 获取指定门店、等级内的会员(公共方法) * @param [string] $storeids 门店id * @param [string] $gradeids 等级会员id * @param [string] $vip_phone 会员手机号、实体卡 * @return [string] $res */ protected function getStoreGradeVipData($storeids, $gradeids, $keyword = '') { /** * $map 门店 * $map2 会员 * $map3 等级 *********************************/ // 实例化模型 $storeModel = new RetailStoreModel(); $vipIntegralModel = new VipIntegralModel(); $vipModel = new RetailVipModel(); $field = 'id,storeid,mobile_phone,balance,name,nickname,integral,card_key,grade_integral,plus_type,plus_state'; // 门店 if ($storeids && ($storeids != 0 || $storeids != '0')) { // 指定门店 $map['id'] = array('in', $storeids); $map['state'] = 0; $map['merchantid'] = $this->merchant_id; $store_list = $storeModel->getFindField($map, 'storetag', true); $map2['storeid'] = array('in', $storeids); } else { $store_list = array('0' => '所有门店'); } // 手机号、实体卡搜索条件 if (trim($keyword)) { $map2['mobile_phone|card_key'] = array('like', '%' . trim($keyword) . '%'); } $map2['status'] = 1; $map2['merchantid'] = $this->merchant_id; // 查询会员 if ($gradeids && ($gradeids != 0 || $gradeids != '0')) { // 查询等级 $map3['id'] = array('in', $gradeids); $map3['merchantid'] = $this->merchant_id; $grade_integrals = $vipIntegralModel->getFindField($map3, 'min_integral', true); if (!$grade_integrals) { $res = array('status' => 0, 'msg' => '暂无会员等级'); return $res; } // 会员 $vip_list = array(); foreach ($grade_integrals as &$val) { // 积分获取 $min_integral = $vipIntegralModel->getFindField(array('min_integral' => array('gt', $val)), 'min_integral', false, 'min_integral asc'); if ($min_integral) { // 之上有积分 $map2['grade_integral'] = array('between', array($val, $min_integral - 1)); } else { // 之上无积分 $map2['grade_integral'] = array('gt', $val); } $vips = $vipModel->getSelect($map2, $field); if ($vips) { $vip_list = array_merge($vip_list, $vips); } unset($map2['grade_integral']); unset($min_integral); unset($vips); } // 等级 $grade_list = $vipIntegralModel->getFindField($map3, 'cardname', true); } else { $vip_list = $vipModel->getSelect($map2, $field); $grade_list = array('0' => '所有等级'); } if (!$vip_list) { $res = array('status' => 0, 'msg' => '暂无可以发放的会员'); return $res; } foreach ($vip_list as &$value) { $value['store_name'] = M('retail_store')->where(array('id' => $value['storeid']))->getField('storetag'); $name = $value['name'] ? $value['name'] : '未设置'; $nickname = $value['nickname'] ? $value['nickname'] : '未设置'; $value['user_name'] = $name . '|' . $nickname; $value['card_key'] = !$value['card_key'] || $value['card_key'] == 0 || $value['card_key'] == '0' ? '暂无' : $value['card_key']; $value['balance'] = $value['balance'] > 0 ? $value['balance'] : 0; $value['integral'] = $value['integral'] > 0 ? $value['integral'] : 0; } $res = array( 'status' => 1, 'store_list' => $store_list ? $store_list : array(), 'grade_list' => $grade_list ? $grade_list : array(), 'vip_list' => $vip_list ? $vip_list : array() ); return $res; } /** * 获取所有的会员 * @param [string] $storeids 门店id * @param [string] $gradeids 等级会员id * @return [array] $res */ public function getVips() { $storeids = I('post.storeids'); $gradeids = I('post.gradeids'); if ($storeids == '' || $gradeids == '') { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } $result = $this->getStoreGradeVipData($storeids, $gradeids, ''); if ($result['status'] == 1) { $res['status'] = 1; $res['vip_list'] = $result['vip_list']; } else { $res['status'] = 0; $res['msg'] = '暂无可以发放的会员'; } $this->ajaxReturn($res); } /** * 会员生成该普通次卡记录(公共方法) * @param [array] $cr 添加条件 * @param [array] $sub_card 次卡信息 * @param [array] $vips 会员列表 * @return [array] $res */ protected function vipGetSubCard($record, $sub_card, $vips) { /** * 直接发放 */ // 实例化模型 $SubCardModel = new SubCardModel(); $VipCardModel = new RetailVipCardModel(); // 循环添加数据 $start_time = strtotime(date('Y-m-d')); foreach ($vips as $key => $val) { if (($key + 1) > $sub_card['surplus']) { $res = array('status' => 2, 'msg' => '次卡发放完毕'); return $res; } else { // 发放 $record['vip_id'] = $val['id']; switch ($sub_card['effective_time_type']) { case '1': //固定时间使用 $record['start_time'] = $start_time; $record['end_time'] = $sub_card['effective_end']; break; case '2': //领卡当日生效 $record['start_time'] = $start_time; $record['end_time'] = $start_time + $sub_card['effective_day'] * 86400 - 1; break; case '3': //领卡次日生效 $record['start_time'] = $start_time + 86400; $record['end_time'] = $start_time + $sub_card['effective_day'] * 86400 - 1; break; } $add = $VipCardModel->addOrder($record); if ($add) { $set_dec = $SubCardModel->toSetDec(array('id' => $sub_card['id']), 'surplus'); if (!$set_dec) { $res = array('status' => 0, 'msg' => '发放出现中断,请刷新重试'); return $res; } } else { continue; // 发放失败,继续下一个发放 } } } $res = array('status' => 1, 'msg' => '发放成功'); return $res; } /** * 批量发放次卡 * @param [int] $id 次卡id * @return [array] $res */ public function batchPutInSubCard() { // 实例化模型 $SubCardModel = new SubCardModel(); $id = I('post.id'); $vips = I('post.vips'); if (!$id || !$vips) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询次卡状态 $map = array( 'id' => $id, 'merchantid' => $this->merchant_id, 'status' => 0, 'end_time' => array('gt', time()), 'card_type' => 1 ); $sub_card = $SubCardModel->toFind($map); if (!$sub_card) { $res = array('status' => 0, 'msg' => '该次卡不符合发放条件'); $this->ajaxReturn($res); } if ($sub_card['surplus'] <= 0) { $res = array('status' => 0, 'msg' => '已达到最大发放量,请选择其他次卡发放'); $this->ajaxReturn($res); } $cr = array( 'merchantid' => $this->merchant_id, 'sub_card_id' => $sub_card['id'], 'addtime' => time(), 'type' => 1 ); $res = $this->vipGetSubCard($cr, $sub_card, $vips); $this->ajaxReturn($res); } /** * 会员生成卡包次卡记录(公共方法) * @param [array] $cr 添加条件 * @param [array] $card_bag 卡包信息 * @param [array] $sub_cards 次卡信息 * @param [array] $vips 会员列表 * @return [array] $res */ protected function vipGetCardBag($cr, $card_bag, $sub_cards, $vips) { /** * 先循环会员,在循环次卡 */ // 实例化模型 $CardBagModel = new CardBagModel(); $VipCardModel = new RetailVipCardModel(); // 循环添加数据 foreach ($vips as $key => $val) { if (($key + 1) > $card_bag['surplus']) { $res = array('status' => 2, 'msg' => '次卡发放完毕'); return $res; } else { // 发放 $record = $cr; $record['vip_id'] = $val['id']; // 循环卡包次卡 $num = 0; foreach ($sub_cards as $value) { switch ($value['effective_time_type']) { case '1': //固定时间使用 $record['start_time'] = time(); $record['end_time'] = $value['effective_end']; break; case '2': //领卡当日生效 $start_time = strtotime(date('Y-m-d')); $record['start_time'] = $start_time; $record['end_time'] = $start_time + $value['effective_day'] * 86400 - 1; break; case '3': //领卡次日生效 $start_time = strtotime(date('Y-m-d')) - 86400; $record['start_time'] = $start_time; $record['end_time'] = $start_time + $value['effective_day'] * 86400 - 1; break; } $record['sub_card_id'] = $value['id']; $add = $VipCardModel->addOrder($record); if (!$add) { $num++; break; } } if ($num > 0) { $res = array('status' => 0, 'msg' => '卡包次卡发放出现中断,请刷新重试'); return $res; } $set_dec = $CardBagModel->toSetDec(array('id' => $card_bag['id']), 'surplus'); if (!$set_dec) { $res = array('status' => 0, 'msg' => '发放出现中断,请刷新重试'); return $res; } } } $res = array('status' => 1, 'msg' => '发放成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($vips);//请求参数 json数据 $desc = '插件-卡包批量发放成功';//操作行为描述 action_log($source, $type, $request_data, $desc); //end return $res; } /** * 卡包批量发送 * @param [int] $id 卡包id * @return [array] $res */ public function batchPutInCardBag() { // 实例化模型 $cardBagModel = new CardBagModel(); $subCardModel = new SubCardModel(); $id = I('post.id'); $vips = I('post.vips'); if (!$id || !$vips) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询卡包状态 $map = array( 'id' => $id, 'merchantid' => $this->merchant_id, 'status' => 0, 'end_time' => array('gt', time()), ); $card_bag = $cardBagModel->toFind($map); if (!$card_bag) { $res = array('status' => 0, 'msg' => '该卡包不符合发放条件'); $this->ajaxReturn($res); } // 判断库存 if ($card_bag['surplus'] <= 0) { $res = array('status' => 0, 'msg' => '已达到最大发放量,请选择其他卡包发放'); $this->ajaxReturn($res); } // 查询卡包次卡状态 $map1 = array( 'merchantid' => $this->merchant_id, 'card_type' => 2, 'bag_id' => $card_bag['id'] ); $sub_cards = $subCardModel->toSelect($map1); if (!$sub_cards) { $res = array('status' => 0, 'msg' => '该卡包暂无可发次卡'); $this->ajaxReturn($res); } $cr = array( 'merchantid' => $this->merchant_id, 'addtime' => time(), 'type' => 2, 'card_bag_id' => $card_bag['id'] ); $res = $this->vipGetCardBag($cr, $card_bag, $sub_cards, $vips); $this->ajaxReturn($res); } /** * 指定发送获取会员 * @param [int] $id 次卡、卡包id * @param [string] $card_mold 类型 1.次卡 2.卡包 * @param [string] $storeids 门店ids * @param [string] $gradeids 等级ids * @param [int] $keyword 手机号、实体卡搜索 * @return [array] $res */ public function getSendOutVipList() { // 实例化模型 $cardBagModel = new CardBagModel(); $subCardModel = new SubCardModel(); // 接收数据 $id = I('post.id'); $card_mold = I('post.card_mold'); $storeids = I('post.storeids'); $gradeids = I('post.gradeids'); $keyword = I('post.keyword') ? I('post.keyword') : ''; if (!$id || !$card_mold || $storeids == '' || $gradeids == '') { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 判断状态 if (intval($card_mold) == 1) { $card_data = $subCardModel->toFind(['id' => $id, 'merchantid' => $this->merchant_id], 'id,name'); } else { $card_data = $cardBagModel->toFind(['id' => $id, 'merchantid' => $this->merchant_id], 'id,name'); } if (!$card_data) { $res = array('status' => 0, 'msg' => intval($card_mold) == 1 ? '无效的次卡' : '无效的卡包'); $this->ajaxReturn($res); } $result = $this->getStoreGradeVipData($storeids, $gradeids, $keyword); if ($result['status'] == 0) { $res = array('status' => 0, 'msg' => $result['msg']); } else { $res = array('status' => 1, 'card_data' => $card_data, 'stores' => $result['store_list'], 'grades' => $result['grade_list'], 'vip_list' => $result['vip_list']); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-卡包指定发放成功';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } $this->ajaxReturn($res); } /** * 获取赠送详情 * @param [int] $id 次卡、卡包id * @param [string] $card_mold 类型 1.次卡 2.卡包 * @return [array] $res */ public function getGiveDetails() { // 判断登录 $merchant_id = I('session.merchant_userinfo')['id']; if (!$merchant_id) { $res = array('status' => 0, 'msg' => '登录超时,请重新登录'); $this->ajaxReturn($res); } // 接受参数 $id = I('post.id'); $card_mold = intval(I('post.card_mold')); // 判断参数 if (!$id || !in_array($card_mold, array(1, 2))) { $res = array('status' => 0, 'msg' => '必要参数缺失'); $this->ajaxReturn($res); } // 判断类型 $field = array( 'id', 'name', 'type', 'money', 'give_num', 'storeids', 'gradeids', 'line_state', 'online_state' ); if ($card_mold == 1) { // 次卡 $subCardModel = new SubCardModel(); $card_data = $subCardModel->toFind(array('id' => $id, 'merchantid' => $merchant_id), $field); } else { // 卡包 $cardBagModel = new CardBagModel(); $card_data = $cardBagModel->toFind(array('id' => $id, 'merchantid' => $merchant_id), $field); } // 判断查询 if (empty($card_data)) { $res = array('status' => 0, 'msg' => $card_mold == 1 ? '无效的次卡,建议删除' : '无效的卡包,建议删除'); $this->ajaxReturn($res); } // 定义数组 $give_details = array( 'name' => $card_data['name'], 'type' => $card_data['type'], 'type_name' => '', 'money' => floatval($card_data['money']), 'give_num' => $card_data['give_num'], 'line_state' => $card_data['line_state'], 'online_state' => $card_data['online_state'], 'stores' => [], 'grades' => [], 'store_name' => '', 'goods' => [] ); // 判断类型 switch ($card_data['type']) { case 1: $give_details['type_name'] = '自主发放'; break; case 2: $give_details['type_name'] = '充值赠送'; break; case 3: $give_details['type_name'] = '消费赠送'; break; case 4: $give_details['type_name'] = '门店购买'; break; case 5: $give_details['type_name'] = '门店购买商品'; break; default: $give_details['type_name'] = '未知类型'; break; } // 判断类型 if ($card_data['type'] == 1 || $card_data['type'] == 2 || $card_data['type'] == 3) { $result = $this->getStoreGradeData($merchant_id, $card_data['storeids'], $card_data['gradeids']); $give_details['stores'] = $result['store_list']; $give_details['grades'] = $result['grade_list']; } else { // 查询门店 $Store = new RetailStoreModel(); $s_map = array( $Store::ID => $card_data['storeids'], $Store::STATE => 0, $Store::MERCHANT_ID => $merchant_id, ); $store_name = $Store->getFindField($s_map, $Store::STORE_NAME); $give_details['store_name'] = $store_name ? $store_name : '异常门店'; // 判断购买商品 if ($card_data['type'] == 5) { // 获取商品 $CardGoods = new RetailCardGoodsModel(); $cg_map = array( 'c.' . $CardGoods::MERCHANT_ID => $merchant_id, 'c.' . $CardGoods::STORE_ID => $card_data['storeids'], 'c.' . $CardGoods::TYPE => $card_mold == 1 ? 0 : 1, 'c.' . $CardGoods::C_ID => $card_data['id'], 'c.' . $CardGoods::STATUS => 1, 'g.status' => 0 ); $cg_field = array( 'c.*', 'g.goods_name', 'g.goods_number', 'g.goods_code', 'g.pricing_method', 'g.attribute_state', 'g.online_state' ); $card_goods = $CardGoods->getCardGoods($cg_map, $cg_field); if (!empty($card_goods)) { // 商品库存公共条件 $SSaleMGoods = new SSaleMGoodsModel(); $sag_map = array( 'merchant_id' => $merchant_id, 'store_id' => $card_data['storeids'], ); // 规格公共条件 $SkuGoods = new RetailSkuGoodsModel(); $sku_map = array( 'k.' . $SkuGoods::STORE_ID => $card_data['storeids'], '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' ); $SkuList = new RetailSkuListModel(); // 处理数据 foreach ($card_goods as &$val) { $val['number'] = floatval($val['number']); // 库存 $sg_map2 = $sag_map; $sg_map2['goods_id'] = $val[$CardGoods::GOODS_ID]; $val['goods_num'] = $SSaleMGoods->getFindField($sg_map2, 'goods_num'); $val['goods_num'] = floatval($val['goods_num']) > 0 ? floatval($val['goods_num']) : 0; // 判断规格 if ($val[$CardGoods::SKU_ID]) { // 条件 $sku_map2 = $sku_map; $sku_map2['k.' . $SkuGoods::ID] = $val[$CardGoods::SKU_ID]; $sku_map2['k.' . $SkuGoods::GOODS_ID] = $val[$CardGoods::GOODS_ID]; // 查询 $sku_goods = $SkuGoods->getSkuGoodsInfo($sku_map2, $sku_field); // 判断 if (empty($sku_goods)) { // 清除指定规格 $val[$CardGoods::SKU_ID] = 0; } else { // 规格 $sku_name = $SkuList->getFindField(array($SkuList::ID => array('in', $sku_goods[$SkuGoods::SKU_ID_LIST])), $SkuList::NAME, true); $val['sku_name'] = implode(',', $sku_name); // 库存 $val['goods_num'] = floatval($sku_goods['goods_num']) > 0 ? floatval($sku_goods['goods_num']) : 0; } } } } $give_details['goods'] = !empty($card_goods) ? $card_goods : array(); } } $res = array('status' => 1, 'give_details' => $give_details); $this->ajaxReturn($res); } /** * 获取指定门店、等级内(公共方法) * @param [string] $storeids 门店id * @param [string] $gradeids 等级会员id * @return [string] $res */ protected function getStoreGradeData($merchant_id, $storeids, $gradeids) { /** * $map 门店 * $map2 等级 *********************************/ // 门店 if ($storeids && intval($storeids) != 0) { $Store = new RetailStoreModel(); $s_map = array( $Store::ID => array('in', $storeids), $Store::MERCHANT_ID => $merchant_id, $Store::STATE => 0, ); $store_list = $Store->getFindField($s_map, $Store::STORE_NAME, true); } else { $store_list = array('0' => '所有门店'); } // 等级 if ($gradeids && intval($gradeids) != 0) { $VipIntegral = new RetailVipIntegralModel(); $vi_map = array( $VipIntegral::ID => array('in', $gradeids), $VipIntegral::MERCHANT_ID => $merchant_id, ); $grade_list = $VipIntegral->getFindField($vi_map, $VipIntegral::CARD_NAME, true); } else { $grade_list = array('0' => '所有等级'); } $res = array('store_list' => $store_list, 'grade_list' => $grade_list); return $res; } /** * 获取次卡发放记录 * @param [int] $id id * @param [string] $keyword 手机号、实体卡搜索 * @return [array] $res */ public function subCardRecord() { // 实例化模型 $subCardViewModel = new SubCardViewModel(); $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询条件 $keyword = I('post.keyword'); if (trim($keyword)) { $map['Vip.mobile_phone|Vip.card_key'] = array('like', '%' . trim($keyword) . '%'); } $map['VipCard.merchantid'] = $this->merchant_id; $map['VipCard.sub_card_id'] = $id; $record_list = $subCardViewModel->toSelect($map, (int)I('post.page'), (int)I('post.limit')); if (!$record_list) { $res = array('status' => 0, 'msg' => '暂无数据'); $this->ajaxReturn($res); } $total = $subCardViewModel->total($map); foreach ($record_list as &$val) { $val['vip_name'] = $val['vip_name'] ? $val['vip_name'] : '未设置'; $val['nickname'] = $val['nickname'] ? $val['nickname'] : '未设置'; $val['mobile_phone'] = $val['mobile_phone'] ? $val['mobile_phone'] : '无'; $val['card_key'] = !$val['card_key'] || $val['card_key'] == 0 || $val['card_key'] == '0' ? '无' : $val['card_key']; $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']); } $res = array('status' => 1, 'record_list' => $record_list, 'total' => (int)$total); $this->ajaxReturn($res); } /** * 获取卡包发放记录 * @param [int] $id id * @param [string] $keyword 手机号、实体卡搜索 * @return [array] $res */ public function cardBagRecord() { // 实例化模型 $subCardModel = new SubCardModel(); $subCardViewModel = new SubCardViewModel(); $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询次卡 $map = array( 'merchantid' => $this->merchant_id, 'card_type' => 2, 'bag_id' => $id ); $ids = $subCardModel->toFindField($map, 'id', true); if (!$ids) { $res = array('status' => 0, 'msg' => '暂无次卡'); $this->ajaxReturn($res); } // 查询条件 $keyword = I('post.keyword'); if (trim($keyword)) { $map1['Vip.mobile_phone|Vip.card_key'] = array('like', '%' . trim($keyword) . '%'); } $map1['VipCard.sub_card_id'] = array('in', $ids); $record_list = $subCardViewModel->toSelect($map1, (int)I('post.page'), (int)I('post.limit')); if (!$record_list) { $res = array('status' => 0, 'msg' => '暂无数据'); $this->ajaxReturn($res); } $total = $subCardViewModel->total($map1); foreach ($record_list as &$val) { $val['vip_name'] = $val['vip_name'] ? $val['vip_name'] : '未设置'; $val['nickname'] = $val['nickname'] ? $val['nickname'] : '未设置'; $val['mobile_phone'] = $val['mobile_phone'] ? $val['mobile_phone'] : '无'; $val['card_key'] = !$val['card_key'] || $val['card_key'] == 0 || $val['card_key'] == '0' ? '无' : $val['card_key']; $val['addtime'] = date('Y-m-d H:i:s', $val['addtime']); } $res = array('status' => 1, 'record_list' => $record_list, 'total' => (int)$total); $this->ajaxReturn($res); } /** * 获取卡面图片 * @param [int] $id 次卡id * @return [array] $res */ public function getCardImage() { // 判断登录 $merchant_id = I('session.merchant_userinfo')['id']; // 登录超时 if (!$merchant_id) { $res = array('status' => 0, 'msg' => '登录超时,请重新登录'); $this->ajaxReturn($res); } // 接收数据 $id = I('post.id'); $type = I('post.type'); // 判断 if (!$id || !in_array($type, array(1, 2))) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 判断类型 if ($type == 1) { // 查询次卡 $SubCard = new SubCardModel(); $data = $SubCard->toFind(array('id' => $id), 'image,gallery_id'); } else { // 查询卡包 $CardBag = new CardBagModel(); $data = $CardBag->toFind(array('id' => $id), 'image,gallery_id'); } // 判断 if (empty($data)) { $res = array( 'status' => 0, 'msg' => intval($type) == 1 ? '次卡活动查询失败' : '卡包活动查询失败' ); $this->ajaxReturn($res); } // 处理图片 $image_url = ''; if ($data['gallery_id'] && $data['gallery_id'] != 0) { $ImageGallery = new ImageGalleryModel(); $image_url = $ImageGallery->getOneField(array('id' => $data['gallery_id']), 'path'); } else if ($data['image'] && ($data['image'] != '' || $data['image'] != '/Uploads')) { $image_url = $data['image']; } $res = array('status' => 1, 'image_url' => $image_url); $this->ajaxReturn($res); } /** * 设置次卡卡面图片 * @param [int] $id 次卡id * @param [array] $file 本地上传图片 * @param [array] $card_image 表单 * @return [array] $res */ public function saveCardSet() { // 实例化模型 $subCardModel = new SubCardModel(); $ImageGalleryModel = new ImageGalleryModel(); // 次卡id $id = I('post.sub_card_id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询次卡当前图片 $sub_card = $subCardModel->toFind(array('id' => $id), 'id,image,gallery_id'); if (!$sub_card) { $res = array('status' => 0, 'msg' => '该张次卡不存在,请刷新重试'); $this->ajaxReturn($res); } // 图库图片 $gallery_id = I('post.gallery_id'); if ($gallery_id && ($gallery_id != '' || $gallery_id != 0)) { // 判断图库图片 $gallery_image = $ImageGalleryModel->getFind(array('id' => $gallery_id)); if (!$gallery_image) { $res = array('status' => 0, 'msg' => '该图片不存在,请重新选择'); $this->ajaxReturn($res); } if ($sub_card['gallery_id'] != $gallery_image['id']) { $up['gallery_id'] = $gallery_image['id']; } if ($sub_card['image'] != $gallery_image['path']) { $up['image'] = $gallery_image['path']; } } else { // 本地图片 $image = $_FILES['image']; if (!$image) { $res = array('status' => 0, 'msg' => '请先选择图片'); $this->ajaxReturn($res); } // 上传图片 $cr = array( 'add_time' => time(), 'from_type_id' => 42, 'localUrl' => 'Merchant/SubCard', 'user_type' => 3, 'merchant_id' => $this->merchant_id, ); $uploads = uploadFiles($cr); if ($uploads['image']['savePath'] && $uploads['image']['id']) { $up['gallery_id'] = $uploads['image']['id']; $up['image'] = $uploads['image']['savePath']; } else { $res = array('status' => 0, 'msg' => '图片上传失败,请重试'); $this->ajaxReturn($res); } } if (!$up) { $res = array('status' => 0, 'msg' => '请先选择其他图片'); $this->ajaxReturn($res); } $save = $subCardModel->toSave(array('id' => $id), $up); if ($save) { $res = array('status' => 1, 'msg' => '设置成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-次卡修改卡面图片';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $res = array('status' => 0, 'msg' => '设置失败,请刷新重试'); } $this->ajaxReturn($res); } /** * 设置次卡卡面图片 * @param [int] $id 次卡id * @param [array] $file 本地上传图片 * @param [array] $card_image 表单 * @return [array] $res */ public function saveBagSet() { // 实例化模型 $CardBag = new CardBagModel(); $ImageGalleryModel = new ImageGalleryModel(); // 次卡id $id = I('post.card_bag_id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询卡包当前图片 $card_bag = $CardBag->toFind(array('id' => $id), 'id,image,gallery_id'); if (empty($card_bag)) { $res = array('status' => 0, 'msg' => '该卡包不存在,请刷新重试'); $this->ajaxReturn($res); } // 图库图片 $gallery_id = I('post.gallery_id'); if ($gallery_id && ($gallery_id != '' || $gallery_id != 0)) { // 判断图库图片 $gallery_image = $ImageGalleryModel->getFind(array('id' => $gallery_id)); if (!$gallery_image) { $res = array('status' => 0, 'msg' => '该图片不存在,请重新选择'); $this->ajaxReturn($res); } if ($card_bag['gallery_id'] != $gallery_image['id']) { $up['gallery_id'] = $gallery_image['id']; } if ($card_bag['image'] != $gallery_image['path']) { $up['image'] = $gallery_image['path']; } } else { // 本地图片 $image = $_FILES['image']; if (!$image) { $res = array('status' => 0, 'msg' => '请先选择图片'); $this->ajaxReturn($res); } // 上传图片 $cr = array( 'add_time' => time(), 'from_type_id' => 42, 'localUrl' => 'Merchant/SubCard', 'user_type' => 3, 'merchant_id' => $this->merchant_id, ); $uploads = uploadFiles($cr); if ($uploads['image']['savePath'] && $uploads['image']['id']) { $up['gallery_id'] = $uploads['image']['id']; $up['image'] = $uploads['image']['savePath']; } else { $res = array('status' => 0, 'msg' => '图片上传失败,请重试'); $this->ajaxReturn($res); } } if (!$up) { $res = array('status' => 0, 'msg' => '请先选择其他图片'); $this->ajaxReturn($res); } $save = $CardBag->toSave(array('id' => $id), $up); if ($save) { $res = array('status' => 1, 'msg' => '设置成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-卡包修改图片';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $res = array('status' => 0, 'msg' => '设置失败,请刷新重试'); } $this->ajaxReturn($res); } /** * 次卡失效 * @param [int] $id 次卡id * @return [array] $res */ public function invalidSubCard() { // 实例化模型 $subCardModel = new SubCardModel(); // 次卡id $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询次卡 $sub_card = $subCardModel->toFind(array('id' => $id, 'status' => 0)); if (!$sub_card) { $res = array('status' => 0, 'msg' => '查询失败,请刷新重试'); $this->ajaxReturn($res); } // 失效次卡 $save = $subCardModel->toSave(array('id' => $id, 'status' => 0), array('status' => 1)); if ($save) { $res = array('status' => 1, 'msg' => '失效成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-失效次卡';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $res = array('status' => 0, 'msg' => '失效失败,请刷新重试'); } $this->ajaxReturn($res); } /** * 失效卡包 * @param [int] $id 卡包id * @return [array] $res */ public function invalidCardBag() { // 实例化模型 $cardBagModel = new CardBagModel(); // 卡包id $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询卡包 $card_bag = $cardBagModel->toFind(array('id' => $id, 'status' => 0)); if (!$card_bag) { $res = array('status' => 0, 'msg' => '查询失败,请刷新重试'); $this->ajaxReturn($res); } // 失效次卡 $save = $cardBagModel->toSave(array('id' => $id, 'status' => 0), array('status' => 1)); if ($save) { $res = array('status' => 1, 'msg' => '失效成功'); //操作日志记录 $source = 0;//请求来源: 0pc系统 1小助手 $type = 3;//平台:1合伙人端 2代理商端 3商户端 4门店端 $request_data = json_encode($_POST);//请求参数 json数据 $desc = '插件-卡包失效';//操作行为描述 action_log($source, $type, $request_data, $desc); //end } else { $res = array('status' => 0, 'msg' => '失效失败,请刷新重试'); } $this->ajaxReturn($res); } /** * 次卡核销记录 * @param $id int 次卡id * @param $keyword string 搜索 * @param $page string 分页 * @param $limit string 数量 * @return $res */ public function subCardUseRecord() { // 判断登录 $merchant_id = I('session.merchant_userinfo')['id']; // 登录超时 if (!$merchant_id) { $res = array('status' => 0, 'msg' => '登录超时,请重新登录'); $this->ajaxReturn($res); } // 接收数据 $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } $VipCardRecord = new VipCardRecordModel(); // 搜索条件 $keyword = I('post.keyword'); if (trim($keyword)) { $map['v.mobile_phone|v.card_key'] = array('like', '%' . trim($keyword) . '%'); } // 实例化模型 $map['vcr.merchantid'] = $merchant_id; $map['vc.sub_card_id'] = $id; $field = array( 'vcr.id', 'vcr.merchantid', 'vcr.vip_id', 'vcr.vip_card_id', 'vcr.addtime', 'vcr.use_store_id', 'vcr.cashier_id', 'vc.sub_card_id', 'v.mobile_phone', 'v.name', 'v.nickname' ); $use_record_list = $VipCardRecord->selectVipCardRecord($map, $field, intval(I('post.page')), intval(I('post.limit')), 'vcr.addtime desc'); if (empty($use_record_list)) { $res = array('status' => 0, 'msg' => '暂无数据'); $this->ajaxReturn($res); } // 统计 $total = $VipCardRecord->totalVipCardRecord($map); // 处理数据 $SubCard = new SubCardModel(); $Store = new RetailStoreModel(); $Cashier = new RetailCashierModel(); foreach ($use_record_list as &$val) { $val['vip_name'] = $val['name'] ? $val['name'] : '未设置'; $val['nickname'] = $val['nickname'] ? $val['nickname'] : '未设置'; $val['mobile_phone'] = $val['mobile_phone'] ? $val['mobile_phone'] : '无'; $val['add_time'] = date('Y-m-d H:i:s', $val['addtime']); // 次卡名称 $val['name'] = $SubCard->toFindField(array('id' => $val['sub_card_id']), 'name'); // 核销门店 $val['use_store_name'] = $Store->getFindField(array($Store::ID => $val['use_store_id']), $Store::STORE_NAME); // 核销人员 $val['cashier_name'] = $Cashier->getFindField(array($Cashier::ID => $val['cashier_id']), $Cashier::NAME); } $res = array('status' => 1, 'use_record_list' => $use_record_list, 'total' => intval($total)); $this->ajaxReturn($res); } /** * 卡包核销记录 * @param $id int 卡包id * @param $keyword string 搜索 * @param $page string 分页 * @param $limit string 数量 * @return $res */ public function cardBagUseRecord() { // 接收数据 $id = I('post.id'); if (!$id) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } // 查询卡包次id $subCardModel = new SubCardModel(); $VipCardRecord = new VipCardRecordModel(); $map = array( 'merchantid' => $this->merchant_id, 'card_type' => 2, 'bag_id' => $id ); $ids = $subCardModel->toFindField($map, 'id', true); if (!$ids) { $res = array('status' => 0, 'msg' => '暂无数据'); $this->ajaxReturn($res); } $map2['vc.sub_card_id'] = array('in', $ids); // 搜索条件 $keyword = I('post.keyword'); if (trim($keyword)) { $map2['v.mobile_phone|v.card_key'] = array('like', '%' . trim($keyword) . '%'); } // 实例化模型 $map2['vcr.merchantid'] = $this->merchant_id; $field = array( 'vcr.id', 'vcr.merchantid', 'vcr.vip_id', 'vcr.vip_card_id', 'vcr.addtime', 'vcr.use_store_id', 'vcr.cashier_id', 'vc.sub_card_id', 'v.mobile_phone', 'v.name', 'v.nickname' ); $use_record_list = $VipCardRecord->selectVipCardRecord($map2, $field, intval(I('post.page')), intval(I('post.limit')), 'vcr.addtime desc'); if (!$use_record_list) { $res = array('status' => 0, 'msg' => '暂无数据2'); $this->ajaxReturn($res); } // 统计 $total = $VipCardRecord->totalVipCardRecord($map2); // 处理数据 $SubCard = new SubCardModel(); $Store = new RetailStoreModel(); $Cashier = new RetailCashierModel(); foreach ($use_record_list as &$val) { $val['vip_name'] = $val['name'] ? $val['name'] : '未设置'; $val['nickname'] = $val['nickname'] ? $val['nickname'] : '未设置'; $val['mobile_phone'] = $val['mobile_phone'] ? $val['mobile_phone'] : '无'; $val['add_time'] = date('Y-m-d H:i:s', $val['addtime']); // 次卡名称 $val['name'] = $SubCard->toFindField(array('id' => $val['sub_card_id']), 'name'); // 核销门店 $val['use_store_name'] = $Store->getFindField(array($Store::ID => $val['use_store_id']), $Store::STORE_NAME); // 核销人员 $val['cashier_name'] = $Cashier->getFindField(array($Cashier::ID => $val['cashier_id']), $Cashier::NAME); } $res = array('status' => 1, 'use_record_list' => $use_record_list, 'total' => intval($total)); $this->ajaxReturn($res); } /** * 获取次卡全局设置 */ public function getCardBaseData() { if (IS_POST) { $Merchant = new MerchantConfig(); $this->ajaxReturn($Merchant->getCardBaseData([ 'merchant_id' => $this->merchant_id, ])); } } /** * 保存次卡全局设置 */ public function setCardBaseData() { if (IS_POST) { $Merchant = new MerchantConfig(); $this->ajaxReturn($Merchant->setCardBaseData([ 'merchant_id' => $this->merchant_id, 'use_sub_card_code' => I('post.use_sub_card_code'), ])); } } }