123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 组织架构
  4. // +----------------------------------------------------------------------
  5. // | Author:
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use app\admin\controller\ApiCommon;
  9. use app\admin\model\Common;
  10. use think\Db;
  11. class Structure extends Common
  12. {
  13. /**
  14. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  15. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  16. */
  17. protected $name = 'admin_structure';
  18. /**
  19. * [getDataList 获取列表]
  20. * @return [array]
  21. */
  22. public function getDataList($type='')
  23. {
  24. $cat = new \com\Category('admin_structure', array('id', 'pid', 'name', 'title'));
  25. $data = $cat->getList('', 0, 'id');
  26. foreach ($data as $k=>$v){
  27. $data[$k]['owner_user_name']=foreachData('admin_structure',$v['owner_user_id'])?:null;
  28. //选择部门传入当前部门权限范围内的员工
  29. $data[$k]['user_id']=$this->getSubUserByStructrue($v['id']);
  30. }
  31. // 若type为tree,则返回树状结构
  32. if ($type == 'tree') {
  33. $tree = new \com\Tree();
  34. $data = $tree->list_to_tree($data, 'id', 'pid', 'child', 0, true, array(''));
  35. }
  36. return $data;
  37. }
  38. //获取部门下权限范围内员工
  39. public function getSubUserByStructrue($param)
  40. {
  41. $userModel = model('User');
  42. $structureList = $userModel->getSubUserByStr($param, 1);
  43. if ($param) {
  44. $where['id'] = ['in', $structureList];
  45. }
  46. $list = Db::name('AdminUser')->field('id,realname')->where($where)->select();
  47. $list = $list ?: array();
  48. return $list;
  49. }
  50. /*
  51. *根据字符串展示参与部门 use by work
  52. *add by yykun
  53. */
  54. public function getDataByStr($idstr)
  55. {
  56. $isArr = stringToArray($idstr);
  57. if (!$isArr) {
  58. return false;
  59. }
  60. $list = $this->field('id as structure_id,name')->where(['id' => ['in',$isArr]])->select();
  61. return $list;
  62. }
  63. /*
  64. *根据部门ID获取信息 use by work
  65. *add by yykun
  66. */
  67. public function getDataByID( $id ='')
  68. {
  69. $det = Db::name('AdminStructure')->where('id ='.$id)->find();
  70. return $det;
  71. }
  72. public function delStrById($id,$userId)
  73. {
  74. if (!$id) {
  75. $this->error = '删除失败';
  76. return false;
  77. }
  78. $dataInfo = $this->getDataByID($id);
  79. if (empty($dataInfo['pid'])) {
  80. $this->error = '删除失败';
  81. return false;
  82. }
  83. //部门是否被使用
  84. $allStrIds = [];
  85. $allStrIds[] = $id;
  86. $allSubStrIds = $this->getAllChild($id);
  87. $allStrIds = array_merge($allStrIds, $allSubStrIds); //全部关联部门(包含下属部门)
  88. $resUser = db('AdminUser')->where(['structure_id' => ['in',$allStrIds]])->find();
  89. if ($resUser) {
  90. $this->error = '该部门或其下属部门已存在员工,不能删除';
  91. return false;
  92. }
  93. $resDel = $this->delDataById($id, true);
  94. if (!$resDel) {
  95. $this->error = '删除失败';
  96. return false;
  97. } else {
  98. $content='删除了部门:'.$dataInfo['name'];
  99. # 添加记录
  100. SystemActionLog($userId, 'admin_structure','structures', $id,'delete', $dataInfo['name'], '', '', $content);
  101. return true;
  102. }
  103. }
  104. /**
  105. * [getStructureNameByArr 根据主键获取详情]
  106. * @param string $id [主键]
  107. * @return [array]
  108. */
  109. public function getStructureNameByArr($ids = [])
  110. {
  111. if (!is_array($ids)) {
  112. $idArr[] = $ids;
  113. } else {
  114. $idArr = $ids;
  115. }
  116. $data = $this->where(['id' => array('in', $idArr)])->column('name');
  117. return $data ? : [];
  118. }
  119. /*
  120. *根据字符串展示参与部门 use by work
  121. *add by yykun
  122. */
  123. public function getListByStr($str)
  124. {
  125. $idArr = stringToArray($str);
  126. $list = $this->field('id,name')->where(['id' => ['in',$idArr]])->select();
  127. return $list;
  128. }
  129. }