| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/2/28
- * Time: 上午11:32
- */
- namespace app\common\models;
- /**
- * Class DispatchType
- * @package app\common\models
- * @property int id
- * @property int need_send
- * @property int code
- * @property int name
- * @property int sort
- * @property int disable
- */
- class DispatchType extends BaseModel
- {
- public $table = 'yz_dispatch_type';
- protected $guarded = ['id'];
- const EXPRESS = 1; // 快递
- const SELF_DELIVERY = 2; // 自提
- const STORE_DELIVERY = 3; // 门店配送
- const HOTEL_CHECK_IN = 4; // 酒店入住
- const DELIVERY_STATION_SELF = 5; // 配送站自提
- const DELIVERY_STATION_SEND = 6; // 配送站送货
- const DRIVER_DELIVERY = 7; //司机配送
- const PACKAGE_DELIVER = 8; //自提点
- const CLOUD_WAREHOUSE = 9; //云仓
- const SUPPLIER_DRIVER_DISTRIBUTION = 10; //供应商订单配送
- const CITY_DELIVERY = 11; //同城配送
- const STORE_PACKAGE_DELIVER = 12; //门店自提点
- const SHOP_POS = 14; //pos收银
- const PACKAGE_DELIVERY = 15; //商城自提
- // public $appends = ['name'];
- public function needSend()
- {
- return $this->need_send;
- }
- public function getNameAttribute()
- {
- if ($this->hasOneSet) {
- return $this->hasOneSet->name ?: $this->attributes['name'];
- }
- return $this->attributes['name'];
- }
- /**
- * 订单支付就直接完成的配送方式
- * @return array
- */
- public function paidCompleted()
- {
- return [self::CLOUD_WAREHOUSE, self::SHOP_POS];
- }
- public static function goodsEnableDispatchTypeIds(array $ids)
- {
- if (empty($ids)) {return [];}
- $dispatchTypes = self::whereIn('id', $ids)->with(['hasOneSet'])->get();
- $dispatchTypes = $dispatchTypes->map(function ($item) {
- if (!is_null($item->hasOneSet)) {
- $item->enable = $item->hasOneSet->enable;
- $item->sort = $item->hasOneSet->sort;
- }
- return $item;
- })->filter(function ($dispatchType) {
- return $dispatchType->enable;
- })->values();
- return $dispatchTypes->pluck('id')->toArray();
- }
- public static function dispatchTypesSetting($dispatchTypes = null)
- {
- $dispatchTypes = $dispatchTypes ?: DispatchType::getCurrentUniacidSet()->toArray();
- $dispatchTypes = array_combine(array_column($dispatchTypes, 'code'), $dispatchTypes);
- $dispatchTypes = array_filter($dispatchTypes, function ($dispatchTypes) {
- return $dispatchTypes['enable'];
- });
- $dispatchTypesSetting = array_sort($dispatchTypes, function ($dispatchType) {
- return $dispatchType['sort'] + $dispatchType['id'] / 100;
- });
- return $dispatchTypesSetting;
- }
- public static function getCurrentUniacidSet($plugin = 0)
- {
- if (is_array($plugin)) {
- $dispatchTypes = self::whereIn('plugin', $plugin)->with(['hasOneSet'])->get();
- } else {
- $dispatchTypes = self::where('plugin', $plugin)->with(['hasOneSet'])->get();
- }
- $dispatchTypes = $dispatchTypes->filter(function ($item) {
- return $item->getPluginEnable();
- })->map(function ($item) {
- if (!is_null($item->hasOneSet)) {
- $item->enable = $item->hasOneSet->enable;
- $item->sort = $item->hasOneSet->sort;
- }
- $item->name = $item->getTypeName();
- return $item;
- })->sortByDesc('sort')->values();
- // $dispatchTypes = $dispatchTypes->map(function ($item) {
- // if (!is_null($item->hasOneSet)) {
- // $item->enable = $item->hasOneSet->enable;
- // $item->sort = $item->hasOneSet->sort;
- // }
- // $item->name = $item->getTypeName();
- //
- // return $item;
- // })->sortByDesc('sort')->values();
- return $dispatchTypes;
- }
- public static function getAllEnableDispatchType()
- {
- $dispatchTypes = self::where('enable', 1)->with(['hasOneSet'])->get();
- $dispatchTypes = $dispatchTypes->map(function ($item) {
- if (!is_null($item->hasOneSet)) {
- $item->enable = $item->hasOneSet->enable;
- $item->sort = $item->hasOneSet->sort;
- }
- return $item;
- })->filter(function ($dispatchType) {
- return $dispatchType->enable;
- })->values();
- return $dispatchTypes;
- }
- //todo 临时加自定义名称
- public function getTypeName()
- {
- $type_name = $this->name;
- switch ($this->id) {
- case self::PACKAGE_DELIVER : $type_name = PackageDeliver; break;
- }
- return $type_name;
- }
- //判断是否有这种配送方式
- public function getPluginEnable()
- {
- if ($this->plugin_id) {
- $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-dispatch-menu')[$this->plugin_id][$this->code];
- } else {
- $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-dispatch-menu.shop')[$this->code];
- }
- return !is_null($configs);
- }
-
- public function hasOneSet()
- {
- return $this->hasOne(DispatchTypeSet::class, 'dispatch_type_id', 'id'); // TODO: Change the autogenerated stub
- }
- }
|