'ID', 'mall_id' => '商城id', 'user_id' => '用户id', 'goods_id' => '商品id', 'num' => '库存数量', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']); } public static function setData(int $mall_id, array $data) { if (!empty($data['id'])) { $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]); } else { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $mall_id; } foreach ($data as $key => $val) { $model->$key = $val; } // dd($model); if (!$model->save()) { self::$static_error = '保存数据失败:' . $model->getErrorMessage(); return false; } return $model; } public static function addData(array $params) { try { if (!empty($params['mall_id']) && !empty($params['user_id']) && !empty($params['goods_id'])) { $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) { $model = new self(); $model->loadDefaultValues(); if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); } else { $model->num += $params['num']; } } else { $model = new self(); $model->loadDefaultValues(); if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); } if (!$model->save()) throw new \Exception($model->getErrorMessage()); $param = [ 'mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'user_id' => $params['user_id'], 'changes_dir' => $params['changes_dir'], 'order_id' => $params['order_id'], 'num' => $params['num'], 'order_type' => $params['order_type'], 'changes_type' => $params['changes_type'], ]; $stock_log = CloudStockTwoGoodsStockLog::setData($param['mall_id'],$param); if ($stock_log == false) throw new Exception(CloudStockTwoGoodsStockLog::getStaticError()); $param = [ 'stock_log_id' => $stock_log->id, 'user_id' => $params['user_id'], 'num' => $params['num'], 'changes_type' => $params['changes_type'], 'goods_id' => $params['goods_id'], 'mall_id' => $params['mall_id'], 'order_id' => $params['order_id'], ]; $increase_detail = CloudStockTwoIncreaseDetail::setData($param); if ($increase_detail == false) throw new Exception(CloudStockTwoIncreaseDetail::getStaticError()); return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } //获取指定等级库存商品价格 public static function getGoodsAgentPrice($agent_price, $level) { if (isset($agent_price['agent_price'])) { $agent_price = \Qiniu\json_decode($agent_price['agent_price'], true); foreach ($agent_price as $value) { if ($value['level'] == $level) { return $value['price']; } } } return 0; } }