调整异常处理和告警

This commit is contained in:
luofei 2023-07-11 10:54:49 +08:00
parent 3893b0f287
commit 03c1c4e40f
10 changed files with 34 additions and 98 deletions

View File

@ -66,7 +66,9 @@ class ExceptionHandle extends Handle
{ {
// 添加自定义异常处理机制 // 添加自定义异常处理机制
$this->report($e); $this->report($e);
if ($e->getCode() != 0) {
DingTalk::exception($e, $e->getMessage(), $request); DingTalk::exception($e, $e->getMessage(), $request);
}
// 其他错误交给系统处理 // 其他错误交给系统处理
if ($e instanceof ValidateException) if ($e instanceof ValidateException)
return app('json')->fail($e->getMessage()); return app('json')->fail($e->getMessage());

View File

@ -59,14 +59,7 @@ class StoreGroupOrderDao extends BaseDao
*/ */
public function search(array $where) public function search(array $where)
{ {
$where['product_type'] = empty($where['product_type']) ? [0, 98] : $where['product_type']; return StoreGroupOrder::getDB()->alias('StoreGroupOrder')->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
if (is_array($where['product_type'])) {
$storeOrderWhere = StoreOrder::whereIn('activity_type', $where['product_type']);
} else {
$storeOrderWhere = StoreOrder::where('activity_type', $where['product_type']);
}
return StoreGroupOrder::hasWhere('orderList', $storeOrderWhere)
->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
if ($where['paid'] == 0) { if ($where['paid'] == 0) {
$query->whereRaw("StoreGroupOrder.paid=0 or (StoreGroupOrder.paid=1 and StoreGroupOrder.pay_type=8)"); $query->whereRaw("StoreGroupOrder.paid=0 or (StoreGroupOrder.paid=1 and StoreGroupOrder.pay_type=8)");
} else { } else {

View File

@ -16,12 +16,14 @@ namespace app\common\repositories\store\order;
use app\common\dao\store\order\StoreGroupOrderDao; use app\common\dao\store\order\StoreGroupOrderDao;
use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\repositories\BaseRepository; use app\common\repositories\BaseRepository;
use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository;
use app\common\repositories\user\UserBillRepository; use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserRepository; use app\common\repositories\user\UserRepository;
use crmeb\jobs\CancelGroupOrderJob; use crmeb\jobs\CancelGroupOrderJob;
use think\db\exception\DataNotFoundException;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\facade\Db; use think\facade\Db;
use think\facade\Queue; use think\facade\Queue;
@ -62,6 +64,10 @@ class StoreGroupOrderRepository extends BaseRepository
public function getList(array $where, $page, $limit) public function getList(array $where, $page, $limit)
{ {
$query = $this->search($where); $query = $this->search($where);
if (isset($where['product_type'])) {
$storeOrderWhere = StoreOrder::where('activity_type', $where['product_type']);
$query->hasWhere('orderList', $storeOrderWhere);
}
$count = $query->count(); $count = $query->count();
$list = $query->with(['orderList' => function (Relation $query) { $list = $query->with(['orderList' => function (Relation $query) {
$query->field('order_id,group_order_id,activity_type,pay_price,status,mer_id')->with(['merchant' => function ($query) { $query->field('order_id,group_order_id,activity_type,pay_price,status,mer_id')->with(['merchant' => function ($query) {
@ -106,6 +112,9 @@ class StoreGroupOrderRepository extends BaseRepository
}, 'orderProduct', 'presellOrder']); }, 'orderProduct', 'presellOrder']);
}, 'interest']) }, 'interest'])
->order('create_time DESC')->append(['cancel_time', 'cancel_unix'])->find(); ->order('create_time DESC')->append(['cancel_time', 'cancel_unix'])->find();
if (empty($order)) {
throw new DataNotFoundException('订单不存在或已取消');
}
$return = $order->toArray(); $return = $order->toArray();
if (!empty($order->interest)) { if (!empty($order->interest)) {
$interest = $order->interest->calculateInterest(); $interest = $order->interest->calculateInterest();
@ -146,7 +155,7 @@ class StoreGroupOrderRepository extends BaseRepository
*/ */
public function cancel($id, $uid = null) public function cancel($id, $uid = null)
{ {
$groupOrder = $this->search(['paid' => 0, 'uid' => $uid ?? ''])->where('StoreGroupOrder.group_order_id', $id)->with(['orderList'])->find(); $groupOrder = $this->search(['paid' => 0, 'uid' => $uid ?? ''])->where('group_order_id', $id)->with(['orderList'])->find();
if (!$groupOrder) if (!$groupOrder)
throw new ValidateException('订单不存在'); throw new ValidateException('订单不存在');
if ($groupOrder['paid'] != 0) if ($groupOrder['paid'] != 0)

View File

@ -43,6 +43,7 @@ use crmeb\services\QrcodeService;
use crmeb\services\RedisCacheService; use crmeb\services\RedisCacheService;
use crmeb\services\SwooleTaskService; use crmeb\services\SwooleTaskService;
use FormBuilder\Factory\Elm; use FormBuilder\Factory\Elm;
use think\db\exception\DataNotFoundException;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Db; use think\facade\Db;
@ -1610,14 +1611,14 @@ class ProductRepository extends BaseRepository
Db::startTrans(); Db::startTrans();
try { try {
if ($this->dao->updates($id,[$field => $status]) === false) { if ($this->dao->updates($id,[$field => $status]) === false) {
throw new \Exception('商品操作出错'); throw new \Exception('商品操作出错', 500);
} }
event('product.sell', ['product_id' => $productIds]); event('product.sell', ['product_id' => $productIds]);
Db::commit(); Db::commit();
Queue::push(ChangeSpuStatusJob::class,['id' => $id,'product_type'=> $product_type]); Queue::push(ChangeSpuStatusJob::class,['id' => $id,'product_type'=> $product_type]);
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
throw new ValidateException($e->getMessage()); throw new \Exception('商品操作出错', $e->getCode());
} }
} }
@ -2282,7 +2283,7 @@ class ProductRepository extends BaseRepository
if (!empty($product)) { if (!empty($product)) {
$unique = $this->importAttrValue($params['order_product_id'], $product, $params['order_unique']); $unique = $this->importAttrValue($params['order_product_id'], $product, $params['order_unique']);
if (!$unique) { if (!$unique) {
throw new \Exception('商品规格导入出错'); throw new \Exception('商品规格导入出错', 500);
} }
$attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $product['product_id'])->where('unique', $unique)->find(); $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $product['product_id'])->where('unique', $unique)->find();
} else { } else {
@ -2296,7 +2297,7 @@ class ProductRepository extends BaseRepository
$attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $params['product_id'])->where('unique', $params['unique'])->find(); $attrValue = ProductAttrValue::where('mer_id', $merId)->where('product_id', $params['product_id'])->where('unique', $params['unique'])->find();
} }
if (!$product || !$attrValue) { if (!$product || !$attrValue) {
throw new \Exception('商品或规格不存在'); throw new DataNotFoundException('商品或规格不存在');
} }
$stockIn = $params['number'] ?? 0; $stockIn = $params['number'] ?? 0;
$price = $params['price'] ?? 0; $price = $params['price'] ?? 0;
@ -2305,20 +2306,20 @@ class ProductRepository extends BaseRepository
$orderMerId = StoreOrder::where('order_id', $params['order_id'])->value('mer_id'); $orderMerId = StoreOrder::where('order_id', $params['order_id'])->value('mer_id');
$orderProduct = StoreOrderProduct::where('order_id', $params['order_id'])->where('product_id', $params['order_product_id'])->where('product_sku', $params['order_unique'])->find(); $orderProduct = StoreOrderProduct::where('order_id', $params['order_id'])->where('product_id', $params['order_product_id'])->where('product_sku', $params['order_unique'])->find();
if (empty($orderProduct) || $orderProduct->is_imported == 1) { if (empty($orderProduct) || $orderProduct->is_imported == 1) {
throw new \Exception('订单商品不存在或已入库'); throw new ValidateException('订单商品不存在或已入库');
} }
$stockIn = $orderProduct['product_num'] ?? 0; $stockIn = $orderProduct['product_num'] ?? 0;
$price = $orderProduct['product_price'] ?? 0; $price = $orderProduct['product_price'] ?? 0;
$supplierMerId = $orderMerId ?? 0; $supplierMerId = $orderMerId ?? 0;
} }
if ($stockIn <= 0) { if ($stockIn <= 0) {
throw new \Exception('入库数量不能小于等于0'); throw new ValidateException('入库数量不能小于等于0');
} }
$attrValue->stock = $attrValue->stock + $stockIn; $attrValue->stock = $attrValue->stock + $stockIn;
$attrValue->save(); $attrValue->save();
$product->stock = $stockIn + $product->stock; $product->stock = $stockIn + $product->stock;
if (!$product->save()) { if (!$product->save()) {
throw new \Exception('商品库存保存失败'); throw new \Exception('商品库存保存失败', 500);
} }
$model = new PurchaseRecord(); $model = new PurchaseRecord();
$data = [ $data = [
@ -2333,15 +2334,15 @@ class ProductRepository extends BaseRepository
'supplier_mer_id' => $supplierMerId, 'supplier_mer_id' => $supplierMerId,
]; ];
if (!$model->save($data)) { if (!$model->save($data)) {
throw new \Exception('入库失败'); throw new \Exception('入库失败', 500);
} }
if (isset($orderProduct) && !$orderProduct->save(['is_imported' => 1])) { if (isset($orderProduct) && !$orderProduct->save(['is_imported' => 1])) {
throw new \Exception('订单商品更新出错'); throw new \Exception('订单商品更新出错', 500);
} }
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage(), $e->getCode());
} }
return true; return true;
} }
@ -2357,16 +2358,16 @@ class ProductRepository extends BaseRepository
{ {
$mer_id = Db::name('store_service')->where('uid', $user['uid'])->where('status', 1)->value('mer_id'); $mer_id = Db::name('store_service')->where('uid', $user['uid'])->where('status', 1)->value('mer_id');
if ($mer_id == 0) { if ($mer_id == 0) {
throw new \Exception('商户不存在'); throw new DataNotFoundException('商户不存在');
} }
$find = Db::name('store_product')->where('product_id', $product_id)->find(); $find = Db::name('store_product')->where('product_id', $product_id)->find();
if ($find) { if ($find) {
if (!in_array($find['product_type'], [0, 98])) { if (!in_array($find['product_type'], [0, 98])) {
throw new \Exception('该商品不是普通商品'); throw new ValidateException('该商品不是普通商品');
} }
$exist = Db::name('store_product')->where('source_product_id', $product_id)->where('mer_id', $mer_id)->find(); $exist = Db::name('store_product')->where('source_product_id', $product_id)->where('mer_id', $mer_id)->find();
if ($exist) { if ($exist) {
throw new \Exception('已经导入过该商品了'); throw new ValidateException('已经导入过该商品了');
} }
$attr = Db::name('store_product_attr')->where('product_id', $find['product_id'])->field('attr_name,attr_values,type')->select(); $attr = Db::name('store_product_attr')->where('product_id', $find['product_id'])->field('attr_name,attr_values,type')->select();
foreach ($attr as $item) { foreach ($attr as $item) {

View File

@ -731,13 +731,13 @@ class MerchantRepository extends BaseRepository
$values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs'); $values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs');
$values['pay_time'] = date('y_m-d H:i:s', time()); $values['pay_time'] = date('y_m-d H:i:s', time());
if (!app()->make(ServeOrderDao::class)->create($values)) { if (!app()->make(ServeOrderDao::class)->create($values)) {
throw new \Exception('serve_order 保存出错'); throw new \Exception('serve_order 保存出错', 500);
} }
$merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2); $merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2);
$merchant->ot_margin = $merchant->paid_margin; $merchant->ot_margin = $merchant->paid_margin;
$merchant->is_margin = MerchantRepository::PaidMargin; $merchant->is_margin = MerchantRepository::PaidMargin;
if ($merchant->save() === false) { if ($merchant->save() === false) {
throw new \Exception('merchant 保存出错'); throw new \Exception('merchant 保存出错', 500);
} }
return [$income, $finance, true]; return [$income, $finance, true];

View File

@ -1,69 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\jobs;
use app\common\dao\system\serve\ServeOrderDao;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\system\serve\ServeOrderRepository;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
class AutoMarginJob implements JobInterface
{
public function fire($job, $data)
{
Log::info('utoMarginStart' . var_export($data, 1));
try {
$merchant = app()->make(MerchantRepository::class)->get($data['merId']);
$orderInfo = [
'type_id' => $merchant['type_id'],
'is_margin' => $merchant['is_margin'],
'margin' => $data['margin'],
];
$values = [
'status' => 1,
'is_del' => 0,
'mer_id' => $merchant['mer_id'],
'type' => ServeOrderRepository::TYPE_MARGIN,
'meal_id'=> $merchant['type_id'],
'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE,
'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE),
'pay_price' => $data['margin'],
'store_order_id' => $data['orderId'],
];
$values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs');
$values['pay_time'] = date('y_m-d H:i:s', time());
if (!app()->make(ServeOrderDao::class)->create($values)) {
throw new \Exception('serve_order 保存出错');
}
$merchant->paid_margin = bcadd($data['margin'], $merchant->paid_margin, 2);
$merchant->ot_margin = $merchant->paid_margin;
$merchant->is_margin = MerchantRepository::PaidMargin;
$merchant->save();
$job->delete();
} catch (\Exception $exception) {
Log::info('更新商户保证金出错:' . var_export($exception, 1));
} finally {
Log::info('autoMarginEnd' . var_export($data, 1));
}
}
public function failed($data)
{
// TODO: Implement failed() method.
}
}

View File

@ -322,7 +322,7 @@ class MiniProgramService
public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS',$openId = null, $transactionId = null) public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS',$openId = null, $transactionId = null)
{ {
if (empty($this->config['payment']['pay_routine_client_key']) || empty($this->config['payment']['pay_routine_client_cert'])) { if (empty($this->config['payment']['pay_routine_client_key']) || empty($this->config['payment']['pay_routine_client_cert'])) {
throw new \Exception('请配置微信支付证书'); throw new \Exception('请配置微信支付证书', 500);
} }
$totalFee = floatval($totalFee); $totalFee = floatval($totalFee);
$refundFee = floatval($refundFee); $refundFee = floatval($refundFee);

View File

@ -520,7 +520,7 @@ class WechatService
public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS') public function refund($orderNo, $refundNo, $totalFee, $refundFee = null, $opUserId = null, $refundReason = '', $type = 'out_trade_no', $refundAccount = 'REFUND_SOURCE_UNSETTLED_FUNDS')
{ {
if (empty($this->config['payment']['pay_weixin_client_cert']) || empty($this->config['payment']['pay_weixin_client_key'])) { if (empty($this->config['payment']['pay_weixin_client_cert']) || empty($this->config['payment']['pay_weixin_client_key'])) {
throw new \Exception('请配置微信支付证书'); throw new \Exception('请配置微信支付证书', 500);
} }
$totalFee = floatval($totalFee); $totalFee = floatval($totalFee);
$refundFee = floatval($refundFee); $refundFee = floatval($refundFee);

View File

@ -28,7 +28,7 @@ class Balance extends PayTool
} }
$user->now_money = bcsub($user->now_money, $order['pay_price'], 2); $user->now_money = bcsub($user->now_money, $order['pay_price'], 2);
if (!$user->save()) { if (!$user->save()) {
throw new \Exception('用户余额扣减失败'); throw new \Exception('用户余额扣减失败', 500);
} }
return true; return true;
} }

View File

@ -61,7 +61,7 @@ class Scrcu extends PayTool
$order['subject'] = $firstGoods->product->store_name . "{$order['total_num']}件商品"; $order['subject'] = $firstGoods->product->store_name . "{$order['total_num']}件商品";
return $order; return $order;
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception('商品信息错误'); throw new \Exception('商品信息错误', 500);
} }
} }