| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace addons\CloudStockTwo\common\models;
- use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_cloud_stock_agent_goods".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $goods_id 商品id
- * @property int $num 库存数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CloudStockTwoAgentGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_two_agent_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'goods_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['goods_id'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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;
- }
- }
|