海南旅游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.

172 lines
5.5 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Renderable\SelectProduct;
  4. use App\AdminSupplier\Repositories\DemandBidding;
  5. use App\Models\AgentProduct;
  6. use App\Models\AgentProductItem;
  7. use App\Models\Demand;
  8. use App\Models\DemandProduct;
  9. use App\Models\Product;
  10. use App\Traits\DemandTraits;
  11. use Dcat\Admin\Admin;
  12. use Dcat\Admin\Form;
  13. use Dcat\Admin\Grid;
  14. use Dcat\Admin\Show;
  15. use Dcat\Admin\Http\Controllers\AdminController;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Support\Arr;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Log;
  20. class DemandBiddingController extends AdminController
  21. {
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new DemandBidding(['demand']), function (Grid $grid) {
  30. $grid->model()->where(['bidding_user_id' => Admin::user()->id,'bidding_user_type' => DemandTraits::$col[1]]);
  31. $grid->column('id')->sortable();
  32. $grid->column('price');
  33. $grid->column('comment');
  34. $grid->column('demand.title','竞拍标题');
  35. $grid->column('demand.comment','竞拍内容');
  36. $grid->column('state','状态')->using(DemandTraits::$biddingState)->dot(
  37. [
  38. 'yellow',
  39. 'success',
  40. 'danger',
  41. ]
  42. );
  43. $grid->column('bidding','操作')
  44. ->if(function (){
  45. return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && empty($this->demand->demand_product_id);
  46. })
  47. ->then(function (Grid\Column $column) {
  48. $column->append('<a class="btn btn-sm btn-success" href="'.admin_url('/demand_bidding/'.$this->id.'/edit?is_bidding=true').'">绑定产品</a>');
  49. })
  50. ->if(function (){
  51. return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && !empty($this->demand->demand_product_id);
  52. })
  53. ->then(function (Grid\Column $column) {
  54. $column->append('<a class="btn btn-sm btn-primary" href="'.admin_url('/demand_product/'.$this->demand->demand_product_id).'">查看产品</a>');
  55. });
  56. $grid->column('created_at');
  57. $grid->column('updated_at')->sortable();
  58. $grid->disableDeleteButton();
  59. $grid->disableEditButton();
  60. $grid->disableQuickEditButton();
  61. $grid->disableViewButton();
  62. $grid->disableCreateButton();
  63. $grid->disableActions();
  64. $grid->filter(function (Grid\Filter $filter) {
  65. $filter->equal('id');
  66. });
  67. });
  68. }
  69. /**
  70. * Make a show builder.
  71. *
  72. * @param mixed $id
  73. *
  74. * @return Show
  75. */
  76. protected function detail($id)
  77. {
  78. return Show::make($id, new DemandBidding(), function (Show $show) {
  79. $show->field('id');
  80. $show->field('price');
  81. $show->field('comment');
  82. $show->field('created_at');
  83. $show->field('updated_at');
  84. });
  85. }
  86. /**
  87. * Make a form builder.
  88. *
  89. * @return Form
  90. */
  91. protected function form()
  92. {
  93. return Form::make(new DemandBidding(), function (Form $form) {
  94. $form->disableEditingCheck();
  95. $form->disableCreatingCheck();
  96. $form->disableDeleteButton();
  97. $form->disableViewButton();
  98. $form->disableViewCheck();
  99. $form->display('id')->disable();
  100. if(request('is_bidding',0)) {
  101. $form->textarea('comment')->disable();
  102. $form->selectTable('demand_product_id', '产品')
  103. ->title('选择产品')
  104. ->dialogWidth('50%;min-width:600px;') //不起作用
  105. ->from(SelectProduct::make())
  106. ->model(DemandProduct::class, 'id', 'title');
  107. }else{
  108. $form->text('price');
  109. $form->textarea('comment');
  110. }
  111. $form->hidden('demand_id')->value(request('demand_id',0));
  112. $form->hidden('bidding_user_type')->disable();
  113. $form->hidden('bidding_user_id')->disable();
  114. $form->saving(function (Form $form) {
  115. $form->deleteInput('demand_product_id');
  116. //发布人身份
  117. $form->bidding_user_type = DemandTraits::$col[1];
  118. $form->bidding_user_id = Admin::user()->id;
  119. });
  120. $form->saved(function (Form $form) {
  121. if($form->isEditing()) {
  122. $productId = request('demand_product_id', 0);
  123. if (!empty($productId)) {
  124. DB::beginTransaction();
  125. try {
  126. //处理订单
  127. //将产品绑给代理商
  128. $demand = Demand::find($form->model()->demand_id);
  129. $demand->demand_product_id = $productId;
  130. $demandProduct = DemandProduct::find($productId);
  131. $product = new Product();
  132. $product->supplier_id = $demandProduct->supplier_id;
  133. $product->category_id = $demandProduct->category_id;
  134. $product->title = $demandProduct->title;
  135. $product->price = $form->model()->price;
  136. $product->original_price = $form->model()->price;
  137. $product->pictures = $demandProduct->pictures;
  138. $product->stock = $demand->stock;
  139. $product->know = $demandProduct->know;
  140. $product->content = $demandProduct->content;
  141. $product->agent_id = $form->model()->bidding_user_id;
  142. $product->save();
  143. //处理需求
  144. $demand = Demand::find($form->model()->demand_id);
  145. $demand->demand_product_id = $productId;
  146. $demand->save();
  147. $demand->product_id = $product->id;
  148. $demand->save();
  149. DB::commit();
  150. } catch (\Exception $e) {
  151. Log::error('分配订单失败::' . $e->getTraceAsString());
  152. DB::rollBack();
  153. return $form->response()->error('分配订单失败,稍后重试或联系管理员!' . $e->getMessage());
  154. }
  155. }
  156. }
  157. $form->response()->success('操作成功')->redirect('demand_bidding');
  158. });
  159. });
  160. }
  161. }