WIN-2308041133\Administrator vor 1 Tag
Ursprung
Commit
426adfd2d1

+ 14 - 45
app/adminapi/controller/v1/merchant/SystemStore.php

@@ -192,7 +192,8 @@ class SystemStore extends AuthController
             [['keywords', 's'], ''],
             [['status', 'd'], '']
         ]);
-        return app('json')->success($this->services->getApplyList($where));
+        $services = app()->make(\app\services\system\store\SystemStoreRecordServices::class);
+        return app('json')->success($services->getApplyList($where));
     }
 
     /**
@@ -204,40 +205,13 @@ class SystemStore extends AuthController
     {
         if (!$id) return app('json')->fail(100100);
 
-        $storeInfo = $this->services->get($id);
-        if (!$storeInfo) {
-            return app('json')->fail('门店不存在');
-        }
-
-        // 更新审核状态为通过
-        $res = $this->services->update($id, ['status' => 1]);
-        if (!$res) {
-            return app('json')->fail('审核失败');
-        }
-
-        // 添加用户为店员
-        $staffServices = app()->make(\app\services\system\store\SystemStoreStaffServices::class);
-        $userServices = app()->make(\app\services\user\UserServices::class);
-
-        // 检查该用户是否已经是店员
-        if (!$staffServices->count(['uid' => $storeInfo['uid']])) {
-            // 从user表获取店员的image和staff_name
-            $userInfo = $userServices->getUserInfo($storeInfo['uid']);
-
-            $staffData = [
-                'uid' => $storeInfo['uid'],
-                'store_id' => $id,
-                'staff_name' => $userInfo['nickname'] ?? '',
-                'avatar' => $userInfo['avatar'] ?? ($userInfo['image'] ?? ''),
-                'phone' => $storeInfo['phone'],
-                'verify_status' => 1,
-                'status' => 1,
-                'add_time' => time(),
-            ];
-            $staffServices->save($staffData);
+        try {
+            $services = app()->make(\app\services\system\store\SystemStoreRecordServices::class);
+            $services->approve($id);
+            return app('json')->success('审核通过');
+        } catch (\Exception $e) {
+            return app('json')->fail($e->getMessage());
         }
-
-        return app('json')->success('审核通过');
     }
 
     /**
@@ -249,17 +223,12 @@ class SystemStore extends AuthController
     {
         if (!$id) return app('json')->fail(100100);
 
-        $storeInfo = $this->services->get($id);
-        if (!$storeInfo) {
-            return app('json')->fail('门店不存在');
+        try {
+            $services = app()->make(\app\services\system\store\SystemStoreRecordServices::class);
+            $services->reject($id);
+            return app('json')->success('已拒绝');
+        } catch (\Exception $e) {
+            return app('json')->fail($e->getMessage());
         }
-
-        // 更新审核状态为拒绝
-        $res = $this->services->update($id, ['status' => 2]);
-        if (!$res) {
-            return app('json')->fail('审核失败');
-        }
-
-        return app('json')->success('已拒绝');
     }
 }

+ 27 - 18
app/api/controller/v1/admin/StoreOrderController.php

@@ -768,13 +768,13 @@ class StoreOrderController
         ]);
         $id = $data['id'];
         unset($data['id']);
-        /** @var SystemStoreServices $storeServices */
-        $storeServices = app()->make(SystemStoreServices::class);
+
         $agent_id = User::where('uid',$uid)->value('agent_level');
         $agent_level = \app\model\agent\AgentLevel::where('id',$agent_id)->value('grade');
         if ($agent_level<3){
             return app('json')->fail("V2会员才能创建门店");
         }
+
         // 处理数据
         $data['uid'] = $uid;
         if (is_array($data['address'])) {
@@ -793,31 +793,40 @@ class StoreOrderController
             $data['image'] = $site_url . $data['image'];
         }
 
+        /** @var SystemStoreRecordServices $recordServices */
+        $recordServices = app()->make(SystemStoreRecordServices::class);
+
         // 设置申请状态为待审核
         $data['status'] = 0;
 
