| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- <?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\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 > 0 && is_array($status)) {
- $query->whereIn('a.status', $status);
- } else if ($status > 0) {
- $query->where('a.status', $status);
- }
- })->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'] = isset($refundStatusArr[$item['refund_status']]) ? $refundStatusArr[$item['refund_status']] : $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(['goods'])
- ->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, 2);
- $orderTotal = 0.1;
- // 订单数据
- $order = [
- 'order_no' => $orderNo,
- 'user_id' => $userId,
- 'store_id' => $storeId,
- 'total' => $orderTotal,
- '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 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;
- }
- }
|