CloudStockTwoAgentGoods.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "{{%addons_cloud_stock_agent_goods".
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城id
  14. * @property int $user_id 用户id
  15. * @property int $goods_id 商品id
  16. * @property int $num 库存数量
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class CloudStockTwoAgentGoods extends BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_cloud_stock_two_agent_goods}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_id', 'goods_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['goods_id'], 'required'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城id',
  48. 'user_id' => '用户id',
  49. 'goods_id' => '商品id',
  50. 'num' => '库存数量',
  51. 'status' => '状态[-1:删除;0:禁用;1启用]',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public function getGoods()
  57. {
  58. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  59. }
  60. public static function setData(int $mall_id, array $data)
  61. {
  62. if (!empty($data['id'])) {
  63. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  64. } else {
  65. $model = new self();
  66. $model->loadDefaultValues();
  67. $model->mall_id = $mall_id;
  68. }
  69. foreach ($data as $key => $val) {
  70. $model->$key = $val;
  71. }
  72. // dd($model);
  73. if (!$model->save()) {
  74. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  75. return false;
  76. }
  77. return $model;
  78. }
  79. public static function addData(array $params)
  80. {
  81. try {
  82. if (!empty($params['mall_id']) && !empty($params['user_id']) && !empty($params['goods_id'])) {
  83. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  84. if (empty($model)) {
  85. $model = new self();
  86. $model->loadDefaultValues();
  87. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  88. } else {
  89. $model->num += $params['num'];
  90. }
  91. } else {
  92. $model = new self();
  93. $model->loadDefaultValues();
  94. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  95. }
  96. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  97. $param = [
  98. 'mall_id' => $params['mall_id'],
  99. 'goods_id' => $params['goods_id'],
  100. 'user_id' => $params['user_id'],
  101. 'changes_dir' => $params['changes_dir'],
  102. 'order_id' => $params['order_id'],
  103. 'num' => $params['num'],
  104. 'order_type' => $params['order_type'],
  105. 'changes_type' => $params['changes_type'],
  106. ];
  107. $stock_log = CloudStockTwoGoodsStockLog::setData($param['mall_id'],$param);
  108. if ($stock_log == false) throw new Exception(CloudStockTwoGoodsStockLog::getStaticError());
  109. $param = [
  110. 'stock_log_id' => $stock_log->id,
  111. 'user_id' => $params['user_id'],
  112. 'num' => $params['num'],
  113. 'changes_type' => $params['changes_type'],
  114. 'goods_id' => $params['goods_id'],
  115. 'mall_id' => $params['mall_id'],
  116. 'order_id' => $params['order_id'],
  117. ];
  118. $increase_detail = CloudStockTwoIncreaseDetail::setData($param);
  119. if ($increase_detail == false) throw new Exception(CloudStockTwoIncreaseDetail::getStaticError());
  120. return $model;
  121. } catch (\Exception $e) {
  122. self::$static_error = $e->getMessage();
  123. return false;
  124. }
  125. }
  126. //获取指定等级库存商品价格
  127. public static function getGoodsAgentPrice($agent_price, $level)
  128. {
  129. if (isset($agent_price['agent_price'])) {
  130. $agent_price = \Qiniu\json_decode($agent_price['agent_price'], true);
  131. foreach ($agent_price as $value) {
  132. if ($value['level'] == $level) {
  133. return $value['price'];
  134. }
  135. }
  136. }
  137. return 0;
  138. }
  139. }