| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace addons\Coupon\common\models;
- use common\enums\StatusEnum;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_coupon_store_goods_relate".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $coupon_id 优惠券id
- * @property int $goods_id 门店商品id
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class CouponStoreGoodsRelate extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_coupon_store_goods_relate';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'status', 'coupon_id', 'goods_id', 'created_at', 'updated_at'], 'integer'],
- [['coupon_id', 'goods_id'], 'required'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'status' => 'Status',
- 'coupon_id' => 'Coupon ID',
- 'goods_id' => 'Goods ID',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- *
- * @param $coupon_id
- * @param $mall_id
- * @param $goods_id
- *
- * @return bool
- * @throws \yii\db\Exception
- */
- public static function setData($coupon_id, $mall_id, $goods_id)
- {
- try {
- if ($coupon_id && $goods_id) {
- self::deleteAll([
- 'and',
- [
- 'coupon_id' => $coupon_id,
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $mall_id
- ],
- ['not in', 'goods_id', $goods_id]
- ]);
- $exist_goods_ids = self::find()
- ->select('goods_id')
- ->where([
- 'mall_id' => $mall_id,
- 'goods_id' => $goods_id,
- 'coupon_id' => $coupon_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->column();
- $insert = [];
- $now = time();
- foreach ($goods_id as $id) {
- if (in_array($id, $exist_goods_ids)) continue;
- $insert[] = [$mall_id, $coupon_id, $id, $now, $now];
- }
- $field = ['mall_id', '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;
- }
- }
- }
|