| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\common\models\goods;
- use app\common\models\BaseModel;
- use app\common\models\DispatchType;
- use app\frontend\modules\order\models\PreOrder;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Contracts\Validation\Validator;
- use app\backend\modules\goods\observers\GoodsDispatchObserver;
- /**
- * Created by PhpStorm.
- * Author: 芸众商城 www.yunzshop.com
- * Date: 2017/2/22
- * Time: 下午5:54
- */
- class GoodsDispatch extends BaseModel
- {
- public $table = 'yz_goods_dispatch';
- const UNIFY_TYPE = 1;
- const TEMPLATE_TYPE = 0;
- /**
- * 不可填充字段.
- *
- * @var array
- */
- protected $guarded = ['created_at', 'updated_at'];
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- }
- /**
- * 自定义显示错误信息
- * @return array
- */
- public static function getDispatchInfo($goodsId)
- {
- $dispatchInfo = self::where('goods_id', $goodsId)
- ->first();
- return $dispatchInfo;
- }
- /**
- * 自定义字段名
- * 可使用
- * @return array
- */
- public function atributeNames()
- {
- return [
- 'dispatch_type' => '配送方式',
- 'dispatch_price' => '统一配送价格',
- 'dispatch_id' => '配送模板',
- //'is_cod' => '是否支持货到付款',
- ];
- }
- public function rules()
- {
- return [
- 'dispatch_type' => 'required|integer|min:0|max:1',
- 'dispatch_price' => 'numeric|min:0',
- 'dispatch_id' => 'integer',
- //'is_cod' => 'required|integer|min:0|max:1',
- ];
- }
- public static function boot()
- {
- parent::boot();
- //注册观察者
- static::observe(new GoodsDispatchObserver);
- }
- public function enableDispatchTypeIds()
- {
- $dispatchTypeIds = [];
- $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-delivery-method');
- if ($configs) {
- $dispatchTypesSetting = $this->dispatchTypesSetting();
- foreach ($configs as $dispatchTypeCode => $dispatchTypeClass) {
- if (!class_exists($dispatchTypeClass)) {
- break;
- }
- $dispatchTypeSetting = $dispatchTypesSetting[$dispatchTypeCode];
- $orderDispatchType = new $dispatchTypeClass($dispatchTypeSetting);
- if ($orderDispatchType->enable()) {
- $dispatchTypeIds[] = $orderDispatchType->getId();
- }
- }
- }
- return $dispatchTypeIds;
- }
- public function getDispatchTypeIds()
- {
- return explode(',', $this->dispatch_type_ids) ?: [];
- }
- /**
- * 返回勾选并开启的配送方式ID
- * @return array|static
- */
- public function getEnableDispatchTypeIds()
- {
- return DispatchType::goodsEnableDispatchTypeIds($this->getDispatchTypeIds());
- }
- public function dispatchTypesSettingV2($plugin=0)
- {
- //$dispatchTypes = DispatchType::where('plugin', 0)->where('enable', 1)->get();
- $dispatchTypes = DispatchType::getCurrentUniacidSet($plugin);
- //过滤未开启的
- $dispatchTypes = $dispatchTypes->filter(function ($dispatchType) {
- return $dispatchType->enable == 1;
- })->values();
- if ($dispatchTypes->isEmpty()) {
- return [];
- }
- if (isset($this->goods_id)) {
- if(!is_array($this->dispatch_type_ids)){
- $this->dispatch_type_ids = explode(',', $this->dispatch_type_ids) ?: [];
- $this->dispatch_type_ids = $this->dispatch_type_ids ?: [];
- }
- $dispatchTypes->map(function ($dispatchType) {
- if (in_array($dispatchType->id, $this->dispatch_type_ids)) {
- $dispatchType->enable = 1;
- }else{
- $dispatchType->enable = 0;
- }
- });
- }
- return $dispatchTypes->toArray() ?: [];
- }
- public function dispatchTypesSetting()
- {
- $dispatchTypes = DispatchType::where('plugin', 0)->get()->toArray();
- $dispatchTypesSetting = DispatchType::dispatchTypesSetting($dispatchTypes);
- if (isset($this->goods_id)) {
- if(!is_array($this->dispatch_type_ids)){
- $this->dispatch_type_ids = explode(',', $this->dispatch_type_ids) ?: [];
- $this->dispatch_type_ids = $this->dispatch_type_ids ?: [];
- }
- foreach ($dispatchTypesSetting as &$dispatchType) {
- if (in_array($dispatchType['id'], $this->dispatch_type_ids)) {
- $dispatchType['enable'] = 1;
- }else{
- $dispatchType['enable'] = 0;
- }
- }
- }
- return $dispatchTypesSetting ?: [];
- }
- }
|