| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace addons\Coupon\common\models;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\RedpackWallet;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderMarketing;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "{{%addons_coupon_reward}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $coupon_id 优惠券id
- * @property int $reward_type 奖励类型,1:转赠奖励,2:使用奖励
- * @property float $balance 奖励余额
- * @property float $score 奖励积分
- * @property float $red_packet 奖励红包
- * @property string $reward_currency_type 奖励货币种类,记录货币 code
- * @property float $number 奖励货币数量
- * @property int $created_at
- * @property int $updated_at
- */
- class AddonsCouponReward extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_coupon_reward}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'coupon_id'], 'required'],
- [['mall_id', 'coupon_id', 'reward_type', 'created_at', 'updated_at'], 'integer'],
- [['balance', 'score', 'red_packet', 'number'], 'number'],
- [['reward_currency_type'], 'string', 'max' => 50],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'coupon_id' => 'Coupon ID',
- 'reward_type' => 'Reward Type',
- 'balance' => 'Balance',
- 'score' => 'Score',
- 'red_packet' => 'Red Packet',
- 'reward_currency_type' => 'Reward Currency Type',
- 'number' => 'Number',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * @desc:订单使用优惠券,赠送奖励
- * @Author: hua
- * @Date: 2021/12/24
- * @Time: 10:48
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_id
- * @param $order_detail_ids
- */
- public static function giveReward($order_id, $order_detail_ids)
- {
- try {
- if (!empty($order_detail_ids)) {
- $order = Order::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]);
- if (empty($order)) return;
- // 当前订单使用的优惠券
- $used_coupons = AddonsCouponUseLog::find()
- ->alias('cul')
- ->select('ac.is_giving_reward,ac.is_consume_reward,cul.coupon_id,cul.user_coupon_id,cul.id')
- ->join('LEFT JOIN', AddonsCoupon::tableName() . ' as ac', 'ac.id = cul.coupon_id')
- ->where(['cul.order_id' => $order_id])
- ->andWhere(['or', ['ac.is_giving_reward' => 1], ['ac.is_consume_reward' => 1]])
- ->asArray()
- ->all();
- if (empty($used_coupons)) return;
- $reward_list = [];
- foreach ($used_coupons as $coupon) {
- if (1 == $coupon['is_consume_reward']) {
- // 优惠券开启了使用奖励
- $desc = '优惠券使用奖励';
- $user_id = $order->user_id;
- $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_use' => 1, 'is_send_reward' => 0]);
- if (!empty($user_coupon)) {
- $used_reward = AddonsCouponReward::find()
- ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_CONSUME])
- ->asArray()
- ->one();
- $used_reward['user_id'] = $user_id;
- $used_reward['desc'] = $desc;
- $used_reward['from_type'] = UserWalletEnum::CONSUME;
- $used_reward['capital_type'] = UserWalletEnum::USE_COUPON_GIVE;
- $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_GIVING;
- $used_reward['source_table_id'] = $coupon['id'];
- $reward_list[] = $used_reward;
- }
- }
- if (1 == $coupon['is_giving_reward']) {
- // 优惠券开启了转赠奖励
- $desc = '优惠券转赠奖励';
- $user_coupon = AddonsCouponUserRelate::findOne(['id' => $coupon['user_coupon_id'], 'is_giving' => 0, 'giving_status' => 0, 'is_use' => 1, 'is_send_reward' => 0]);
- if (!empty($user_coupon)) {
- $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'get_user_coupon_id' => $coupon['user_coupon_id'], 'receive_user_id' => $order->user_id, 'giving_status' => 2]);
- if (empty($shared_log)) continue;
- $user_id = $shared_log->giving_user_id;
- // 如果转出会员的 id=0 说明是平台赠送的优惠券,不发放转赠奖励
- if (!empty($user_id)) {
- $used_reward = AddonsCouponReward::find()
- ->where(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['coupon_id'], 'reward_type' => AddonsCouponEnum::REWARD_TYPE_GIVING])
- ->asArray()
- ->one();
- $used_reward['user_id'] = $user_id;
- $used_reward['desc'] = $desc;
- $used_reward['from_type'] = UserWalletEnum::CONSUME;
- $used_reward['capital_type'] = UserWalletEnum::GIVING_COUPON_REWARD;
- $used_reward['reward_scenario'] = AddonsCouponEnum::REWARD_TYPE_CONSUME;
- $used_reward['source_table_id'] = $coupon['id'];
- $reward_list[] = $used_reward;
- }
- }
- }
- }
- $insert_wallet = [];
- foreach ($reward_list as $value) {
- $balance = floatval($value['balance']);
- $score = floatval($value['score']);
- $red_packet = floatval($value['red_packet']);
- $currency = floatval($value['number']);
- if (!empty($balance)) {
- $insert_wallet[] = [
- 'mall_id' => $order['mall_id'],
- 'user_id' => $value['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
- 'money' => $balance,
- 'desc' => $value['desc'] . '余额 ' . $balance . ' 元',
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => $value['from_type'],
- 'capital_type' => $value['capital_type'],
- ];
- }
- if (!empty($score)) {
- $insert_wallet[] = [
- 'mall_id' => $order['mall_id'],
- 'user_id' => $value['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
- 'money' => $score,
- 'desc' => $value['desc'] . ' ' . $score . ' 积分',
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => $value['from_type'],
- 'capital_type' => $value['capital_type'],
- ];
- }
- if (!empty($red_packet)) {
- if (MallAddons::isInstall($order['mall_id'], RedpackEnum::ADDONS_NAME)) {
- // 红包
- RedpackWallet::setData([
- 'mall_id' => $order['mall_id'],
- 'user_id' => $value['user_id'],
- 'is_frozen' => true,
- 'money' => $red_packet,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => RedpackEnum::FROM_USE_COUPON,
- 'desc' => "订单ID: {$order_id}" . $value['desc'],
- ]);
- };
- }
- if (!empty($currency)) {
- // 系统虚拟货币
-
- }
- }
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- // 更新 qimall_addons_coupon_user_relate 表奖励状态
- $user_coupon_ids = array_column($used_coupons, 'user_coupon_id');
- $rst = AddonsCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]);
- }
- }
- return true;
- } catch (\Exception $e) {
- Yii::error('优惠券奖励发放失败');
- Yii::error($e);
- echo 'AddonsCouponReward:giveReward:' . $e->getMessage();
- }
- }
- /**
- * @name:
- * @msg: 保存数据
- * @param {int} $mall_id
- * @param {int} $coupon_id
- * @param {int} $reward_type 奖励类型
- * @param {array} $rewards 具体奖励
- * @return {*}
- */
- public static function setData(int $mall_id, int $coupon_id, int $reward_type, array $rewards): bool
- {
- if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards)) {
- self::$static_error = '参数错误';
- return false;
- }
- $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'reward_type' => $reward_type]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->coupon_id = $coupon_id;
- }
- foreach ($rewards as $key => $val) {
- $model->$key = $val;
- }
- $model->reward_type = $reward_type;
- if (!$model->save()) {
- self::$static_error = '保存奖励数据失败:' . $model->getErrorMessage();
- return false;
- }
- return true;
- }
- }
|