| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace addons\Store\common\models;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponReward;
- use addons\Coupon\common\models\AddonsCouponShareLog;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\base\BaseActiveRecord;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_coupon_reward".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $store_id 门店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 $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class StoreCouponReward extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_coupon_reward}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'coupon_id'], 'required'],
- [['mall_id', 'store_id', 'coupon_id', 'reward_type', 'status', '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',
- 'store_id' => 'Store 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',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData(int $mall_id, int $coupon_id, int $store_id, int $reward_type, array $rewards): bool
- {
- if (empty($mall_id) || empty($coupon_id) || empty($reward_type) || empty($rewards) || empty($store_id)) {
- self::$static_error = '参数错误';
- return false;
- }
- $model = self::findOne(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'store_id' => $store_id, 'reward_type' => $reward_type]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->store_id = $store_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;
- }
- public static function giveReward($order_id, $order_detail_ids)
- {
- try {
- if (!empty($order_detail_ids)) {
- $order = StoreOrder::findOne(['id' => $order_id, 'status' => StatusEnum::ENABLED, 'pay_status' => OrderStatusEnum::PAY, 'is_recycle' => 0]);
- if (empty($order)) return;
- // 当前订单使用的优惠券
- $store_coupon_use_logs = StoreCouponUseLog::lists(['where' => [['order_id' => $order_id]]]);
- if (empty($store_coupon_use_logs)) return;
- $user_coupon_ids = array_column($store_coupon_use_logs, 'user_coupon_id');
- $store_coupon_user_relate_list = StoreCouponUserRelate::lists(['where' => [['id' => $user_coupon_ids]]]);
- $platform_coupon_ids = $store_coupon_ids = [];
- $platform_coupon_arr = $store_coupon_arr = [];
- foreach ($store_coupon_user_relate_list as $item) {
- switch ($item['from_type']) {
- case 1:
- $platform_coupon_ids[] = $item['coupon_id'];
- $platform_coupon_arr[$item['coupon_id']] = $item['id'];
- break;
- case 2:
- $store_coupon_ids[] = $item['coupon_id'];
- $store_coupon_arr[$item['coupon_id']] = $item['id'];
- break;
- }
- }
- $reward_list = [];
- if (!empty($platform_coupon_ids)) {
- $platform_coupon_list = AddonsCoupon::find()->where(['id' => $platform_coupon_ids])
- ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]])
- ->all();
- if (!empty($platform_coupon_list)) {
- foreach ($platform_coupon_list as $coupon) {
- if (1 == $coupon['is_consume_reward']) {
- // 优惠券开启了使用奖励
- $desc = '优惠券使用奖励';
- $user_id = $order->user_id;
- $used_reward = AddonsCouponReward::find()
- ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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 = '优惠券转赠奖励';
- $shared_log = AddonsCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $platform_coupon_arr[$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['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;
- }
- }
- }
- }
- }
- if (!empty($store_coupon_ids)) {
- $store_coupon_list = StoreCoupon::find()->where(['id' => $store_coupon_ids])
- ->andWhere(['or', ['is_giving_reward' => 1], ['is_consume_reward' => 1]])
- ->all();
- if (!empty($store_coupon_list)) {
- foreach ($store_coupon_list as $coupon) {
- if (1 == $coupon['is_consume_reward']) {
- // 优惠券开启了使用奖励
- $desc = '优惠券使用奖励';
- $user_id = $order->user_id;
- $used_reward = StoreCouponReward::find()
- ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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 = '优惠券转赠奖励';
- $shared_log = StoreCouponShareLog::findOne(['mall_id' => $order->mall_id, 'coupon_id' => $coupon['id'], 'get_user_coupon_id' => $store_coupon_arr[$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 = StoreCouponReward::find()
- ->where(['mall_id' => $order->mall_id, 'coupon_id' => $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']);
- 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' => StoreOrder::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' => StoreOrder::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => $value['from_type'],
- 'capital_type' => $value['capital_type'],
- ];
- }
- }
- if ($insert_wallet) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- // 更新 qimall_addons_coupon_user_relate 表奖励状态
- $rst = StoreCouponUserRelate::updateAll(['is_send_reward' => 1, 'coupon_status' => AddonsCouponEnum::STATUS_FAILURE], ['id' => $user_coupon_ids, 'is_use' => 1]);
- }
- }
- return true;
- } catch (\Exception $e) {
- echo 'StoreCouponReward:giveReward:' . $e->getMessage().$e->getLine().$e->getFile();
- }
- }
- }
|