255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城ID', 'is_enable' => '是否独立设置:0:否;1是', 'item_id' => '项目id', 'item_type' => '项目类型:商品goods', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public function getDetail() { return $this->hasMany(CouponGiveItemSettingDetail::class, ['coupon_give_item_setting_id' => 'id']); } public static function setData($params, $data) { try { $model = self::findOne([ 'mall_id' => $params['mall_id'], 'item_type' => $data['item_type'], 'item_id' => $params['item_id'], 'status' => [StatusEnum::ENABLED] ]); if (empty($model)) { $model = new self(); $model->mall_id = $params['mall_id']; $model->item_id = $params['item_id']; $model->loadDefaultValues(); } $data['mall_id'] = $params['mall_id']; $data['item_id'] = $params['item_id']; $data['send_type'] = $params['send_type']; isset($params['is_enable']) && $data['is_enable'] = $params['is_enable']; if (!$model->load($data, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } // 插入配置详情 $detail = ['mall_id' => $data['mall_id'], 'coupon_give_item_setting_id' => $model->id]; $res = CouponGiveItemSettingDetail::setData($detail, $data['detail']); if ($res == false) throw new \Exception(CouponGiveItemSettingDetail::getStaticError()); return true; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 商品设置优惠券 * @Author: lun * @DateTime: 2022/3/9 0009 10:55 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $goods_id * @param $coupons * @return bool */ public static function setGoodsSetting($mall_id, $goods_id, $coupons) { try { $base_params = ['mall_id' => $mall_id, 'item_id' => $goods_id, 'is_enable' => $coupons['is_enable'], 'send_type' => $coupons['send_type']]; // 获取有效的优惠券 $coupon_arr = []; foreach ($coupons['multiple_selection'] as $key => $val) { if ($val['num'] > 0) { $coupon_arr[] = [ 'coupon_id' => $val['relevant_id'], 'num' => $val['num'], ]; } } $coupon_data = ['detail' => $coupon_arr, 'item_id' => $goods_id, 'item_type' => 'goods']; $res = CouponGiveItemSetting::setData($base_params, $coupon_data); if ($res == false) throw new \Exception(CouponGiveItemSetting::getStaticError()); return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } }