| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace addons\Coupon\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_coupon_give_item_setting_detail".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $coupon_give_item_setting_id 设置id
- * @property int $coupon_id 优惠券id
- * @property int $num 赠送数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CouponGiveItemSettingDetail extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_coupon_give_item_setting_detail}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'coupon_give_item_setting_id', 'coupon_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'coupon_give_item_setting_id' => '设置id',
- 'coupon_id' => '优惠券id',
- 'num' => '赠送数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getCoupon()
- {
- return $this->hasOne(AddonsCoupon::class, ['id' => 'coupon_id'])->where(['status' => StatusEnum::ENABLED]);
- }
- public static function setData($params, $data)
- {
- try{
- $coupon_id_arr = array_column($data,'coupon_id');
- self::updateAll(['status'=>StatusEnum::DELETE],[
- 'and',
- ['coupon_give_item_setting_id'=>$params['coupon_give_item_setting_id']],
- ['not in','coupon_id',$coupon_id_arr],
- ]);
- foreach ($data as $item) {
- $model = self::findOne([
- 'mall_id' => $params['mall_id'],
- 'coupon_id' => $item['coupon_id'],
- 'coupon_give_item_setting_id' => $params['coupon_give_item_setting_id'],
- 'status' => [StatusEnum::ENABLED]
- ]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->coupon_give_item_setting_id = $params['coupon_give_item_setting_id'];
- $model->loadDefaultValues();
- }
- if(!$model->load($item,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- }
- return true;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|