From bf95dd00dee28f78fa189d124c87fa04e800d1f3 Mon Sep 17 00:00:00 2001
From: limu <limu@163.com>
Date: Mon, 29 Jan 2024 14:51:14 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BA=97=E4=B8=BB=E8=AE=A2=E5=8D=95=E6=93=8D?=
 =?UTF-8?q?=E4=BD=9C=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/api/controller/StoreKeeper.php | 173 +++++++++++++++++++++++++++++
 app/store/model/Order.php          |  70 ++++++++++--
 app/store/model/OrderRefund.php    |  33 ++++--
 3 files changed, 255 insertions(+), 21 deletions(-)

diff --git a/app/api/controller/StoreKeeper.php b/app/api/controller/StoreKeeper.php
index d72bca20..f879baa9 100644
--- a/app/api/controller/StoreKeeper.php
+++ b/app/api/controller/StoreKeeper.php
@@ -13,6 +13,9 @@ declare (strict_types=1);
 namespace app\api\controller;
 
 use app\api\service\User as UserService;
+use app\store\model\Express as ExpressModel;
+use app\store\model\store\shop\Clerk as ClerkModel;
+use app\store\service\order\Delivery as DeliveryService;
 use cores\exception\BaseException;
 use think\App;
 use think\response\Json;
@@ -21,6 +24,8 @@ use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
 use think\db\exception\ModelNotFoundException;
 use app\store\model\Goods as GoodsModel;
