| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\MemberModel;
- use App\Models\OrderModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- 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', 'desc')
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $statusArr = [1 => '待审核', 2 => '进行中', 3 => '已完成'];
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
- $status = isset($item['status']) ? $item['status'] : 0;
- $item['status_text'] = '待审核';
- if ($status) {
- $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
- }
- $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
- $item['goods']['shipper_phone_text'] = $item['goods']['shipper_phone']?format_mobile($item['goods']['shipper_phone']) : '';
- $item['goods']['receiver_phone_text'] = $item['goods']['receiver_phone']?format_mobile($item['goods']['receiver_phone']) : '';
- }
- 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(['goods'])
- ->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.nickname', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- }
- });
- }
- /**
- * 订单详情
- * @param $id
- */
- public function getOrderInfo($id)
- {
- $statusArr = [1 => '待审核', 2 => '进行中', 3 => '已完成'];
- $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] : '';
- }
- $info['goods'] = isset($info['goods']) && $info['goods'] ? $info['goods'] : [];
- $info['goods']['shipper_phone_text'] = $info['goods']['shipper_phone']?format_mobile($info['goods']['shipper_phone']) : '';
- $info['goods']['receiver_phone_text'] = $info['goods']['receiver_phone']?format_mobile($info['goods']['receiver_phone']) : '';
- }
- return $info;
- }
- /**
- * 创建订单
- * @param $userId 用户
- * @param $params 参数
- * @return array|false
- */
- public function createOrder($userId, $params)
- {
- $goodsId = isset($params['goods_id']) && $params['goods_id'] ? intval($params['goods_id']) : 0;
- // 参数验证
- if (empty($goodsId)) {
- $this->error = 2103;
- return false;
- }
- // 缓存锁
- $cacheLockKey = "caches:orders:submit_lock:{$userId}_{$goodsId}";
- if (RedisService::get($cacheLockKey)) {
- $this->error = 2107;
- return false;
- }
- // 货物信息
- RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(2, 3));
- $goodsInfo = GoodsModel::where(['id' => $goodsId,'status'=>1, 'mark' => 1])
- ->select(['id','num', 'price','picker_status', 'status'])
- ->first();
- $pickerStatus = isset($goodsInfo['picker_status']) ? $goodsInfo['picker_status'] : 0;
- $price = isset($goodsInfo['price']) ? floatval($goodsInfo['price']) : 0;
- $num = isset($goodsInfo['num']) ? intval($goodsInfo['num']) : 0;
- if(empty($goodsInfo)){
- $this->error = 2201;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if(in_array($pickerStatus,[2,3])){
- $this->error = "220{$pickerStatus}";
- RedisService::clear($cacheLockKey);
- return false;
- }
- if($price<=0){
- $this->error = 2204;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 用户信息
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
- ->select(['id', 'mobile', 'nickname','deposit','picker_status','confirm_status', 'status'])
- ->first();
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
- $confirmStatus = isset($userInfo['confirm_status']) ? $userInfo['confirm_status'] : 0;
- $userPickerStatus = isset($userInfo['picker_status']) ? $userInfo['picker_status'] : 0;
- $userDeposit = isset($userInfo['deposit']) ? floatval($userInfo['deposit']) : 0;
- if(empty($userInfo) || $status != 1){
- $this->error = 2016;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 账号审核情况
- if($confirmStatus != 1){
- $this->error = 2042;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if($userPickerStatus != 1){
- $this->error = 2205;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 缴纳保证金验证
- $depositMoney = ConfigService::make()->getConfigByCode('deposit_money',0);
- $depositTotal = DepositService::make()->getPayDeposit($userId);
- if($depositMoney>0 && ($userDeposit < $depositMoney || $depositTotal < $depositMoney)){
- $this->error = 2206;
- RedisService::clear($cacheLockKey);
- return false;
- }
- // 订单数据
- $pickerCount = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->whereIn('status',[1,2])->count('id');
- if($this->model->where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->whereIn('status',[1,2,3])->value('id')){
- $this->error = 2209;
- RedisService::clear($cacheLockKey);
- return false;
- }
- if($this->model->where(['user_id'=>$userId,'mark'=>1])->whereIn('status',[1,2])->value('id')){
- $this->error = 2209;
- RedisService::clear($cacheLockKey);
- return false;
- }
- $bonusRate = ConfigService::make()->getConfigByCode('picker_bonus_rate',100);
- $bonusRate = $bonusRate>0 && $bonusRate<=100? intval($bonusRate) : 100;
- $bonus = moneyFormat($price * $bonusRate/100,2);
- $orderNo = get_order_num('PD');
- $order = [
- 'order_no' => $orderNo,
- 'user_id' => $userId,
- 'goods_id' => $goodsId,
- 'total' => $price,
- 'bonus' => $bonus,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- // 订单处理
- DB::beginTransaction();
- if (!$orderId = $this->model->insertGetId($order)) {
- DB::rollBack();
- $this->error = 2207;
- RedisService::clear($cacheLockKey);
- return false;
- }
- $title = "用户[{$nickname}]有新的抢单";
- $message = [
- 'from_uid'=> $userId,
- 'to_uid'=> 1,
- 'op'=> 'notice',
- 'scene'=> 'picker',
- 'message'=> '接单消息',
- 'type'=> 5,
- 'order'=>[
- 'title'=> $title.'<span class="ele-text-primary">查看订单</span>',
- 'order_no'=> $orderNo,
- 'money'=> $bonus,
- 'date'=> date('Y-m-d H:i:s'),
- 'user_id'=> $userId,
- 'type'=> 'deposit',
- 'remark'=> '接单消息',
- ]
- ];
- DB::commit();
- $pickerCount = $pickerCount+1;
- $this->error = lang('2208',['num'=>$pickerCount]);
- RedisService::clear($cacheLockKey);
- RedisService::keyDel("caches:goods:picker*");
- return ['order_id' => $orderId,'message'=>$message, 'total' => $price, 'num' => $pickerCount, 'goods' => $goodsId];
- }
- /**
- * 今日数量统计数据
- * @param $userId
- * @param int $type
- * @return array|mixed
- */
- public function getCountByDay($userId, $status=3)
- {
- $cacheKey = "caches:accounts:count_{$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;
- }
- }
|