| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace addons\Scratch\common\models;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Scratch\common\enums\ScratchEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\mall\MallAddons;
- /**
- *
- * @property int $id
- * @property int $mall_id
- * @property string $name
- * @property string $pic
- * @property int $type 1.红包2.余额3.优惠卷4.积分5.实物
- * @property int $status 状态 -1 删除 0 关闭 1开启
- * @property string $goods_id 商品
- * @property string $attr_id 商品
- * @property int $num 积分数量
- * @property string $price 红包价格
- * @property int $coupon_id 优惠券
- * @property int $stock 库存
- * @property int $balance 余额
- * @property int $created_at
- * @property int $updated_at
- * @property Goods $goods
- */
- class ScratchModel extends BaseActiveRecord
- {
- const TYPE_REDPACKET = 1;
- const TYPE_BALANCE = 2;
- const TYPE_COUPON = 3;
- const TYPE_SCORE = 4;
- const TYPE_GOODS = 5;
- const TYPE_THANKS = 6;
- public static $typeList = [
- self::TYPE_REDPACKET => "红包",
- self::TYPE_BALANCE => "余额",
- self::TYPE_COUPON => "优惠券",
- self::TYPE_SCORE => "积分",
- self::TYPE_GOODS => "商品",
- ];
- const STATUS_ON = 1;
- const STATUS_OFF = 0;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_scratch}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'type', 'status'], 'required'],
- [['mall_id', 'type', 'status', 'num', 'coupon_id', 'stock', 'goods_id', 'attr_id'], 'integer'],
- [['price', 'balance'], 'number'],
- [['name', 'pic'], 'string'],
- [['created_at', 'updated_at'], 'safe'],
- [['stock', 'coupon_id', 'price', 'num', 'goods_id', 'attr_id'], 'default', 'value' => 0],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'type' => '1.红包2.余额3.优惠卷4.积分5.实物',
- 'status' => '状态-1 删除 0 关闭 1开启',
- 'goods_id' => '商品id',
- 'attr_id' => '规格id',
- 'pic' => '奖品图标',
- 'name' => '奖品名称',
- 'num' => '积分数量',
- 'balance' => '余额',
- 'price' => '红包价格',
- 'coupon_id' => '优惠券',
- 'stock' => '库存',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getCoupon()
- {
- return $this->hasOne(AddonsCoupon::className(), ['id' => 'coupon_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getGoods()
- {
- return $this->hasOne(Goods::className(), ['id' => 'goods_id']);
- }
- /**
- * 获取类型
- * @param $mall_id
- * @return array
- */
- public static function getTypeList($mall_id)
- {
- $typeList = self::$typeList;
- return $typeList;
- }
- /**
- * @param $mallId
- * @param $params
- * @return ScratchModel
- * @throws \Exception
- */
- public static function setData($mallId, $params)
- {
- try {
- $id = $params['id'] ?? 0;
- $data = self::findOne([
- 'id' => $id,
- 'status' => ScratchEnum::ENABLED,
- ]);
- if (!empty($data)) {
- return $data;
- }
- $model = new self();
- $model->mall_id = $mallId;
- $model->name = $params['name'] ?? '';
- $model->pic = $params['pic'] ?? '';
- $model->type = $params['type'] ?? '';
- $model->goods_id = $params['goods_id'] ?? 0;
- $model->num = $params['num'] ?? 0;
- $model->price = $params['price'] ?? 0;
- $model->coupon_id = $params['coupon_id'] ?? 0;
- $model->stock = $params['stock'] ?? 0;
- $model->balance = $params['balance'] ?? 0;
- $model->status = $params['status'] ?? 1;
- return $model;
- } catch (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- }
- }
- }
|