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

257 lines
8.2 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Actions\Grid\DemandConfirm;
  4. use App\AdminAgent\Lazys\DemandBiddingLazys;
  5. use App\Common\AgentType;
  6. use App\Models\AgentProduct;
  7. use App\Models\Product;
  8. use App\Traits\ResponseHelper;
  9. use App\AdminAgent\Repositories\Demand;
  10. use App\Models\DemandBidding;
  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 App\Traits\DemandTraits;
  17. use Illuminate\Database\Eloquent\Model;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Arr;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Log;
  22. use Illuminate\Support\Facades\URL;
  23. class DemandController extends AdminController
  24. {
  25. use ResponseHelper;
  26. /**
  27. * Make a grid builder.
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(new Demand(['publisher','biddingUser']), function (Grid $grid) {
  34. if (\request('self',0)) {
  35. $grid->model()->where(['publisher_id' => Admin::user()->id,'publisher_type' => DemandTraits::$col[0]]);
  36. } else {
  37. $grid->model()->where(['province_id' => Admin::user()->province_id]);
  38. }
  39. $grid->column('id')->sortable();
  40. $grid->column('title');
  41. $grid->column('detail','内容')->display('查看')->modal('详情',function ($modal) {
  42. $modal->xl();
  43. return $this->comment;
  44. });
  45. $grid->column('images','图片')->display(function ($image) {
  46. return json_decode($image,true);
  47. })->image('', 60, 60);
  48. //$grid->column('deadline');
  49. $grid->column('publisher.name','发布人');
  50. $grid->column('publisher_type')->using(DemandTraits::$polymorphic);
  51. $grid->column('biddingUser.name','中标人');
  52. $grid->column('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
  53. $grid->column('price');
  54. $grid->column('stock');
  55. $grid->column('state')->using(DemandTraits::$state)->dot(
  56. [
  57. 1 => 'yellow',
  58. 2 => 'success',
  59. 3 => 'danger',
  60. ]
  61. );
  62. //$grid->setActionClass(Grid\Displayers\Actions::class);
  63. //
  64. //$grid->actions(function (Grid\Displayers\Actions $actions) {
  65. // $actions->append(new DemandConfirm(1));
  66. //});
  67. if (\request('self',0)) {
  68. $grid->column('bidding','竞标明细')
  69. ->display(function (){
  70. return '竞标数 : '. DemandBidding::query()->where('demand_id',$this->id)->count();
  71. })
  72. ->modal('竞标明细',function () {
  73. return DemandBiddingLazys::make(['demand_id' => $this->id]);
  74. });
  75. }else{
  76. $grid->column('bidding','竞标')
  77. ->if(function () {
  78. $isBidding = DemandBidding::query()
  79. ->where('demand_id',$this->id)
  80. ->where([
  81. 'bidding_user_type' => DemandTraits::$col[0],
  82. 'bidding_user_id' => Admin::user()->id
  83. ])
  84. ->exists();
  85. return $this->state == 1 && $this->bidding_user_type == Arr::first(DemandTraits::$col) && $isBidding;
  86. })
  87. ->then(function (Grid\Column $column) {
  88. $column->display('已竞标');
  89. })
  90. ->if(function (){
  91. $isNotBidding = DemandBidding::query()
  92. ->where('demand_id',$this->id)
  93. ->where([
  94. 'bidding_user_type' => DemandTraits::$col[0],
  95. 'bidding_user_id' => Admin::user()->id
  96. ])
  97. ->doesntExist();
  98. return $this->state == 1 && $this->bidding_user_type == Arr::first(DemandTraits::$col) && $isNotBidding;
  99. })
  100. ->then(function (Grid\Column $column) {
  101. $column->append('<a class="btn btn-sm btn-primary" href="'.admin_url('/demand_bidding/create?demand_id='.$this->id).'">参与竞标</a>');
  102. });
  103. }
  104. $grid->column('created_at')->sortable();
  105. $grid->disableDeleteButton();
  106. $grid->disableEditButton();
  107. $grid->disableQuickEditButton();
  108. $grid->disableViewButton();
  109. $grid->disableRowSelector();
  110. //$grid->disableActions();
  111. $grid->filter(function (Grid\Filter $filter) {
  112. $filter->equal('id');
  113. $filter->equal('bidding_user_type','竞标用户类型')->select(DemandTraits::$polymorphic);
  114. });
  115. });
  116. }
  117. /**
  118. * Make a show builder.
  119. *
  120. * @param mixed $id
  121. *
  122. * @return Show
  123. */
  124. protected function detail($id)
  125. {
  126. return Show::make($id, new Demand(['publisher','biddingUser']), function (Show $show) {
  127. $show->field('id');
  128. $show->field('title');
  129. $show->field('comment');
  130. $show->field('images')->image();
  131. $show->field('deadline');
  132. $show->field('bidding_user_type','竞标用户类型')->using(DemandTraits::$polymorphic);
  133. $show->field('price');
  134. $show->field('stock');
  135. $show->field('publisher_type')->using(DemandTraits::$polymorphic);
  136. $show->field('publisher.name','发布人');
  137. $show->field('state')->using(DemandTraits::$state)->dot(
  138. [
  139. 1 => 'yellow',
  140. 2 => 'danger',
  141. 3 => 'success',
  142. ]
  143. );;
  144. $show->field('created_at');
  145. });
  146. }
  147. /**
  148. * Make a form builder.
  149. *
  150. * @return Form
  151. */
  152. protected function form()
  153. {
  154. return Form::make(new Demand(), function (Form $form) {
  155. $form->disableEditingCheck();
  156. $form->disableCreatingCheck();
  157. $form->disableViewCheck();
  158. $form->display('id');
  159. $form->text('title')->required()->maxLength(50);
  160. $form->textarea('comment')->required();
  161. $form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
  162. $form->multipleImage('images','图片')->limit(5)->saving(function ($path) {
  163. return json_encode($path);
  164. });
  165. $form->hidden('deadline');
  166. if (Admin::user()->type == AgentType::CLUSTER) {
  167. $form->select('bidding_user_type', '竞标用户类型')
  168. ->when([2], function (Form $form) {
  169. $form->select('agent_product_id', '产品')->options(function () {
  170. return AgentProduct::query()->where('agent_id', Admin::user()->id)->where('status', 1)->pluck('title', 'id');
  171. });
  172. })
  173. ->options([
  174. 1 => '供应商',
  175. 2 => '地接'
  176. ])
  177. ->default(1)
  178. ->required();
  179. }else{
  180. $form->select('bidding_user_type', '竞标用户类型')
  181. ->options([
  182. 1 => '供应商',
  183. ])
  184. ->default(1)
  185. ->required();
  186. }
  187. $form->decimal('price','价格')->rules('required|numeric|min:0|not_in:0',[
  188. '*' => '金额为必填字段且必须大于0',
  189. ]);
  190. $form->number('stock')->required()->min(1);
  191. $form->hidden('publisher_type');
  192. $form->hidden('publisher_id');
  193. $form->saving(function (Form $form) {
  194. // 判断是否是新增操作
  195. if ($form->isCreating()) {
  196. if ($form->bidding_user_type != 2) {
  197. $form->demand_product_id = 0;
  198. }
  199. $form->bidding_user_type = DemandTraits::$col[$form->bidding_user_type];
  200. //处理流拍时间
  201. $form->deadline = now()->addDays(5);
  202. //发布人身份
  203. $form->publisher_type = DemandTraits::$col[0];
  204. $form->publisher_id = Admin::user()->id;
  205. }
  206. });
  207. });
  208. }
  209. //public function binding()
  210. //{
  211. // $demandBiddingId = request('demand_bidding_id',0);
  212. // $demandBidding = DemandBidding::find($demandBiddingId);
  213. // if (empty($demandBidding)) {
  214. // return false;
  215. // }
  216. //
  217. // $demand = \App\Models\Demand::find($demandBidding->demand_id);
  218. //
  219. // if (empty($demand)) {
  220. // return false;
  221. // }
  222. //
  223. // DB::beginTransaction();
  224. // try {
  225. // $demandBidding->state = 1;
  226. // $demandBidding->save();
  227. // //改变订单状态
  228. // $demand->bidding_id = $demandBidding->id;
  229. // $demand->bidding_user_id = $demandBidding->bidding_user_id;
  230. // $demand->state = DemandTraits::$stateKey[1];
  231. // $demand->save();
  232. // //如果是地接类型 绑定地接到订单
  233. // if ($demand->bidding_user_type == DemandTraits::$col[2]){
  234. // $agentProduct = AgentProduct::find($demand->agent_product_id);
  235. // $agentProduct->guide_id = $demandBidding->bidding_user_id;
  236. // $agentProduct->save();
  237. // }
  238. // DB::commit();
  239. // } catch (\Exception $e) {
  240. // Log::error('选中竞标失败::'.$e->getTraceAsString());
  241. // DB::rollBack();
  242. // return $this->jsonFail(1001,'选中竞标失败,稍后重试或联系管理员!'.$e->getMessage());
  243. // }
  244. // return back();
  245. //}
  246. }