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

204 lines
6.5 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
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->disableRowSelector();
  70. $grid->filter(function (Grid\Filter $filter) {
  71. $filter->equal('id');
  72. });
  73. });
  74. }
  75. /**
  76. * Make a show builder.
  77. *
  78. * @param mixed $id
  79. *
  80. * @return Show
  81. */
  82. protected function detail($id)
  83. {
  84. return Show::make($id, new DemandBidding(), function (Show $show) {
  85. $show->field('id');
  86. $show->field('price');
  87. $show->field('comment');
  88. $show->field('created_at');
  89. $show->field('updated_at');
  90. });
  91. }
  92. /**
  93. * Make a form builder.
  94. *
  95. * @return Form
  96. */
  97. protected function form()
  98. {
  99. return Form::make(new DemandBidding(), function (Form $form) {
  100. $form->disableEditingCheck();
  101. $form->disableCreatingCheck();
  102. $form->disableDeleteButton();
  103. $form->disableViewButton();
  104. $form->disableViewCheck();
  105. $form->display('id')->disable();
  106. if(request('is_bidding',0)) {
  107. $form->textarea('comment')->disable()->required();
  108. $form->selectTable('demand_product_id', '产品')
  109. ->title('选择产品')
  110. ->dialogWidth('50%;min-width:600px;') //不起作用
  111. ->from(SelectProduct::make())
  112. ->model(DemandProduct::class)
  113. ->required();
  114. }else{
  115. $form->text('price')->required();
  116. $form->textarea('comment')->required();
  117. }
  118. $form->hidden('demand_id')->value(request('demand_id',0));
  119. $form->hidden('bidding_user_type')->disable();
  120. $form->hidden('bidding_user_id')->disable();
  121. $form->saving(function (Form $form) {
  122. $form->deleteInput('demand_product_id');
  123. //发布人身份
  124. $form->bidding_user_type = DemandTraits::$col[1];
  125. $form->bidding_user_id = Admin::user()->id;
  126. });
  127. $form->saving(function (Form $form) {
  128. if ($form->isCreating()) {
  129. $isBidding = \App\Models\DemandBidding::query()
  130. ->where('demand_id',$form->demand_id)
  131. ->where([
  132. 'bidding_user_type' => DemandTraits::$col[1],
  133. 'bidding_user_id' => Admin::user()->id
  134. ])
  135. ->exists();
  136. if ($isBidding) {
  137. Admin::exit('你已经竞标过了,无需重复参加');
  138. }
  139. }
  140. });
  141. $form->saved(function (Form $form) {
  142. $provinceId = Demand::query()->where('id',$this->demand_id)->value('province_id');
  143. if ($provinceId != Admin::user()->province_id) {
  144. $form->response()->error('竞标失败,只能竞标跟自己相同省份的的需求');
  145. }
  146. if($form->isEditing()) {
  147. $productId = request('demand_product_id', 0);
  148. if (!empty($productId)) {
  149. DB::beginTransaction();
  150. try {
  151. //处理订单
  152. //将产品绑给代理商
  153. $demand = Demand::find($form->model()->demand_id);
  154. $demand->demand_product_id = $productId;
  155. $demandProduct = DemandProduct::find($productId);
  156. $product = new Product();
  157. $product->supplier_id = $demandProduct->supplier_id;
  158. $product->category_id = $demandProduct->category_id;
  159. $product->title = $demandProduct->title;
  160. $product->price = $form->model()->price;
  161. $product->original_price = $form->model()->price;
  162. $product->pictures = $demandProduct->pictures;
  163. $product->stock = $demand->stock;
  164. $product->know = $demandProduct->know;
  165. $product->content = $demandProduct->content;
  166. $product->agent_id = $form->model()->bidding_user_id;
  167. $product->type = $demandProduct->type;
  168. $product->extends = $demandProduct->extends;
  169. $product->service_persons = $demandProduct->service_persons;
  170. $product->save();
  171. //处理需求
  172. $demand = Demand::find($form->model()->demand_id);
  173. $demand->demand_product_id = $productId;
  174. $demand->save();
  175. $demand->product_id = $product->id;
  176. $demand->save();
  177. DB::commit();
  178. } catch (\Exception $e) {
  179. Log::error('分配订单失败::' . $e->getTraceAsString());
  180. DB::rollBack();
  181. return $form->response()->error('分配订单失败,稍后重试或联系管理员!' . $e->getMessage());
  182. }
  183. }
  184. }
  185. $form->response()->success('操作成功')->redirect('demand_bidding');
  186. });
  187. });
  188. }
  189. }