RepoService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\Repo;
  4. use app\common\model\Stock;
  5. use app\common\util\WhereBuilder;
  6. use think\facade\Validate;
  7. class RepoService extends Service
  8. {
  9. public function page($params = [])
  10. {
  11. $this->autoParams($params);
  12. $keyword = $this->pg('keyword');
  13. $valid = $this->pg('valid');
  14. $where = WhereBuilder::builder()
  15. ->like('name|address', $keyword)
  16. ->eq('valid', $valid)
  17. ->build();
  18. $page = (new Repo)
  19. ->where($where)
  20. ->paginate($this->tp6Page());
  21. return $page;
  22. }
  23. public function all($params = [])
  24. {
  25. return (new Repo)->select();
  26. }
  27. public function create($params = [])
  28. {
  29. $params = $this->autoParams($params);
  30. return Repo::create($params);
  31. }
  32. public function update($params = [])
  33. {
  34. $params = $this->autoParams($params);
  35. if (!$params) {
  36. throw $this->exception('未选中任何商品进行更新');
  37. }
  38. return (new Repo)->allowField(['name', 'desc', 'address', 'valid'])->update($params);
  39. }
  40. public function delete($params = [])
  41. {
  42. $params = $this->autoParams($params);
  43. $this->validate($params, Validate::rule(['id' => 'array']));
  44. $id = $this->req('id');
  45. $force = $this->pgd(false, 'force');
  46. if (is_int($id)) {
  47. $id = [$id];
  48. }
  49. // 非强制删除检查仓库库存
  50. if (!$force) {
  51. foreach ($id as $i) {
  52. $sum = (new Stock)->where('repo_id', '=', $i)->sum('num');
  53. if ($sum > 0) {
  54. $repo = (new Repo)->find($id);
  55. throw $this->exception("仓库{$repo->name}仍有库存未处理,请使用调拨/出库清理库存,或使用强制删除", 1);
  56. }
  57. }
  58. }
  59. return Repo::destroy($id);
  60. }
  61. }