Delivery.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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;
  13. use app\store\model\Goods as GoodsModel;
  14. use app\common\model\Delivery as DeliveryModel;
  15. use app\store\model\DeliveryRule as DeliveryRuleModel;
  16. /**
  17. * 配送模板模型
  18. * Class Delivery
  19. * @package app\common\model
  20. */
  21. class Delivery extends DeliveryModel
  22. {
  23. /**
  24. * 获取全部
  25. * @return \think\Collection
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public static function getAll(): \think\Collection
  31. {
  32. $model = new static;
  33. return $model->where('is_delete', '=', 0)
  34. ->order(['sort', $model->getPk()])
  35. ->select();
  36. }
  37. /**
  38. * 获取列表
  39. * @param array $param
  40. * @return \think\Paginator
  41. * @throws \think\db\exception\DbException
  42. */
  43. public function getList(array $param = []): \think\Paginator
  44. {
  45. // 默认查询条件
  46. $params = $this->setQueryDefaultValue($param, ['search' => '']);
  47. // 检索查询条件
  48. $filter = [];
  49. !empty($params['search']) && $filter[] = ['name', 'like', "%{$params['search']}%"];
  50. // 查询列表数据
  51. return $this->with(['rule'])
  52. ->where($filter)
  53. ->where('is_delete', '=', 0)
  54. ->order(['sort', $this->getPk()])
  55. ->paginate(15);
  56. }
  57. /**
  58. * 添加新记录
  59. * @param array $data
  60. * @return bool
  61. */
  62. public function add(array $data): bool
  63. {
  64. // 表单验证
  65. if (!$this->onValidate($data)) {
  66. return false;
  67. }
  68. $data['store_id'] = self::$storeId;
  69. // 事务处理
  70. $this->transaction(function () use ($data) {
  71. // 保存数据
  72. $this->save($data);
  73. // 添加模板区域及运费
  74. DeliveryRuleModel::increased((int)$this['delivery_id'], $data['rules']);
  75. });
  76. return true;
  77. }
  78. /**
  79. * 编辑记录
  80. * @param array $data
  81. * @return bool
  82. */
  83. public function edit(array $data): bool
  84. {
  85. // 表单验证
  86. if (!$this->onValidate($data)) {
  87. return false;
  88. }
  89. // 事务处理
  90. $this->transaction(function () use ($data) {
  91. // 保存数据
  92. $this->save($data);
  93. // 更新模板区域及运费
  94. DeliveryRuleModel::updates((int)$this['delivery_id'], $data['rules']);
  95. });
  96. return true;
  97. }
  98. /**
  99. * 表单验证
  100. * @param array $data
  101. * @return bool
  102. */
  103. private function onValidate(array $data): bool
  104. {
  105. if (empty($data['rules'])) {
  106. $this->error = '请选择可配送区域';
  107. return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * 删除记录
  113. * @return bool
  114. */
  115. public function remove(): bool
  116. {
  117. // 验证运费模板是否被商品使用
  118. if (!$this->checkIsUseGoods($this['delivery_id'])) {
  119. return false;
  120. }
  121. // 删除运费模板
  122. return $this->save(['is_delete' => 1]);
  123. }
  124. /**
  125. * 验证运费模板是否被商品使用
  126. * @param int $deliveryId
  127. * @return bool
  128. */
  129. private function checkIsUseGoods(int $deliveryId): bool
  130. {
  131. // 判断是否存在商品
  132. $goodsCount = (new GoodsModel)->getGoodsTotal(['delivery_id' => $deliveryId]);
  133. if ($goodsCount > 0) {
  134. $this->error = "该模板被{$goodsCount}个商品使用,不允许删除";
  135. return false;
  136. }
  137. return true;
  138. }
  139. }