Service.php 3.1 KB

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