From 3720f44065eb3deeada02c50c14bffc0cf812059 Mon Sep 17 00:00:00 2001
From: tioncico <1067197739@qq.com>
Date: Fri, 18 Nov 2022 11:58:27 +0800
Subject: [PATCH] 完善客户编码等问题

---
 app/Admin/Renderable/CreateUser.php |  2 +-
 app/Admin/Service/UserService.php   | 10 ++++++++++
 app/Models/BaseModel.php            | 35 +++++++++++++++++++++++++++++++++++
 app/Models/User.php                 |  9 ++++++++-
 4 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 app/Models/BaseModel.php

diff --git a/app/Admin/Renderable/CreateUser.php b/app/Admin/Renderable/CreateUser.php
index ea89a23..ef95f1a 100644
--- a/app/Admin/Renderable/CreateUser.php
+++ b/app/Admin/Renderable/CreateUser.php
@@ -27,7 +27,7 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable
             $form->disableCreatingCheck();
             $form->row(function (Form\Row $form) {
                 $form->width(4)->text('company_name')->required();
-                $form->width(4)->text('user_sn');
+                $form->width(4)->text('user_sn')->disable();
             });
             $form->row(function (Form\Row $form) {
                 $form->width(4)->text('first_name')->required();
diff --git a/app/Admin/Service/UserService.php b/app/Admin/Service/UserService.php
index 4775e74..d2a4c3b 100644
--- a/app/Admin/Service/UserService.php
+++ b/app/Admin/Service/UserService.php
@@ -114,6 +114,8 @@ class UserService
                 "update_time"  => time(),
             ];
             $userId = User::insertData($userData);
+            $userSn = self::generateSn($userId);
+            User::updateById($userId, ["user_sn" => $userSn]);
 
             foreach ($params['user_address_list'] as $address) {
                 if ($address['_remove_']) {
@@ -162,6 +164,12 @@ class UserService
         }
     }
 
+    public static function generateSn($userId)
+    {
+        $id = str_pad($userId, 6, 0, STR_PAD_LEFT);
+        return 'SU' . $id;
+    }
+
     public static function transferUser($ids, $saleId)
     {
         try {
@@ -178,6 +186,8 @@ class UserService
                 "sale_name" => $saleInfo["name"],
             ];
             User::updateByIdArr($userIds, $update);
+            InquiryService::batchUpdateSalesId($saleId, $userIds);
+//            OrderService::changeOrderSales($userIds,$saleId,$saleInfo["name"]);
 
             DB::commit();
         } catch (\Throwable $throwable) {
diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php
new file mode 100644
index 0000000..c476e7c
--- /dev/null
+++ b/app/Models/BaseModel.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Models;
+
+use App\Http\Services\PermService;
+use Illuminate\Database\Eloquent\Model;
+
+class BaseModel extends Model
+{    /*
+     * 权限
+     */
+    public function scopeRule($query, $viewList,$adminUser="sale_id")
+    {
+        if (getAdminUserId() == 1000) {
+            return $query;
+        }
+        //查看所有
+        if (PermService::hasPerm($viewList[0])) {
+            return $query;
+        }
+
+        //查看下级
+        if (PermService::hasPerm($viewList[1])) { //查看下级的权限//获取用户部门下的所有用户
+            $userIds = PermService::getSubUserId(getAdminUserId());
+            if (!empty($userIds)) {
+                return $query->whereIn($adminUser, $userIds);
+            }
+        }
+
+        $query = $query->where($adminUser, getAdminUserId());
+
+        return $query;
+    }
+
+}
diff --git a/app/Models/User.php b/app/Models/User.php
index eb015e2..3f08121 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -26,7 +26,7 @@ use Illuminate\Database\Eloquent\Model;
  * @property $created_time  创建时间
  * @property $update_time  更新时间
  */
-class User extends Model
+class User extends BaseModel
 {
     use HasDateTimeFormatter;
     protected $table = 'users';
@@ -39,6 +39,13 @@ class User extends Model
 
     const ACCOUNT_PROPERTIES_PERSONAL = 1;
     const ACCOUNT_PROPERTIES_ENTERPRISE = 2;
+    //查看权限
+    public static $ruleViewList = [
+        self::SEM_USER_VIEW_ALL, //查看所有
+        self::SEM_USER_VIEW_SUB,//查看下级
+    ];
+    const SEM_USER_VIEW_ALL = "sem_user_viewAllList";//查看所有
+    const SEM_USER_VIEW_SUB = "sem_user_viewSubList";//查看下级
 
     public static function insertData($data)
     {
--
libgit2 0.25.0