Delivery.php 4.2 KB

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