DispatchType.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/28
  6. * Time: 上午11:32
  7. */
  8. namespace app\common\models;
  9. /**
  10. * Class DispatchType
  11. * @package app\common\models
  12. * @property int id
  13. * @property int need_send
  14. * @property int code
  15. * @property int name
  16. * @property int sort
  17. * @property int disable
  18. */
  19. class DispatchType extends BaseModel
  20. {
  21. public $table = 'yz_dispatch_type';
  22. protected $guarded = ['id'];
  23. const EXPRESS = 1; // 快递
  24. const SELF_DELIVERY = 2; // 自提
  25. const STORE_DELIVERY = 3; // 门店配送
  26. const HOTEL_CHECK_IN = 4; // 酒店入住
  27. const DELIVERY_STATION_SELF = 5; // 配送站自提
  28. const DELIVERY_STATION_SEND = 6; // 配送站送货
  29. const DRIVER_DELIVERY = 7; //司机配送
  30. const PACKAGE_DELIVER = 8; //自提点
  31. const CLOUD_WAREHOUSE = 9; //云仓
  32. const SUPPLIER_DRIVER_DISTRIBUTION = 10; //供应商订单配送
  33. const CITY_DELIVERY = 11; //同城配送
  34. const STORE_PACKAGE_DELIVER = 12; //门店自提点
  35. const SHOP_POS = 14; //pos收银
  36. const PACKAGE_DELIVERY = 15; //商城自提
  37. // public $appends = ['name'];
  38. public function needSend()
  39. {
  40. return $this->need_send;
  41. }
  42. public function getNameAttribute()
  43. {
  44. if ($this->hasOneSet) {
  45. return $this->hasOneSet->name ?: $this->attributes['name'];
  46. }
  47. return $this->attributes['name'];
  48. }
  49. /**
  50. * 订单支付就直接完成的配送方式
  51. * @return array
  52. */
  53. public function paidCompleted()
  54. {
  55. return [self::CLOUD_WAREHOUSE, self::SHOP_POS];
  56. }
  57. public static function goodsEnableDispatchTypeIds(array $ids)
  58. {
  59. if (empty($ids)) {return [];}
  60. $dispatchTypes = self::whereIn('id', $ids)->with(['hasOneSet'])->get();
  61. $dispatchTypes = $dispatchTypes->map(function ($item) {
  62. if (!is_null($item->hasOneSet)) {
  63. $item->enable = $item->hasOneSet->enable;
  64. $item->sort = $item->hasOneSet->sort;
  65. }
  66. return $item;
  67. })->filter(function ($dispatchType) {
  68. return $dispatchType->enable;
  69. })->values();
  70. return $dispatchTypes->pluck('id')->toArray();
  71. }
  72. public static function dispatchTypesSetting($dispatchTypes = null)
  73. {
  74. $dispatchTypes = $dispatchTypes ?: DispatchType::getCurrentUniacidSet()->toArray();
  75. $dispatchTypes = array_combine(array_column($dispatchTypes, 'code'), $dispatchTypes);
  76. $dispatchTypes = array_filter($dispatchTypes, function ($dispatchTypes) {
  77. return $dispatchTypes['enable'];
  78. });
  79. $dispatchTypesSetting = array_sort($dispatchTypes, function ($dispatchType) {
  80. return $dispatchType['sort'] + $dispatchType['id'] / 100;
  81. });
  82. return $dispatchTypesSetting;
  83. }
  84. public static function getCurrentUniacidSet($plugin = 0)
  85. {
  86. if (is_array($plugin)) {
  87. $dispatchTypes = self::whereIn('plugin', $plugin)->with(['hasOneSet'])->get();
  88. } else {
  89. $dispatchTypes = self::where('plugin', $plugin)->with(['hasOneSet'])->get();
  90. }
  91. $dispatchTypes = $dispatchTypes->filter(function ($item) {
  92. return $item->getPluginEnable();
  93. })->map(function ($item) {
  94. if (!is_null($item->hasOneSet)) {
  95. $item->enable = $item->hasOneSet->enable;
  96. $item->sort = $item->hasOneSet->sort;
  97. }
  98. $item->name = $item->getTypeName();
  99. return $item;
  100. })->sortByDesc('sort')->values();
  101. // $dispatchTypes = $dispatchTypes->map(function ($item) {
  102. // if (!is_null($item->hasOneSet)) {
  103. // $item->enable = $item->hasOneSet->enable;
  104. // $item->sort = $item->hasOneSet->sort;
  105. // }
  106. // $item->name = $item->getTypeName();
  107. //
  108. // return $item;
  109. // })->sortByDesc('sort')->values();
  110. return $dispatchTypes;
  111. }
  112. public static function getAllEnableDispatchType()
  113. {
  114. $dispatchTypes = self::where('enable', 1)->with(['hasOneSet'])->get();
  115. $dispatchTypes = $dispatchTypes->map(function ($item) {
  116. if (!is_null($item->hasOneSet)) {
  117. $item->enable = $item->hasOneSet->enable;
  118. $item->sort = $item->hasOneSet->sort;
  119. }
  120. return $item;
  121. })->filter(function ($dispatchType) {
  122. return $dispatchType->enable;
  123. })->values();
  124. return $dispatchTypes;
  125. }
  126. //todo 临时加自定义名称
  127. public function getTypeName()
  128. {
  129. $type_name = $this->name;
  130. switch ($this->id) {
  131. case self::PACKAGE_DELIVER : $type_name = PackageDeliver; break;
  132. }
  133. return $type_name;
  134. }
  135. //判断是否有这种配送方式
  136. public function getPluginEnable()
  137. {
  138. if ($this->plugin_id) {
  139. $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-dispatch-menu')[$this->plugin_id][$this->code];
  140. } else {
  141. $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-dispatch-menu.shop')[$this->code];
  142. }
  143. return !is_null($configs);
  144. }
  145. public function hasOneSet()
  146. {
  147. return $this->hasOne(DispatchTypeSet::class, 'dispatch_type_id', 'id'); // TODO: Change the autogenerated stub
  148. }
  149. }