| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace app\api\model\order;
- use app\api\model\product\Product as ProductModel;
- use app\api\service\order\paysuccess\type\MasterPaySuccessService;
- use app\api\service\order\PaymentService;
- use app\api\model\settings\Setting as SettingModel;
- use app\common\enum\order\OrderPayTypeEnum;
- use app\common\enum\order\OrderSourceEnum;
- use app\common\enum\order\OrderTypeEnum;
- use app\common\enum\order\OrderPayStatusEnum;
- use app\common\enum\order\OrderStatusEnum;
- use app\common\exception\BaseException;
- use app\common\service\order\OrderCompleteService;
- use app\common\enum\settings\DeliveryTypeEnum;
- use app\common\library\helper;
- use app\common\model\order\Order as OrderModel;
- use app\api\service\order\checkpay\CheckPayFactory;
- use app\common\service\product\factory\ProductFactory;
- use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
- /**
- * 普通订单模型
- */
- class Order extends OrderModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'app_id',
- 'update_time'
- ];
- /**
- * 订单支付事件
- */
- public function onPay($payType, $pay_source)
- {
- // 判断订单状态
- $checkPay = CheckPayFactory::getFactory($this['order_source']);
- if (!$checkPay->checkOrderStatus($this)) {
- $this->error = $checkPay->getError();
- return false;
- }
- // 余额支付
- if ($payType == OrderPayTypeEnum::BALANCE) {
- $data['attach'] = '{"pay_source":"' . $pay_source . '"}';
- return $this->onPaymentByBalance($this['order_no'], $data);
- }
- return true;
- }
- /**
- * 用户中心订单列表
- */
- public function getList($user_id, $type, $params)
- {
- // 筛选条件
- $filter = [];
- // 订单数据类型
- switch ($type) {
- case 'all':
- break;
- case 'payment';//待付款
- $filter['pay_status'] = OrderPayStatusEnum::PENDING;//付款状态(10未付款)
- $filter['order_status'] = 10;//订单状态10=>进行中
- break;
- case 'delivery';//代发货
- $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;//已付款
- $filter['delivery_status'] = 10;//未发货
- $filter['order_status'] = 10;//订单状态10=>进行中
- break;
- case 'received';//待收货
- $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
- $filter['delivery_status'] = 20;//已发货
- $filter['receipt_status'] = 10;//未收货
- $filter['order_status'] = 10;//订单状态10=>进行中
- break;
- case 'comment';//待评价
- $filter['is_comment'] = 0;//未评价
- $filter['order_status'] = 30;//订单状态30=>已完成
- break;
- }
- return $this->with(['product.image'])
- ->where('user_id', '=', $user_id)
- ->where($filter)
- ->where('is_delete', '=', 0)
- ->order(['create_time' => 'desc'])
- ->paginate($params);
- }
- /**
- * 确认收货
- */
- public function receipt()
- {
- // 验证订单是否合法
- // 条件1: 订单必须已发货
- // 条件2: 订单必须未收货
- if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
- $this->error = '该订单不合法';
- return false;
- }
- return $this->transaction(function () {
- // 更新订单状态
- $status = $this->save([
- 'receipt_status' => 20,
- 'receipt_time' => time(),
- 'order_status' => 30
- ]);
- // 执行订单完成后的操作
- $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
- $OrderCompleteService->complete([$this], static::$app_id);
- return $status;
- });
- }
- /**
- * 立即购买:获取订单商品列表
- */
- public static function getOrderProductListByNow($params)
- {
- // 商品详情
- $product = ProductModel::detail($params['product_id']);
- // 商品sku信息
- $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
- $product['total_pv'] = helper::bcmul($product['pv'], $params['product_num']);
-
- // 商品列表
- $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
- foreach ($productList as &$item) {
- // 商品单价
- $item['product_price'] = $item['product_sku']['product_price'];
- // 商品购买数量
- $item['total_num'] = $params['product_num'];
- $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
- $item['pv'] = $item['product_sku']['pv'];
- $item['total_pv'] = helper::bcmul($item['pv'], $params['product_num']);
- // 商品购买总金额
- $item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
- }
-
- return $productList;
- }
- /**
- * 获取订单总数
- */
- public function getCount($user, $type = 'all')
- {
- if ($user === false) {
- return false;
- }
- // 筛选条件
- $filter = [];
- // 订单数据类型
- switch ($type) {
- case 'all':
- break;
- case 'payment';
- $filter['pay_status'] = OrderPayStatusEnum::PENDING;
- break;
- case 'delivery';
- $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
- $filter['delivery_status'] = 10;
- $filter['order_status'] = 10;
- break;
- case 'received';
- $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
- $filter['delivery_status'] = 20;
- $filter['receipt_status'] = 10;
- break;
- case 'comment';
- $filter['order_status'] = 30;
- $filter['is_comment'] = 0;
- break;
- }
- return $this->where('user_id', '=', $user['user_id'])
- ->where('order_status', '<>', 20)
- ->where($filter)
- ->where('is_delete', '=', 0)
- ->count();
- }
- /**
- * 取消订单
- */
- public function cancel($user)
- {
- if ($this['delivery_status']['value'] == 20) {
- $this->error = '已发货订单不可取消';
- return false;
- }
- //进行中的拼团订单不能取消
- if($this['order_source'] == OrderSourceEnum::ASSEMBLE){
- if($this['assemble_status'] == 10){
- $this->error = '订单正在拼团,到期后如果订单未拼团成功将自动退款';
- return false;
- }
- }
- // 订单取消事件
- return $this->transaction(function () use ($user) {
- // 订单是否已支付
- $isPay = $this['pay_status']['value'] == OrderPayStatusEnum::SUCCESS;
- // 未付款的订单
- if ($isPay == false) {
- //主商品退回库存
- ProductFactory::getFactory($this['order_source'])->backProductStock($this['product'], $isPay);
- // 回退用户优惠券
- $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
- // 回退用户积分
- $describe = "订单取消:{$this['order_no']}";
- $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
- }
- // 更新订单状态
- return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
- });
- }
- /**
- * 订单详情
- */
- public static function getUserOrderDetail($order_id, $user_id)
- {
- $model = new static();
- $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id])->with(['product' => ['image', 'refund','last_refund'], 'address', 'express', 'extractStore'])->find();
- if (empty($order)) {
- throw new BaseException(['msg' => '订单不存在']);
- }
- return $order;
- }
- /**
- * 余额支付标记订单已支付
- */
- public function onPaymentByBalance($orderNo, $data)
- {
- // 获取订单详情
- $PaySuccess = new MasterPaySuccessService($orderNo);
- // 发起余额支付
- $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE, $data);
- if (!$status) {
- $this->error = $PaySuccess->getError();
- }
- return $status;
- }
- /**
- * 构建微信支付请求
- */
- protected static function onPaymentByWechat($user, $order, $pay_source)
- {
- return PaymentService::wechat(
- $user,
- $order['order_id'],
- $order['order_no'],
- $order['pay_price'],
- OrderTypeEnum::MASTER,
- $pay_source
- );
- }
- /**
- * 待支付订单详情
- */
- public static function getPayDetail($orderNo, $pay_status)
- {
- $model = new static();
- $model = $model->where('order_no', '=', $orderNo)->where('is_delete', '=', 0);
- if($pay_status > 0){
- $model = $model->where('pay_status', '=', 10);
- }
- return $model->with(['product', 'user'])->find();
- }
- /**
- * 构建支付请求的参数
- */
- public static function onOrderPayment($user, $order, $payType, $pay_source)
- {
- //如果来源是h5,首次不处理,payH5再处理
- if($pay_source == 'h5'){
- return [];
- }
- if ($payType == OrderPayTypeEnum::WECHAT) {
- return self::onPaymentByWechat($user, $order, $pay_source);
- }
- if ($payType == OrderPayTypeEnum::ALIPAY) {
- return self::onPaymentByAlipay($user, $order, $pay_source);
- }
- return [];
- }
- /**
- * 判断当前订单是否允许核销
- */
- public function checkExtractOrder($order)
- {
- if (
- $order['pay_status']['value'] == OrderPayStatusEnum::SUCCESS
- && $order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT
- && $order['delivery_status']['value'] == 10
- ) {
- return true;
- }
- $this->setError('该订单不能被核销');
- return false;
- }
- /**
- * 当前订单是否允许申请售后
- */
- public function isAllowRefund()
- {
- // 必须是已发货的订单
- if ($this['delivery_status']['value'] != 20) {
- return false;
- }
- // 允许申请售后期限(天)
- $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
- // 不允许售后
- if ($refundDays == 0) {
- return false;
- }
- // 当前时间超出允许申请售后期限
- if (
- $this['receipt_status']['value'] == 20
- && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
- ) {
- return false;
- }
- return true;
- }
- /**
- * 获取活动订单
- * 已付款,未取消
- */
- public static function getPlusOrderNum($user_id, $product_id, $order_source)
- {
- $model = new static();
- return $model->alias('order')->where('order.user_id', '=', $user_id)
- ->join('order_product', 'order_product.order_id = order.order_id', 'left')
- ->where('order_product.product_source_id', '=', $product_id)
- ->where('order.pay_status', '=', 20)
- ->where('order.order_source', '=', $order_source)
- ->where('order.order_status', '<>', 20)
- ->count();
- }
- /**
- * 构建支付宝请求
- */
- protected static function onPaymentByAlipay($user, $order, $pay_source)
- {
- return PaymentService::alipay(
- $user,
- $order['order_id'],
- $order['order_no'],
- $order['pay_price'],
- OrderTypeEnum::MASTER,
- $pay_source
- );
- }
- /**
- * 主订单购买的数量
- * 未取消的订单
- */
- public static function getHasBuyOrderNum($user_id, $product_id)
- {
- $model = new static();
- return $model->alias('order')->where('order.user_id', '=', $user_id)
- ->join('order_product', 'order_product.order_id = order.order_id', 'left')
- ->where('order_product.product_id', '=', $product_id)
- ->where('order.order_source', '=', OrderSourceEnum::MASTER)
- ->where('order.pay_status', '<>', 10)
- ->sum('total_num');
- }
- /**
- * 设置错误信息
- */
- protected function setError($error)
- {
- empty($this->error) && $this->error = $error;
- }
- /**
- * 是否存在错误
- */
- public function hasError()
- {
- return !empty($this->error);
- }
- }
|