CloudStockTwoAgentQuota.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_cloud_stock_agent_quota".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $user_id 代理商用户id
  13. * @property int $level 等级权重
  14. * @property int $num 名额数量
  15. * @property int $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at
  17. * @property int $updated_at
  18. */
  19. class CloudStockTwoAgentQuota extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_cloud_stock_two_agent_quota}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'user_id', 'level', 'num'], 'required'],
  35. [['mall_id', 'user_id', 'level', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => '商城id',
  46. 'user_id' => '代理商用户id',
  47. 'level' => '等级权重',
  48. 'num' => '名额数量',
  49. 'status' => '状态[-1:删除;0:禁用;1启用]',
  50. 'created_at' => 'Created At',
  51. 'updated_at' => 'Updated At',
  52. ];
  53. }
  54. public function getStockLevel()
  55. {
  56. return $this->hasOne(CloudStockTwoLevel::class, ['level' => 'level', 'mall_id' => 'mall_id'])->where(['status'=>StatusEnum::ENABLED]);
  57. }
  58. public static function setData($params)
  59. {
  60. try {
  61. if (!empty($params['mall_id']) && !empty($params['user_id']) && !empty($params['level'])) {
  62. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'level' => $params['level'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  63. if (empty($model)) {
  64. $model = new self();
  65. $model->loadDefaultValues();
  66. }
  67. } else {
  68. $model = new self();
  69. $model->loadDefaultValues();
  70. }
  71. $model->num += $params['num'];
  72. unset($params['num']);
  73. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  74. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  75. return true;
  76. } catch (\Exception $e) {
  77. self::$static_error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. }