hasOne(Goods::class, ['id' => 'goods_id']); } public function getGoods() { return $this->hasMany(Goods::class, ['id' => 'goods_id']); } // 分类关系 public function getGoodsCateRelate() { return $this->hasMany(GoodsCateRelate::class, ['goods_id' => 'goods_id']); } // 分类 public function getCats() { return $this->hasMany(GoodsCate::class, ['id' => 'cate_id']) // ->where(['mch_id' => 0]) ->via('goodsCateRelate'); } public function getAttr() { return $this->hasMany(GoodsAttr::class, ['goods_id' => 'goods_id'])->select('id,goods_id,name,price')->where(['is_show'=> 1, 'status' => 1])->indexBy('id'); } public function getAttrs() { return $this->hasMany(GoodsAttr::class, ['goods_id' => 'goods_id'])->where(['is_show'=> 1, 'status' => 1]); } public function getYunAttr() { return $this->hasMany(HappinessStoreGoodsAttr::class, ['yun_goods_id' => 'id'])->where(['status' => 1]) ->indexBy('attr_id'); } public function getYunGoods() { return $this->hasOne(HappinessGoods::class, ['goods_id' => 'goods_id'])->where(['is_on_sale' => 1, 'status' => 1]); } /** * @desc:保存数据 * @Author: hua * @Date: 2021/10/23 * @Time: 18:07 * @Copyright: copyright (c) 2021 广东七件事集团 * @param array $params * @return StoreClerk|false|null */ public static function setData(array $params) { try { if (!empty($params['mall_id']) && (!empty($params['goods_id']) || !empty($params['id'])) && !empty($params['store_id'])) { if (!empty($params['id'])) { $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'id' => $params['id'], 'status' => StatusEnum::ENABLED]); } else { $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]); } if (empty($model)) { $model = new self(); $model->loadDefaultValues(); } else { $params['stock']=$params['stock']+$model->stock; } } else { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); if (isset($params['is_on_sale']) && !empty($model->goods_id) && !empty($model->store_id)) { HappinessStoreGoodsAttr::updateAll(['is_on_sale' => $params['is_on_sale']], ['mall_id' => $params['mall_id'], 'goods_id' => $model->goods_id, 'store_id' => $params['store_id']]); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } public static function computeMemberPrice($price, $sign_id, $user_level, $is_attr, $is_member_price, $member_price_setting, $discount) { $after_price = $price; switch ($is_member_price) { case 0: $after_price = bcdiv($price * $discount, 10, 2); break; case 1: //单独设置会员价 if (!empty($is_attr)) { //多规格 if (isset($member_price_setting['level' . $user_level][$sign_id])) $member_price_discount = $member_price_setting['level' . $user_level][$sign_id]; } else { //默认规格 if (isset($member_price_setting[$user_level])) $member_price_discount = $member_price_setting[$user_level]; } if (!empty($member_price_discount)) { $after_price = 0; if ($price >= $member_price_discount) $after_price = bcmul($member_price_discount, 1, 2); } break; } return $after_price; } }