fund(Order::find(221))); } public function fund($order) { $service = new WithdrawalService(); DB::beginTransaction(); try { //最后批量插入 $statementCreate = []; $cost = 0; //如果有地接价格 分帐给地接 if ($order->guide_price > 0) { $guidePrice = $order->guide_price; $cost = bcadd($cost, $order->guide_price, 6); //成本价 加上地接价格 $statementCreate[] = $service->createByOrder( $order->guide_price, StatementType::ORDER, $order->guide->id, DemandTraits::$col[2], $order->id, StatementTraits::$type[0] ); //抽成 $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first(); $guide->balance = bcadd($guide->balance, $guidePrice, 6); $guide->save(); } //分账给供应商 $orderItem = OrderProductItem::query() ->where('order_id', $order->id) ->with('supplier') ->select('*') ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons') ->groupBy('supplier_id') ->get(); foreach ($orderItem as $v) { $cost = bcadd($cost, $v->sum_price, 6); $supplierPrice = $v->sum_price; $statementCreate[] = $service->createByOrder( $v->sum_price, StatementType::ORDER, $v->supplier_id, DemandTraits::$col[1], $order->id, StatementTraits::$type[0] ); $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first(); //处理交易金 if ($v->sum_persons > 0) { //计算交易金 $deposit = $v->sum_persons; //流水 //扣 $supplier->trade_balance = bcsub($supplier->trade_balance,$deposit,6); } $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6); $supplier->save(); } //分账给代理商 //成本价 加上地接价格 $agentPrice = bcsub($order->price, $cost, 2); $statementCreate[] = $service->createByOrder( $agentPrice, StatementType::ORDER, $order->agent_id, DemandTraits::$col[0], $order->id, StatementTraits::$type[0] ); $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first(); $agent->balance = bcadd($agent->balance, $agentPrice, 6); $agent->save(); if (!empty($statementCreate)) { $order->statement()->createMany($statementCreate); } DB::commit(); } catch (\Exception $e) { DB::rollBack(); return $this->error($e->getMessage()); } } /** * 模拟登录 * @param $user_id * @return string */ private function login($user_id) { $token_key = md5($user_id . env('APP_KEY')); Cache::put($token_key, $user_id); return $token_key; } public function index2() { $handle = fopen(base_path('area.txt'), 'r'); if ($handle) { DB::statement('TRUNCATE `areas`;'); while (($line = fgets($handle, 4096)) !== false) { $line = trim($line); $last2 = substr($line, 4, 2); //区划码后2位 $front2 = substr($line, 0, 2); //区划码前2位 $front4 = substr($line, 0, 4); //区划码前4位 //判断是否是直辖市,是直辖市则再插入一次上条记录作为二级联动 if (isset($last4, $arr, $parent_arr[$front2]) && $last4 == '0000' && $last2 != '00') { $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]); $parent_arr[$front4] = $insert_id; //对直辖市不做省直辖县处理,如:重庆市 $parent_arr[$front2] = $insert_id; } $last4 = substr($line, 2, 4); //区划码后4位 $arr = preg_split('/\s+/', $line); if (count($arr) !== 2) continue; if ($last4 == '0000') { $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => 0, 'name' => $arr[1]]); $parent_arr[$front2] = $insert_id; } else if ($last2 == '00') { $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]); $parent_arr[$front4] = $insert_id; } else { //考虑到省直辖县级市情况,如:海南琼海市、湖北仙桃市等,但重庆市的省辖县除外(已在上面判断直辖市逻辑中做处理) $parent_id = $parent_arr[$front4] ?? $parent_arr[$front2]; DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_id, 'name' => $arr[1]]); } } } else { return 'open file fail!'; } return 'success'; }}