hasOne(StoreProduct::class, 'id', 'product_id')->bind(['image' => 'image', 'store_name' => 'store_name']); } public function giveMoney($product_id, $uid) { try { $product = new StoreProduct(); $ProductRelated = new ProductRelated(); $benefit_money = $product->where('id', $product_id)->value('benefit_money'); if (!empty($benefit_money)) { $result = $ProductRelated->where('product_id', $product_id)->where('is_delete', 0)->where("uid != " . $uid)->field('uid')->with(['getUserOneData'])->select()->toArray(); if ($result) { $time = $_SERVER['REQUEST_TIME']; $User = new User(); $UserMoney = new UserMoney(); $UserMoney_data = []; $User->startTrans(); $cx = 1; foreach ($result as $k => $v) { $UserMoney_data[$k]['uid'] = $v['uid']; $UserMoney_data[$k]['link_id'] = $product_id;//关联商品id $UserMoney_data[$k]['type'] = 'benefit_get'; $UserMoney_data[$k]['title'] = '分润商品获得'; $UserMoney_data[$k]['number'] = $benefit_money; $UserMoney_data[$k]['balance'] = $v['now_money'] + $benefit_money; $UserMoney_data[$k]['pm'] = 1; $UserMoney_data[$k]['mark'] = '分润商品获得' . $benefit_money . '余额'; $UserMoney_data[$k]['status'] = 1; $UserMoney_data[$k]['add_time'] = $time; $User->where('uid', $v['uid'])->update(['now_money' => $UserMoney_data[$k]['balance']]); } $UserMoney->insertAll($UserMoney_data); $User->commit(); } } return true; } catch (\Exception $e) { if (isset($cx)) { $User->rollback(); } //失败的话,记录下来 CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id); return false; } catch (\Throwable $e) { if (isset($cx)) { $User->rollback(); } //失败的话,记录下来 CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id); return false; } } public function getUserOneData() { return $this->hasOne(User::class, 'uid', 'uid')->bind(['now_money' => 'now_money']); } }