海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Common\StatementType;
  4. use App\Models\Agent;
  5. use App\Models\Guide;
  6. use App\Models\Order;
  7. use App\Models\OrderProductItem;
  8. use App\Models\Supplier;
  9. use App\Service\WithdrawalService;
  10. use App\Traits\DemandTraits;
  11. use App\Traits\StatementTraits;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 仅用于测试
  16. * Class TController
  17. * @package App\Http\Controllers\Api
  18. */
  19. class TestController
  20. {
  21. public function index()
  22. {
  23. dd($this->fund(Order::find(221)));
  24. }
  25. public function fund($order)
  26. {
  27. $service = new WithdrawalService();
  28. DB::beginTransaction();
  29. try {
  30. //最后批量插入
  31. $statementCreate = [];
  32. $cost = 0;
  33. //如果有地接价格 分帐给地接
  34. if ($order->guide_price > 0) {
  35. $guidePrice = $order->guide_price;
  36. $cost = bcadd($cost, $order->guide_price, 6);
  37. //成本价 加上地接价格
  38. $statementCreate[] = $service->createByOrder(
  39. $order->guide_price,
  40. StatementType::ORDER,
  41. $order->guide->id,
  42. DemandTraits::$col[2],
  43. $order->id,
  44. StatementTraits::$type[0]
  45. );
  46. //抽成
  47. $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
  48. $guide->balance = bcadd($guide->balance, $guidePrice, 6);
  49. $guide->save();
  50. }
  51. //分账给供应商
  52. $orderItem = OrderProductItem::query()
  53. ->where('order_id', $order->id)
  54. ->with('supplier')
  55. ->select('*')
  56. ->selectRaw('sum(price) as sum_price,sum(single_deposit * num) as sum_persons')
  57. ->groupBy('supplier_id')
  58. ->get();
  59. foreach ($orderItem as $v) {
  60. $cost = bcadd($cost, $v->sum_price, 6);
  61. $supplierPrice = $v->sum_price;
  62. $statementCreate[] = $service->createByOrder(
  63. $v->sum_price,
  64. StatementType::ORDER,
  65. $v->supplier_id,
  66. DemandTraits::$col[1],
  67. $order->id,
  68. StatementTraits::$type[0]
  69. );
  70. $supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
  71. //处理交易金
  72. if ($v->sum_persons > 0) {
  73. //计算交易金
  74. $deposit = $v->sum_persons;
  75. //流水
  76. //扣
  77. $supplier->trade_balance = bcsub($supplier->trade_balance,$deposit,6);
  78. }
  79. $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
  80. $supplier->save();
  81. }
  82. //分账给代理商
  83. //成本价 加上地接价格
  84. $agentPrice = bcsub($order->price, $cost, 2);
  85. $statementCreate[] = $service->createByOrder(
  86. $agentPrice,
  87. StatementType::ORDER,
  88. $order->agent_id,
  89. DemandTraits::$col[0],
  90. $order->id,
  91. StatementTraits::$type[0]
  92. );
  93. $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
  94. $agent->balance = bcadd($agent->balance, $agentPrice, 6);
  95. $agent->save();
  96. if (!empty($statementCreate)) {
  97. $order->statement()->createMany($statementCreate);
  98. }
  99. DB::commit();
  100. } catch (\Exception $e) {
  101. DB::rollBack();
  102. return $this->error($e->getMessage());
  103. }
  104. }
  105. /**
  106. * 模拟登录
  107. * @param $user_id
  108. * @return string
  109. */
  110. private function login($user_id)
  111. {
  112. $token_key = md5($user_id . env('APP_KEY'));
  113. Cache::put($token_key, $user_id);
  114. return $token_key;
  115. }
  116. public function index2()
  117. {
  118. $handle = fopen(base_path('area.txt'), 'r');
  119. if ($handle) {
  120. DB::statement('TRUNCATE `areas`;');
  121. while (($line = fgets($handle, 4096)) !== false) {
  122. $line = trim($line);
  123. $last2 = substr($line, 4, 2); //区划码后2位
  124. $front2 = substr($line, 0, 2); //区划码前2位
  125. $front4 = substr($line, 0, 4); //区划码前4位
  126. //判断是否是直辖市,是直辖市则再插入一次上条记录作为二级联动
  127. if (isset($last4, $arr, $parent_arr[$front2]) && $last4 == '0000' && $last2 != '00') {
  128. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
  129. $parent_arr[$front4] = $insert_id;
  130. //对直辖市不做省直辖县处理,如:重庆市
  131. $parent_arr[$front2] = $insert_id;
  132. }
  133. $last4 = substr($line, 2, 4); //区划码后4位
  134. $arr = preg_split('/\s+/', $line);
  135. if (count($arr) !== 2) continue;
  136. if ($last4 == '0000') {
  137. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => 0, 'name' => $arr[1]]);
  138. $parent_arr[$front2] = $insert_id;
  139. } else if ($last2 == '00') {
  140. $insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
  141. $parent_arr[$front4] = $insert_id;
  142. } else {
  143. //考虑到省直辖县级市情况,如:海南琼海市、湖北仙桃市等,但重庆市的省辖县除外(已在上面判断直辖市逻辑中做处理)
  144. $parent_id = $parent_arr[$front4] ?? $parent_arr[$front2];
  145. DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_id, 'name' => $arr[1]]);
  146. }
  147. }
  148. } else {
  149. return 'open file fail!';
  150. }
  151. return 'success';
  152. }}