| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 门店
- * @property int $is_enable 是否独立设置:0:否;1是
- * @property int $item_id 项目id
- * @property string $item_type 项目类型:商品goods
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class StoreCouponGiveItemSetting extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_coupon_give_item_setting}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id'], 'required'],
- [['mall_id', 'store_id', 'is_enable', 'item_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['item_type'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'is_enable' => 'Is Enable',
- 'item_id' => 'Item ID',
- 'item_type' => 'Item Type',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getDetail()
- {
- return $this->hasMany(StoreCouponGiveItemSettingDetail::class, ['coupon_give_item_setting_id' => 'id']);
- }
- public static function setData($params, $data)
- {
- try {
- $model = self::findOne([
- 'mall_id' => $params['mall_id'],
- 'store_id' => $params['store_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->store_id = $params['store_id'];
- $model->item_id = $params['item_id'];
- $model->loadDefaultValues();
- }
- $data['mall_id'] = $params['mall_id'];
- $data['store_id'] = $params['store_id'];
- $data['item_id'] = $params['item_id'];
- isset($params['is_enable']) && $data['is_enable'] = $params['is_enable'];
- if (!$model->load($data, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- // 插入配置详情
- if($data['detail']){
- foreach ($data['detail'] as $from_type=>$value){
- $detail = ['mall_id' => $data['mall_id'],'from_type'=>$from_type, 'store_id' => $data['store_id'], 'coupon_give_item_setting_id' => $model->id];
- $res = StoreCouponGiveItemSettingDetail::setData($detail,$value);
- if ($res == false) throw new \Exception(StoreCouponGiveItemSettingDetail::getStaticError());
- }
- }else{
- //删除优惠券
- StoreCouponGiveItemSettingDetail::updateAll(['status'=>StatusEnum::DELETE],[
- 'and',
- ['coupon_give_item_setting_id'=> $model->id],
- ['store_id'=>$params['store_id']],
- ]);
- }
- 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 $store_id
- * @param $params
- * @return bool
- */
- public static function setGoodsSetting($mall_id,$store_id,$goods_id, $params)
- {
- try {
- $base_params = ['mall_id' => $mall_id, 'item_id' => $goods_id,'store_id' => $store_id, 'is_enable' => $params['coupon_is_enable']];
- // 获取有效的优惠券
- $coupon_arr = [];
- if (!empty($params['coupon_selection'])) {
- foreach ($params['coupon_selection'] as $key => $val) {
- if ($val['num'] > 0) {
- $coupon_arr[$val['from_type']][] = [
- 'coupon_id' => $val['coupon_id'],
- 'num' => $val['num'],
- 'from_type' => $val['from_type'],
- ];
- }
- }
- }
- $coupon_data = ['detail' => $coupon_arr, 'store_id'=>$store_id,'item_id' => $goods_id, 'item_type' => 'goods'];
- $res = StoreCouponGiveItemSetting::setData($base_params, $coupon_data);
- if ($res == false) throw new \Exception(StoreCouponGiveItemSetting::getStaticError());
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|