Blame view

application/admin/controller/qingdong/dingding/Department.php 3.48 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
  <?php

  

  namespace app\admin\controller\qingdong\dingding;

  

  use app\common\controller\Backend;

  use addons\qingdong\model\StaffDepartment;

  use fast\Tree;

  use app\admin\model\AuthGroup;

  use think\Exception;

  

  /**

   * 钉钉同步部门

   *

   */

  class Department extends Backend

  {

      public function _initialize()

      {

          parent::_initialize();

          $this->model = new StaffDepartment();

      }

      /**

       * 部门列表

       */

      public function index(){

  

          //设置过滤方法

          $this->request->filter(['strip_tags', 'trim']);

          if ($this->request->isAjax()) {

              // 必须将结果集转换为数组

              $ruleList = collection($this->model->where(['type'=>0])->order('id', 'desc')->select())->toArray();

              foreach ($ruleList as $k => &$v) {

                  $v['name'] = __($v['name']);

              }

              unset($v);

              Tree::instance()->init($ruleList);

              $rulelists = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');

  

              $ruledata = [0 => __('None')];

              foreach ($rulelists as $k => &$v) {

                  $ruledata[$v['id']] = $v['name'];

              }

              $list  = $rulelists;

              $total = count($rulelists);

  

              $result = array("total" => $total, "rows" => $list);

  

              return json($result);

          }

          return $this->view->fetch();

      }

      /**

       * 删除部门

       */

      public function del($ids = null) {

          if ($this->request->isAjax()) {

  

              //删除子部门

              $where=['pid' => array('in', $ids),'type'=>0];

              StaffDepartment::where($where)->delete();

  

              $map['id'] = array('in', $ids);

              $result    = StaffDepartment::where($map)->delete();

              if (!$result) {

                  $this->error('删除失败');

              }

              $this->success('删除成功');

          }

  

          return $this->view->fetch();

      }

      /**

       * 同步到CRM

       */

      public function batch(){

          $info = $this->model->where(array('status'=>0))->find();

          if(!$info){

              $this->error('无数据可同步');

          }

          try {

              $result=$this->setDepartment();

          }catch (Exception $e){

              $this->error($e->getMessage());

          }

          if($result){

              $this->success('同步成功');

          }

          $this->error('无数据可同步');

      }

      /**

       * 设置部门

       */

      private  function setDepartment($pid=0,$idinfo=0){

          $list = $this->model->where(array('type'=>0,'pid'=>$pid))->select();

          if(empty($list)){

              return false;

          }

          foreach ($list as $k => $v) {

              if($v['status'] != 0){

                  $this->setDepartment($v['id'], $v['role_id']);

                  continue;

              }

              $res1 = array(

                  'pid' => $idinfo,

                  'name' => $v['name'],

                  'status' => 'normal',

                  'rules' => '*'

              );

              $result1 = AuthGroup::create($res1);

              if (!$result1) {

                  throw new Exception('同步失败');

              }

              $idinfo = AuthGroup::getLastInsID();

              $pid = $v['id'];

              $this->setDepartment($pid, $idinfo);

              $this->model->where(array('id' => $v['id']))->update(array('status' => 1, 'role_id' => $idinfo));

          }

          return true;

      }

  

  }