Delivery.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\model;
  10. use app\store\model\Goods as GoodsModel;
  11. use app\common\model\Delivery as DeliveryModel;
  12. /**
  13. * 配送模板模型
  14. * Class Delivery
  15. * @package app\common\model
  16. */
  17. class Delivery extends DeliveryModel
  18. {
  19. /**
  20. * 添加新记录
  21. * @param $data
  22. * @return bool|int
  23. * @throws \think\Exception
  24. * @throws \think\exception\PDOException
  25. */
  26. public function add($data)
  27. {
  28. // 表单验证
  29. if (!$this->onValidate($data)) return false;
  30. // 保存数据
  31. $data['wxapp_id'] = self::$wxapp_id;
  32. if ($this->allowField(true)->save($data)) {
  33. return $this->createDeliveryRule($data['rule']);
  34. }
  35. return false;
  36. }
  37. /**
  38. * 编辑记录
  39. * @param $data
  40. * @return bool|int
  41. * @throws \think\Exception
  42. * @throws \think\exception\PDOException
  43. */
  44. public function edit($data)
  45. {
  46. // 表单验证
  47. if (!$this->onValidate($data)) return false;
  48. // 保存数据
  49. if ($this->allowField(true)->save($data)) {
  50. return $this->createDeliveryRule($data['rule']);
  51. }
  52. return false;
  53. }
  54. /**
  55. * 表单验证
  56. * @param $data
  57. * @return bool
  58. */
  59. private function onValidate($data)
  60. {
  61. if (!isset($data['rule']) || empty($data['rule'])) {
  62. $this->error = '请选择可配送区域';
  63. return false;
  64. }
  65. foreach ($data['rule']['first'] as $value) {
  66. if ((int)$value <= 0 || (int)$value != $value) {
  67. $this->error = '首件或首重必须是正整数';
  68. return false;
  69. }
  70. }
  71. foreach ($data['rule']['additional'] as $value) {
  72. if ((int)$value <= 0 || (int)$value != $value) {
  73. $this->error = '续件或续重必须是正整数';
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. /**
  80. * 获取配送区域及运费设置项
  81. * @return array
  82. */
  83. public function getFormList()
  84. {
  85. // 所有地区
  86. $regions = Region::getCacheAll();
  87. $list = [];
  88. foreach ($this['rule'] as $rule) {
  89. $citys = explode(',', $rule['region']);
  90. $province = [];
  91. foreach ($citys as $cityId) {
  92. if (!isset($regions[$cityId])) continue;
  93. !in_array($regions[$cityId]['pid'], $province) && $province[] = $regions[$cityId]['pid'];
  94. }
  95. $list[] = [
  96. 'first' => $rule['first'],
  97. 'first_fee' => $rule['first_fee'],
  98. 'additional' => $rule['additional'],
  99. 'additional_fee' => $rule['additional_fee'],
  100. 'province' => $province,
  101. 'citys' => $citys,
  102. ];
  103. }
  104. return $list;
  105. }
  106. /**
  107. * 添加模板区域及运费
  108. * @param $data
  109. * @return int
  110. * @throws \think\Exception
  111. * @throws \think\exception\PDOException
  112. */
  113. private function createDeliveryRule($data)
  114. {
  115. $save = [];
  116. $connt = count($data['region']);
  117. for ($i = 0; $i < $connt; $i++) {
  118. $save[] = [
  119. 'region' => $data['region'][$i],
  120. 'first' => $data['first'][$i],
  121. 'first_fee' => $data['first_fee'][$i],
  122. 'additional' => $data['additional'][$i],
  123. 'additional_fee' => $data['additional_fee'][$i],
  124. 'wxapp_id' => self::$wxapp_id
  125. ];
  126. }
  127. $this->rule()->delete();
  128. return $this->rule()->saveAll($save);
  129. }
  130. /**
  131. * 删除记录
  132. * @return int
  133. * @throws \think\Exception
  134. * @throws \think\exception\PDOException
  135. */
  136. public function remove()
  137. {
  138. // 验证运费模板是否被商品使用
  139. if (!$this->checkIsUseGoods($this['delivery_id'])) {
  140. return false;
  141. }
  142. // 删除运费模板
  143. $this->rule()->delete();
  144. return $this->delete();
  145. }
  146. /**
  147. * 验证运费模板是否被商品使用
  148. * @param int $deliveryId
  149. * @return bool
  150. * @throws \think\Exception
  151. */
  152. private function checkIsUseGoods($deliveryId)
  153. {
  154. // 判断是否存在商品
  155. $goodsCount = (new GoodsModel)->where('delivery_id', '=', $deliveryId)
  156. ->where('is_delete', '=', 0)
  157. ->count();
  158. if ($goodsCount > 0) {
  159. $this->error = '该模板被' . $goodsCount . '个商品使用,不允许删除';
  160. return false;
  161. }
  162. return true;
  163. }
  164. }