| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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 HappinessStoreGoodsAttr extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_happiness_store_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'store_id', 'attr_id', 'stock', 'created_at', 'updated_at', 'status', 'yun_goods_id', 'is_on_sale'], 'integer'],
- ];
- }
- /**
- * @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['id']) && !empty($params['store_id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'id' => $params['id'], 'status' => StatusEnum::ENABLED]);
- } else if(!empty($params['mall_id']) && !empty($params['goods_id'])&&!empty($params['attr_id']) && !empty
- ($params['store_id']))
- {
- $model = self::findOne(['mall_id' => $params['mall_id'],'attr_id' => $params['attr_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;
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function handleStock($goods_attr_id,$num, $store_id){
- try{
- $model = self::findOne(['attr_id'=>$goods_attr_id,'status'=>StatusEnum::ENABLED, 'store_id' => $store_id, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]);
- if(empty($model)) throw new \Exception('规格不存在');
- if(!is_int($num)) throw new \Exception('库存数量必须为整数');
- if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
- $model->stock += $num;
- $res = $model->save();
- if($res === false) throw new \Exception($model->getErrorMessage());
- return true;
- }catch(\Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public function getAttr(){
- return $this->hasOne(GoodsAttr::class,['id'=>'attr_id']);
- }
- public function getGoods(){
- return $this->hasOne(Goods::class,['id'=>'goods_id']);
- }
- }
|