| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace addons\Store\common\models;
- use common\models\base\BaseActiveRecord;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_coupon_goods_relate".
- *
- * @property int $id
- * @property int $coupon_id 优惠券id
- * @property int $goods_id 商品id
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class StoreCouponGoodsRelate extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_coupon_goods_relate}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['coupon_id', 'goods_id'], 'required'],
- [['coupon_id', 'goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'coupon_id' => '优惠券id',
- 'goods_id' => '商品id',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- /**
- * 保存数据
- * @param $coupon_id
- * @param $goods_id
- * @return bool
- * @throws \yii\db\Exception
- */
- public static function setData($coupon_id, $goods_id)
- {
- try {
- if ($coupon_id && $goods_id) {
- $count = self::deleteAll([
- 'and',
- ['coupon_id' => $coupon_id],
- ['not in', 'goods_id', $goods_id]
- ]);
- $exist_goods_ids = self::find()->select('goods_id')
- ->where(['goods_id' => $goods_id,'coupon_id' => $coupon_id])
- ->column();
- $insert = [];
- $now = time();
- foreach ($goods_id as $key => $id) {
- if (in_array($id, $exist_goods_ids)) continue;
- $insert[] = [$coupon_id, $id, $now, $now];
- }
- $field = ['coupon_id', 'goods_id', 'created_at', 'updated_at'];
- // 批量插入数据
- Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- }
- return true;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|