ScratchModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\Scratch\common\models;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Scratch\common\enums\ScratchEnum;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\goods\Goods;
  7. use common\models\mall\MallAddons;
  8. /**
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property string $name
  13. * @property string $pic
  14. * @property int $type 1.红包2.余额3.优惠卷4.积分5.实物
  15. * @property int $status 状态 -1 删除 0 关闭 1开启
  16. * @property string $goods_id 商品
  17. * @property string $attr_id 商品
  18. * @property int $num 积分数量
  19. * @property string $price 红包价格
  20. * @property int $coupon_id 优惠券
  21. * @property int $stock 库存
  22. * @property int $balance 余额
  23. * @property int $created_at
  24. * @property int $updated_at
  25. * @property Goods $goods
  26. */
  27. class ScratchModel extends BaseActiveRecord
  28. {
  29. const TYPE_REDPACKET = 1;
  30. const TYPE_BALANCE = 2;
  31. const TYPE_COUPON = 3;
  32. const TYPE_SCORE = 4;
  33. const TYPE_GOODS = 5;
  34. const TYPE_THANKS = 6;
  35. public static $typeList = [
  36. self::TYPE_REDPACKET => "红包",
  37. self::TYPE_BALANCE => "余额",
  38. self::TYPE_COUPON => "优惠券",
  39. self::TYPE_SCORE => "积分",
  40. self::TYPE_GOODS => "商品",
  41. ];
  42. const STATUS_ON = 1;
  43. const STATUS_OFF = 0;
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public static function tableName()
  48. {
  49. return '{{%addons_scratch}}';
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function rules()
  55. {
  56. return [
  57. [['mall_id', 'type', 'status'], 'required'],
  58. [['mall_id', 'type', 'status', 'num', 'coupon_id', 'stock', 'goods_id', 'attr_id'], 'integer'],
  59. [['price', 'balance'], 'number'],
  60. [['name', 'pic'], 'string'],
  61. [['created_at', 'updated_at'], 'safe'],
  62. [['stock', 'coupon_id', 'price', 'num', 'goods_id', 'attr_id'], 'default', 'value' => 0],
  63. ];
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'id' => 'ID',
  72. 'mall_id' => 'Mall ID',
  73. 'type' => '1.红包2.余额3.优惠卷4.积分5.实物',
  74. 'status' => '状态-1 删除 0 关闭 1开启',
  75. 'goods_id' => '商品id',
  76. 'attr_id' => '规格id',
  77. 'pic' => '奖品图标',
  78. 'name' => '奖品名称',
  79. 'num' => '积分数量',
  80. 'balance' => '余额',
  81. 'price' => '红包价格',
  82. 'coupon_id' => '优惠券',
  83. 'stock' => '库存',
  84. 'created_at' => 'Created At',
  85. 'updated_at' => 'Updated At',
  86. ];
  87. }
  88. /**
  89. * @return \yii\db\ActiveQuery
  90. */
  91. public function getCoupon()
  92. {
  93. return $this->hasOne(AddonsCoupon::className(), ['id' => 'coupon_id']);
  94. }
  95. /**
  96. * @return \yii\db\ActiveQuery
  97. */
  98. public function getGoods()
  99. {
  100. return $this->hasOne(Goods::className(), ['id' => 'goods_id']);
  101. }
  102. /**
  103. * 获取类型
  104. * @param $mall_id
  105. * @return array
  106. */
  107. public static function getTypeList($mall_id)
  108. {
  109. $typeList = self::$typeList;
  110. return $typeList;
  111. }
  112. /**
  113. * @param $mallId
  114. * @param $params
  115. * @return ScratchModel
  116. * @throws \Exception
  117. */
  118. public static function setData($mallId, $params)
  119. {
  120. try {
  121. $id = $params['id'] ?? 0;
  122. $data = self::findOne([
  123. 'id' => $id,
  124. 'status' => ScratchEnum::ENABLED,
  125. ]);
  126. if (!empty($data)) {
  127. return $data;
  128. }
  129. $model = new self();
  130. $model->mall_id = $mallId;
  131. $model->name = $params['name'] ?? '';
  132. $model->pic = $params['pic'] ?? '';
  133. $model->type = $params['type'] ?? '';
  134. $model->goods_id = $params['goods_id'] ?? 0;
  135. $model->num = $params['num'] ?? 0;
  136. $model->price = $params['price'] ?? 0;
  137. $model->coupon_id = $params['coupon_id'] ?? 0;
  138. $model->stock = $params['stock'] ?? 0;
  139. $model->balance = $params['balance'] ?? 0;
  140. $model->status = $params['status'] ?? 1;
  141. return $model;
  142. } catch (\Exception $exception) {
  143. throw new \Exception($exception->getMessage());
  144. }
  145. }
  146. }