where('uid', 'in', $uids); else $model = $model->where('uid', $uids); return $model->count(); } /** * 设置查询初始化条件 * @param string $alias 表别名 * @param null $model 模型实例化对象 * @return UserLevel */ public static function valiWhere($alias = '', $model = null) { $model = is_null($model) ? new self() : $model; if ($alias) { $model = $model->alias($alias); $alias .= '.'; } return $model->where("{$alias}status", 1)->where("{$alias}is_del", 0); } /** * 设置会员等级 * @param int $uid 用户uid * @param int $level_id 等级id * @return UserLevel|bool|Model * @throws DataNotFoundException * @throws ModelNotFoundException * @throws \think\exception\DbException */ public static function setUserLevel($uid, $level_id) { $vipinfo = SystemUserLevel::get($level_id); if (!$vipinfo) return false; $userinfo = User::find($uid); if (!$userinfo) return false; $add_valid_time = (int)$vipinfo->valid_date * 86400; $uservipinfo = self::valiWhere()->where('uid', $uid)->where('level_id', $level_id)->find(); //检查是否购买过 if ($uservipinfo) { $stay = 0; //剩余时间 if (time() < $uservipinfo->valid_time) $stay = $uservipinfo->valid_time - time(); //如果购买过当前等级的会员过期了.从当前时间开始计算 //过期时效: 剩余时间+当前会员等级时间+当前time $add_valid_time = $stay + $add_valid_time + time(); $data['is_forever'] = $vipinfo->is_forever; $data['valid_time'] = $add_valid_time; User::where('uid', $uid)->update(['level' => $level_id]); return self::where('uid', $uid)->where('level_id', $level_id)->update($data); } else { $data = [ 'is_forever' => $vipinfo->is_forever, 'status' => 1, 'is_del' => 0, 'grade' => $vipinfo->grade, 'uid' => $uid, 'add_time' => time(), 'level_id' => $level_id, 'discount' => $vipinfo->discount, ]; if ($data['is_forever']) $data['valid_time'] = 0; else $data['valid_time'] = $add_valid_time + time(); $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name']; $res = self::create($data); if (!$res) return false; User::where('uid', $uid)->update(['level' => $level_id]); return $res; } } /** * 获取当前用户会员等级返回当前用户等级id * @param int $uid 用户uid * @param int $grade 会员id * @return bool|mixed * @throws DataNotFoundException * @throws ModelNotFoundException * @throws \think\exception\DbException */ public static function getUserLevel($uid, $grade = 0) { $model = self::valiWhere(); if ($grade) $model = $model->where('grade', '<', $grade); $level = $model->where('uid', $uid)->order('grade desc')->field('level_id,is_forever,valid_time,id,status,grade')->find(); if (!$level) return false; if ($level->is_forever) return $level->id; //会员已经过期 if (time() > $level->valid_time) { if ($level->status == 1) { $level->status = 0; $level->save(); } return self::getUserLevel($uid, $level->grade); } else //会员没有过期 return $level->id; } /** * 获取会员详细信息 * @param $id 会员记录id * @param string $keyName 字段名 * @return array|mixed|string|Model|null * @throws DataNotFoundException * @throws ModelNotFoundException * @throws \think\exception\DbException */ public static function getUserLevelInfo($id, $keyName = '') { $vipinfo = self::valiWhere('a')->where('a.id', $id)->field('l.id,a.add_time,l.discount,a.level_id,l.name,l.money,l.icon,l.is_pay,l.grade') ->join('system_user_level l', 'l.id=a.level_id')->find(); if ($keyName) if (isset($vipinfo[$keyName])) return $vipinfo[$keyName]; else return ''; return $vipinfo; } /** * 获取当前用户已成为的vip id * @param $uid 用户id * @return array */ public static function getUserLevelIds($uid) { return self::valiWhere()->group('level_id')->where('uid', $uid)->order('grade asc')->column('level_id', 'level_id'); } /** * 余额支付 * @param $order_id * @param $uid * @param string $formId * @return bool * @throws \think\Exception * @throws DataNotFoundException * @throws ModelNotFoundException * @throws \think\exception\DbException */ public static function yuePay($order_id, $uid, $formId = '') { $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find(); if (!$orderInfo) return self::setErrorInfo('订单不存在!'); if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!'); // if($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!'); $userInfo = User::getUserInfo($uid); if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])]); self::beginTrans(); $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid'); $res2 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元购买商品'); $res3 = self::paySuccess($order_id, 'yue', $formId);//余额支付成功 try { PaymentRepositories::yuePayProduct($userInfo, $orderInfo); } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo($e->getMessage()); } $res = $res1 && $res2 && $res3; self::checkTrans($res); return $res; } /** * 微信支付 为 0元时 * @param $order_id * @param $uid * @param string $formId * @return bool * @throws \think\Exception * @throws DataNotFoundException * @throws ModelNotFoundException * @throws \think\exception\DbException */ public static function jsPayPrice($order_id, $uid, $formId = '') { $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find(); if (!$orderInfo) return self::setErrorInfo('订单不存在!'); if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!'); $userInfo = User::getUserInfo($uid); self::beginTrans(); $res1 = UserBill::expend('购买商品', $uid, 'now_money', 'pay_product', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元购买商品'); $res2 = self::paySuccess($order_id, 'weixin', $formId);//微信支付为0时 $res = $res1 && $res2; self::checkTrans($res); return $res; } /** * //TODO 支付成功后 * @param $orderId * @param string $paytype * @param string $formId * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public static function paySuccess($orderId, $paytype = 'weixin', $formId = '') { $order = self::where('order_id', $orderId)->find(); $resPink = true; $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time(), 'is_del' => 0, 'is_system_del' => 0, 'mark' => '']);//订单改为支付 if ($order->combination_id && $res1 && !$order->refund_status) $resPink = StorePink::createPink($order);//创建拼团 $oid = self::where('order_id', $orderId)->value('id'); StoreOrderStatus::status($oid, 'pay_success', '用户付款成功'); $now_money = User::where('uid', $order['uid'])->value('now_money'); UserBill::expend('购买商品', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元购买商品'); //支付成功后 event('OrderPaySuccess', [$order, $formId]); $res = $res1 && $resPink && UserSpread::setSpreadSure($order['uid']) && User::backOrderBrokerage($order); return false !== $res; } // 创建会员订单 public static function CreateOrder($uid, $level_id) { self::beginTrans(); try { $orderInfo = [ 'uid' => $uid, 'order_id' => self::getNewOrderId(), // 'mer_id' => ]; if ($shipping_type === 2) { $orderInfo['verify_code'] = self::getStoreCode(); $orderInfo['store_id'] = SystemStore::getStoreDispose($storeId, 'id'); if (!$orderInfo['store_id']) return self::setErrorInfo('暂无门店无法选择门店自提!', true); } $order = self::create($orderInfo); if (!$order) return self::setErrorInfo('订单生成失败!', true); $res5 = true; foreach ($cartInfo as $cart) { //减库存加销量 if ($combinationId) $res5 = $res5 && StoreCombination::decCombinationStock($cart['cart_num'], $combinationId, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : ''); else if ($seckill_id) $res5 = $res5 && StoreSeckill::decSeckillStock($cart['cart_num'], $seckill_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : ''); else if ($bargain_id) $res5 = $res5 && StoreBargain::decBargainStock($cart['cart_num'], $bargain_id, isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : ''); else $res5 = $res5 && StoreProduct::decProductStock($cart['cart_num'], $cart['productInfo']['id'], isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : ''); } //保存购物车商品信息 $res4 = false !== StoreOrderCartInfo::setCartInfo($order['id'], $cartInfo); //购物车状态修改 $res6 = false !== StoreCart::where('id', 'IN', $cartIds)->update(['is_pay' => 1]); if (!$res4 || !$res5 || !$res6) return self::setErrorInfo('订单生成失败!', true); //自动设置默认地址 UserRepository::storeProductOrderCreateEbApi($order, compact('cartInfo', 'addressId')); self::clearCacheOrderInfo($uid, $key); self::commitTrans(); StoreOrderStatus::status($order['id'], 'cache_key_create_order', '订单生成'); return $order; } catch (\PDOException $e) { self::rollbackTrans(); return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage()); } catch (\Exception $e) { self::rollbackTrans(); return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage()); } } }