Structure.php 2.9KB

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