StoreProductUnit.php 5.2 KB

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