StoreProductLabelCate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\store\product\label;
  12. use app\controller\store\AuthController;
  13. use app\Request;
  14. use app\services\product\label\StoreProductLabelCateServices;
  15. use app\services\product\label\StoreProductLabelServices;
  16. use think\facade\App;
  17. /**
  18. * 商品标签组
  19. * Class StoreProductLabelCate
  20. * @package app\controller\store\product\label
  21. */
  22. class StoreProductLabelCate extends AuthController
  23. {
  24. /**
  25. * @var StoreProductLabelCateServices
  26. */
  27. protected $services;
  28. /**
  29. * StoreProductLabelCate constructor.
  30. * @param App $app
  31. * @param StoreProductLabelCateServices $services
  32. */
  33. public function __construct(App $app, StoreProductLabelCateServices $services)
  34. {
  35. parent::__construct($app);
  36. $this->services = $services;
  37. }
  38. /**
  39. * 获取标签组列表
  40. * @param Request $request
  41. * @return mixed
  42. */
  43. public function index(Request $request)
  44. {
  45. $where = $request->postMore([
  46. ['name', '']
  47. ]);
  48. $where['type'] = 1;
  49. $where['relation_id'] = $this->storeId;
  50. $where['group'] = 2;
  51. $where['product_label'] = 1;
  52. return $this->success($this->services->getProductLabelCateList($where));
  53. }
  54. /**
  55. * 生成新增表单
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function create()
  60. {
  61. return $this->success($this->services->createForm());
  62. }
  63. /**
  64. * 保存新增标签组
  65. * @return mixed
  66. */
  67. public function save()
  68. {
  69. $data = $this->request->postMore([
  70. ['name', ''],
  71. ['sort', 0],
  72. ]);
  73. if (!trim($data['name'])) {
  74. return $this->fail('请输入标签组名称');
  75. }
  76. if ($this->services->getOne(['name' => $data['name'], 'group' => 2, 'type' => 1, 'relation_id' => $this->storeId])) {
  77. return $this->fail('标签组已存在');
  78. }
  79. $data['type'] = 1;
  80. $data['group'] = 2;
  81. $data['relation_id'] = $this->storeId;
  82. $data['add_time'] = time();
  83. $this->services->save($data);
  84. return $this->success('添加标签组成功!');
  85. }
  86. /**
  87. * 生成更新表单
  88. * @param $id
  89. * @return mixed
  90. * @throws \FormBuilder\Exception\FormBuilderException
  91. */
  92. public function edit($id)
  93. {
  94. return $this->success($this->services->editForm((int)$id));
  95. }
  96. /**
  97. * 更新标签组
  98. * @param $id
  99. * @return mixed
  100. */
  101. public function update($id)
  102. {
  103. $data = $this->request->postMore([
  104. ['name', ''],
  105. ['sort', 0],
  106. ]);
  107. if (!$data['name']) {
  108. return $this->fail('请输入标签组名称');
  109. }
  110. $cate = $this->services->getOne(['name' => $data['name'], 'group' => 2, 'type' => 1, 'relation_id' => $this->storeId]);
  111. if ($cate && $cate['id'] != $id) {
  112. return $this->fail('标签组已存在');
  113. }
  114. $this->services->update($id, $data);
  115. return $this->success('修改成功!');
  116. }
  117. /**
  118. * 删除标签组
  119. * @param StoreProductLabelServices $labelServices
  120. * @param $id
  121. * @return mixed
  122. */
  123. public function delete(StoreProductLabelServices $labelServices, $id)
  124. {
  125. if (!$id || !$this->services->count(['id' => $id])) {
  126. return $this->fail('删除的数据不存在');
  127. }
  128. if ($labelServices->count(['label_cate' => $id])) {
  129. return $this->fail('标签组下有标签不能删除');
  130. }
  131. $this->services->delete((int)$id);
  132. return $this->success('删除成功!');
  133. }
  134. }