Exemple #1
0
 /**
  * 水地图亲水包获取
  * @param $params
  * @param $user_id
  * @return Array
  */
 public function mapBag($params, $user_id)
 {
     $user_financial = new UserFinancial();
     $result = $user_financial->where('user_id', $params->get('storeId'))->first();
     if (empty($result)) {
         return ['status' => false, 'message' => '店铺ID不存在!', 'info' => []];
     }
     if ($result->giving == 0 || $result->water_count < $result->giving) {
         return ['status' => false, 'message' => '已经领取完了!', 'info' => []];
     }
     $start_now_time = Carbon::now()->format('Y-m-d') . ' 00:00:01';
     $end_now_time = Carbon::now()->format('Y-m-d') . ' 23:23:59';
     $get_show_water_log = new GetShopWaterLog();
     $is_giving = $get_show_water_log->where('user_id', $params->get('storeId'))->where('giving_user_id', $user_id)->whereBetween('created_at', [$start_now_time, $end_now_time])->first();
     if (!empty($is_giving)) {
         return ['status' => false, 'message' => '今天您已经领取过,请勿重复领取!', 'info' => []];
     }
     $bool = $user_financial->where('user_id', $params->get('storeId'))->update(['water_count' => $result->water_count - $result->giving, 'send_water' => $result->send_water + $result->giving]);
     if ($bool) {
         $my_result = $user_financial->where('user_id', $user_id)->first();
         if (!empty($my_result)) {
             $user_financial->where('user_id', $user_id)->update(['public_count' => $my_result->public_count + $result->giving]);
         } else {
             $user_financial->user_id = $user_id;
             $user_financial->public_count = $result->giving;
             $user_financial->save();
         }
         //给与推荐给我相关的人分享一半的值
         $user_relationship = new UserRelationship();
         $user_relationship_rt = $user_relationship->where('guest_id', $user_id)->first();
         $has_guest = false;
         if (!empty($user_relationship_rt) && $result->water_count - $result->giving - $result->giving * UserRelationship::getGuestRate() >= 0) {
             $has_guest = true;
             $be_guest_result = $user_financial->where('user_id', $params->get('storeId'))->first();
             $user_financial->where('user_id', $params->get('storeId'))->update(['water_count' => $result->water_count - $result->giving - $result->giving * UserRelationship::getGuestRate(), 'send_water' => $result->send_water + $result->giving + $result->giving * UserRelationship::getGuestRate()]);
             if (!empty($be_guest_result)) {
                 $be_guest_user = $user_financial->where('user_id', $user_relationship_rt->user_id)->first();
                 if (!empty($be_guest_user)) {
                     $user_financial->where('user_id', $user_relationship_rt->user_id)->update(['public_count' => $be_guest_user->public_count + $result->giving * UserRelationship::getGuestRate()]);
                 } else {
                     $user_financial->user_id = $user_relationship_rt->user_id;
                     $user_financial->public_count = $result->giving * UserRelationship::getGuestRate();
                     $user_financial->save();
                 }
                 $get_show_water_log = new GetShopWaterLog();
                 $get_show_water_log->user_id = $params->get('storeId');
                 $get_show_water_log->water_count = $result->giving * UserRelationship::getGuestRate();
                 $get_show_water_log->giving_user_id = $user_relationship_rt->user_id;
                 $get_show_water_log->type = GetShopWaterLog::GUEST_GET_TYPE;
                 $get_show_water_log->save();
                 unset($get_show_water_log);
             }
         }
         $guest_val = 0;
         if ($has_guest == true) {
             $guest_val = UserRelationship::getGuestRate();
         }
         //再扣除公益值
         if ($result->water_count - $result->giving - $result->giving * $guest_val - $result->giving * SystemHasWater::getRate() >= 0) {
             $user_financial->where('user_id', $params->get('storeId'))->update(['water_count' => $result->water_count - $result->giving - $result->giving * $guest_val - $result->giving * SystemHasWater::getRate(), 'send_water' => $result->send_water + $result->giving + $result->giving * $guest_val + $result->giving * SystemHasWater::getRate()]);
             $system_has_water = new SystemHasWater();
             $system_has_water->business_id = $params->get('storeId');
             $system_has_water->sys_water_count = $result->giving * SystemHasWater::getRate();
             $system_has_water->rate = SystemHasWater::getRate();
             $system_has_water->save();
             unset($system_has_water);
             $get_show_water_log = new GetShopWaterLog();
             $get_show_water_log->user_id = $params->get('storeId');
             $get_show_water_log->water_count = $result->giving * SystemHasWater::getRate();
             $get_show_water_log->type = GetShopWaterLog::SYS_GET_TYPE;
             $get_show_water_log->save();
             unset($get_show_water_log);
         }
         $get_show_water_log = new GetShopWaterLog();
         $get_show_water_log->user_id = $params->get('storeId');
         $get_show_water_log->water_count = $result->giving;
         $get_show_water_log->giving_user_id = $user_id;
         $get_show_water_log->type = GetShopWaterLog::USER_GET_TYPE;
         $get_show_water_log->save();
         unset($get_show_water_log);
         return ['status' => true, 'message' => '领取成功!', 'info' => ['water_count' => $result->giving]];
     }
     return ['status' => false, 'message' => '领取失败!', 'info' => []];
 }