public function get($f3) { global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits()->min(0)->validate('pageNo'); $pageSize = $validator->digits()->min(0)->validate('pageSize'); // 设置缺省值 $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = isset($pageSize) && $pageSize > 0 ? $pageSize : 10; if (!$this->validate($validator)) { goto out_display; } $userInfo = AuthHelper::getAuthUser(); $userBasicService = new UserBasicService(); $userInfo = $userBasicService->loadUserById($userInfo['user_id']); // 用户总共有资金余额 $smarty->assign('userMoney', $userInfo['user_money']); $accountLog = new AccountLogService(); // 用户总共有多少account_log $totalCount = $accountLog->countUserMoneyArray($userInfo['user_id'], 10); //缓存 10 秒钟 if ($totalCount <= 0) { // 没资金变动记录,可以直接退出了 goto out_display; } $smarty->assign('totalCount', $totalCount); // 页数超过最大值,返回第一页 if ($pageNo * $pageSize >= $totalCount) { RouteHelper::reRoute($this, '/My/Money'); } // 传递分页的变量 $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); // account_log 列表 $accountLogArray = $accountLog->fetchUserMoneyArray($userInfo['user_id'], $pageNo * $pageSize, $pageSize, 10); //缓存 10 秒钟 foreach ($accountLogArray as &$accountLogItem) { $accountLogItem['change_type_desc'] = AccountLogService::$changeTypeDesc[$accountLogItem['change_type']]; } unset($accountLogItem); $smarty->assign('accountLogArray', $accountLogArray); out_display: $smarty->display('my_money.tpl', 'get'); }
/** * 取消还没有支付的订单,订单状态必须是 OS_UNCONFIRMED 才可以取消 * * @return boolean * * @param int $orderid 订单ID */ public function cancelUnpayOrderInfo($orderId) { // 参数验证 $validator = new Validator(array('orderId' => $orderId)); $orderId = $validator->required()->digits()->min(1)->validate('orderId'); $this->validate($validator); $orderBasicService = new Order(); $orderInfo = $orderBasicService->loadOrderInfoById($orderId); if ($orderInfo->isEmpty() || Order::OS_UNCONFIRMED != $orderInfo['order_status']) { return false; } if ($orderInfo->surplus > 0) { // 如果使用了余额支付,退还余额 $accountLog = new AccountLog(); $accountLog->logChange($orderInfo['user_id'], $orderInfo->surplus, 0, 0, 0, '退还余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER); $orderInfo->order_amount += $orderInfo->surplus; // 修复 order_amount 的值 $orderInfo->surplus = 0; } if ($orderInfo->bonus_id > 0) { // 使用了红包支付,退还红包 $bonusService = new Bonus(); $bonusService->unUseBonus($orderInfo->bonus_id); $orderInfo->order_amount += $orderInfo->bonus; // 修复 order_amount 的值 $orderInfo->bonus_id = 0; $orderInfo->bonus = 0; } $orderInfo->order_status = Order::OS_CANCELED; // 设置为取消订单 $orderInfo->save(); return true; }
/** * 把购物车中的信息保存到数据库订单中,如果订单已经存在,则更新订单数据, * 注意:这个方法必须在购物车计算完成之后调用,否则无法正确保存计算之后的数据 * * @return mixed 失败返回 false,成功返回订单记录 * * @param int $userId 用户的数字 ID * @param string $username 用户名 */ public function saveOrder($userId, $username) { // 参数验证 $validator = new Validator(array('userId' => $userId)); $userId = $validator->required()->digits()->min(1)->validate('userId'); $this->validate($validator); $orderBasicService = new OrderBasicService(); $orderId = isset($this->cartContext->orderInfo) ? $this->cartContext->orderInfo['order_id'] : null; // 不允许创建空订单 if (!$orderId && $this->cartContext->isEmpty()) { return false; } // 是否需要新建订单 $isNewOrder = false; //检查这个 order_info 是否合法 if ($orderId) { $orderInfo = $orderBasicService->loadOrderInfoById($orderId); // 不存在的 order_info 或者 已经付款了的订单不能再用了 if ($orderInfo->isEmpty() || OrderBasicService::PS_UNPAYED != $orderInfo->pay_status) { $orderId = null; $isNewOrder = true; } } else { $isNewOrder = true; } // 创建 order_info 记录 $orderInfoArray = $this->cartContext->orderInfoValue; $orderInfoArray['user_id'] = $userId; $orderInfoArray['order_status'] = OrderBasicService::OS_UNCONFIRMED; $orderInfoArray['pay_status'] = OrderBasicService::PS_UNPAYED; if (!$orderId) { // 新订单,生成 add_time 和 order_sn $orderInfoArray['add_time'] = Time::gmTime(); $orderInfoArray['order_sn'] = OrderBasicService::generateOrderSn(); $orderInfoArray['system_id'] = Cart::$cartSystemId; // 记录订单来自于哪个系统 } elseif ($this->cartContext->isOrderAmountChange()) { // 去支付网关支付,如果订单金额发生变化都需要重新生成一次 order_sn ,因为订单金额如果发生变化,支付网关会拒绝支付 $orderInfoArray['order_sn'] = OrderBasicService::generateOrderSn(); } else { // do nothing } // 我们需要数据库事务的保障 $dbEngine = DataMapper::getDbEngine(); $dbEngine->begin(); // 记录余额支付 $oldSurplus = $this->cartContext->orderInfo ? $this->cartContext->orderInfo['surplus'] : 0; $newSurplus = $this->cartContext->getValue('surplus') > 0 ? $this->cartContext->getValue('surplus') : 0; // 记录使用红包 $oldBonusId = $this->cartContext->orderInfo ? $this->cartContext->orderInfo['bonus_id'] : 0; $newBonusId = $this->cartContext->getValue('bonus_id') > 0 ? $this->cartContext->getValue('bonus_id') : 0; // 创建订单或者更新 order_info $orderInfo = $orderBasicService->saveOrderInfo($orderId, $orderInfoArray); // 创建订单失败,返回 false if ($orderInfo->isEmpty()) { goto out_fail; } // 处理余额支付 if ($oldSurplus != $newSurplus) { // 前后发生了支付余额的改变 $accountLog = new AccountLog(); if ($oldSurplus > 0) { // 把之前的余额退还 $accountLog->logChange($userId, $oldSurplus, 0, 0, 0, '退还余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER); } if ($newSurplus > 0) { // 使用余额 $accountLog->logChange($userId, -1 * $newSurplus, 0, 0, 0, '使用余额,订单:' . $orderInfo['order_id'], AccountLog::ACT_OTHER); } // 由于修改了用户信息,需要 reload 用户数据 AuthHelper::reloadAuthUser(); } // 处理红包支付 if ($oldBonusId != $newBonusId) { // 红包支付前后发生了变化 $bonusService = new Bonus(); if ($oldBonusId > 0) { //退还之前使用的红包 $bonusService->unUseBonus($oldBonusId); } if ($newBonusId > 0) { // 使用新的红包 $bonusService->useBonus($newBonusId, $orderInfo['order_id']); } } // 创建 orderRefer 对象,记录订单的来源 $orderReferService = new OrderReferService(); $orderRefer = $orderReferService->loadOrderReferByOrderId($orderInfo['order_id']); // order_refer 记录创建一次之后就不会再修改了 if ($orderRefer->isEmpty()) { // 新的 order_refer 记录,设置 order_id 值 $orderRefer->order_id = $orderInfo['order_id']; $orderRefer->create_time = Time::gmTime(); $orderRefer->login_type = AuthHelper::getLoginType(); global $f3; $orderReferArray = ReferHelper::parseOrderRefer($f3); if (!empty($orderReferArray)) { unset($orderReferArray['refer_id']); //清除掉危险字段 unset($orderReferArray['order_id']); //清除掉危险字段 $orderRefer->copyFrom($orderReferArray); } $orderRefer->save(); // 保存 order_refer 记录 } // 取得数据库中已经存在的 order_goods 列表 $dbOrderGoodsArray = array(); if ($orderId > 0) { $dbOrderGoodsArray = $orderBasicService->fetchOrderGoodsArray($orderId); } $stillExistOrderGoodsIdArray = array(); // 在购物车中仍然存在的 orderGoods 对象 // 创建 orderInfo 对应的 order_goods 对象 foreach ($this->cartContext->orderGoodsArray as $orderGoodsItem) { $orderGoodsValueArray = array(); $orderGoodsValueArray['order_id'] = $orderInfo['order_id']; //从 goods 中复制属性 $orderGoodsValueArray['goods_id'] = $orderGoodsItem->goods['goods_id']; $orderGoodsValueArray['goods_admin_user_id'] = $orderGoodsItem->goods['admin_user_id']; $orderGoodsValueArray['goods_admin_user_name'] = $orderGoodsItem->goods['admin_user_name']; $orderGoodsValueArray['goods_name'] = $orderGoodsItem->goods['goods_name_short']; $orderGoodsValueArray['goods_sn'] = $orderGoodsItem->goods['goods_sn']; $orderGoodsValueArray['warehouse'] = $orderGoodsItem->goods['warehouse']; $orderGoodsValueArray['shelf'] = $orderGoodsItem->goods['shelf']; $orderGoodsValueArray['market_price'] = $orderGoodsItem->goods['market_price']; $orderGoodsValueArray['shop_price'] = $orderGoodsItem->goods['shop_price']; $orderGoodsValueArray['shipping_fee'] = $orderGoodsItem->goods['shipping_fee']; $orderGoodsValueArray['is_real'] = $orderGoodsItem->goods['is_real']; $orderGoodsValueArray['extension_code'] = $orderGoodsItem->goods['extension_code']; $orderGoodsValueArray['suppliers_id'] = $orderGoodsItem->goods['suppliers_id']; $orderGoodsValueArray['suppliers_price'] = $orderGoodsItem->goods['suppliers_price']; $orderGoodsValueArray['suppliers_shipping_fee'] = $orderGoodsItem->goods['suppliers_shipping_fee']; // 合并一些因为计算而覆盖的值 $orderGoodsValueArray = array_merge($orderGoodsValueArray, $orderGoodsItem->orderGoodsValue); $orderGoodsId = null; if (!$isNewOrder && $orderGoodsItem->orderGoods) { // 如果不是新订单,并且已经存在的 order_goods,做更新 $orderGoodsId = $orderGoodsItem->orderGoods['rec_id']; $stillExistOrderGoodsIdArray[] = $orderGoodsId; } else { // 新建的 order_goods,我们需要取得它的 CPS 信息,CPS 信息只在一开始加入,后面不会再被修改了 $cpsArray = ReferHelper::getCpsParam($orderRefer, $orderInfo, $orderGoodsValueArray); if (!empty($cpsArray)) { $orderGoodsValueArray['cps_rate'] = isset($cpsArray['cps_rate']) ? $cpsArray['cps_rate'] : 0; $orderGoodsValueArray['cps_fix_fee'] = isset($cpsArray['cps_fix_fee']) ? $cpsArray['cps_fix_fee'] : 0; } } $orderGoods = $orderBasicService->saveOrderGoods($orderGoodsId, $orderInfo['order_id'], $orderGoodsValueArray); if ($orderGoods->isEmpty()) { //创建失败 goto out_fail; } } // 删除已经被用户删除的 order_goods foreach ($dbOrderGoodsArray as $dbOrderGoodsItem) { // 这个 order_goods 已经不存在了,删除它 if (!in_array($dbOrderGoodsItem['rec_id'], $stillExistOrderGoodsIdArray)) { $orderBasicService->removeOrderGoods($dbOrderGoodsItem['rec_id']); } } // 记录订单操作日志 $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderInfo['order_id'], 0, OrderBasicService::OS_UNCONFIRMED, OrderBasicService::PS_UNPAYED, OrderGoodsService::OGS_UNPAY, '订单创建或更新', $username, 0, 0); $dbEngine->commit(); return $orderInfo; // 返回创建的订单 out_fail: $dbEngine->rollback(); return false; }
/** * 用户资金变动列表 * * @param $f3 */ public function Money($f3) { // 权限检查 $this->requirePrivilege('manage_account_user_money'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $pageNo = $validator->digits()->min(0)->validate('pageNo'); $pageSize = $validator->digits()->min(0)->validate('pageSize'); //查询条件 $formQuery = array(); $formQuery['user_id'] = $validator->filter('ValidatorIntValue')->validate('user_id'); $formQuery['admin_user_id'] = $validator->filter('ValidatorIntValue')->validate('admin_user_id'); $formQuery['change_type'] = $validator->filter('ValidatorIntValue')->validate('change_type'); $formQuery['change_desc'] = $validator->validate('change_desc'); // 设置缺省值 $pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0; $pageSize = isset($pageSize) && $pageSize > 0 ? $pageSize : 20; if (!$this->validate($validator)) { goto out_display; } // 建立查询条件 $condArray = QueryBuilder::buildQueryCondArray($formQuery); // 查询资金列表 $accountLogService = new AccountLogService(); $totalCount = $accountLogService->_countArray('account_log', $condArray); if ($totalCount <= 0) { // 没用户,可以直接退出了 goto out_display; } // 页数超过最大值,返回第一页 if ($pageNo * $pageSize >= $totalCount) { RouteHelper::reRoute($this, '/Account/User/Money'); } // 资金列表 $accountLogArray = $accountLogService->_fetchArray('account_log', '*', $condArray, array('order' => 'log_id desc'), $pageNo * $pageSize, $pageSize); // 取用户信息 $userIdArray = array(); foreach ($accountLogArray as $accountLogItem) { $userIdArray[] = $accountLogItem['user_id']; } $userIdArray = array_unique($userIdArray); $userBasicService = new UserBasicService(); $userInfoArray = $userBasicService->fetchUserArrayByUserIdArray($userIdArray); //建立倒查表 $userIdToUserInfoMap = array(); foreach ($userInfoArray as $userInfo) { $userIdToUserInfoMap[$userInfo['user_id']] = $userInfo; } // 转换显示 foreach ($accountLogArray as &$accountLogItem) { $accountLogItem['user_name'] = @$userIdToUserInfoMap[$accountLogItem['user_id']]['user_name']; $accountLogItem['change_type_desc'] = AccountLogService::$changeTypeDesc[$accountLogItem['change_type']]; } // 给模板赋值 $smarty->assign('totalCount', $totalCount); $smarty->assign('pageNo', $pageNo); $smarty->assign('pageSize', $pageSize); $smarty->assign('accountLogArray', $accountLogArray); out_display: $smarty->display('account_user_money.tpl'); }