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

208 lines
6.3 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\Models\AgentProduct;
  4. use App\Models\Product;
  5. use App\Traits\ResponseHelper;
  6. use App\AdminAgent\Repositories\Demand;
  7. use App\Models\DemandBidding;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use App\Traits\DemandTraits;
  14. use Illuminate\Database\Eloquent\Model;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\URL;
  20. class DemandController extends AdminController
  21. {
  22. use ResponseHelper;
  23. /**
  24. * Make a grid builder.
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(new Demand(['publisher','biddingUser']), function (Grid $grid) {
  31. $grid->column('id')->sortable();
  32. $grid->column('title');
  33. $grid->column('detail','内容')->display('查看')->modal('详情',function ($modal) {
  34. $modal->xl();
  35. return $this->comment;
  36. });
  37. $grid->column('images','图片')->display(function ($image) {
  38. return json_decode($image,true);
  39. })->image();
  40. $grid->column('deadline');
  41. $grid->column('publisher.name','发布人');
  42. $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  43. $grid->column('biddingUser.name','中标人');
  44. $grid->column('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
  45. $grid->column('price');
  46. $grid->column('stock');
  47. $grid->column('state')->using(DemandTraits::$state)->dot(
  48. [
  49. 1 => 'yellow',
  50. 2 => 'success',
  51. 3 => 'danger',
  52. ]
  53. );
  54. $grid->column('bidding','竞标')
  55. ->if(function (){
  56. return $this->state == 1 && $this->bidding_user_type == Arr::first(DemandTraits::$col);
  57. })
  58. ->then(function (Grid\Column $column) {
  59. $column->append('<a class="btn btn-sm btn-primary" href="'.admin_url('/demand_bidding/create?demand_id='.$this->id).'">发起竞标</a>');
  60. });
  61. $grid->column('created_at')->sortable();
  62. $grid->disableDeleteButton();
  63. $grid->disableEditButton();
  64. $grid->disableQuickEditButton();
  65. $grid->disableViewButton();
  66. $grid->disableActions();
  67. $grid->filter(function (Grid\Filter $filter) {
  68. $filter->equal('id');
  69. $filter->equal('bidding_user_type','竞标用户类型')->select(DemandTraits::$polymorphic);
  70. });
  71. });
  72. }
  73. /**
  74. * Make a show builder.
  75. *
  76. * @param mixed $id
  77. *
  78. * @return Show
  79. */
  80. protected function detail($id)
  81. {
  82. return Show::make($id, new Demand(['publisher','biddingUser']), function (Show $show) {
  83. $show->field('id');
  84. $show->field('title');
  85. $show->field('comment');
  86. $show->field('images')->image();
  87. $show->field('deadline');
  88. $show->field('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
  89. $show->field('price');
  90. $show->field('stock');
  91. $show->field('publisher_type')->using(DemandTraits::$polymorphic);
  92. $show->field('publisher.name','发布人');
  93. $show->field('state')->using(DemandTraits::$state)->dot(
  94. [
  95. 1 => 'yellow',
  96. 2 => 'danger',
  97. 3 => 'success',
  98. ]
  99. );;
  100. $show->field('created_at');
  101. });
  102. }
  103. /**
  104. * Make a form builder.
  105. *
  106. * @return Form
  107. */
  108. protected function form()
  109. {
  110. return Form::make(new Demand(), function (Form $form) {
  111. $form->disableEditingCheck();
  112. $form->disableCreatingCheck();
  113. $form->disableViewCheck();
  114. $form->display('id');
  115. $form->text('title');
  116. $form->text('comment');
  117. $form->multipleImage('images','图片')->limit(5)->saving(function ($path) {
  118. return json_encode($path);
  119. });
  120. $form->hidden('deadline');
  121. $form->select('bidding_user_type','竞标用户类型')
  122. ->when([2],function (Form $form){
  123. $form->select('product_id','产品')->options(function (){
  124. return Product::query()->whereIn('agent_id',[0,Admin::user()->id])->pluck('title','id');
  125. });
  126. })
  127. ->options([
  128. 1 => '供应商',
  129. 2 => '地接'
  130. ])
  131. ->default(1);
  132. $form->decimal('price');
  133. $form->number('stock');
  134. $form->hidden('publisher_type');
  135. $form->hidden('publisher_id');
  136. $form->saving(function (Form $form) {
  137. // 判断是否是新增操作
  138. if ($form->isCreating()) {
  139. if ($form->bidding_user_type != 2) {
  140. $form->product_id = 0;
  141. }
  142. $form->bidding_user_type = DemandTraits::$col[$form->bidding_user_type];
  143. //处理流拍时间
  144. $form->deadline = now()->addDays(5);
  145. //发布人身份
  146. $form->publisher_type = DemandTraits::$col[0];
  147. $form->publisher_id = Admin::user()->id;
  148. }
  149. });
  150. });
  151. }
  152. public function binding()
  153. {
  154. $demandBiddingId = request('demand_bidding_id',0);
  155. $demandBidding = DemandBidding::find($demandBiddingId);
  156. if (empty($demandBidding)) {
  157. return false;
  158. }
  159. $demand = \App\Models\Demand::find($demandBidding->demand_id);
  160. if (empty($demand)) {
  161. return false;
  162. }
  163. DB::beginTransaction();
  164. try {
  165. //新建供应商订单
  166. $product = new Product();
  167. $product->agent_id = $demand->publisher_id;
  168. $product->title = $demand->title;
  169. $product->pictures = $demand->images;
  170. $product->price = $demand->price;
  171. $product->original_price = $demand->price;
  172. $product->stock = $demand->stock;
  173. $product->state = 0;
  174. $product->save();
  175. //处理代理商订单
  176. $agentProduct = new AgentProduct();
  177. $agentProduct->agent_id = $demand->publisher_id;
  178. $agentProduct->product_id = $product->id;
  179. $agentProduct->stock = $demand->stock;
  180. $agentProduct->price = $demand->price;
  181. $agentProduct->original_price = $demand->price;
  182. $agentProduct->state = 0;
  183. $agentProduct->save();
  184. //改变订单状态
  185. $demand->bidding_id = $demandBidding->id;
  186. $demand->bidding_user_id = $demandBidding->bidding_user_id;
  187. $demand->state = DemandTraits::$stateKey[1];
  188. $demand->save();
  189. DB::commit();
  190. } catch (\Exception $e) {
  191. Log::error('选中竞标失败::'.$e->getTraceAsString());
  192. DB::rollBack();
  193. return $this->jsonFail(1001,'选中竞标失败,稍后重试或联系管理员!'.$e->getMessage());
  194. }
  195. return back();
  196. }
  197. }