"红包", 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()); } } }