Delivery.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\supplier\model\settings;
  3. use app\common\model\settings\DeliveryRule as DeliveryRuleModel;
  4. use app\supplier\model\product\Product as ProductModel;
  5. use app\common\model\settings\Delivery as DeliveryModel;
  6. use app\common\model\settings\Region as RegionModel;
  7. /**
  8. * 配送模板模型
  9. */
  10. class Delivery extends DeliveryModel
  11. {
  12. /**
  13. * 添加新记录
  14. */
  15. public function add($data, $shop_supplier_id)
  16. {
  17. if (!isset($data['rule']) || empty($data['rule'])) {
  18. $this->error = '请选择可配送区域';
  19. return false;
  20. }
  21. $delivery = [
  22. 'name' => $data['name'],
  23. 'method' => $data['radio'],
  24. 'shop_supplier_id' => $shop_supplier_id,
  25. 'sort' => $data['sort'],
  26. 'app_id' => self::$app_id
  27. ];
  28. if ($this->save($delivery)) {
  29. return $this->createDeliveryRule($data['rule'], 'add');
  30. }
  31. return false;
  32. }
  33. /**
  34. * 编辑记录
  35. */
  36. public function edit($data)
  37. {
  38. if (!isset($data['rule']) || empty($data['rule'])) {
  39. $this->error = '请选择可配送区域';
  40. return false;
  41. }
  42. $delivery = [
  43. 'name' => $data['name'],
  44. 'method' => $data['radio'],
  45. 'sort' => $data['sort'],
  46. ];
  47. if ($this->save($delivery)) {
  48. return $this->createDeliveryRule($data['rule'],'edit');
  49. }
  50. return false;
  51. }
  52. /**
  53. * 添加模板区域及运费
  54. */
  55. private function createDeliveryRule($data, $scene = 'add')
  56. {
  57. $save = [];
  58. $count = count($data);
  59. for ($i = 0; $i < $count; $i++) {
  60. $save[] = [
  61. 'region' => join(',', array_values($data[$i]['citys'])),
  62. 'first' => $data[$i]['first'],
  63. 'first_fee' => $data[$i]['first_fee'],
  64. 'additional' => $data[$i]['additional'],
  65. 'additional_fee' => $data[$i]['additional_fee'],
  66. 'app_id' => self::$app_id
  67. ];
  68. }
  69. if($scene == 'edit'){
  70. (new DeliveryRuleModel())->where('delivery_id', '=', $this['delivery_id'])->delete();
  71. }
  72. return $this->rule()->saveAll($save);
  73. }
  74. /**
  75. * 获取配送区域及运费设置项
  76. */
  77. public function getFormList()
  78. {
  79. // 所有地区
  80. $regions = RegionModel::getCacheAll();
  81. $list = [];
  82. foreach ($this['rule'] as $rule) {
  83. $citys = explode(',', $rule['region']);
  84. $province = [];
  85. foreach ($citys as $cityId) {
  86. if (!isset($regions[$cityId])) continue;
  87. !in_array($regions[$cityId]['pid'], $province) && $province[] = $regions[$cityId]['pid'];
  88. }
  89. $list[] = [
  90. 'first' => $rule['first'],
  91. 'first_fee' => $rule['first_fee'],
  92. 'additional' => $rule['additional'],
  93. 'additional_fee' => $rule['additional_fee'],
  94. 'province' => $province,
  95. 'citys' => $citys,
  96. ];
  97. }
  98. return $list;
  99. }
  100. /**
  101. * 删除记录
  102. */
  103. public function remove()
  104. {
  105. // 验证运费模板是否被商品使用
  106. if (!$this->checkIsUseProduct($this['delivery_id'])) {
  107. return false;
  108. }
  109. // 删除运费模板
  110. $this->rule()->delete();
  111. return $this->delete();
  112. }
  113. /**
  114. * 验证运费模板是否被商品使用
  115. */
  116. private function checkIsUseProduct($deliveryId)
  117. {
  118. // 判断是否存在商品
  119. $productCount = (new ProductModel)->where('delivery_id', '=', $deliveryId)
  120. ->where('is_delete', '=', 0)
  121. ->count();
  122. if ($productCount > 0) {
  123. $this->error = '该模板被' . $productCount . '个商品使用,不允许删除';
  124. return false;
  125. }
  126. return true;
  127. }
  128. }