| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace addons\ElectronCoupon\common\models;
- use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_electron_coupon}}".
- *
- * @property int $id 自增id
- * @property int|null $mall_id 商城id
- * @property string $name 电子券名称
- * @property int $amount 发行数量;单张券为张
- * @property int $receive_times 最多领取次数
- * @property int $unlimit_receive_times 领取次数不限制
- * @property int $pieces_type 1单张2多张;
- * @property int $pieces_amount 多张数量;单位张
- * @property int $receive_codition 领取条件;1全体会员;2指定会员等级;
- * @property int|null $receive_member_level 指定会员等级;默认为null
- * @property int|null $receive_begin_time 领取开始时间
- * @property int|null $receive_end_time 领取结束时间
- * @property int|null $use_begin_time 使用开始时间
- * @property int|null $use_end_time 使用结束时间
- * @property float $price 激活金额
- * @property float $reward_money 核销奖励金额
- * @property int $use_way 适用范围;1扫描核销
- * @property int $is_can_receive 是否可以被领取
- * @property int $is_show_in_center 是否出现在领取中心
- * @property string|null $rules 电子券规则
- * @property string|null $pic 缩略图
- * @property int|null $no_receive_amount 未领取数量
- * @property int $receive_amount 领取数量;单张券为张;
- * @property int $used_amount 兑换数量;单位张或多张
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property int $sort 排序;由大到小
- * @property int $is_give 是否赠送电子券
- * @property int $is_proper 是否专有券
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property float $give_score 赠送积分
- * @property float $give_price 赠送佣金
- * @property float $give_store_price 核销门店奖励
- * @property int $share_expiry 分享有效期,单位小时
- * @property int $give_quantity 赠送数量
- */
- class ElectronCoupon extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_electron_coupon}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id', 'mall_id', 'amount', 'receive_times', 'unlimit_receive_times', 'pieces_type', 'pieces_amount', 'receive_codition',
- 'receive_member_level', 'receive_begin_time', 'receive_end_time', 'use_begin_time', 'use_end_time', 'use_way',
- 'is_can_receive', 'is_show_in_center', 'receive_amount', 'used_amount', 'created_at', 'updated_at', 'sort',
- 'is_give', 'is_proper', 'status', 'share_expiry', 'give_quantity'], 'integer'],
- [['name', 'amount'], 'required'],
- [['price', 'give_score', 'give_price', 'reward_money', 'give_store_price'], 'number'],
- [['rules'], 'string'],
- [['name', 'pic'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '自增id',
- 'mall_id' => '商城id',
- 'name' => '电子券名称',
- 'amount' => '发行数量;单张券为张',
- 'receive_times' => '最多领取次数',
- 'unlimit_receive_times' => '领取次数不限制',
- 'pieces_type' => '1单张2多张;',
- 'pieces_amount' => '多张数量;单位张',
- 'receive_codition' => '领取条件;1全体会员;2指定会员等级;',
- 'receive_member_level' => '指定会员等级;默认为null',
- 'receive_begin_time' => '领取开始时间',
- 'receive_end_time' => '领取结束时间',
- 'use_begin_time' => '使用开始时间',
- 'use_end_time' => '使用结束时间',
- 'price' => '激活金额',
- 'reward_money' => '奖励金额',
- 'use_way' => '适用范围;1扫描核销',
- 'is_can_receive' => '是否可以被领取',
- 'is_show_in_center' => '是否出现在领取中心',
- 'rules' => '电子券规则',
- 'pic' => '缩略图',
- 'no_receive_amount' => '未领取数量',
- 'receive_amount' => '领取数量;单张券为张;',
- 'used_amount' => '兑换数量;单位张或多张',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'sort' => '排序;由大到小',
- 'is_give' => '是否赠送电子券',
- 'is_proper' => '是否专有券',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'give_score' => '赠送积分',
- 'give_price' => '赠送佣金',
- 'give_store_price' => '核销门店奖励',
- 'share_expiry' => '分享有效期,单位小时',
- 'give_quantity' => '赠送数量',
- ];
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/6/27 0012 17:30
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function getGoodSetting($params)
- {
- // 后台页面组件展示
- if ($params['type'] == 'show_component') {
- return [
- 'component' => 'com-electron-coupon',
- 'path' => Yii::$app->basePath . '/../addons/ElectronCoupon/backend/views/goods',
- ];
- } else {
- try {
- if (empty($params['setting'][ElectronCouponEnums::ADDONS_NAME])) return true;
- $setting = $params['setting'][ElectronCouponEnums::ADDONS_NAME];
- // 数据保存
- //var_dump($setting['electron_selection']);die;
- //统一设置无效
- ElectronCouponGoods::setCanceGoods($params);
- if ($setting['electron_selection']) {
- $data['is_open'] = $setting['is_open'];
- $data['send_type'] = $setting['send_type'];
- foreach ($setting['electron_selection'] as $k => $v) {
- $data['num'] = $v['num'];
- $data['status'] = StatusEnum::ENABLED;
- $data['e_coupon_id'] = $v['coupon_id'];
- $res = ElectronCouponGoods::setGoodsData($params['mall_id'], $params['goods_id'], $data);
- if ($res === false) throw new Exception(ElectronCouponGoods::getStaticError());
- }
- }
- return true;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/6/27 0012 12:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function isInstall(array $params)
- {
- //插件判断 是否显示卡密信息
- $addons = MallAddons::isInstall($params['mall_id'], ElectronCouponEnums::ADDONS_NAME);
- $data = $params['order'];
- if ($addons) {
- $data[ElectronCouponEnums::ADDONS_NAME]['is_show_addons'] = 1;
- } else {
- $data[ElectronCouponEnums::ADDONS_NAME]['is_show_addons'] = 0;
- }
- return $data;
- }
- /**
- * 数据设置
- * @param array $params
- * int receive_amount 未激活数量,传入数量进行加减计算
- * int used_amount 已使用数量,传入数量进行加减计算
- * @return ElectronCoupon|bool|null
- */
- public static function setData(array $params)
- {
- try {
- $params['receive_amount'] = $params['receive_amount'] ?? 0;
- $params['used_amount'] = $params['used_amount'] ?? 0;
- $params['status'] = $params['status'] ?? 1;
- $params['give_quantity'] = $params['give_quantity'] ?? 0;
- if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
- $mall_id = $params['mall_id'];
- if (isset($params['id']) && !empty($params['id'])) {
- $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new Exception('电子券不存在或已删除');
- $bcmath = ['receive_amount', 'used_amount'];
- foreach ($bcmath as $field) {
- if (isset($params[$field])) {
- $model[$field] = $model[$field] + $params[$field];
- unset($params[$field]);
- }
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 修改是否显示在首页
- *
- * @param $mall_id
- * @param $id
- * @param $is_show_in_center
- * @return ElectronCoupon|false
- */
- public static function changeIsShowInCenter($mall_id, $id, $is_show_in_center)
- {
- try {
- $model = self::findOne(['id' => $id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new Exception('电子券不存在或已删除');
- $model->is_show_in_center = $is_show_in_center;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 修改状态
- *
- * @param $mall_id
- * @param $id
- * @param $status
- * @return ElectronCoupon|false
- */
- public static function changeStatus($mall_id, $id, $status)
- {
- try {
- $model = self::findOne(['id' => $id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new Exception('电子券不存在或已删除');
- $model->status = $status;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|