|
|
<?php
namespace App\Service;
use App\Model\Order;use App\Model\OrderMain;use App\Model\Users;
class UserService implements UserServiceInterface{
/** * 是否平台新用户 * 在很多奖励的地方会需要用到这个查询 * 判定条件: * 没有在平台下过单(包括线上和线下) * @param $user_id * @param $order_main_id * @return mixed|void */ public function isPlatformNewUser($user_id, $order_main_id): bool { $exist = OrderMain::query() ->where(['user_id' => $user_id]) ->where('id', '!=', $order_main_id) ->where(function ($query){ $query->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND]) ->orWhereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY]); }) ->exists();
return !$exist; } /** * 根据用户的openid更新unionid信息 * 如果没有找到用户,则不做任何处理 * @param $openid * @param $unionid * @return array */ public function saveUserUnionid($openid,$unionid) { $result = [ 'status' => false, 'msg' => '用户不存在或者已存在相同unionid' ];
// 查询用户是否存在
$userinfo = Users::select('id','unionid')->where('openid',$openid)->first(); if($userinfo && $userinfo->unionid != $unionid){ $userinfo->unionid = $unionid; if($res = $userinfo->save()){ $result['status'] = true; $result['msg'] = '更改用户unionid信息成功'; $result['res'] = $res; }else{ $result['msg'] = '更改用户unionid信息失败'; } }
return $result; }
/** * @inheritDoc */ public function isStoreFirstOrderToday($user_id, $store_id, $current_order_id, $limit_amount = 3) { return !Order::query() ->where(['user_id' => $user_id, 'store_id' => $store_id]) ->whereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY]) ->where('time_add', '>=', date('Y-m-d 00:00:00')) ->where('time_add', '<=', date('Y-m-d 23:59:59')) ->where('money', '>=', $limit_amount) ->where('id', '!=', $current_order_id) ->exists(); }
/** * 用户账户(钱包) * @inheritDoc * $type 1增 2减 */ public function userWallet($userId,$money,$type) { $user = Users::select('id','wallet')->where('id',$userId)->lockForUpdate()->first(); $res = false; if(!empty($user) && is_numeric($money) && $money > 0){ if($type == Users::WALLET_TYPE_INC){ $res = $user->increment('wallet',$money); }else if($type == Users::WALLET_TYPE_DEC){ $res = $user->decrement('wallet',$money); } } return $res; }}
|