StoreProductUnit.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  12. use app\controller\store\AuthController;
  13. use app\services\product\product\StoreProductUnitServices;
  14. use think\facade\App;
  15. use app\Request;
  16. /**
  17. * 商品单位
  18. * Class StoreProductUnit
  19. * @package app\controller\store\product
  20. */
  21. class StoreProductUnit extends AuthController
  22. {
  23. /**
  24. * StoreProductUnit constructor.
  25. * @param App $app
  26. * @param StoreProductUnitServices $services
  27. */
  28. public function __construct(App $app, StoreProductUnitServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 获取所有商品单位
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getAllUnit()
  41. {
  42. return $this->success($this->services->getAllUnitList(1, (int)$this->storeId));
  43. }
  44. /**
  45. * 显示资源列表
  46. *
  47. * @return \think\Response
  48. */
  49. public function index(Request $request)
  50. {
  51. $where = $request->postMore([
  52. ['name', '']
  53. ]);
  54. $where['type'] = 1;
  55. $where['relation_id'] = $this->storeId;
  56. $where['status'] = 1;
  57. $where['is_del'] = 0;
  58. return $this->success($this->services->getUnitList($where));
  59. }
  60. /**
  61. * 显示创建资源表单页.
  62. *
  63. * @return \think\Response
  64. */
  65. public function create()
  66. {
  67. return $this->success($this->services->createForm());
  68. }
  69. /**
  70. * 保存新建的资源
  71. *
  72. * @param \app\Request $request
  73. * @return \think\Response
  74. */
  75. public function save(Request $request)
  76. {
  77. $data = $request->postMore([
  78. ['name', ''],
  79. ['sort', 0]
  80. ]);
  81. validate(\app\validate\admin\product\StoreProductUnitValidate::class)->scene('get')->check(['name' => $data['name']]);
  82. if ($this->services->getCount(['name' => $data['name'], 'is_del' => 0, 'type' => 1, 'relation_id' => $this->storeId])) {
  83. return $this->fail('单位已经存在,请勿重复添加');
  84. }
  85. $data['add_time'] = time();
  86. if ($this->services->save($data)) {
  87. return $this->success('保存成功');
  88. } else {
  89. return $this->fail('保存失败');
  90. }
  91. }
  92. /**
  93. * 显示指定的资源
  94. *
  95. * @param int $id
  96. * @return \think\Response
  97. */
  98. public function read($id)
  99. {
  100. if (!$id) {
  101. return $this->fail('缺少ID');
  102. }
  103. $info = $this->services->get($id);
  104. if (!$info) {
  105. return $this->fail('获取商品单位失败');
  106. }
  107. return $this->success($info->toArray());
  108. }
  109. /**
  110. * 显示编辑资源表单页.
  111. *
  112. * @param int $id
  113. * @return \think\Response
  114. */
  115. public function edit($id)
  116. {
  117. return $this->success($this->services->updateForm((int)$id));
  118. }
  119. /**
  120. * 保存更新的资源
  121. *
  122. * @param \app\Request $request
  123. * @param int $id
  124. * @return \think\Response
  125. */
  126. public function update(Request $request, $id)
  127. {
  128. $data = $request->postMore([
  129. ['name', ''],
  130. ['sort', 0]
  131. ]);
  132. validate(\app\validate\admin\product\StoreProductUnitValidate::class)->scene('get')->check(['name' => $data['name']]);
  133. $unit = $this->services->getOne(['name' => $data['name'], 'is_del' => 0, 'type' => 1, 'relation_id' => $this->storeId]);
  134. if ($unit && $unit['id'] != $id) {
  135. return $this->fail('单位名称已经存在');
  136. }
  137. if ($this->services->update($id, $data)) {
  138. return $this->success('修改成功');
  139. } else {
  140. return $this->fail('修改失败');
  141. }
  142. }
  143. /**
  144. * 删除指定资源
  145. *
  146. * @param int $id
  147. * @return \think\Response
  148. */
  149. public function delete($id)
  150. {
  151. if (!$id || !($info = $this->services->get($id))) {
  152. return $this->fail('删除的数据不存在');
  153. }
  154. if ($info && $info['is_del'] == 0) {
  155. $this->services->update($id, ['is_del' => 1]);
  156. }
  157. return $this->success('删除成功');
  158. }
  159. }