Db::startTrans();
            try{
                /***①减少用户棒棒糖数量***/
                $res = Db::name('user')->where('id',$userId)->setDec('transaction_lollipop',($transactionCharge+$orderInfo['count']));
                if(empty($res))
                    throw new \Exception("减少用户棒棒糖余额失败");
 
                $before = $userInfo['transaction_lollipop'];
                $count = $transactionCharge+$orderInfo['count'];
                $after = $before - $count;
                /***②写入棒棒糖记录日志***/
                $res = $this->lolliLog($userId,$before,-$count,$after,1,1,'用户在交易大厅出售棒棒糖,减少棒棒糖数量',1);
                if(empty($res))
                    throw new \Exception("写入棒棒糖变更日志失败");
 
                /***③更新棒棒糖订单状态***/
                $result = Db::name('lolli_order')->where('id',$orderId)->update($orderInfo);
                if(empty($result))
                    throw new \Exception("更新棒棒糖订单失败");
 
                /***提交订单***/
                Db::commit();
            }catch(\Exception $e) {
                // 捕获异常回滚事务
                Db::rollback();
            }
   public function cancelOrder()
    {
        if ($this->request->isPost()) {
            $userId = $this->request->request('user_id');
            $userInfo = $this->getUserInfo($userId);
            $orderId = $this->request->request('order_id');
            $orderInfo = Db::name('lolli_order')->where('id', $orderId)->find();
            if ($orderInfo['status'] != 0) {
                $this->error('不是发布中的订单无法撤销哦~');
            }
            if ($orderInfo['buyer_id'] != $userId && $orderInfo['sailer_id'] != $userId) {
                $this->error('无法撤销不属于你的订单哦~');
            }
            $data['status'] = 2;
            $data['cancel_time'] = time();
            if ($orderInfo['from'] == 1 && $orderInfo['sailer_id'] == $userId) { // 出售者发起的订单   出售棒棒糖的人撤销订单
                /*****出售棒棒糖撤销订单   事务开启****/
                Db::startTrans();
                try {
                    // ①更改订单交易状态
                    $res = Db::name('lolli_order')->where('id', $orderId)->update($data);
                    if (!$res) {
                        throw new \Exception('订单状态更新失败');
                    }
                    // ②撤销订单写入日志
                    $poundageMoney = $orderInfo['poundage_money'];  // 手续费用
                    $count = $orderInfo['棒棒糖交易数量'];  // 手续费用
                    $res = $this->lolliLog($userId, $userInfo['transaction_lollipop'], $count, $userInfo['transaction_lollipop'] + $count + $poundageMoney, 5, 1, '撤销棒棒糖订单退回的费用+手续费用', 1);
                    if (!$res) {
                        throw new \Exception('棒棒糖日志添加失败');
                    }
                    // ③给用户增加交易棒棒糖
                    $res = Db::name('user')->where('id', $userId)->setInc('transaction_lollipop', $poundageMoney + $count);
                    if (!$res) {
                        throw new \Exception('撤销订单,返还交易棒棒糖失败');
                    }
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error('订单撤销失败');
                }
                /*****出售棒棒糖撤销订单   事务结束****/
            } elseif ($orderInfo['from'] == 2 && $orderInfo['buyer_id'] == $userId) {  // 出售者发起的订单   发布购买需求的人撤销订单
                $res = Db::name('lolli_order')->where('id', $orderId)->update($data);
                if (empty($res)) {
                    $this->error('订单撤销失败');
                }
            } else {
                $this->error('无法撤销不属于你的订单哦~');
            }
            $this->success('订单撤销成功');
        } else {
            $this->error('请求方式有误哦');
        }
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注