123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace app\common\service;
- use app\common\model\Repo;
- use app\common\model\Stock;
- use app\common\util\WhereBuilder;
- use think\facade\Validate;
- class RepoService extends Service
- {
- public function page($params = [])
- {
- $this->autoParams($params);
- $keyword = $this->pg('keyword');
- $valid = $this->pg('valid');
- $where = WhereBuilder::builder()
- ->like('name|address', $keyword)
- ->eq('valid', $valid)
- ->build();
- $page = (new Repo)
- ->where($where)
- ->paginate($this->tp6Page());
- return $page;
- }
- public function all($params = [])
- {
- return (new Repo)->select();
- }
- public function create($params = [])
- {
- $params = $this->autoParams($params);
- return Repo::create($params);
- }
- public function update($params = [])
- {
- $params = $this->autoParams($params);
- if (!$params) {
- throw $this->exception('未选中任何商品进行更新');
- }
- return (new Repo)->allowField(['name', 'desc', 'address', 'valid'])->update($params);
- }
- public function delete($params = [])
- {
- $params = $this->autoParams($params);
- $this->validate($params, Validate::rule(['id' => 'array']));
- $id = $this->req('id');
- $force = $this->pgd(false, 'force');
- if (is_int($id)) {
- $id = [$id];
- }
- // 非强制删除检查仓库库存
- if (!$force) {
- foreach ($id as $i) {
- $sum = (new Stock)->where('repo_id', '=', $i)->sum('num');
- if ($sum > 0) {
- $repo = (new Repo)->find($id);
- throw $this->exception("仓库{$repo->name}仍有库存未处理,请使用调拨/出库清理库存,或使用强制删除", 1);
- }
- }
- }
- return Repo::destroy($id);
- }
- }
|