Service.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model\goods;
  13. use think\Paginator;
  14. use think\Collection;
  15. use app\common\model\goods\Service as ServiceModel;
  16. use app\store\model\goods\ServiceRel as ServiceRelModel;
  17. /**
  18. * 商品服务与承诺模型
  19. * Class Service
  20. */
  21. class Service extends ServiceModel
  22. {
  23. /**
  24. * 获取全部记录
  25. * @param array $param
  26. * @return Collection
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function getAll(array $param = []): Collection
  32. {
  33. return $this->where($this->getFilter($param))
  34. ->where('is_delete', '=', 0)
  35. ->order(['sort', $this->getPk()])
  36. ->select();
  37. }
  38. /**
  39. * 获取列表记录
  40. * @param array $param
  41. * @return Paginator
  42. * @throws \think\db\exception\DbException
  43. */
  44. public function getList(array $param = []): Paginator
  45. {
  46. return $this->where($this->getFilter($param))
  47. ->where('is_delete', '=', 0)
  48. ->order(['sort', $this->getPk()])
  49. ->paginate();
  50. }
  51. /**
  52. * 获取查询条件
  53. * @param array $param
  54. * @return array
  55. */
  56. private function getFilter(array $param = []): array
  57. {
  58. // 默认查询参数
  59. $params = $this->setQueryDefaultValue($param, ['search' => '']);
  60. // 检索查询条件
  61. $filter = [];
  62. !empty($params['search']) && $filter[] = ['name', 'like', "%{$params['search']}%"];
  63. return $filter;
  64. }
  65. /**
  66. * 新增记录
  67. * @param array $data
  68. * @return bool|false
  69. */
  70. public function add(array $data): bool
  71. {
  72. $data['store_id'] = self::$storeId;
  73. return $this->save($data);
  74. }
  75. /**
  76. * 更新记录
  77. * @param array $data
  78. * @return bool
  79. */
  80. public function edit(array $data): bool
  81. {
  82. return $this->save($data) !== false;
  83. }
  84. /**
  85. * 删除记录(软删除)
  86. * @return bool
  87. * @throws \Exception
  88. */
  89. public function remove(): bool
  90. {
  91. // 判断该服务是否被商品引用
  92. $goodsCount = ServiceRelModel::getCountByServiceId($this['service_id']);
  93. if ($goodsCount > 0) {
  94. $this->error = "该记录被{$goodsCount}个商品引用,不允许删除";
  95. return false;
  96. }
  97. return $this->save(['is_delete' => 1]) !== false;
  98. }
  99. }