Structure.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // 若type为tree,则返回树状结构
  27. if ($type == 'tree') {
  28. $tree = new \com\Tree();
  29. $data = $tree->list_to_tree($data, 'id', 'pid', 'child', 0, true, array(''));
  30. }
  31. foreach ($data as $k=>$v){
  32. $data[$k]['owner_user_name']=foreachData('admin_structure',$v['owner_user_id'])?:null;
  33. }
  34. return $data;
  35. }
  36. /*
  37. *根据字符串展示参与部门 use by work
  38. *add by yykun
  39. */
  40. public function getDataByStr($idstr)
  41. {
  42. $isArr = stringToArray($idstr);
  43. if (!$isArr) {
  44. return false;
  45. }
  46. $list = $this->field('id as structure_id,name')->where(['id' => ['in',$isArr]])->select();
  47. return $list;
  48. }
  49. /*
  50. *根据部门ID获取信息 use by work
  51. *add by yykun
  52. */
  53. public function getDataByID( $id ='')
  54. {
  55. $det = Db::name('AdminStructure')->where('id ='.$id)->find();
  56. return $det;
  57. }
  58. public function delStrById($id)
  59. {
  60. if (!$id) {
  61. $this->error = '删除失败';
  62. return false;
  63. }
  64. $dataInfo = $this->getDataByID($id);
  65. if (empty($dataInfo['pid'])) {
  66. $this->error = '删除失败';
  67. return false;
  68. }
  69. //部门是否被使用
  70. $allStrIds = [];
  71. $allStrIds[] = $id;
  72. $allSubStrIds = $this->getAllChild($id);
  73. $allStrIds = array_merge($allStrIds, $allSubStrIds); //全部关联部门(包含下属部门)
  74. $resUser = db('AdminUser')->where(['structure_id' => ['in',$allStrIds]])->find();
  75. if ($resUser) {
  76. $this->error = '该部门或其下属部门已存在员工,不能删除';
  77. return false;
  78. }
  79. $resDel = $this->delDataById($id, true);
  80. if (!$resDel) {
  81. $this->error = '删除失败';
  82. return false;
  83. } else {
  84. $apiCommon = new ApiCommon();
  85. $userInfo = $apiCommon->userInfo;
  86. $content='删除了部门:'.$dataInfo['name'];
  87. # 添加记录
  88. SystemActionLog($userInfo['id'], 'admin_structure','structures', $id,'delete', $dataInfo['name'], '', '', $content);
  89. return true;
  90. }
  91. }
  92. /**
  93. * [getStructureNameByArr 根据主键获取详情]
  94. * @param string $id [主键]
  95. * @return [array]
  96. */
  97. public function getStructureNameByArr($ids = [])
  98. {
  99. if (!is_array($ids)) {
  100. $idArr[] = $ids;
  101. } else {
  102. $idArr = $ids;
  103. }
  104. $data = $this->where(['id' => array('in', $idArr)])->column('name');
  105. return $data ? : [];
  106. }
  107. /*
  108. *根据字符串展示参与部门 use by work
  109. *add by yykun
  110. */
  111. public function getListByStr($str)
  112. {
  113. $idArr = stringToArray($str);
  114. $list = $this->field('id,name')->where(['id' => ['in',$idArr]])->select();
  115. return $list;
  116. }
  117. }