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

289 lines
12 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\IndustryProduct;
  4. use App\Common\ProductStatus;
  5. use App\Models\Category;
  6. use App\Models\Order;
  7. use App\Models\Supplier;
  8. use App\Models\SystemSetting;
  9. use Dcat\Admin\Admin;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Form\NestedForm;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Http\Controllers\AdminController;
  15. use Illuminate\Support\Facades\DB;
  16. class IndustryProductController extends AdminController
  17. {
  18. /**
  19. * Make a grid builder.
  20. *
  21. * @return Grid
  22. */
  23. protected function grid()
  24. {
  25. return Grid::make(new IndustryProduct(['category:id,name']), function (Grid $grid) {
  26. $grid->disableDeleteButton();
  27. $grid->disableRowSelector();
  28. $grid->model()->where('supplier_id', Admin::user()->id);
  29. $grid->column('id')->sortable();
  30. $grid->column('type')->using(admin_trans('product.options.publish_type'));
  31. $grid->column('category.name', '分类');
  32. $grid->column('title')->limit(15);
  33. $grid->column('picture')->image('', 60,60);
  34. $grid->column('price');
  35. $grid->column('original_price');
  36. $grid->column('stock');
  37. $grid->column('sale');
  38. $grid->column('status')
  39. /*->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]))
  40. ->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
  41. ->switch()
  42. ->else()*/
  43. ->using(ProductStatus::array());
  44. $grid->column('service_persons');
  45. $grid->column('min_sale');
  46. $grid->column('created_at');
  47. $grid->filter(function (Grid\Filter $filter) {
  48. $filter->equal('id')->width(2);
  49. });
  50. });
  51. }
  52. /**
  53. * Make a show builder.
  54. *
  55. * @param mixed $id
  56. *
  57. * @return Show
  58. */
  59. protected function detail($id)
  60. {
  61. return Show::make($id, new IndustryProduct(['category:id,name']), function (Show $show) {
  62. $show->disableDeleteButton();
  63. //不允许查看非自己的数据
  64. if ($show->model()->supplier_id != Admin::user()->id) {
  65. Admin::exit('数据不存在');
  66. }
  67. $show->field('id');
  68. $show->field('type')->using(admin_trans('product.options.publish_type'));
  69. $show->field('category.name', '分类');
  70. $show->field('title');
  71. $show->field('pictures')->image('', 80, 80);
  72. $show->field('price');
  73. $show->field('original_price');
  74. $show->field('stock');
  75. $show->field('sale');
  76. $show->field('status')->using(ProductStatus::array());
  77. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  78. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  79. $show->field('service_persons');
  80. $show->field('min_sale');
  81. $show->field('verify_mobile');
  82. $show->field('created_at');
  83. $show->field('updated_at');
  84. });
  85. }
  86. /**
  87. * Make a form builder.
  88. *
  89. * @return Form
  90. */
  91. protected function form()
  92. {
  93. Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true);
  94. //注:这2个变量需要通过引用方式传值才可以
  95. $change_deposit = 0; //变动的
  96. $old_deposit = 0; //原来交易金数
  97. return Form::make(new IndustryProduct(), function (Form $form) {
  98. $form->disableDeleteButton();
  99. $form->display('id');
  100. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  101. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  102. $form->text('title')->required();
  103. $form->currency('price')->required();
  104. $form->currency('original_price')->required();
  105. $form->number('service_persons')->min(0)->required();
  106. $form->number('stock')->required();
  107. $form->number('min_sale')->min(1)->required();
  108. $form->radio('status')->options([1 => '上架', -2 => '下架'])->default(1);
  109. $form->multipleImage('pictures')->required();
  110. $form->editor('know');
  111. $form->editor('content')->required();
  112. $form->mobile('verify_mobile')->required();
  113. //扩展字段
  114. $publish_type = array_intersect_key(
  115. admin_trans('product.options.publish_type'),
  116. array_flip(Admin::user()->publish_type)
  117. );
  118. $form->radio('type', '产品类型')
  119. ->options($publish_type)->disable($form->isEditing())
  120. ->default(current(Admin::user()->publish_type))
  121. ->when(0, function (Form $form) { //旅游线路
  122. if ($form->isEditing() && $form->model()->type != 0) {
  123. return;
  124. }
  125. $form->table('extends.field_0.project', '包含项目', function (NestedForm $table) {
  126. $table->text('name', '字段1');
  127. $table->text('num', '字段2');
  128. $table->text('price', '字段3');
  129. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  130. $form->dateRange('extends.field_0.date.start', 'extends.field_0.date.end', '行程时间');
  131. })->when(1, function (Form $form) { //酒店
  132. if ($form->isEditing() && $form->model()->type != 1) {
  133. return;
  134. }
  135. $default = [
  136. ['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
  137. ['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
  138. ['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
  139. ['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
  140. ];
  141. $form->table('extends.field_1.tags', '酒店设施', function (NestedForm $table) {
  142. $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
  143. })->value($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
  144. $form->text('extends.field_1.name', '酒店名');
  145. $form->text('extends.field_1.address', '地址');
  146. $form->map('extends.field_1.latitude', 'extends.field_1.longitude', '位置');
  147. })->when(2, function (Form $form) { //景区
  148. if ($form->isEditing() && $form->model()->type != 2) {
  149. return;
  150. }
  151. $form->table('extends.field_2.open_time', '开放时间', function (NestedForm $table) {
  152. $table->text('node', '字段1')->placeholder('如:周一至周五');
  153. $table->text('summer', '字段2')->placeholder('如:08:00~19:00');
  154. $table->text('winter', '字段3')->placeholder('如:08:00~18:00');
  155. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  156. $form->table('extends.field_2.project', '包含项目', function (NestedForm $table) {
  157. $table->text('name', '字段1');
  158. $table->text('num', '字段2');
  159. $table->text('price', '字段3');
  160. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  161. $form->text('extends.field_2.name', '景区名');
  162. $form->text('extends.field_2.address', '地址');
  163. $form->map('extends.field_2.latitude', 'extends.field_2.longitude', '位置');
  164. })->when(3, function (Form $form) { //餐厅
  165. if ($form->isEditing() && $form->model()->type != 3) {
  166. return;
  167. }
  168. $form->table('extends.field_3.open_time', '开放时间', function (NestedForm $table) {
  169. $table->text('week', '字段1')->placeholder('如:周一至周五');
  170. $table->text('section', '字段2')->placeholder('如:上午/下午');
  171. $table->text('time', '字段3')->placeholder('如:08:00~18:00');
  172. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  173. $form->table('extends.field_3.package', '包含套餐', function (NestedForm $table) {
  174. $table->text('name', '字段1')->placeholder('如:清蒸鱿鱼');
  175. $table->text('num', '字段2')->placeholder('如:1条');
  176. $table->text('price', '字段3')->placeholder('如:99元');
  177. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  178. $form->text('extends.field_3.name', '餐厅名');
  179. $form->text('extends.field_3.address', '地址');
  180. $form->map('extends.field_3.latitude', 'extends.field_3.longitude', '位置');
  181. });
  182. })->saving(function (Form $form) use (&$change_deposit, &$old_deposit) {
  183. //不允许编辑非自己数据
  184. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  185. return $form->response()->error('数据不存在');
  186. }
  187. if ($form->isCreating()) {
  188. if (!Admin::user()->publish_type || !in_array($form->type, Admin::user()->publish_type)) {
  189. return $form->response()->error('对不起,你没有此类产品的发布、编辑权限');
  190. }
  191. } else if ($form->isEditing()) { //type不允许编辑
  192. $form->deleteInput('type');
  193. }
  194. //忽略字段
  195. $form->ignore(['id', 'sale', 'created_at', 'updated_at', 'deleted_at']);
  196. $form->hidden(['status', 'supplier_id']);
  197. $form->supplier_id = Admin::user()->id;
  198. if ($form->isCreating()) {
  199. $form->status = ProductStatus::UNAUDITED;
  200. } else if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) { //如果原来是下架或上架状态才允许修改
  201. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  202. }
  203. //处理交易金
  204. $single = SystemSetting::val('single', 'price'); //单人头交易费
  205. if (!$single) {
  206. return $form->response()->error('未设置单人头交易费,请联系管理员在后台设置');
  207. }
  208. //目前逻辑不考虑总后台修改单人头交易费的情况
  209. if ($form->isCreating()) {
  210. $change_deposit = $single * $form->service_persons * $form->stock;
  211. }
  212. //库存和涉及用户数变动时冻结或解冻相应的保证金
  213. else if ($form->isEditing() && ($form->model()->stock != $form->stock || $form->model()->service_persons != $form->service_persons)) {
  214. $new_deposit = $single * $form->service_persons * $form->stock; //新的涉及用户数及库存计算出来的值
  215. $old_deposit = $single * $form->model()->service_persons * $form->model()->stock; //原来的涉及用户数及库存计算出来的值
  216. $change_deposit = $new_deposit - $old_deposit; //saved里面获取的$form->model()->stock是不对的
  217. }
  218. //判断可用交易金是否充足
  219. if (Admin::user()->deposit_normal < $change_deposit) {
  220. return $form->response()->error('交易金不足,请先联系管理员充值');
  221. }
  222. })->saved(function (Form $form, $result) use (&$change_deposit, &$old_deposit) {
  223. if ($form->isEditing()) {
  224. $supplier = Supplier::query()->find(Admin::user()->id); //不能使用Admin::user()修改,必须使用Supplier模型才能正确记录资金变动日志
  225. //需要审核的情况
  226. if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) { //有extends判断不准
  227. DB::beginTransaction();
  228. try {
  229. //将产品状态改为未审核
  230. $form->model()->update(['status' => ProductStatus::UNAUDITED]);
  231. //如果产品未审核,原来冻结过的交易金先全部返还,待总后台审核通过之后再做扣除处理
  232. $supplier->deposit_normal = $supplier->deposit_normal + $old_deposit; //正常交易金
  233. $supplier->deposit_frozen = $supplier->deposit_frozen - $old_deposit; //冻结交易金
  234. $supplier->save();
  235. DB::commit();
  236. } catch (\Exception $e) {
  237. DB::rollBack();
  238. }
  239. }
  240. //不需要审核的情况,$change_deposit无论是正数还是负数都一样计算
  241. else if ($change_deposit != 0) {
  242. $supplier->deposit_normal = $supplier->deposit_normal - $change_deposit; //正常交易金
  243. $supplier->deposit_frozen = $supplier->deposit_frozen + $change_deposit; //冻结交易金
  244. $supplier->save();
  245. }
  246. }
  247. /*if ($result && ($form->isCreating() || $form->isEditing() && $form->model()->wasChanged('stock'))) {
  248. }*/
  249. })->deleting(function (Form $form) {
  250. //不允许删除非自己的数据
  251. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  252. return $form->response()->error('数据不存在');
  253. }
  254. });
  255. }
  256. }