Files
xk-api-yii/common/handlers/orderHandler/BaseOrderCanceledHandler.php
2024-09-19 11:32:39 +08:00

159 lines
4.8 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: 风哀伤
* Date: 2019/4/13
* Time: 17:55
* @copyright: ©2019 浙江禾匠信息科技
* @link: http://www.zjhejiang.com
*/
namespace common\handlers\orderHandler;
use common\events\OrderEvent;
use common\forms\order\CommonLog;
use common\forms\order\CommonOrder;
use common\models\GoodsAttr;
use common\models\Order;
use common\models\OrderDetail;
use common\models\UserCoupon;
use yii\db\Exception;
/**
* @property User $user
*/
abstract class BaseOrderCanceledHandler extends BaseOrderHandler
{
public $user;
public function handle()
{
return $this->cancel();
}
protected function cancel()
{
$t = \Yii::$app->db->beginTransaction();
try {
CommonLog::saveLog($this->event->order->id,'订单取消canceled事件start');
/* @var OrderEvent $event */
$this->action();
$t->commit();
CommonLog::saveLog($this->event->order->id,'订单取消canceled事件end');
} catch (\Exception $exception) {
$t->rollBack();
throw $exception;
}
}
protected function action()
{
$this->couponResume()->extraCancel($this->event->order)->goodsAddStock($this->event->order);
// $this->couponResume()->sendTemplate()->goodsAddStock($this->event->order)->sendSmsToUser();
}
/**
* 优惠券恢复
* @return $this
*/
protected function couponResume()
{
// 优惠券恢复
if ($this->event->order->use_user_coupon_id) {
$userCoupon = UserCoupon::findOne(['id' => $this->event->order->use_user_coupon_id]);
$userCoupon->is_use = 0;
$userCoupon->save();
}
CommonLog::saveLog($this->event->order->id,'订单取消优惠券恢复');
return $this;
}
/**
* 额外的取消处理
* @param $order
* @throws Exception
* @throws \yii\base\Exception
*/
protected function extraCancel($order)
{
(new CommonOrder())->extraCancel($order);
return $this;
}
protected function sendTemplate()
{
// try {
// $order = $this->event->order;
// $remark = $order->cancel_status == 1 ? '商家同意取消' : '商家拒绝取消';
//
// $goodsName = '';
// foreach ($order->detail as $orderDetail) {
// $goodsName .= $orderDetail->goods->name;
// }
//
// TemplateList::getInstance()->getTemplateClass(OrderCancelInfo::TPL_NAME)->send([
// 'goodsName' => $goodsName,
// 'order_no' => $order->order_no,
// 'price' => $order->total_pay_price,
// 'remark' => $remark,
// 'user' => $order->user,
// 'page' => 'pages/order/index/index?status=2'
// ]);
// } catch (\Exception $exception) {
// \Yii::error('模板消息发送: ' . $exception->getMessage());
// }
return $this;
}
/**
* @param Order $order
* @throws Exception
*/
protected function goodsAddStock($order)
{
/* @var OrderDetail[] $orderDetail */
$orderDetail = $order->detail;
$goodsAttrIdList = [];
$goodsNum = [];
foreach ($orderDetail as $item) {
$goodsInfo = json_decode($item->goods_info,true);
$goodsAttrIdList[] = $goodsInfo['goods_attr']['id'];
$goodsNum[$goodsInfo['goods_attr']['id']] = $item->num;
}
$goodsAttrList = GoodsAttr::find()->where(['id' => $goodsAttrIdList])->all();
/* @var GoodsAttr[] $goodsAttrList */
foreach ($goodsAttrList as $goodsAttr) {
$goodsAttr->updateStock($goodsNum[$goodsAttr->id], 'add');
}
CommonLog::saveLog($this->event->order->id,'订单取消商品库存返还');
return $this;
}
protected function sendSmsToUser()
{
// try {
// \Yii::warning('----消息发送提醒----');
// $order = $this->event->order;
// if (!$order->user->mobile) {
// throw new \Exception('用户未绑定手机号无法发送');
// }
// $messageService = new MessageService();
// $messageService->user = $order->user;
// $messageService->content = [
// 'mch_id' => $order->mch_id,
// 'args' => [substr($order->order_no, -6)]
// ];
// $messageService->platform = PlatformConfig::getInstance()->getPlatform($order->user);
// $messageService->tplKey = OrderCancelInfo::TPL_NAME;
// $res = $messageService->templateSend();
// } catch (\Exception $exception) {
// \Yii::error('向用户发送短信消息失败');
// \Yii::error($exception);
// }
return $this;
}
}