-        // 如果传入了id,则修改门店信息(仅修改基本信息,不涉及审核)
-        if ($id) {
-            // 检查门店是否存在
-            $storeInfo = $storeServices->getOne(['id' => $id]);
-            if (!$storeInfo) {
-                return app('json')->fail('门店不存在');
-            }
+        // 检查是否有未审核或已通过的申请记录
+        $existingRecord = $recordServices->getLatestRecord($uid);
 
-            $storeServices->saveStore($id, $data);
-            return app('json')->success(100014);
+        if ($existingRecord && ($existingRecord['status'] == 0 || $existingRecord['status'] == 1)) {
+            return app('json')->fail('您已有门店申请记录,请勿重复提交');
         }
 
-        // 检查同uid是否已有门店
-        $existingStore = $storeServices->getOne(['uid' => $uid]);
-        if ($existingStore) {
-            // 如果已有门店,更新信息(不改变审核状态)
-            $storeServices->saveStore($existingStore['id'], $data);
+        // 如果传入了id,则修改申请记录(仅修改基本信息,不改变审核状态)
+        if ($id) {
+            // 检查申请记录是否存在
+            $recordInfo = $recordServices->get($id);
+            if (!$recordInfo) {
+                return app('json')->fail('申请记录不存在');
+            }
+            if ($recordInfo['uid'] != $uid) {
+                return app('json')->fail('无权修改他人申请');
+            }
+            if ($recordInfo['status'] != 2) {
+                return app('json')->fail('只能修改已拒绝的申请');
+            }
+
+            $recordServices->update($id, $data);
             return app('json')->success(100014);
         }
 
-        // 保存门店申请信息
-        $storeServices->saveStore(0, $data);
+        // 保存门店申请记录
+        $data['add_time'] = time();
+        $recordServices->save($data);
 
         return app('json')->success('申请已提交,请等待审核');
     }

+ 79 - 0
app/dao/system/store/SystemStoreRecordDao.php

@@ -0,0 +1,79 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+namespace app\dao\system\store;
+
+use app\dao\BaseDao;
+use app\model\system\store\SystemStoreRecord;
+
+/**
+ * 门店申请记录 dao
+ * Class SystemStoreRecordDao
+ * @package app\dao\system\store
+ */
+class SystemStoreRecordDao extends BaseDao
+{
+    /**
+     * 设置模型
+     * @return string
+     */
+    protected function setModel(): string
+    {
+        return SystemStoreRecord::class;
+    }
+
+    /**
+     * 获取门店申请列表
+     * @param array $where
+     * @param int $page
+     * @param int $limit
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getApplyList(array $where, int $page = 0, int $limit = 0)
+    {
+        $query = $this->search($where)->with(['user']);
+        
+        if ($page && $limit) {
+            $query->page($page, $limit);
+        }
+        
+        return $query->order('id desc')->select()->toArray();
+    }
+
+    /**
+     * 获取门店申请记录
+     * @param int $id
+     * @return array|\think\Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getApplyInfo(int $id)
+    {
+        return $this->search(['id' => $id])->with(['user'])->find();
+    }
+
+    /**
+     * 根据用户ID获取申请记录
+     * @param int $uid
+     * @return array|\think\Model|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getByUid(int $uid)
+    {
+        return $this->search(['uid' => $uid])->order('id desc')->find();
+    }
+}

+ 71 - 0
app/model/system/store/SystemStoreRecord.php

@@ -0,0 +1,71 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+namespace app\model\system\store;
+
+use think\Model;
+
+/**
+ * 门店申请记录模型
+ * Class SystemStoreRecord
+ * @package app\model\system\store
+ */
+class SystemStoreRecord extends Model
+{
+    /**
+     * 表名
+     * @var string
+     */
+    protected $name = 'system_store_record';
+
+    /**
+     * 自动写入时间戳
+     * @var bool
+     */
+    protected $autoWriteTimestamp = 'int';
+
+    /**
+     * 创建时间字段
+     * @var string
+     */
+    protected $createTime = 'add_time';
+
+    /**
+     * 更新时间字段
+     * @var string
+     */
+    protected $updateTime = 'update_time';
+
+    /**
+     * 获取状态文本
+     * @param $value
+     * @param $data
+     * @return string
+     */
+    public function getStatusTextAttr($value, $data)
+    {
+        $status = [
+            0 => '待审核',
+            1 => '已通过',
+            2 => '已拒绝',
+        ];
+        return $status[$value] ?? '未知';
+    }
+
+    /**
+     * 关联用户表
+     * @return \think\model\relation\BelongsTo
+     */
+    public function user()
+    {
+        return $this->belongsTo(\app\model\user\User::class, 'uid', 'uid')->field('uid,nickname,avatar,phone');
+    }
+}

+ 164 - 0
app/services/system/store/SystemStoreRecordServices.php

@@ -0,0 +1,164 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+namespace app\services\system\store;
+
+use app\dao\system\store\SystemStoreRecordDao;
+use app\services\BaseServices;
+use crmeb\exceptions\AdminException;
+use app\services\system\store\SystemStoreServices;
+use app\services\system\store\SystemStoreStaffServices;
+
+/**
+ * 门店申请记录服务
+ * Class SystemStoreRecordServices
+ * @package app\services\system\store
+ */
+class SystemStoreRecordServices extends BaseServices
+{
+    /**
+     * 构造方法
+     * SystemStoreRecordServices constructor.
+     * @param SystemStoreRecordDao $dao
+     */
+    public function __construct(SystemStoreRecordDao $dao)
+    {
+        $this->dao = $dao;
+    }
+
+    /**
+     * 获取门店申请列表
+     * @param array $where
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getApplyList(array $where)
+    {
+        $searchWhere = [];
+        if (isset($where['keywords']) && $where['keywords'] !== '') {
+            $searchWhere[] = ['name|phone', 'like', '%' . $where['keywords'] . '%'];
+        }
+        if (isset($where['status']) && $where['status'] !== '') {
+            $searchWhere['status'] = $where['status'];
+        }
+
+        [$page, $limit] = $this->getPageValue();
+        $list = $this->dao->getApplyList($searchWhere, $page, $limit);
+        $count = $this->dao->count($searchWhere);
+
+        return compact('list', 'count');
+    }
+
+    /**
+     * 通过门店申请
+     * @param int $id
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function approve(int $id)
+    {
+        $recordInfo = $this->dao->getApplyInfo($id);
+        if (!$recordInfo) {
+            throw new AdminException('申请记录不存在');
+        }
+
+        if ($recordInfo['status'] != 0) {
+            throw new AdminException('该申请已审核');
+        }
+
+        return $this->transaction(function () use ($recordInfo, $id) {
+            // 更新申请状态为通过
+            $res = $this->dao->update($id, ['status' => 1]);
+            if (!$res) {
+                throw new AdminException('审核失败');
+            }
+
+            // 创建正式门店记录
+            $storeServices = app()->make(SystemStoreServices::class);
+            $storeData = [
+                'name' => $recordInfo['name'],
+                'phone' => $recordInfo['phone'],
+                'address' => $recordInfo['address'],
+                'detailed_address' => $recordInfo['detailed_address'],
+                'latlng' => $recordInfo['latlng'],
+                'latitude' => $recordInfo['latitude'],
+                'longitude' => $recordInfo['longitude'],
+                'day_time' => $recordInfo['day_time'],
+                'valid_time' => $recordInfo['valid_time'],
+                'image' => $recordInfo['image'],
+                'uid' => $recordInfo['uid'],
+                'is_show' => 1,
+                'add_time' => time(),
+            ];
+            $storeId = $storeServices->save($storeData);
+
+            if (!$storeId) {
+                throw new AdminException('创建门店失败');
+            }
+
+            // 添加用户为店员
+            $staffServices = app()->make(SystemStoreStaffServices::class);
+            $userServices = app()->make(\app\services\user\UserServices::class);
+
+            // 检查该用户是否已经是店员
+            if (!$staffServices->count(['uid' => $recordInfo['uid']])) {
+                // 从user表获取店员的image和staff_name
+                $userInfo = $userServices->getUserInfo($recordInfo['uid']);
+
+                $staffData = [
+                    'uid' => $recordInfo['uid'],
+                    'store_id' => $storeId,
+                    'staff_name' => $userInfo['nickname'] ?? '',
+                    'avatar' => $userInfo['avatar'] ?? ($userInfo['image'] ?? ''),
+                    'phone' => $recordInfo['phone'],
+                    'verify_status' => 1,
+                    'status' => 1,
+                    'add_time' => time(),
+                ];
+                $staffServices->save($staffData);
+            }
+
+            return true;
+        });
+    }
+
+    /**
+     * 拒绝门店申请
+     * @param int $id
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function reject(int $id)
+    {
+        $recordInfo = $this->dao->getApplyInfo($id);
+        if (!$recordInfo) {
+            throw new AdminException('申请记录不存在');
+        }
+
+        if ($recordInfo['status'] != 0) {
+            throw new AdminException('该申请已审核');
+        }
+
+        // 更新申请状态为拒绝
+        $res = $this->dao->update($id, ['status' => 2]);
+        if (!$res) {
+            throw new AdminException('审核失败');
+        }
+
+        return true;
+    }
+}

+ 3 - 3
app/services/system/store/SystemStoreServices.php

@@ -67,15 +67,15 @@ class SystemStoreServices extends BaseServices
     {
         $data['show'] = [
             'name' => '显示中的提货点',
-            'num' => $this->dao->count(['type' => 0,'status'=>1]),
+            'num' => $this->dao->count(['type' => 0]),
         ];
         $data['hide'] = [
             'name' => '隐藏中的提货点',
-            'num' => $this->dao->count(['type' => 1,'status'=>1]),
+            'num' => $this->dao->count(['type' => 1]),
         ];
         $data['recycle'] = [
             'name' => '回收站的提货点',
-            'num' => $this->dao->count(['type' => 2,'status'=>1])
+            'num' => $this->dao->count(['type' => 2])
         ];
         return $data;
     }