Delivery.php 4.5 KB

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