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

189 lines
6.1 KiB

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\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. ->display('查看')
  37. ->modal('详情',function ($modal) {
  38. $modal->xl();
  39. return $this->demand->comment;
  40. });
  41. $grid->column('state','状态')->using(DemandTraits::$biddingState)->dot(
  42. [
  43. 'yellow',
  44. 'success',
  45. 'danger',
  46. ]
  47. );
  48. $grid->column('bidding','操作')
  49. ->if(function (){
  50. return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && empty($this->demand->demand_product_id);
  51. })
  52. ->then(function (Grid\Column $column) {
  53. $column->append('<a class="btn btn-sm btn-success" href="'.admin_url('/demand_bidding/'.$this->id.'/edit?is_bidding=true').'">添加中标产品</a>');
  54. })
  55. ->if(function (){
  56. return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && !empty($this->demand->demand_product_id);
  57. })
  58. ->then(function (Grid\Column $column) {
  59. $column->append('<a class="btn btn-sm btn-primary" href="'.admin_url('/demand_product/'.$this->demand->demand_product_id).'">查看产品</a>');
  60. });
  61. $grid->column('created_at');
  62. $grid->column('updated_at')->sortable();
  63. $grid->disableDeleteButton();
  64. $grid->disableEditButton();
  65. $grid->disableQuickEditButton();
  66. $grid->disableViewButton();
  67. $grid->disableCreateButton();
  68. $grid->disableActions();
  69. $grid->filter(function (Grid\Filter $filter) {
  70. $filter->equal('id');
  71. });
  72. });
  73. }
  74. /**
  75. * Make a show builder.
  76. *
  77. * @param mixed $id
  78. *
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make($id, new DemandBidding(), function (Show $show) {
  84. $show->field('id');
  85. $show->field('price');
  86. $show->field('comment');
  87. $show->field('created_at');
  88. $show->field('updated_at');
  89. });
  90. }
  91. /**
  92. * Make a form builder.
  93. *
  94. * @return Form
  95. */
  96. protected function form()
  97. {
  98. return Form::make(new DemandBidding(), function (Form $form) {
  99. $form->disableEditingCheck();
  100. $form->disableCreatingCheck();
  101. $form->disableDeleteButton();
  102. $form->disableViewButton();
  103. $form->disableViewCheck();
  104. $demand_id = request('demand_id');
  105. if ($demand_id && \App\Models\DemandBidding::find($demand_id)) {
  106. Admin::exit('你已经竞标过了,无需重复参加');
  107. }
  108. $form->display('id')->disable();
  109. if(request('is_bidding',0)) {
  110. $form->textarea('comment')->disable()->required();
  111. $form->selectTable('demand_product_id', '产品')
  112. ->title('选择产品')
  113. ->dialogWidth('50%;min-width:600px;') //不起作用
  114. ->from(SelectProduct::make())
  115. ->model(DemandProduct::class)
  116. ->required();
  117. }else{
  118. $form->text('price')->required();
  119. $form->textarea('comment')->required();
  120. }
  121. $form->hidden('demand_id')->value(request('demand_id',0));
  122. $form->hidden('bidding_user_type')->disable();
  123. $form->hidden('bidding_user_id')->disable();
  124. $form->saving(function (Form $form) {
  125. $form->deleteInput('demand_product_id');
  126. //发布人身份
  127. $form->bidding_user_type = DemandTraits::$col[1];
  128. $form->bidding_user_id = Admin::user()->id;
  129. });
  130. $form->saved(function (Form $form) {
  131. $provinceId = Demand::query()->where('id',$this->demand_id)->value('province_id');
  132. if ($provinceId != Admin::user()->province_id) {
  133. $form->response()->error('竞标失败,指能竞标跟自己相同省份的的需求');
  134. }
  135. if($form->isEditing()) {
  136. $productId = request('demand_product_id', 0);
  137. if (!empty($productId)) {
  138. DB::beginTransaction();
  139. try {
  140. //处理订单
  141. //将产品绑给代理商
  142. $demand = Demand::find($form->model()->demand_id);
  143. $demand->demand_product_id = $productId;
  144. $demandProduct = DemandProduct::find($productId);
  145. $product = new Product();
  146. $product->supplier_id = $demandProduct->supplier_id;
  147. $product->category_id = $demandProduct->category_id;
  148. $product->title = $demandProduct->title;
  149. $product->price = $form->model()->price;
  150. $product->original_price = $form->model()->price;
  151. $product->pictures = $demandProduct->pictures;
  152. $product->stock = $demand->stock;
  153. $product->know = $demandProduct->know;
  154. $product->content = $demandProduct->content;
  155. $product->agent_id = $form->model()->bidding_user_id;
  156. $product->save();
  157. //处理需求
  158. $demand = Demand::find($form->model()->demand_id);
  159. $demand->demand_product_id = $productId;
  160. $demand->save();
  161. $demand->product_id = $product->id;
  162. $demand->save();
  163. DB::commit();
  164. } catch (\Exception $e) {
  165. Log::error('分配订单失败::' . $e->getTraceAsString());
  166. DB::rollBack();
  167. return $form->response()->error('分配订单失败,稍后重试或联系管理员!' . $e->getMessage());
  168. }
  169. }
  170. }
  171. $form->response()->success('操作成功')->redirect('demand_bidding');
  172. });
  173. });
  174. }
  175. }