GoodsDispatch.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\common\models\goods;
  3. use app\common\models\BaseModel;
  4. use app\common\models\DispatchType;
  5. use app\frontend\modules\order\models\PreOrder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Contracts\Validation\Validator;
  8. use app\backend\modules\goods\observers\GoodsDispatchObserver;
  9. /**
  10. * Created by PhpStorm.
  11. * Author: 芸众商城 www.yunzshop.com
  12. * Date: 2017/2/22
  13. * Time: 下午5:54
  14. */
  15. class GoodsDispatch extends BaseModel
  16. {
  17. public $table = 'yz_goods_dispatch';
  18. const UNIFY_TYPE = 1;
  19. const TEMPLATE_TYPE = 0;
  20. /**
  21. * 不可填充字段.
  22. *
  23. * @var array
  24. */
  25. protected $guarded = ['created_at', 'updated_at'];
  26. public function __construct(array $attributes = [])
  27. {
  28. parent::__construct($attributes);
  29. }
  30. /**
  31. * 自定义显示错误信息
  32. * @return array
  33. */
  34. public static function getDispatchInfo($goodsId)
  35. {
  36. $dispatchInfo = self::where('goods_id', $goodsId)
  37. ->first();
  38. return $dispatchInfo;
  39. }
  40. /**
  41. * 自定义字段名
  42. * 可使用
  43. * @return array
  44. */
  45. public function atributeNames()
  46. {
  47. return [
  48. 'dispatch_type' => '配送方式',
  49. 'dispatch_price' => '统一配送价格',
  50. 'dispatch_id' => '配送模板',
  51. //'is_cod' => '是否支持货到付款',
  52. ];
  53. }
  54. public function rules()
  55. {
  56. return [
  57. 'dispatch_type' => 'required|integer|min:0|max:1',
  58. 'dispatch_price' => 'numeric|min:0',
  59. 'dispatch_id' => 'integer',
  60. //'is_cod' => 'required|integer|min:0|max:1',
  61. ];
  62. }
  63. public static function boot()
  64. {
  65. parent::boot();
  66. //注册观察者
  67. static::observe(new GoodsDispatchObserver);
  68. }
  69. public function enableDispatchTypeIds()
  70. {
  71. $dispatchTypeIds = [];
  72. $configs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.order-delivery-method');
  73. if ($configs) {
  74. $dispatchTypesSetting = $this->dispatchTypesSetting();
  75. foreach ($configs as $dispatchTypeCode => $dispatchTypeClass) {
  76. if (!class_exists($dispatchTypeClass)) {
  77. break;
  78. }
  79. $dispatchTypeSetting = $dispatchTypesSetting[$dispatchTypeCode];
  80. $orderDispatchType = new $dispatchTypeClass($dispatchTypeSetting);
  81. if ($orderDispatchType->enable()) {
  82. $dispatchTypeIds[] = $orderDispatchType->getId();
  83. }
  84. }
  85. }
  86. return $dispatchTypeIds;
  87. }
  88. public function getDispatchTypeIds()
  89. {
  90. return explode(',', $this->dispatch_type_ids) ?: [];
  91. }
  92. /**
  93. * 返回勾选并开启的配送方式ID
  94. * @return array|static
  95. */
  96. public function getEnableDispatchTypeIds()
  97. {
  98. return DispatchType::goodsEnableDispatchTypeIds($this->getDispatchTypeIds());
  99. }
  100. public function dispatchTypesSettingV2($plugin=0)
  101. {
  102. //$dispatchTypes = DispatchType::where('plugin', 0)->where('enable', 1)->get();
  103. $dispatchTypes = DispatchType::getCurrentUniacidSet($plugin);
  104. //过滤未开启的
  105. $dispatchTypes = $dispatchTypes->filter(function ($dispatchType) {
  106. return $dispatchType->enable == 1;
  107. })->values();
  108. if ($dispatchTypes->isEmpty()) {
  109. return [];
  110. }
  111. if (isset($this->goods_id)) {
  112. if(!is_array($this->dispatch_type_ids)){
  113. $this->dispatch_type_ids = explode(',', $this->dispatch_type_ids) ?: [];
  114. $this->dispatch_type_ids = $this->dispatch_type_ids ?: [];
  115. }
  116. $dispatchTypes->map(function ($dispatchType) {
  117. if (in_array($dispatchType->id, $this->dispatch_type_ids)) {
  118. $dispatchType->enable = 1;
  119. }else{
  120. $dispatchType->enable = 0;
  121. }
  122. });
  123. }
  124. return $dispatchTypes->toArray() ?: [];
  125. }
  126. public function dispatchTypesSetting()
  127. {
  128. $dispatchTypes = DispatchType::where('plugin', 0)->get()->toArray();
  129. $dispatchTypesSetting = DispatchType::dispatchTypesSetting($dispatchTypes);
  130. if (isset($this->goods_id)) {
  131. if(!is_array($this->dispatch_type_ids)){
  132. $this->dispatch_type_ids = explode(',', $this->dispatch_type_ids) ?: [];
  133. $this->dispatch_type_ids = $this->dispatch_type_ids ?: [];
  134. }
  135. foreach ($dispatchTypesSetting as &$dispatchType) {
  136. if (in_array($dispatchType['id'], $this->dispatch_type_ids)) {
  137. $dispatchType['enable'] = 1;
  138. }else{
  139. $dispatchType['enable'] = 0;
  140. }
  141. }
  142. }
  143. return $dispatchTypesSetting ?: [];
  144. }
  145. }