342 lines
12 KiB
PHP
342 lines
12 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
|
// +----------------------------------------------------------------------
|
|
// | Author: 萤火科技 <admin@yiovo.com>
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types=1);
|
|
|
|
namespace app\api\service\order;
|
|
|
|
use app\api\model\Goods as GoodsModel;
|
|
use app\common\library\helper;
|
|
use app\common\lock\RedisLock;
|
|
use app\common\model\Coupon;
|
|
use app\common\service\SybOil;
|
|
use app\common\service\IdingjuOil;
|
|
use app\common\service\MallOil;
|
|
use app\store\model\Company as CompanyModel;
|
|
use app\store\model\Coupon as CouponModel;
|
|
use app\store\model\GoodsCoupon as GoodsCouponModel;
|
|
use app\common\model\UserCoupon as UserCouponModel;
|
|
use think\facade\Db;
|
|
use think\facade\Event;
|
|
use app\common\service\BaseService;
|
|
use app\api\model\User as UserModel;
|
|
use app\api\model\Order as OrderModel;
|
|
use app\api\model\user\BalanceLog as BalanceLogModel;
|
|
use app\common\service\goods\source\Factory as StockFactory;
|
|
use app\common\enum\OrderType as OrderTypeEnum;
|
|
use app\common\enum\order\PayStatus as PayStatusEnum;
|
|
use app\common\enum\order\PayType as OrderPayTypeEnum;
|
|
use app\common\enum\user\balanceLog\Scene as SceneEnum;
|
|
|
|
/**
|
|
* 订单支付成功服务类
|
|
* Class PaySuccess
|
|
* @package app\api\service\order
|
|
*/
|
|
class PaySuccess extends BaseService
|
|
{
|
|
// 订单模型
|
|
public $model;
|
|
|
|
// 当前用户信息
|
|
private $user;
|
|
|
|
/**
|
|
* 构造函数
|
|
* PaySuccess constructor.
|
|
* @param $orderNo
|
|
*/
|
|
public function __construct($orderNo)
|
|
{
|
|
parent::__construct();
|
|
// 实例化订单模型
|
|
$this->model = OrderModel::getPayDetail($orderNo);
|
|
// 获取用户信息
|
|
$this->user = UserModel::detail($this->model['user_id']);
|
|
}
|
|
|
|
/**
|
|
* 获取订单详情
|
|
* @return OrderModel|null
|
|
*/
|
|
public function getOrderInfo()
|
|
{
|
|
return $this->model;
|
|
}
|
|
|
|
/**
|
|
* 订单支付成功业务处理
|
|
* @param $payType
|
|
* @param array $payData
|
|
* @return bool
|
|
*/
|
|
public function onPaySuccess($payType, $payData = [])
|
|
{
|
|
if (empty($this->model)) {
|
|
$this->error = '未找到该订单信息';
|
|
return false;
|
|
}
|
|
// 更新付款状态
|
|
$status = $this->updatePayStatus($payType, $payData);
|
|
// 订单支付成功事件
|
|
if ($status == true) {
|
|
Event::trigger('OrderPaySuccess', ['order' => $this->model, 'orderType' => OrderTypeEnum::ORDER]);
|
|
}
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* 更新付款状态
|
|
* @param $payType
|
|
* @param array $payData
|
|
* @return bool
|
|
*/
|
|
private function updatePayStatus($payType, $payData = [])
|
|
{
|
|
// 验证余额支付时用户余额是否满足
|
|
if ($payType == OrderPayTypeEnum::BALANCE) {
|
|
if ($this->user['balance'] < $this->model['pay_price']) {
|
|
$this->error = '用户余额不足,无法使用余额支付';
|
|
return false;
|
|
}
|
|
}
|
|
// 获取订单详情
|
|
$detail = OrderModel::getUserOrderDetail((int)$this->model['order_id']);
|
|
// 事务处理
|
|
$this->model->transaction(function () use ($payType, $payData, $detail) {
|
|
// 更新订单状态
|
|
$this->updateOrderInfo($payType, $payData);
|
|
// 累积用户总消费金额
|
|
UserModel::setIncPayMoney($detail['user_id'], (float)$this->model['pay_price']);
|
|
// 记录订单支付信息
|
|
$this->updatePayInfo($payType);
|
|
});
|
|
// 触发分油/发券(放在事务之后:企油通发放是真金白银,必须等订单确实置为已支付再发,
|
|
// 否则事务回滚会出现「订单未支付但油费已下发」)
|
|
$shareStatus = $this->shareOil($detail);
|
|
if (!$shareStatus) {
|
|
// 记录日志;不影响支付成功的结果,发放失败由 7.8 判定后自动退回用户余额
|
|
log_record(['name' => '分油失败', [
|
|
'order_id' => $this->model['order_id'],
|
|
'error' => $this->error,
|
|
]]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function processUserOrders($companyId) {
|
|
$redisLock = new RedisLock('user_order_lock');
|
|
$maxAttempts = 10; // 最大尝试次数
|
|
$attempt = 0;
|
|
$sleepTime = 500000; // 每次尝试失败后的等待时间(微秒),500000微秒 = 0.5秒
|
|
|
|
while ($attempt < $maxAttempts) {
|
|
$lockAcquired = $redisLock->acquireLock();
|
|
if ($lockAcquired) {
|
|
break;
|
|
}
|
|
|
|
$attempt++;
|
|
usleep($sleepTime); // 等待一段时间再尝试
|
|
}
|
|
|
|
if (!$lockAcquired) {
|
|
throwError('网络繁忙请稍后');
|
|
}
|
|
|
|
$todayStart = strtotime(date('Y-m-d 00:00:00'));
|
|
// 查找一个未被调用的用户
|
|
$account = Db::name('account')
|
|
->alias('a')
|
|
->join('company c', 'a.company_id = c.company_id')
|
|
->where('a.last_called_at', '<', $todayStart)
|
|
->where('a.is_delete', '=', 0)
|
|
->where('a.state', '=', 0)
|
|
->where('a.company_id', '=', $companyId)
|
|
->where('c.status', '=', 10)
|
|
->field('a.*')
|
|
->limit(1)
|
|
->find();
|
|
if (!$account) {
|
|
throwError("没有可用的用户");
|
|
}
|
|
// 更新用户的 `last_called_at`
|
|
Db::name('account')
|
|
->where('account_id', $account['account_id'])
|
|
->update(['last_called_at' => time(), 'state' => 1]);
|
|
|
|
$redisLock->releaseLock();
|
|
return $account;
|
|
}
|
|
|
|
/**
|
|
* 分油
|
|
* @param $order
|
|
* @return bool
|
|
*/
|
|
// private function shareOil($order): bool
|
|
// {
|
|
// $SybOil = new SybOil();
|
|
// foreach ($order->goods as $goods) {
|
|
// /* @var GoodsModel $goods */
|
|
// if($goods['oilCardType']) {
|
|
// $data['amount'] = $goods['total_price']; // 金额
|
|
// try {
|
|
// // 查找一个未被调用的用户
|
|
// $account = $this->processUserOrders();
|
|
// } catch (\Throwable $e) {
|
|
// $this->error = $e->getMessage();
|
|
// return false;
|
|
// }
|
|
// $company = CompanyModel::detail($account['company_id']);
|
|
// // 查询公司余额
|
|
// if ($data['amount'] > $company['balance']) {
|
|
// $this->error = '公司余额不足';
|
|
// return false;
|
|
// }
|
|
// // 对接平台接口
|
|
// $res = $SybOil->oilOilCardShare(['goodsNo'=>$goods['goods_no'],'amount'=>$data['amount'],'userNo'=>$account['userNo'],'mobile'=>$account['mobile'],'saleOrderNo'=>$this->model['order_no'],'oilCardType'=>$goods['oilCardType']], $company);
|
|
// //$res = json_decode('{"code":"00","message":"请求成功","errCode":null,"errMessage":null,"data":{"oilCardNo":null,"amount":100,"balance":null,"shareNo":"CDV2408021928160001"}}', true);
|
|
// if ($res['code'] !== '00') {
|
|
// $this->error = ($res['errMessage'] ?? $res['message']);
|
|
// return false;
|
|
// }
|
|
// $item['account_id'] = $account['account_id'];
|
|
// $item['userNo'] = $account['userNo'];
|
|
// $item['mobile'] = $account['mobile'];
|
|
// $item['company_id'] = $company['company_id'];
|
|
// $item['oilCardNo'] = $goods['oilCardType'] == 'zsy_ticket' ? $res['data']['shareNo'] : $res['data']['oilCardNo'];
|
|
// $item['shareNo'] = $res['data']['shareNo'] ?? '';
|
|
// $item['oil_time'] = time();
|
|
// $item['oil_status'] = 1;
|
|
// $goods->save($item);
|
|
// }
|
|
// }
|
|
// return true;
|
|
// }
|
|
|
|
/**
|
|
* @param $goodsNo
|
|
* @return string
|
|
*/
|
|
public function getCompany($goodsNo): string
|
|
{
|
|
switch ($goodsNo) {
|
|
case 'djgdface100':
|
|
case 'djgdface200':
|
|
case 'djgdface300':
|
|
return 'umQZhIHKofyA';
|
|
case 'jmgxyyface100':
|
|
case 'jmgxyyface200':
|
|
case 'jmgxyyface300':
|
|
|
|
case 'jmgzface100':
|
|
case 'jmgzface200':
|
|
case 'jmgzface300':
|
|
|
|
case 'jmfjface100':
|
|
case 'jmfjface200':
|
|
case 'jmfjface300':
|
|
return 'HpWqXIgYvTBg';
|
|
case 'face003':
|
|
return 'NKJBgaaRNdiL'; // 测试参数
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 支付成功后的商品发放
|
|
* · 油券(zsy_ticket) -> 建企油通发放单,由 partners:queue 调 7.7 下发
|
|
* · 优惠券(coupon) -> 直接发券(原逻辑不变)
|
|
*
|
|
* 返回 false 只用于写一条「分油失败」日志,不阻断支付成功:用户已经付过款,
|
|
* 订单必须正常置为已支付。发放失败由 7.8 判定后自动退回用户余额,或后台「重新发放」。
|
|
*/
|
|
private function shareOil($order): bool
|
|
{
|
|
$ok = true;
|
|
// 下单用户(含实名信息:姓名/身份证号/车牌号,结算时已校验必填)
|
|
$user = Db::name('user')->where('user_id', (int)$order['user_id'])->find() ?: [];
|
|
foreach ($order->goods as $goods) {
|
|
/* @var GoodsModel $goods */
|
|
if ($goods['oilCardType'] == 'zsy_ticket') {
|
|
// 企油通:一个油券商品行建一条发放单,金额取该行实付金额
|
|
$res = MallOil::create($order->toArray(), $goods->toArray(), $user);
|
|
if (!$res['ok']) {
|
|
$this->error = $res['message'];
|
|
$goods->save(['oil_text' => mb_substr($res['message'], 0, 190)]);
|
|
$ok = false;
|
|
}
|
|
} elseif ($goods['oilCardType'] == 'coupon') {
|
|
// 发放优惠券
|
|
$coupons = GoodsCouponModel::getCouponList($goods['goods_id'])->toArray();
|
|
if($coupons && is_array($coupons)) {
|
|
foreach ($coupons as $item) {
|
|
$coupon = CouponModel::detail($item['coupon_id']);
|
|
for ($i=0;$i<$item['send_num'];$i++){
|
|
$userCouponModel = new UserCouponModel();
|
|
$userCouponModel->add($goods['user_id'], $coupon);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $ok;
|
|
}
|
|
|
|
/**
|
|
* 更新订单记录
|
|
* @param int $payType
|
|
* @param array $payData
|
|
* @return false|int
|
|
* @throws \Exception
|
|
*/
|
|
private function updateOrderInfo(int $payType, array $payData)
|
|
{
|
|
// 更新商品库存、销量
|
|
StockFactory::getFactory($this->model['order_source'])->updateStockSales($this->model['goods']);
|
|
// 整理订单信息
|
|
$order = [
|
|
'pay_type' => $payType,
|
|
'pay_status' => PayStatusEnum::SUCCESS,
|
|
'pay_time' => time()
|
|
];
|
|
if ($payType == OrderPayTypeEnum::WECHAT) {
|
|
$order['transaction_id'] = $payData['transaction_id'];
|
|
}
|
|
// 更新订单状态
|
|
return $this->model->save($order);
|
|
}
|
|
|
|
/**
|
|
* 记录订单支付信息
|
|
* @param int $payType
|
|
*/
|
|
private function updatePayInfo(int $payType)
|
|
{
|
|
// 余额支付
|
|
if ($payType == OrderPayTypeEnum::BALANCE) {
|
|
// 更新用户余额
|
|
UserModel::setDecBalance((int)$this->user['user_id'], (float)$this->model['pay_price']);
|
|
// 新增余额变动记录
|
|
BalanceLogModel::add(SceneEnum::CONSUME, [
|
|
'user_id' => (int)$this->user['user_id'],
|
|
'money' => -$this->model['pay_price'],
|
|
], ['order_no' => $this->model['order_no']]);
|
|
}
|
|
// 微信支付
|
|
if ($payType == OrderPayTypeEnum::WECHAT) {
|
|
|
|
}
|
|
}
|
|
|
|
}
|