123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Repositories;
- use App\Models\CardRidingOrder;
- use App\Models\CardRidingUserBags;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- class CardRidingUserBagsRepository extends BaseRepository
- {
- public $userRepository;
- public $cardRidingRepository;
- public $cardRidingOrderRepository;
- public function __construct(CardRidingUserBags $cardRidingUserBags,UserRepository $userRepository,CardRidingRepository $cardRidingRepository,CardRidingOrderRepository $cardRidingOrderRepository)
- {
- $this->model = $cardRidingUserBags;
- $this->userRepository = $userRepository;
- $this->cardRidingRepository = $cardRidingRepository;
- $this->cardRidingOrderRepository = $cardRidingOrderRepository;
- }
- /**
- * 判断用户是否存在有效骑行卡 有的话返回该卡 没有的话 返回[]
- *
- * @param $user_id integer
- *
- * @return mixed
- *
- * User:Fx
- * */
- public function isExist($user_id,$order_start_time=''){
- $cardRidingUser = $this->model->query()
- ->where('user_id',$user_id)
- ->where('status',CardRidingUserBags::STATUS_OK)
- ->first();
- if(empty($cardRidingUser)){
- // 不存在
- return [];
- }
- $t1 = Carbon::make($cardRidingUser->expiration_time); // 骑行卡过期时间
- $t2 = Carbon::now(); // 对比过期时间
- if(!empty($order_start_time)){
- $t2 = Carbon::make($order_start_time);
- }
- if($t1->lt($t2)){
- // 已过期
- $cardRidingUser->status = CardRidingUserBags::STATUS_NO;
- $cardRidingUser->save();
- return [];
- }
- if($cardRidingUser->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES){
- // 限次卡 判断是否次数归0
- if($cardRidingUser->can_ridding_times <= 0){
- // 次数已经归0 更新卡失效
- $cardRidingUser->status = CardRidingUserBags::STATUS_NO;
- $cardRidingUser->save();
- return [];
- }
- }
- // 存在
- return $cardRidingUser;
- }
- /**
- * 更新用户骑行卡失效
- *
- * @param $id integer
- * @param $user_id integer
- *
- * @return bool
- * */
- public function updateStatusNo($id){
- try{
- $this->model->query()->where('id',$id)->update(['status'=>CardRidingUserBags::STATUS_NO]);
- return true;
- }catch (\Exception $exception){
- Log::error($exception->getMessage());
- return false;
- }
- }
- /**
- * 邀请新用户送卡 inviteNewRewardCard
- *
- * @param $card_id
- * @param $user_id
- * @return void
- * @author Fx
- *
- */
- public function inviteNewRewardCard($card_id,$user_id){
- $users = $this->userRepository->byIdGetModel($user_id);
- $cardRiding = $this->cardRidingRepository->byIdGetModel($card_id);
- $cardRidingOrdersData = [
- 'no' => CardRidingOrder::makeNo(),
- 'area_id' => $users->register_area_id,
- 'user_id' => $users->id,
- 'riding_card_id' => $card_id,
- 'money' => 0,
- 'discount' => 1,
- 'pay_money' => 0,
- 'pay_type' => CardRidingOrder::PAY_TYPE_INVITE_NEW_USER_GIVE,
- 'pay_status' => CardRidingOrder::PAY_STATUS_OK,
- 'pay_time' => Carbon::now(),
- ];
- $cardRidingOrder = $this->cardRidingOrderRepository->model::firstOrCreate($cardRidingOrdersData);
- $expiration_time = Carbon::now()->addDays($cardRiding->effective_days)->format('Y-m-d H:i:s');
- $data = [
- 'user_id' => $users->id,
- 'riding_card_id' => $card_id,
- 'card_riding_order_id' => $cardRidingOrder->id,
- 'take_effect_time' => Carbon::now(),
- 'deduction_money' => $cardRiding->deduction_money,
- 'expiration_time' => $expiration_time,
- 'is_limit_times' => $cardRiding->is_limit_times,
- 'can_ridding_times' => $cardRiding->times,
- 'day_can_ridding_times' => $cardRiding->day_can_ridding_times,
- 'status' => CardRidingUserBags::STATUS_OK
- ];
- // Log::info($data);
- return $this->model::firstOrCreate(['card_riding_order_id' => $cardRidingOrder->id], $data);
- }
- }
|