+use app\store\model\Order as OrderModel;
+use app\store\model\OrderRefund as OrderRefundModel;
 
 /**
  * 店主管理
@@ -165,5 +170,173 @@ class StoreKeeper extends Controller
         return $this->renderSuccess('删除成功');
     }
 
+    //订单相关
+
+    /**
+     * 订单列表
+     * @param string $dataType
+     * @return Json
+     */
+    public function orderList(string $dataType): Json
+    {
+        $params = $this->request->param();
+        $params['searchType'] = 'all';
+        // 订单列表
+        if (!empty($params['dataType']) && $params['dataType'] == 'refund') {
+            $model = new OrderRefundModel;
+            $list = $model->getList($params);
+            return $this->renderSuccess(compact('list'));
+        } else {
+            $model = new OrderModel;
+            $list = $model->getList($params);
+            return $this->renderSuccess(compact('dataType', 'list'));
+        }
+
+    }
+
+    /**
+     * 获取全部记录
+     * @return Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function expressAll(): Json
+    {
+        $model = new ExpressModel;
+        $list = $model->getAll($this->request->param());
+        return $this->renderSuccess(compact('list'));
+    }
+
+    /**
+     * 修改订单价格
+     * @param int $orderId
+     * @return Json
+     */
+    public function updatePrice(int $orderId): Json
+    {
+        // 订单详情
+        $model = OrderModel::detail($orderId);
+        if ($model->updatePrice($this->postForm())) {
+            return $this->renderSuccess('操作成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
+    /**
+     * 修改订单地址
+     * @param int $orderId
+     * @return Json
+     */
+    public function updateAddress(int $orderId): Json
+    {
+        // 订单详情
+        $model = OrderModel::detail($orderId);
+        if ($model->updateAddress($this->postForm())) {
+            return $this->renderSuccess('操作成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
+    /**
+     * 审核:用户取消订单
+     * @param $orderId
+     * @return Json
+     */
+    public function confirmCancel($orderId): Json
+    {
+        // 订单详情
+        $model = OrderModel::detail($orderId);
+        if ($model->confirmCancel($this->postForm())) {
+            return $this->renderSuccess('操作成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
+    /**
+     * 删除订单记录
+     * @param int $orderId
+     * @return Json
+     */
+    public function oderDelete(int $orderId): Json
+    {
+        // 订单详情
+        $model = OrderModel::detail($orderId);
+        // 确认核销
+        if ($model->setDelete()) {
+            return $this->renderSuccess('删除成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
+    /**
+     * 门店自提核销
+     * @param int $orderId
+     * @return Json
+     */
+    public function extract(int $orderId): Json
+    {
+        // 订单详情
+        $model = OrderModel::detail($orderId);
+        if ($model->extractEvent($this->postForm())) {
+            return $this->renderSuccess('核销成功');
+        }
+        return $this->renderError($model->getError() ?: '核销失败');
+    }
+
+    public function clerkAll(): Json
+    {
+        // 店员列表
+        $model = new ClerkModel;
+        $list = $model->getAll($this->request->param());
+        return $this->renderSuccess(compact('list'));
+    }
+
+    /**
+     * 确认发货 (手动录入)
+     * @param int $orderId 订单ID
+     * @return Json
+     */
+    public function delivery(int $orderId): Json
+    {
+        $service = new DeliveryService;
+        if ($service->delivery($orderId, $this->postForm())) {
+            return $this->renderSuccess('发货成功');
+        }
+        return $this->renderError($service->getError() ?: '发货失败');
+    }
+
+    /**
+     * 商家审核
+     * @param int $orderRefundId
+     * @return Json
+     */
+    public function audit(int $orderRefundId): Json
+    {
+        // 售后单详情
+        $model = OrderRefundModel::detail($orderRefundId);
+        // 确认审核
+        if ($model->audit($this->postForm())) {
+            return $this->renderSuccess('操作成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
+    /**
+     * 确认收货并退款
+     * @param int $orderRefundId
+     * @return Json
+     */
+    public function receipt(int $orderRefundId): Json
+    {
+        // 售后单详情
+        $model = OrderRefundModel::detail($orderRefundId);
+        // 确认收货并退款
+        if ($model->receipt($this->postForm())) {
+            return $this->renderSuccess('操作成功');
+        }
+        return $this->renderError($model->getError() ?: '操作失败');
+    }
+
 
 }
\ No newline at end of file
diff --git a/app/store/model/Order.php b/app/store/model/Order.php
index a0b6b01a..e228cb9b 100644
--- a/app/store/model/Order.php
+++ b/app/store/model/Order.php
@@ -61,19 +61,33 @@ class Order extends OrderModel
     {
         // 检索查询条件
         $filter = $this->getQueryFilter($param);
+        $filterOr = [];
+        if (!empty($param['searchValue']) && $param['searchType'] == 'all') {
+            $filterOr = [
+                ['order.order_no', 'like', "%{$param['searchValue']}%"],
+                ['user.nick_name', 'like', "%{$param['searchValue']}%"],
+                ['address.name', 'like', "%{$param['searchValue']}%"],
+                ['address.phone', 'like', "%{$param['searchValue']}%"],
+            ];
+        }
+
         // 设置订单类型条件
         $dataTypeFilter = $this->getFilterDataType($param['dataType']);
         // 获取数据列表
-        return $this->with(['goods.image', 'user.avatar', 'address'])
+        $query = $this->with(['goods.image', 'user.avatar', 'address'])
             ->alias('order')
             ->field('order.*')
             ->leftJoin('user', 'user.user_id = order.user_id')
             ->leftJoin('order_address address', 'address.order_id = order.order_id')
             ->where($dataTypeFilter)
             ->where($filter)
-            ->where('order.is_delete', '=', 0)
-            ->order(['order.create_time' => 'desc'])
+            ->where('order.is_delete', '=', 0);
+        if (!empty($filterOr)) {
+            $query = $query->whereOr($filterOr);
+        }
+        $list = $query->order(['order.create_time' => 'desc'])
             ->paginate(10);
+        return $list;
     }
 
     /**
@@ -122,14 +136,16 @@ class Order extends OrderModel
         $filter = [];
         // 关键词
         if (!empty($params['searchValue'])) {
-            $searchWhere = [
-                10 => ['order.order_no', 'like', "%{$params['searchValue']}%"],
-                20 => ['user.nick_name', 'like', "%{$params['searchValue']}%"],
-                30 => ['order.user_id', '=', (int)$params['searchValue']],
-                40 => ['address.name', 'like', "%{$params['searchValue']}%"],
-                50 => ['address.phone', 'like', "%{$params['searchValue']}%"],
-            ];
-            array_key_exists($params['searchType'], $searchWhere) && $filter[] = $searchWhere[$params['searchType']];
+            if ($params['searchType'] != 'all') {
+                $searchWhere = [
+                    10 => ['order.order_no', 'like', "%{$params['searchValue']}%"],
+                    20 => ['user.nick_name', 'like', "%{$params['searchValue']}%"],
+                    30 => ['order.user_id', '=', (int)$params['searchValue']],
+                    40 => ['address.name', 'like', "%{$params['searchValue']}%"],
+                    50 => ['address.phone', 'like', "%{$params['searchValue']}%"],
+                ];
+                array_key_exists($params['searchType'], $searchWhere) && $filter[] = $searchWhere[$params['searchType']];
+            }
         }
         // 起止时间
         if (!empty($params['betweenTime'])) {
@@ -221,6 +237,35 @@ class Order extends OrderModel
             ]) !== false;
     }
 
+    /**
+     * 修改收货地址
+     * @param array $data
+     * @return bool
+     */
+    public function updateAddress(array $data): bool
+    {
+        if ($this['pay_status'] != PayStatusEnum::SUCCESS
+            || $this['delivery_status'] == DeliveryStatusEnum::DELIVERED || $this['delivery_type'] != 10) {
+            $this->error = "订单号[{$this['order_no']}]不满足修改条件!";
+            return false;
+        }
+        if (empty($data['region']) || empty($data['name']) || empty($data['phone'])) {
+            $this->error = "请补全地址信息!";
+            return false;
+        }
+        $update = [
+            'name' => $data['name'],
+            'phone' => $data['phone'],
+            'province_id' => $data['region'][0]['value'],
+            'city_id' => $data['region'][1]['value'],
+            'region_id' => $data['region'][2]['value'],
+            'detail' => $data['detail'],
+
+        ];
+        return (bool)OrderAddress::where(['order_id' => $this['order_id']])->update($update);
+
+    }
+
     /**
      * 修改商家备注
      * @param array $data
@@ -276,7 +321,8 @@ class Order extends OrderModel
             }
             // 更新订单状态
             return $this->save([
-                'order_status' => $data['status'] ? OrderStatusEnum::CANCELLED : OrderStatusEnum::NORMAL
+                'order_status' => $data['status'] ? OrderStatusEnum::CANCELLED : OrderStatusEnum::NORMAL,
+                'remark' => $data['remark'] ?? '',
             ]);
         });
     }
diff --git a/app/store/model/OrderRefund.php b/app/store/model/OrderRefund.php
index 64cc5485..c43c714f 100644
--- a/app/store/model/OrderRefund.php
+++ b/app/store/model/OrderRefund.php
@@ -36,13 +36,25 @@ class OrderRefund extends OrderRefundModel
     {
         // 检索查询条件
         $filter = $this->getFilter($param);
+        $filterOr = [];
+        if (!empty($param['searchValue']) && $param['searchType'] == 'all') {
+            $filterOr = [
+                ['order.order_no', 'like', "%{$param['searchValue']}%"],
+                ['user.nick_name', 'like', "%{$param['searchValue']}%"],
+                ['order.user_id', '=', (int)$param['searchValue']]
+            ];
+        }
+
         // 获取列表数据
-        $list = $this->alias('refund')
+        $query = $this->alias('refund')
             ->field('refund.*, order.order_no')
             ->join('order', 'order.order_id = refund.order_id')
             ->join('user', 'user.user_id = order.user_id')
-            ->where($filter)
-            ->order(['refund.create_time' => 'desc', 'refund.' . $this->getPk()])
+            ->where($filter);
+        if(!empty($filterOr)){
+            $query = $query->whereOr($filterOr);
+        }
+        $list = $query->order(['refund.create_time' => 'desc', 'refund.' . $this->getPk()])
             ->paginate(10);
         // 加载关联订单数据
         return static::preload($list, ['orderGoods.image', 'orderData', 'user.avatar']);
@@ -79,12 +91,15 @@ class OrderRefund extends OrderRefundModel
         $filter = [];
         // 关键词
         if (!empty($params['searchValue'])) {
-            $searchWhere = [
-                10 => ['order.order_no', 'like', "%{$params['searchValue']}%"],
-                20 => ['user.nick_name', 'like', "%{$params['searchValue']}%"],
-                30 => ['order.user_id', '=', (int)$params['searchValue']]
-            ];
-            array_key_exists($params['searchType'], $searchWhere) && $filter[] = $searchWhere[$params['searchType']];
+            if ($params['searchType'] != 'all') {
+                $searchWhere = [
+                    10 => ['order.order_no', 'like', "%{$params['searchValue']}%"],
+                    20 => ['user.nick_name', 'like', "%{$params['searchValue']}%"],
+                    30 => ['order.user_id', '=', (int)$params['searchValue']]
+                ];
+                array_key_exists($params['searchType'], $searchWhere) && $filter[] = $searchWhere[$params['searchType']];
+            }
+
         }
         // 起止时间
         if (!empty($params['betweenTime'])) {