判断权益卡订单,是通过订单名称storeTitle来作的,后面一定要先修改订单状态为2,再用statusService的方法保存

applistenerorderOrderPaySuccessListener.php
底部添加

//权益卡订单特殊处理
        $orderServices = app()->make(StoreOrderServices::class);
        $orderInfo = $orderServices->get($orderInfo['id']);

        $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
        $storeName = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
        $storeTitle = Str::substrUTf8($storeName, 20, 'UTF-8', '');
       
        if($storeTitle == sys_config('cardorder_name')){
            trace('-----------权益卡订单特殊处理------', 'info');

            $orderServices->update($orderInfo['id'], ['status' => 2]);

            //设置订单状态为已收货
            $statusService->save([
                'oid' => $orderInfo['id'],
                'change_type' => 'take_delivery',
                'change_message' => '已收货[自动收货]',
                'change_time' => time()
            ]);

            $orderTakeServices = app()->make(appservicesorderStoreOrderTakeServices::class);
            $orderTakeServices->storeProductOrderUserTakeDelivery($orderInfo);
        }

优惠券判断发放

appservicesorderStoreOrderTakeServices.php中

storeProductOrderUserTakeDelivery

方法

// 判断订单ID是否是权益卡订单
                trace('判断是否权益卡', 'info');
                trace($order['storeTitle'], 'info');
                trace(sys_config('cardorder_name'), 'info');
                if ($order['storeTitle'] == sys_config('cardorder_name')) {
                    trace('-----------------权益卡订单开始----------------------', 'info');
                    trace('是权益卡订单 ' . $order['uid'], 'info');
                    $this->adjustAgentLevel((int)$order['uid'], 3);
                    // 发放优惠券
                    $this->giveCouponToUser((int)$order['uid']);
                }

下面是具体的提高等级和发放优惠券

/**
     * 手动调整分销等级
     * @param int $uid 用户ID
     * @param int $levelId 分销等级ID
     * @return bool
     * @throws thinkdbexceptionDataNotFoundException
     * @throws thinkdbexceptionDbException
     * @throws thinkdbexceptionModelNotFoundException
     */
    public function adjustAgentLevel(int $uid, int $levelId)
    {
        /** @var AgentLevelServices $agentLevelServices */
        $agentLevelServices = app()->make(AgentLevelServices::class);
        return $agentLevelServices->givelevel($uid, $levelId);
    }

    //发放优惠券
    /**
     * 发放优惠券方法 - 发放指定数量的指定优惠券给用户
     * @param int $uid 用户ID
     * @return bool
     * @throws thinkdbexceptionDataNotFoundException
     * @throws thinkdbexceptionDbException
     * @throws thinkdbexceptionModelNotFoundException
     */
    public function giveCouponToUser(int $uid)
    {
        try {
            trace('发放优惠券给用户ID: ' . $uid, 'info');
            // 优惠券ID
            $couponId = (int)sys_config('moneycoupon_id');
            // 发放数量
            $issueCount = (int)sys_config('moneycoupon_num');

            // 实例化优惠券服务
            $couponIssueServices = app()->make(appservicesactivitycouponStoreCouponIssueServices::class);

            // 获取优惠券信息,使用appdaoactivitycouponStoreCouponIssueDao.php中的getCouponInfo方法
            $couponIssueDao = app()->make(appdaoactivitycouponStoreCouponIssueDao::class);

            $couponInfo = $couponIssueDao->getCouponInfo($couponId);

            trace('获取到的优惠券信息: ' . json_encode($couponInfo), 'info');

            // 后续可以使用$couponInfo进行业务处理
            if (!$couponInfo || $issueCount <= 0) {
                trace('优惠券ID ' . $couponId . ' 不存在或已失效', 'error');
                return false;
            } else {
                // 循环发放指定数量的优惠券
                for ($i = 0; $i < $issueCount; $i++) {
                    trace('发放优惠券第 ' . ($i + 1) . ' 张', 'info');
                    $couponIssueServices->setCoupon($couponInfo, [$uid]);
                }

                // 记录发放成功日志
                trace("成功为用户ID {$uid} 发放优惠券ID {$couponId},共 {$issueCount} 张", 'info');
            }



            //金粉团长专用杯子专用优惠券
            $couponId = (int)sys_config('cupcoupon_id');
            $couponInfo = $couponIssueDao->getCouponInfo($couponId);
            if (!$couponInfo) {
                trace('优惠券ID ' . $couponId . ' 不存在或已失效', 'error');
                return false;
            }
            $couponIssueServices->setCoupon($couponInfo, [$uid]);
            trace("成功为用户ID {$uid} 发放金粉团长专用杯子专用优惠券优惠券ID {$couponId},共 1 张", 'info');

            return true;
        } catch (Exception $e) {
            // 记录错误日志
            trace("为用户ID {$uid} 发放优惠券失败: " . $e->getMessage(), 'error');
            return false;
        }
    }


点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
0.406829s