| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\GoodsModel;
- use App\Models\MemberModel;
- use App\Models\OrderGoodsModel;
- use App\Models\OrderModel;
- use App\Models\StoreModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\Kd100Service;
- use App\Services\PaymentService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 订单-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class OrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new OrderModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 订单列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $model = $this->getQuery($params);
- // 数据
- $list = $model->where(function ($query) use ($params) {
- $status = isset($params['status']) ? $params['status'] : 0;
- // 进行中
- if ($status == 9) {
- $query->where('a.refund_status', '>', 0);
- } elseif ($status > 0 && is_array($status)) {
- $query->whereIn('a.status', $status)->where('a.refund_status', 0);
- } else if ($status == 4) {
- $query->where('a.status', $status)->where('a.refund_status', 0);
- } else if ($status > 0) {
- $query->where('a.status', $status)->where('a.refund_status', 0);
- }
- })->select(['a.*'])
- ->orderBy('a.status', 'asc')
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '确认收货'];
- $refundStatusArr = [1 => '已退款', 2 => '退款中', 3 => '退款审核', 4 => '退款驳回'];
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y/m/d H:i') : '';
- $status = isset($item['status']) ? $item['status'] : 0;
- $item['status_text'] = '待支付';
- if ($status) {
- $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
- }
- if ($item['refund_status'] > 0) {
- $item['status_text'] = '退款/售后';
- }
- $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
- }
- unset($item);
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 查询条件
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- return $this->model->from('orders as a')->with(['orderGoods', 'store'])
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($params) {
- $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- if ($keyword) {
- $query->where('a.order_no', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- }
- });
- }
- /**
- * 订单详情
- * @param $id
- */
- public function getOrderInfo($id)
- {
- $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '已完成'];
- $info = $this->model->from('orders as a')->with(['orderGoods','store'])
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where(['a.id' => $id, 'a.mark' => 1])
- ->select(['a.*'])
- ->first();
- if ($info) {
- $info = $info->toArray();
- $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
- $status = isset($info['status']) ? $info['status'] : 0;
- $info['status_text'] = '待付款';
- if ($status) {
- $info['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
- }
- }
- return $info;
- }
- /**
- * 创建订单
- * @param $userId 用户
- * @param $params 参数
- * @return array|false
- */
- public function createOrder($userId, $params)
- {
- $addressId = isset($params['address_id']) && $params['address_id'] ? $params['address_id'] : 0;
- $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
- $ids = $goods ? array_column($goods, 'id') : [];
- // 参数验证
- if (empty($goods) || empty($ids)) {
- $this->error = '商品参数不为空';
- return false;
- }
- if ($addressId <= 0) {
- $this->error = '请选择收货地址';
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:submit_lock:{$userId}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = '订单处理中~';
- return false;
- }
- // 商品数据
- $orderNo = get_order_num('GX');
- RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
- $result = GoodsService::make()->getOrderGoods($ids, $goods, $orderNo, $userId);
- if (empty($result)) {
- RedisService::clear($cacheLockKey);
- $this->error = GoodsService::make()->getError();
- return false;
- }
- $orderGoods = isset($result['goods']) ? $result['goods'] : [];
- $orderTotal = isset($result['total']) ? $result['total'] : 0;
- $orderCount = isset($result['count']) ? $result['count'] : 0;
- $storeId = isset($result['store_id']) ? $result['store_id'] : 0;
- if (empty($orderGoods)) {
- RedisService::clear($cacheLockKey);
- $this->error = '获取订单商品错误~';
- return false;
- }
- if ($orderTotal <= 0) {
- RedisService::clear($cacheLockKey);
- $this->error = '订单金额错误~';
- return false;
- }
- if ($orderCount <= 0) {
- RedisService::clear($cacheLockKey);
- $this->error = '订单商品数量错误~';
- return false;
- }
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
- if (empty($userInfo) || $status != 1) {
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if (empty($openid)) {
- $this->error = '用户微信未授权,请重新授权登录';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 收货地址信息
- $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
- $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
- $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
- $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
- $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
- if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
- RedisService::clear($cacheLockKey);
- $this->error = '收货地址信息错误,请核对后重试~';
- return false;
- }
- // 商家佣金
- $storeInfo = StoreModel::where(['id' => $storeId])->first();
- $bonusRate = isset($storeInfo['bonus_rate']) ? floatval($storeInfo['bonus_rate']) : 0;
- $storeBonusRate = ConfigService::make()->getConfigByCode('store_bonus_rate', 0);
- $storeBonusRate = $storeBonusRate > 0 && $storeBonusRate <= 100 ? $storeBonusRate : 0;
- $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
- $bonus = moneyFormat($orderTotal * $bonusRate / 100, 2);
- $total = $orderTotal;
- if (env('PAY_DEBUG')) {
- $orderTotal = 0.1;
- }
- // 订单数据
- $order = [
- 'order_no' => $orderNo,
- 'user_id' => $userId,
- 'store_id' => $storeId,
- 'total' => $total,
- 'num' => $orderCount,
- 'pay_total' => $orderTotal,
- 'receiver_name' => $realname,
- 'receiver_mobile' => $mobile,
- 'receiver_area' => $area,
- 'receiver_address' => $address,
- 'bonus' => $bonus,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- // 订单处理
- DB::beginTransaction();
- if (!$orderId = $this->model->insertGetId($order)) {
- DB::rollBack();
- $this->error = '创建订单失败';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单商品
- if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
- DB::rollBack();
- $this->error = '处理订单商品错误';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 获取支付参数
- /* TODO 支付处理 */
- $payOrder = [
- 'type' => 1,
- 'order_no' => $orderNo,
- 'pay_money' => $orderTotal,
- 'body' => '购物消费',
- 'openid' => $openid
- ];
- // 调起支付
- $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
- if (empty($payment)) {
- DB::rollBack();
- RedisService::clear($cacheLockKey);
- $this->error = PaymentService::make()->getError();
- return false;
- }
- // 用户操作记录
- DB::commit();
- $this->error = '订单创建成功,请前往支付~';
- RedisService::clear($cacheLockKey);
- return [
- 'order_id' => $orderId,
- 'payment' => $payment,
- 'total' => $payOrder['pay_money'],
- 'pay_type' => 10,
- ];
- }
- /**
- * 订单支付
- * @param $userId
- * @param $id
- * @return array|false
- */
- public function pay($userId, $id)
- {
- if ($id <= 0) {
- $this->error = '请选择支付订单';
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:pay_lock:{$userId}_{$id}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = '订单处理中~';
- return false;
- }
- // 商品数据
- RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
- if (empty($userInfo) || $status != 1) {
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if (empty($openid)) {
- $this->error = '用户微信未授权,请重新授权登录';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单信息
- $info = $this->model->where(['id' => $id, 'mark' => 1])
- ->select(['id', 'order_no', 'pay_total', 'status'])
- ->first();
- $orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $status = isset($info['status']) ? $info['status'] : 0;
- if (empty($info) || empty($orderNo)) {
- $this->error = '订单信息不存在';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($status != 1) {
- $this->error = '订单已支付';
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 获取支付参数
- /* TODO 支付处理 */
- $payOrder = [
- 'type' => 1,
- 'order_no' => $orderNo,
- 'pay_money' => $orderTotal,
- 'body' => '购物消费',
- 'openid' => $openid
- ];
- // 调起支付
- $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
- if (empty($payment)) {
- DB::rollBack();
- RedisService::clear($cacheLockKey);
- $this->error = PaymentService::make()->getError();
- return false;
- }
- // 用户操作记录
- DB::commit();
- $this->error = '支付请求成功,请前往支付~';
- RedisService::clear($cacheLockKey);
- return [
- 'order_id' => $id,
- 'payment' => $payment,
- 'total' => $payOrder['pay_money'],
- 'pay_type' => 10,
- ];
- }
- /**
- * 订单取消
- * @param $userId
- * @param $id
- * @return array|false
- */
- public function cancel($userId, $id)
- {
- if ($id <= 0) {
- $this->error = '请选择订单';
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:cancel_lock:{$userId}_{$id}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = '订单处理中~';
- return false;
- }
- // 商品数据
- RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- if (empty($userInfo) || $status != 1) {
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单信息
- $info = $this->model->where(['id' => $id, 'mark' => 1])
- ->select(['id', 'order_no', 'pay_total', 'status'])
- ->first();
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $status = isset($info['status']) ? $info['status'] : 0;
- if (empty($info) || empty($orderNo)) {
- $this->error = '订单信息不存在';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($status != 1) {
- $this->error = '订单已支付';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $this->error = '取消订单成功';
- $this->model->where(['user_id' => $userId, 'mark' => 0])->where('update_time', '<=', time() - 300)->delete();
- $this->model->where(['id' => $id])->update(['mark' => 0, 'update_time' => time()]);
- return ['id' => $id];
- }
- /**
- * 订单完成
- * @param $userId 订单用户ID
- * @param $id 订单ID
- * @return array|false
- */
- public function complete($userId, $id)
- {
- if ($id <= 0) {
- $this->error = '请选择订单';
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:complete_lock:{$userId}_{$id}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = '订单处理中~';
- return false;
- }
- // 商品数据
- RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'openid', 'mobile', 'parent_id', 'nickname', 'realname', 'balance', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
- if (empty($userInfo) || $status != 1) {
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单信息
- $info = $this->model->with(['orderGoods'])->where(['id' => $id, 'mark' => 1])
- ->select(['id', 'order_no', 'store_id', 'pay_total', 'bonus', 'delivery_no', 'delivery_company', 'delivery_code', 'status'])
- ->first();
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
- $deliverCompany = isset($info['delivery_company']) ? $info['delivery_company'] : '';
- $deliverCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
- $storeId = isset($info['store_id']) ? $info['store_id'] : 0;
- $orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
- $bonus = isset($info['bonus']) ? $info['bonus'] : 0;
- $status = isset($info['status']) ? $info['status'] : 0;
- $orderGoods = isset($info['order_goods']) ? $info['order_goods'] : [];
- if (empty($info) || empty($orderNo)) {
- $this->error = '订单信息不存在';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($status != 3) {
- $this->error = '订单未发货';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ((empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode))) {
- $this->error = '订单发货信息错误';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if (empty($deliveryNo) || empty($deliverCompany) || empty($deliverCode)) {
- $this->error = '订单发货信息错误,请联系客服';
- RedisService::clear($cacheLockKey);
- return false;
- }
- DB::beginTransaction();
- $this->model->where(['id' => $id])->update(['status' => '4', 'update_time' => time()]);
- // 商家订单数据统计
- $updateData = ['order_count' => DB::raw('order_count+1'), 'order_total' => DB::raw("order_total + {$orderTotal}")];
- StoreModel::where(['id' => $storeId])->update($updateData);
- // 商品销量数据
- if ($orderGoods) {
- $counts = [];
- foreach ($orderGoods as $item) {
- $counts[$item['goods_id']] = isset($counts[$item['goods_id']]) ? $counts[$item['goods_id']] : 0;
- $counts[$item['goods_id']] += $item['num'];
- }
- if ($counts) {
- foreach ($counts as $id => $v) {
- GoodsModel::where(['id' => $id])->update(['sales' => DB::raw("sales + {$v}"), 'update_time' => time()]);
- }
- }
- }
- // 结算商家收益
- if (SettleService::make()->storeBonus($storeId, $bonus, $info) < 0) {
- DB::rollBack();
- $this->error = SettleService::make()->getError();
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 代理佣金结算
- if (SettleService::make()->agentBonus($userId, $orderTotal, $info, $parentId) < 0) {
- DB::rollBack();
- $this->error = SettleService::make()->getError();
- RedisService::clear($cacheLockKey);
- return false;
- }
- DB::commit();
- $this->error = '确认收货成功';
- return ['id' => $id];
- }
- /**
- * 售后或退款
- * @param $userId
- * @param $params
- * @return array|false
- */
- public function after($userId, $params)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $afterType = isset($params['after_type']) ? $params['after_type'] : 1;
- if ($id <= 0) {
- $this->error = '请选择订单';
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:after_lock:{$userId}_{$id}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = '订单处理中~';
- return false;
- }
- // 商品数据
- RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- if (empty($userInfo) || $status != 1) {
- $this->error = 1045;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单信息
- $info = $this->model->where(['id' => $id, 'mark' => 1])
- ->select(['id', 'order_no', 'after_type', 'refund_status', 'pay_total', 'status'])
- ->first();
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $status = isset($info['status']) ? $info['status'] : 0;
- $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
- if (empty($info) || empty($orderNo)) {
- $this->error = '订单信息不存在';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($status == 1) {
- $this->error = '订单未支付';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($status == 4 && $afterType==2) {
- $this->error = '订单已完成';
- RedisService::clear($cacheLockKey);
- return false;
- }
- if ($refundStatus > 0 && $refundStatus != 4) {
- $this->error = '订单售后处理中';
- RedisService::clear($cacheLockKey);
- return false;
- }
- $afterRealname = isset($params['after_realname']) ? $params['after_realname'] : '';
- $afterPhone = isset($params['after_phone']) ? $params['after_phone'] : '';
- $afterRemark = isset($params['after_remark']) ? $params['after_remark'] : '';
- if ($afterType == 1) {
- if (empty($afterRealname) || empty($afterPhone) || empty($afterRemark)) {
- $this->error = '请填写售后信息';
- RedisService::clear($cacheLockKey);
- return false;
- }
- }
- $data = [
- 'after_type' => $afterType,
- 'after_realname' => $afterRealname,
- 'after_phone' => $afterPhone,
- 'after_remark' => $afterRemark,
- 'refund_remark' => isset($params['refund_remark']) ? $params['refund_remark'] : '',
- 'refund_status' => 3,
- 'update_time' => time()
- ];
- $this->model->where(['id' => $id])->update($data);
- $this->error = '订单申请售后成功';
- return ['id' => $id];
- }
- /**
- * 今日数量统计数据
- * @param $userId
- * @param int $type
- * @return array|mixed
- */
- public function getCountByDay($userId, $status = 3)
- {
- $cacheKey = "caches:orders:count_day_{$userId}_{$status}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
- ->where('create_time', '>=', strtotime(date('Y-m-d')))
- ->count('id');
- if ($data) {
- RedisService::set($cacheKey, $data, rand(5, 10));
- }
- return $data;
- }
- /**
- * 订单数
- * @param $userId
- * @param int $status
- * @return array|mixed
- */
- public function getCountByStatus($userId, $status = 3)
- {
- $cacheKey = "caches:orders:count_status_{$userId}_{$status}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $data = $this->model->where(['user_id' => $userId, 'status' => $status, 'mark' => 1])
- ->count('id');
- if ($data) {
- RedisService::set($cacheKey, $data, rand(5, 10));
- }
- return $data;
- }
- /**
- * 获取订单审核状态
- * @param $userId
- * @return array|mixed
- */
- public function checkOrderStatus($userId)
- {
- $cacheKey = "caches:orders:checkOrder:{$userId}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $data = $this->model->with(['confirm'])->where(function ($query) {
- $query->where(function ($query) {
- $query->where('status', 2)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 3600));
- })->orWhere(function ($query) {
- $query->where('status', 9)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
- })->orWhere(function ($query) {
- $query->where('status', 3)->where('confirm_at', '>=', date('Y-m-d H:i:s', time() - 600));
- })->orWhere('status', 1);
- })
- ->where(['user_id' => $userId, 'mark' => 1])
- ->select(['id', 'order_no', 'user_id', 'goods_id', 'status'])
- ->orderBy('id', 'desc')
- ->first();
- $data = $data ? $data->toArray() : [];
- $result = [];
- if ($data) {
- $orderId = isset($data['id']) ? $data['id'] : 0;
- $status = isset($data['status']) ? $data['status'] : 0;
- $goodsId = isset($data['goods_id']) ? $data['goods_id'] : 0;
- $confirmOrder = isset($data['confirm']) ? $data['confirm'] : [];
- $confirmOrderId = isset($confirmOrder['id']) ? $confirmOrder['id'] : 0;
- $confirmOrderUser = isset($confirmOrder['user']) ? $confirmOrder['user'] : [];
- $realname = isset($confirmOrderUser['realname']) ? $confirmOrderUser['realname'] : '';
- if ($status == 9) {
- $message = "抱歉,订单已被其他师傅接走";
- } else if ($status == 2) {
- $message = "恭喜您,抢单成功,请前往完成订单!";
- } else if ($status == 3) {
- $message = "恭喜您,订单已经完成!";
- } else {
- $pickerCount = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->whereIn('status', [1, 2])->count('id');
- $pickerCount = $pickerCount <= 3 ? 3 : $pickerCount;
- $message = "<p style='padding: 5px 0;'>正在抢单</p><p>({$pickerCount}位师傅正在抢单中)</p>";
- }
- $result = ['order_id' => $orderId, 'goods_id' => $goodsId, 'confirm_order_id' => $confirmOrderId, 'status' => $status, 'message' => $message];
- RedisService::set($cacheKey, $result, rand(10, 20));
- }
- return $result;
- }
- /**
- * 物流查询
- * @param $id
- * @return array|false|mixed
- */
- public function getDelivery($id)
- {
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
- $deliveryCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
- $mobile = isset($info['receiver_mobile']) ? $info['receiver_mobile'] : '';
- if (empty($info)) {
- $this->error = '请选择订单';
- return false;
- }
- $cacheKey = "caches:kd100_{$id}";
- $data = RedisService::get($cacheKey);
- $data = [
- [
- "time" => "2025-12-24 11:29:23",
- "context" => "您的快件已投递,收件人在[沙岗镇绕城路天猫1号店妈妈驿站]取件(凭取件码签收),如有疑问请联系站点:13086796129,快递员电话:13086796129,投诉电话:15278929512。感谢使用圆通速递,期待再次为您服务!",
- "ftime" => "2025-12-24 11:29:23",
- "areaCode" => "CN450521000000",
- "areaName" => "广西,北海市,合浦县",
- "status" => "签收",
- ],
- [
- "time" => "2025-12-22 17:29:40",
- "context" => "您的快件已到达[妈妈驿站]沙岗镇绕城路1号天猫店,请您及时取件,如有取件码问题或找不到包裹等问题,请联系站点:13086796129,快递员电话:13086796129,投诉电话:15278929512。感谢使用圆通速递,期待再次为您服务!",
- "ftime" => "2025-12-22 17:29:40",
- "areaCode" => "CN450521000000",
- "areaName" => "广西,北海市,合浦县",
- "status" => "派件",
- ],[
- "time" => "2025-12-22 17:28:40",
- "context" => "【广西北海市合浦县沙岗镇】的莫业琨(13086796129)正在派件,(有事先呼我,勿找平台,少一次投诉,多一份感恩)!如有疑问请联系网点:15278929512,投诉电话:15278929512。[95161和18521号段的上海号码为圆通快递员专属号码,请放心接听]",
- "ftime" => "2025-12-22 17:28:40",
- "areaCode" => "CN450521100000",
- "areaName" => "广西,北海市,合浦县,廉州镇",
- "status" => "派件",
- ],[
- "time" => "2025-12-22 02:44:12",
- "context" => "您的快件离开【南宁转运中心】,已发往【广西北海市合浦】",
- "ftime" => "2025-12-22 02:44:12",
- "areaCode" => "CN450108000000",
- "areaName" => "广西,南宁市,良庆区",
- "status" => "在途",
- ],[
- "time" => "2025-12-22 02:21:21",
- "context" => "您的快件已经到达【南宁转运中心】【物流问题无需找商家或平台,请致电(专属热线:95554)更快解决】",
- "ftime" => "2025-12-22 02:21:21",
- "areaCode" => "CN450108000000",
- "areaName" => "广西,南宁市,良庆区",
- "status" => "在途",
- ],[
- "time" => "2025-12-20 23:47:58",
- "context" => "您的快件离开【临海转运中心】,已发往【南宁转运中心】。预计【12月22日】到达【南宁市】,因运输距离较远,预计将在【22日晚上】为您更新快件状态,请您放心!",
- "ftime" => "2025-12-20 23:47:58",
- "areaCode" => "CN331082000000",
- "areaName" => "浙江,台州市,临海市",
- "status" => "在途",
- ],[
- "time" => "2025-12-20 23:45:58",
- "context" => "您的快件已经到达【临海转运中心】【物流问题无需找商家或平台,请致电(专属热线:95554)更快解决】",
- "ftime" => "2025-12-20 23:45:58",
- "areaCode" => "CN331082000000",
- "areaName" => "浙江,台州市,临海市",
- "status" => "在途",
- ]
- ];
- if ($data) {
- return $data;
- }
- $result = Kd100Service::make()->query($deliveryNo, $mobile, $deliveryCode);
- $status = isset($result['status'])?$result['status']:0;
- $data = isset($result['data'])?$result['data']:[];
- if ($data && $status==200) {
- RedisService::set($cacheKey, $data, 300);
- }
- return $data;
- }
- }
|