Delivery.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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->where('is_delete', '=', 0)->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. $method = $data['method'];
  108. if (count($data['rules'])){
  109. foreach ($data['rules'] as $rule){
  110. if (is_null($rule['first'])){
  111. if ($method == 10){
  112. $this->error = '首件不能为空';
  113. }
  114. if ($method == 20){
  115. $this->error = '首重不能为空';
  116. }
  117. return false;
  118. }
  119. if (is_null($rule['first_fee'])){
  120. $this->error = '运费不能为空';
  121. return false;
  122. }
  123. if (is_null($rule['additional'])){
  124. if ($method == 10){
  125. $this->error = '续件不能为空';
  126. }
  127. if ($method == 20){
  128. $this->error = '续重不能为空';
  129. }
  130. return false;
  131. }
  132. if (is_null($rule['additional_fee'])){
  133. $this->error = '续费不能为空';
  134. return false;
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * 删除记录
  142. * @return bool
  143. */
  144. public function remove()
  145. {
  146. // 验证运费模板是否被商品使用
  147. if (!$this->checkIsUseGoods($this['delivery_id'])) {
  148. return false;
  149. }
  150. // 删除运费模板
  151. return $this->save(['is_delete' => 1]);
  152. }
  153. /**
  154. * 验证运费模板是否被商品使用
  155. * @param int $deliveryId
  156. * @return bool
  157. */
  158. private function checkIsUseGoods(int $deliveryId)
  159. {
  160. // 判断是否存在商品
  161. $goodsCount = (new GoodsModel)->getGoodsTotal(['delivery_id' => $deliveryId]);
  162. if ($goodsCount > 0) {
  163. $this->error = "该模板被{$goodsCount}个商品使用,不允许删除";
  164. return false;
  165. }
  166. return true;
  167. }
  168. }