| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_coupon_give_item_setting_detail".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 商城ID
- * @property int $coupon_give_item_setting_id 设置id
- * @property int $coupon_id 优惠券id
- * @property int $from_type 类型:1:平台优惠券;2:门店优惠券
- * @property int $num 赠送数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class StoreCouponGiveItemSettingDetail extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_coupon_give_item_setting_detail}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'from_type'], 'required'],
- [['mall_id', 'store_id', 'coupon_give_item_setting_id', 'coupon_id', 'from_type', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'coupon_give_item_setting_id' => 'Coupon Give Item Setting ID',
- 'coupon_id' => 'Coupon ID',
- 'from_type' => 'From Type',
- 'num' => 'Num',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getCoupon()
- {
- return $this->hasOne(StoreCoupon::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']],
- ['from_type'=>$params['from_type']],
- ['not in','coupon_id',$coupon_id_arr],
- ]);
- foreach ($data as $item) {
- $where = [
- 'mall_id' => $params['mall_id'],
- 'coupon_id' => $item['coupon_id'],
- 'store_id' => $params['store_id'],
- 'from_type' => $params['from_type'],
- 'coupon_give_item_setting_id' => $params['coupon_give_item_setting_id'],
- 'status' => [StatusEnum::ENABLED]];
- $model = self::findOne($where);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->store_id = $params['store_id'];
- $model->from_type = $params['from_type'];
- $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;
- }
- }
- }
|