فهرست منبع

Wesmiler 校企小程序 更新 6.20

wesmiler 3 سال پیش
والد
کامیت
14a193218f
1فایلهای تغییر یافته به همراه318 افزوده شده و 53 حذف شده
  1. 318 53
      app/api/model/book/Order.php

+ 318 - 53
app/api/model/book/Order.php

@@ -19,7 +19,9 @@ use app\api\model\SpecialityBook;
 use app\api\model\User;
 use app\api\service\User as UserService;
 use app\common\library\helper;
+use app\common\library\wechat\WxPay;
 use app\common\model\book\Order as OrderModel;
+use app\common\model\wxapp\Setting as WxappSettingModel;
 use app\common\service\Order as OrderService;
 use app\api\service\book\PaySuccess as OrderPaySuccesService;
 use app\api\service\Order\source\Factory as OrderSourceFactory;
@@ -34,6 +36,8 @@ use app\common\exception\BaseException;
  */
 class Order extends OrderModel
 {
+    protected $globalScope = [''];
+
     /**
      * 隐藏字段
      * @var array
@@ -89,25 +93,6 @@ class Order extends OrderModel
         return $order;
     }
 
-    /**
-     * 订单支付事件
-     * @param int $payType
-     * @return bool
-     */
-    public function onPay(int $payType = OrderPayTypeEnum::WECHAT): bool
-    {
-        // 判断订单状态
-        $orderSource = OrderSourceFactory::getFactory('Book');
-        if (!$orderSource->checkOrderStatusOnPay($this)) {
-            $this->error = $orderSource->getError();
-            return false;
-        }
-        // 余额支付
-        if ($payType == OrderPayTypeEnum::BALANCE) {
-//            return $this->onPaymentByBalance($this['order_no']);
-        }
-        return true;
-    }
 
     /**
      * 获取用户报名订单详情(仅订单记录)
@@ -119,10 +104,7 @@ class Order extends OrderModel
     public static function getDetail(int $orderId, array $with = [])
     {
         // 查询订单记录
-        $order = static::detail([
-            'order_id' => $orderId,
-            'user_id' => UserService::getCurrentLoginUserId(),
-        ], $with);
+        $order = static::detail($orderId, $with);
         empty($order) && throwError('订单不存在');
         return $order;
     }
@@ -198,7 +180,7 @@ class Order extends OrderModel
         }
 
         $trade = \app\store\model\Setting::getItem('trade');
-        $price = isset($trade['costs']['book_cost'])? floatval($trade['costs']['book_cost']) : 0;
+        $price = isset($trade['books']['book_cost'])? floatval($trade['books']['book_cost']) : 0;
         if($price<=0){
             $this->error = '请先后台设置报名费用';
             return false;
@@ -249,60 +231,343 @@ class Order extends OrderModel
         return $this->save($order);
     }
 
+    /**
+     * 验证支付订单
+     * @param $order
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function checkPay($order)
+    {
+        $orderId = isset($order['order_id'])? $order['order_id'] : 0;
+        if($orderId<=0){
+            return false;
+        }
+        $orderStatus = isset($order['status'])? $order['status'] : 0;
+        if($orderStatus == 3){
+            $this->error = '订单已支付';
+            return false;
+        }
+
+        if($orderStatus != 2){
+            $this->error = '订单未确认或状态不可支付';
+            return false;
+        }
+
+        $specialityId = isset($order['speciality_id'])? intval($order['speciality_id']) : 0;
+        // 专业参数验证
+        $speciality = SchoolSpeciality::detail($specialityId);
+        if(empty($speciality)){
+            $this->error = '报名专业不存在';
+            return false;
+        }
+
+        // 报名人数验证
+        $bookNum = SpecialityBook::getBooks($specialityId);
+        $remainBookNum = max(0, $speciality['recruit_num'] - $bookNum);
+        if($remainBookNum <= 0){
+            $this->error = '该专业报名人数已满';
+            return false;
+        }
+
+        // 已经报名次数
+        $trade = \app\store\model\Setting::getItem('books');
+        $bookLimit = isset($trade['books']['book_limit'])? intval($trade['books']['book_limit']) : 0;
+        $bookLimit = $bookLimit? $bookLimit : 3;
+        if(SpecialityBook::getUserBooks($orderId) >= $bookLimit){
+            $this->error = "您已经报名了{$bookLimit}次,不能再报名";
+            return false;
+        }
+
+        return true;
+    }
 
     /**
-     * 生成订单
-     * @param int $payType 支付方式
-     * @param array $params
-     * @return array|array[]|bool
+     * 确认报名订单
+     * @param $orderId
+     * @param $noticeImg
+     * @return bool
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function getOrderData(int $payType, array $params=[])
+    public function confirmOrder($orderId, $noticeImg)
     {
-        // 订单信息
-        $data = [
-            'user_id' => UserService::getCurrentLoginUserId(),
-            'order_no' => 'BK' . OrderService::createOrderNo(),
-            'pay_type' => $payType,
-            'price' => 0.00,
-            'store_id' => self::$storeId,
-        ];
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+        if(empty($noticeImg)){
+            $this->error = '请上传录取通知书';
+            return false;
+        }
+
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+        // 订单所属老师才何以设置
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+        if($model['status'] != 1){
+            $this->error = '订单已确认或状态不可操作';
+            return false;
+        }
 
-        // 实际到账金额
-        $data['order']['total_money'] = 0;
-        return $data;
+        return $model->save(['status'=> 2,'notice_img'=> $noticeImg]);
     }
 
     /**
-     * 表单验证
-     * @param int $payType
-     * @param int $schoolId
-     * @param int $specialityId
-     * @param float $price
+     * 订单发货设置
+     * @param int $orderId 订单ID
+     * @param $params 发货参数:快递公司ID、快递公司名称、快递单号
      * @return bool
+     * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function validateForm(int $payType, int $schoolId, int $specialityId, float $price)
+    public function setDelivery(int $orderId, $params)
     {
-        if ($schoolId <= 0) {
-            $this->error = '解锁专业所属学校参数错误';
+        $data['express_id'] = isset($params['express_id'])? $params['express_id'] : '';
+        $data['express_company'] = isset($params['express_company'])? $params['express_company'] : '';
+        $data['express_no'] = isset($params['express_no'])? $params['express_no'] : '';
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
             return false;
         }
-        if ($specialityId <= 0) {
-            $this->error = '解锁专业参数错误';
+        if(empty($data['express_id'])){
+            $this->error = '请选择快递公司';
             return false;
         }
-        if ($price <= 0) {
-            $this->error = '请先后台设置报名费用';
+        if(empty($data['express_company'])){
+            $this->error = '请选择快递公司';
+            return false;
+        }
+        if(empty($data['express_no'])){
+            $this->error = '请填写快递单号';
             return false;
         }
 
-        return true;
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+        // 订单所属老师才何以设置对应发货信息
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+        if($model['status'] != 3){
+            $this->error = '订单已发货或状态不可操作';
+            return false;
+        }
+
+        $data['status'] = 4;
+        $data['delivery_time'] = time();
+        return $model->save($data);
     }
 
+    /**
+     * 订单确认收货
+     * @param $orderId
+     * @return false
+     * @throws BaseException
+     * @throws \cores\exception\BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function receiptOrder($orderId)
+    {
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['book_user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+        if($model['refund_status'] != 4){
+            $this->error = '订单已收货或状态不可操作';
+            return false;
+        }
+
+        // 直接收货完成
+        $data['status'] = 6;
+        $data['receipt_time'] = time();
+        return $model->save($data);
+    }
+
+    /**
+     * 设置订单为可退款
+     * @param int $orderId
+     * @return bool
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function setRefund(int $orderId)
+    {
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+
+        // 订单所属老师才可以操作
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+        if($model['refund_status'] != 1 || $model['status'] <=1){
+            $this->error = '订单设置为可退款或状态不可操作';
+            return false;
+        }
+
+        return $model->save(['refund_status'=> 2]);
+    }
+
+    /**
+     * 订单申请退款
+     * @param $orderId
+     * @param $refundResult 退款原因
+     * @return bool
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function refundOrder($orderId, $refundResult)
+    {
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+
+        if(empty($refundResult)){
+            $this->error = '请填写退款原因';
+            return false;
+        }
+
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+        // 订单所属用户才可以操作
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['book_user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+        if($model['refund_status'] != 2 || $model['status'] <=1){
+            $this->error = '订单已申请退款或状态不可操作';
+            return false;
+        }
+
+        return $model->save(['refund_status'=> 3,'refund_result'=> $refundResult]);
+    }
+
+
+    /**
+     * 订单退款确认
+     * @param $orderId
+     * @param $refundResult 退款凭证
+     * @return bool
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function refundConfirm($orderId, $refundImg)
+    {
+        if(empty($orderId)){
+            $this->error = '订单参数错误';
+            return false;
+        }
+
+        if(empty($refundResult)){
+            $this->error = '请填写退款原因';
+            return false;
+        }
+
+        $model = new Order();
+        $model = $model::getUserOrderDetail($orderId);
+        if(!$model){
+            $this->error = '报名订单不存在';
+            return false;
+        }
+        // 订单所属老师才可以处理
+        $userInfo = UserService::getCurrentLoginUser(true);
+        if($userInfo['user_id'] != $model['user_id']){
+            $this->error = '订单无权操作';
+            return false;
+        }
+
+        if($model['refund_status'] != 3 || $model['status'] <= 0){
+            $this->error = '订单已退款或状态不可操作';
+            return false;
+        }
+
+        // 调起退款
+        if(!$this->refund($model)){
+            $this->error = '退款处理失败';
+            return false;
+        }
+
+        return $model->save(['refund_status'=> 4,'refund_img'=> $refundImg,'status'=> -2]);
+    }
+
+    public function cancel()
+    {
+
+    }
+
+    /**
+     * 退款调起
+     * @param $order
+     * @param int $money
+     * @return bool
+     * @throws BaseException
+     * @throws \cores\exception\BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    private function refund($order, $money=0)
+    {
+        $wxConfig = WxappSettingModel::getWxappConfig(getStoreId());
+        $WxPay = new WxPay($wxConfig, getStoreId());
+        $money = $money? $money : $order['pay_price'];
+        return $WxPay->refund($order['transaction_id'], $order['pay_price'], $money);
+    }
 }