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

197 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\Product;
  4. use App\Common\ProductStatus;
  5. use App\Models\AgentProduct;
  6. use App\Models\AgentProductItem;
  7. use App\Models\AgentSetting;
  8. use App\Models\Category;
  9. use Dcat\Admin\Admin;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Dcat\Admin\Http\Controllers\AdminController;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class ProductController extends AdminController
  17. {
  18. protected $title = '产品';
  19. /**
  20. * Make a grid builder.
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
  27. $grid->model()->where('supplier_id', Admin::user()->id);
  28. $grid->column('id')->sortable();
  29. $grid->column('category.name', '产品分类');
  30. $grid->column('title');
  31. $grid->column('picture')->image('', 60, 60);
  32. $grid->column('price');
  33. $grid->column('original_price');
  34. $grid->column('stock');
  35. $grid->column('sale');
  36. $grid->column('status')->using(ProductStatus::array());
  37. $grid->column('verify_mobile','核销号码');
  38. $grid->column('created_at');
  39. $grid->column('updated_at');
  40. $grid->filter(function (Grid\Filter $filter) {
  41. $filter->equal('id');
  42. });
  43. });
  44. }
  45. /**
  46. * Make a show builder.
  47. *
  48. * @param mixed $id
  49. *
  50. * @return Show
  51. */
  52. protected function detail($id)
  53. {
  54. return Show::make($id, new Product(), function (Show $show) {
  55. $show->field('id');
  56. $show->field('supplier_id');
  57. $show->field('category_id');
  58. $show->field('title');
  59. $show->field('price');
  60. $show->field('original_price');
  61. $show->field('pictures')->image('', 80, 80);
  62. $show->field('stock');
  63. $show->field('sale');
  64. $show->field('status');
  65. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  66. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  67. $show->field('verify_mobile','核销号码');
  68. $show->field('created_at');
  69. $show->field('updated_at');
  70. });
  71. }
  72. /**
  73. * Make a form builder.
  74. *
  75. * @return Form
  76. */
  77. protected function form()
  78. {
  79. return Form::make(new Product(), function (Form $form) {
  80. //不允许编辑非自己数据
  81. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  82. return $form->response()->error('数据不存在');
  83. }
  84. $form->display('id');
  85. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  86. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  87. $form->text('title')->required();
  88. $form->text('price')->required();
  89. $form->text('original_price')->required();
  90. $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
  91. $form->text('stock')->default(9999)->required();
  92. $form->editor('know');
  93. $form->editor('content')->required();
  94. $form->text('verify_mobile','核销号码');
  95. if ($form->isEditing()) {
  96. $form->confirm('提示', '编辑产品需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
  97. }
  98. })->saving(function (Form $form) {
  99. //不允许编辑非自己数据
  100. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  101. return $form->response()->error('数据不存在');
  102. }
  103. //不允许编辑的字段 TODO 忽略字段不起作用
  104. $form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
  105. //null字段转为''
  106. foreach ($form->input() as $k => $v) {
  107. if (is_null($v)) {
  108. $form->$k = '';
  109. }
  110. }
  111. //特殊字段处理
  112. $form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
  113. $form->status = ProductStatus::UNAUDITED;
  114. $form->supplier_id = Admin::user()->id;
  115. })->saved(function (Form $form, $result) {
  116. //下架代理商产品
  117. $id = $form->getKey();
  118. if ($result) {
  119. AgentProduct::where('product_id', $id)
  120. ->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
  121. ->update(['status' => ProductStatus::SOLD_OUT]);
  122. }
  123. //自动上架
  124. if ($form->isCreating()) {
  125. $agentIds = AgentProductItem::query()->withoutGlobalScope('orderById')->where('supplier_id',$form->supplier_id)->distinct()->pluck('agent_id');
  126. foreach ($agentIds as $v) {
  127. //如果没开启自动上架 滚蛋
  128. if(empty(AgentSetting::val($v,'auto_shelves'))) {
  129. continue;
  130. }
  131. DB::beginTransaction();
  132. try {
  133. $agentProduct = new AgentProduct();
  134. $agentProduct->title = $form->title;
  135. $agentProduct->agent_id = $v;
  136. $agentProduct->product_id = $id;
  137. $agentProduct->product_ids = explode(',',$id);
  138. $agentProduct->stock = $form->stock;
  139. $agentProduct->status = 1;
  140. $agentProduct->pictures = explode(',',$form->pictures);
  141. $agentProduct->content = $form->content;
  142. $agentProduct->know = $form->know;
  143. //计算价格
  144. $profit = AgentSetting::val($v, 'profit') ?? 0;
  145. $price = bcmul($form->price, bcdiv($profit + 100, 100, 6), 2);
  146. $agentProduct->price = $price;
  147. $agentProduct->original_price = $price;
  148. //自动添加分类
  149. $autoCategory = AgentSetting::val($v, 'auto_category') ?? 0;
  150. if (!empty($autoCategory)) {
  151. $categoryName = Category::query()->where('id', $form->category_id)->value('name');
  152. $category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]);
  153. $agentProduct->category_id = $category->id;
  154. }
  155. $agentProduct->save();
  156. //维护关联表
  157. $agentProductItem = AgentProductItem::query()->create([
  158. 'agent_id' => $v,
  159. 'supplier_id' => $form->supplier_id,
  160. 'agent_product_id' => $agentProduct->id,
  161. 'product_id' => $id,
  162. ]);
  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. })->deleting(function (Form $form) {
  172. //不允许删除非自己的数据
  173. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  174. return $form->response()->error('数据不存在');
  175. }
  176. });
  177. }
  178. }