| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <?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 = OrderPayTypeEnum::WECHAT)
- {
- // 判断订单状态
- $checkPay = CheckPayFactory::getFactory($this['order_source']);
- if (!$checkPay->checkOrderStatus($this)) {
- $this->error = $checkPay->getError();
- return false;
- }
- // 余额支付
- if ($payType == OrderPayTypeEnum::BALANCE) {
- return $this->onPaymentByBalance($this['order_no']);
- }
- return true;
- }
- /**
- * 用户中心订单列表
- */
- public function getList($user_id, $type = 'all', $params)
- {
- // 筛选条件
- $filter = [];
- // 订单数据类型
- switch ($type) {
- case 'all':
- break;
- case 'payment';
- $filter['pay_status'] = OrderPayStatusEnum::PENDING;
- $filter['order_status'] = 10;
- 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;
- $filter['order_status'] = 10;
- break;
- case 'comment';
- $filter['is_comment'] = 0;
- $filter['order_status'] = 30;
- break;
- }
- $model = new self;
- if(isset($params['shop_supplier_id'])&&$params['shop_supplier_id']){
- $model = $model->where('shop_supplier_id','=',$params['shop_supplier_id']);
- }else{
- // 用户查询
- $model = $model->where('user_id', '=', $user_id);
- }
- return $model->with(['product.image', 'supplier'])
- ->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']);
- // 商品列表
- $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['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
- }
- $supplierData[] = [
- 'shop_supplier_id' => $product['shop_supplier_id'],
- 'supplier' => $product['supplier'],
- 'productList' => $productList
- ];
- unset($product['supplier']);
- return $supplierData;
- }
- /**
- * 获取订单总数
- */
- 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'], 'address', 'express', 'extractStore', 'supplier'])->find();
- if (empty($order)) {
- throw new BaseException(['msg' => '订单不存在']);
- }
- return $order;
- }
- /**
- * 供应商查看订单详情
- */
- public static function getSupplierOrderDetail($order_id, $shop_supplier_id)
- {
- $model = new static();
- $order = $model->where(['order_id' => $order_id, 'shop_supplier_id' => $shop_supplier_id,])->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore', 'supplier'])->find();
- if (empty($order)) {
- throw new BaseException(['msg' => '订单不存在']);
- }
- return $order;
- }
- /**
- * 余额支付标记订单已支付
- */
- public function onPaymentByBalance($orderNo)
- {
- // 获取订单详情
- $PaySuccess = new MasterPaySuccessService($orderNo);
- // 发起余额支付
- $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
- if (!$status) {
- $this->error = $PaySuccess->getError();
- }
- return $status;
- }
- /**
- * 构建微信支付请求
- */
- protected static function onPaymentByWechat($user, $order_arr, $pay_source)
- {
- return PaymentService::wechat(
- $user,
- $order_arr,
- OrderTypeEnum::MASTER,
- $pay_source
- );
- }
- /**
- * 构建支付宝请求
- */
- protected static function onPaymentByAlipay($user, $order_arr, $pay_source)
- {
- return PaymentService::alipay(
- $user,
- $order_arr,
- OrderTypeEnum::MASTER,
- $pay_source
- );
- }
- /**
- * 待支付订单详情
- */
- public static function getPayDetail($orderNo)
- {
- $model = new static();
- return $model->where(['order_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0])->with(['product', 'user'])->find();
- }
- /**
- * 构建支付请求的参数
- */
- public static function onOrderPayment($user, $order_arr, $payType, $pay_source)
- {
- //如果来源是h5,首次不处理,payH5再处理
- if($pay_source == 'h5'){
- return [];
- }
- if ($payType == OrderPayTypeEnum::WECHAT) {
- return self::onPaymentByWechat($user, $order_arr, $pay_source);
- }
- if ($payType == OrderPayTypeEnum::ALIPAY) {
- return self::onPaymentByAlipay($user, $order_arr, $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'] == 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)
- ->sum('total_num');
- }
- /**
- * 累计成交笔数
- */
- public static function getTotalPayOrder($shop_supplier_id){
- //累积成交笔数
- return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
- ->where('pay_status', '=', 20)
- ->where('order_status', 'in', [10,30])
- ->count();
- }
- public static function getTodayPayOrder($shop_supplier_id){
- //开始
- $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
- //结束
- $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
- //今日成交笔数
- return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
- ->where('pay_status', '=', 20)
- ->where('order_status', 'in', [10,30])
- ->whereBetweenTime('create_time', $beginToday, $endToday)
- ->count();
- }
- /**
- * 设置错误信息
- */
- protected function setError($error)
- {
- empty($this->error) && $this->error = $error;
- }
- /**
- * 是否存在错误
- */
- public function hasError()
- {
- return !empty($this->error);
- }
- /**
- * 获取直播订单
- */
- public function getLiveOrder($params){
- $model = $this;
- if(isset($params['room_id'])&&$params['room_id']){
- $model = $model->where('room_id','=',$params['room_id']);
- }
- if(isset($params['pay_status'])&&$params['pay_status']){
- $model = $model->where('pay_status','=',$params['pay_status']);
- }
- return $model->with(['product.image'])
- ->where('shop_supplier_id', '=', $params['shop_supplier_id'])
- ->where('room_id', '>', 0)
- ->where('is_delete', '=', 0)
- ->order(['create_time' => 'desc'])
- ->paginate($params);
- }
- /**
- * 主订单购买的数量
- * 未取消的订单
- */
- 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.order_status', '<>', 21)
- ->sum('total_num');
- }
- }
|