| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace addons\Happiness\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsCate;
- use common\models\goods\GoodsCateRelate;
- use common\models\user\User;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_clerk".
- */
- class HappinessStoreGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_happiness_store_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'store_id', 'sales_num', 'is_on_sale', 'created_at', 'updated_at', 'status', 'stock'], 'integer'],
- [['freight_type'], 'string']
- ];
- }
- public function getGood()
- {
- return $this->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;
- }
- }
|