Просмотр исходного кода

1.获取当前账号绑定公司列表,2.切换默认公司,3.组织架构层级列表

wufeng 2 лет назад
Родитель
Сommit
d2635a694e
4 измененных файлов с 116 добавлено и 3 удалено
  1. 37 0
      app/common.php
  2. 34 2
      app/controller/CompanyItem.php
  3. 42 1
      app/controller/UserCompany.php
  4. 3 0
      route/app.php

+ 37 - 0
app/common.php

@@ -1,6 +1,8 @@
 <?php
 // 应用公共文件
 
+use think\facade\Db;
+
 if(!function_exists("json_show")){
  function json_show(int $code = 0, string $message = '请求成功', array $data = [])
     {
@@ -81,4 +83,39 @@ if(!function_exists('GetPart')){
     function GetPart(int $item_id=0){
         return '';
     }
+}
+
+//获取组织架构的层级列表
+//@param $data 顶级列表的数据集合
+//@param $vio 是否展示部门下的账号
+if (!function_exists('crea')) {
+    function crea($data, $vio = 0)
+    {
+        $db = Db::name("company_item")
+            ->field(true)
+            ->where(['pid' => $data['id'], 'is_del' => 0])
+            ->select()
+            ->toArray();
+        if ($vio == 1) {
+            $d = Db::name("sys_account_item")
+                ->alias('a')
+                ->field('a.account_id,b.username,c.nickname,c.mobile,b.status')
+                ->leftJoin('account b','b.id=a.account_id AND b.is_del=0')
+                ->leftJoin('user c','c.account_id=a.account_id')
+                ->where(['itemid' => $data['id'], 'is_del' => 0])
+                ->select()
+                ->toArray();
+            $data['item'] = $d;
+        }
+
+        if (empty($db)) {
+            $data['child'] = [];
+            return $data;
+        }
+        //var_dump($db);
+        foreach ($db as $p) {
+            $data['child'][] = crea($p, $vio);
+        }
+        return $data;
+    }
 }

+ 34 - 2
app/controller/CompanyItem.php

@@ -217,11 +217,43 @@ class CompanyItem extends BaseController
 
     }
 
-    //查询
+    //查询,层级列表
     public function query()
     {
-    }
+        $param = $this->request->filter('trim')->only(['companyNo'=>''],'post');
+
+        $where=[['pid','=',0],['is_del','=',0]];
+        if($param['companyNo'] != '') $where[]=['companyNo','=',$param['companyNo']];
+
+        //所有已经存在的公司
+        $companyNo = Db::name('company_item')
+            ->field('companyNo')
+            ->where($where)
+            ->group('companyNo')
+            ->buildSql();
+
+        $company = Db::name('headquarters')
+            ->field('id,code,name,type')
+            ->where('is_del', 0)
+            ->where('code IN ' . $companyNo)
+            ->select()
+            ->toArray();
 
+        foreach ($company as &$value) {
+            $value['child'] = Db::name('company_item')
+                ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $value['code']])
+                ->order("weight desc")
+                ->select()
+                ->toArray();
+
+            foreach ($value['child'] as &$item) {
+                $item = crea($item);
+            }
+        }
+
+        return json_show(0, '获取成功', $company);
+
+    }
 
     //删除
     public function delete()

+ 42 - 1
app/controller/UserCompany.php

@@ -119,7 +119,7 @@ class UserCompany extends BaseController
             ->where(['id' => $post['id'], 'is_del' => 0])
             ->update(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
 
-        return $rs ? json_show(0, '操作成功',$tmp) : json_show(1004, '操作失败');
+        return $rs ? json_show(0, '操作成功', $tmp) : json_show(1004, '操作失败');
 
     }
 
@@ -143,5 +143,46 @@ class UserCompany extends BaseController
         return json_show(0, '获取成功', $rs);
     }
 
+    //切换默认公司
+    public function changeMain()
+    {
+
+        $param = $this->request->only(['account_id','companyCode'], 'post', 'trim');
+
+        $val = Validate::rule([
+            'account_id|账号id' => 'require|number|gt:0',
+            'companyCode|公司编码' => 'require',
+        ]);
+
+        if ($val->check($param) == false) return json_show(1004, $val->getError());
+
+        Db::startTrans();
+        try {
+
+            $rs = Db::name('account_company')
+                ->field('id')
+                ->where(['account_id' => $param['account_id'], 'companyCode' => $param['companyCode'], 'is_del' => 0])
+                ->findOrEmpty();
+
+            if (empty($rs)) throw new Exception('该账号尚未绑定该公司');
+
+            Db::name('account_company')
+                ->where(['account_id' => $param['account_id'], 'is_main' => 1, 'is_del' => 0])
+                ->update(['is_main' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
+
+            Db::name('account_company')
+                ->where('id', $rs['id'])
+                ->update(['is_main' => 1]);
+
+            Db::commit();
+            return json_show(0, '切换成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(1005, '切换失败,' . $exception->getMessage());
+        }
+
+
+    }
+
 
 }

+ 3 - 0
route/app.php

@@ -37,6 +37,7 @@ Route::rule('userCompanyList', 'admin/UserCompany/getList');
 Route::rule('userCompanyInfo', 'admin/UserCompany/info');
 Route::rule('userCompanyUpdate', 'admin/UserCompany/update');
 Route::rule('userCompanyStatus', 'admin/UserCompany/status');
+Route::rule('changeMain', 'admin/UserCompany/changeMain');
 
 //公司汇总
 Route::rule('hqAdd', 'Headquarters/add');
@@ -64,4 +65,6 @@ Route::rule('refresh', 'CompanyItem/update');
 Route::rule('itemdel', 'CompanyItem/delete');
 Route::rule('stat', 'CompanyItem/status');
 Route::rule('userp', 'CompanyItem/userp');
+Route::rule('query', 'CompanyItem/query');
+