Org.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace app\common\model\base\org;
  3. /**
  4. * @title : 组织模型
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2021-11-27 19:49:29
  8. * @LastEditTime: 2024-12-04 11:40:04
  9. */
  10. use app\common\model\Common;
  11. use app\common\model\base\other\Area;
  12. class Org extends Common
  13. {
  14. protected $name = "system_org";
  15. protected $createTime = "create_at";
  16. protected $updateTime = "update_at";
  17. protected $deleteTime = "delete_at";
  18. protected $pk = "org_id";
  19. private $autoCalc = true;
  20. protected $schema = [
  21. 'org_id' => 'int', //组织ID
  22. 'pid' => 'int', //上级组织ID
  23. 'parent_name' => 'varchar', //上级组织名称
  24. 'lft' => 'int', //二叉树左标记
  25. 'rgt' => 'int', //二叉树右标记
  26. 'name' => 'varchar', //组织全称
  27. 'short_name' => 'varchar', //简称
  28. 'sort' => 'int', //排序
  29. 'status' => 'tinyint', //状态
  30. 'create_at' => 'datetime', //创建时间
  31. 'update_at' => 'datetime', //更新时间
  32. 'delete_at' => 'datetime', //删除时间
  33. 'type' => 'varchar', //组织类型码
  34. 'menu_ids' => 'text', //组织菜单权限
  35. 'request_ids' => 'text', //组织请求权限
  36. 'front_ids' => 'text', //组织移动端权限
  37. 'area_code' => 'varchar',//所在地区编码
  38. 'path' => 'varchar',//组织级别路径
  39. 'area_id' => 'int',//组织级别路径
  40. ];
  41. protected $append = [
  42. 'status_txt',
  43. 'type_txt',
  44. 'hasChildren',
  45. 'area_code_txt',
  46. ];
  47. // 组织状态列表
  48. public function statusList()
  49. {
  50. return [1 => '启用', 2 => '禁用', 3 => '待审核', 4 => '已驳回'];
  51. }
  52. // 组织状态
  53. public function getStatusTxtAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  56. $statusList = $this->statusList();
  57. return isset($statusList[$value]) ? $statusList[$value] : '';
  58. }
  59. // 组织类型列表
  60. static public function typeList()
  61. {
  62. return OrgType::cache(60)->column('org_type_name', 'code');
  63. }
  64. // 组织类型名称
  65. public function getTypeTxtAttr($value, $data)
  66. {
  67. $typeList = self::typeList();
  68. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  69. if (empty($value)) {
  70. return "无";
  71. }
  72. return isset($typeList[$value]) ? $typeList[$value] : '无';
  73. }
  74. // 菜单权限修改器
  75. public function setMenuIdsAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['menu_ids']) ? $data['menu_ids'] : '');
  78. if (!empty($value) && is_array($value)) {
  79. return implode(',', $value);
  80. } else {
  81. return $value;
  82. }
  83. }
  84. // 菜单权限获取器
  85. public function getMenuIdsAttr($value, $data)
  86. {
  87. $value = $value ? $value : (isset($data['menu_ids']) ? $data['menu_ids'] : []);
  88. return is_string($value) ? explode(',', $value) : $value;
  89. }
  90. // 请求权限修改器
  91. public function setRequestIdsAttr($value, $data)
  92. {
  93. $value = $value ? $value : (isset($data['request_ids']) ? $data['request_ids'] : '');
  94. if (!empty($value) && is_array($value)) {
  95. return implode(',', $value);
  96. } else {
  97. return $value;
  98. }
  99. }
  100. // 请求权限获取器
  101. public function getRequestIdsAttr($value, $data)
  102. {
  103. $value = $value ? $value : (isset($data['request_ids']) ? $data['request_ids'] : []);
  104. return is_string($value) ? explode(',', $value) : $value;
  105. }
  106. // 手机端权限修改器
  107. public function setFrontIdsAttr($value, $data)
  108. {
  109. $value = $value ? $value : (isset($data['front_ids']) ? $data['front_ids'] : '');
  110. if (!empty($value) && is_array($value)) {
  111. return implode(',', $value);
  112. } else {
  113. return $value;
  114. }
  115. }
  116. // 手机端权限获取器
  117. public function getFrontIdsAttr($value, $data)
  118. {
  119. $value = $value ? $value : (isset($data['front_ids']) ? $data['front_ids'] : []);
  120. return is_string($value) ? explode(',', $value) : $value;
  121. }
  122. // 判断是否有下级部门
  123. public function getHasChildrenAttr($value, $data)
  124. {
  125. if (isset($data['rgt']) && isset($data['lft'])) {
  126. return $data['rgt'] - $data['lft'] > 2;
  127. } else {
  128. return false;
  129. }
  130. }
  131. // 部门列表
  132. public function depart()
  133. {
  134. return $this->hasMany(OrgDepart::class, 'org_id', 'org_id');
  135. }
  136. // 设置上级组织名称
  137. public function setParentNameAttr($value, $data)
  138. {
  139. if (isset($data['pid']) && $data['pid'] != 0) {
  140. return self::where('org_id', $data['pid'])->value('name');
  141. } else {
  142. return '';
  143. }
  144. }
  145. // 逐级获取pid,返回一个pid数组
  146. static public function getPid($org_id, &$ary = [])
  147. {
  148. $allOrg = self::cache(60)->column("pid", 'org_id');
  149. if (isset($allOrg[$org_id])) {
  150. $pid = $allOrg[$org_id];
  151. if ($pid != 0) {
  152. $ary[] = $pid;
  153. return self::getPid($pid, $ary);
  154. } else {
  155. return $ary;
  156. }
  157. } else {
  158. return $ary;
  159. }
  160. }
  161. // 设置组织级别路径
  162. public function setPathAttr($value, $data)
  163. {
  164. $ary = [];
  165. if (isset($data['pid'])) {
  166. $pid = $data['pid'];
  167. $ary[] = $pid;
  168. $ary = self::getPid($pid, $ary);
  169. sort($ary);
  170. return implode(",", $ary);
  171. }
  172. }
  173. // 缓存地区列表
  174. static public function areaList()
  175. {
  176. return Area::cache(86400)->column('name', 'code');
  177. }
  178. // 获取地区文本信息
  179. public function getAreaCodeTxtAttr($value, $data)
  180. {
  181. $value = $value ? $value : (isset($data['area_code']) ? $data['area_code'] : '');
  182. $areaList = $this->areaList();
  183. return isset($areaList[$value]) ? $areaList[$value] : '';
  184. }
  185. /**
  186. * @title: 获取下级组织
  187. * @desc: 描述
  188. * @parem {int} {org_id} {0} {组织ID}
  189. * @parem {boolean} {self} {false} {是否包含自己}
  190. * @return {*}
  191. * @author: Rock
  192. * @Date: 2023-03-23 20:27:35
  193. */
  194. static public function getChildrenIds(int $org_id = 0, $self = false)
  195. {
  196. $info = self::where('org_id', $org_id)->find();
  197. $where = [];
  198. if ($self) {
  199. $where[] = ['lft', '>=', $info->lft];
  200. $where[] = ['rgt', '<=', $info->rgt];
  201. } else {
  202. $where[] = ['lft', '>', $info->lft];
  203. $where[] = ['rgt', '<', $info->rgt];
  204. }
  205. return self::where($where)->column('org_id');
  206. }
  207. /**
  208. * @title 获取直接上级
  209. * @desc: 获取当前组织的直接上级
  210. * @param : {int} {org_id} {0} {组织ID}
  211. * @return {int} {org_id} {} {上级组织ID}
  212. * @return {varchar} {name} {} {上级组织名称}
  213. * @author: Rock
  214. */
  215. static public function getSuperior(int $org_id)
  216. {
  217. $list = self::cache(600)->column('org_id,name', 'org_id');
  218. return isset($list[$org_id]) ? $list[$org_id] : null;
  219. }
  220. /**
  221. * @title 获取所有上级
  222. * @desc:获取当前组织的所有上级
  223. * @param: {int} {org_id} {0} {组织ID}
  224. * @return {array.int} {org_id} {} {组织ID}
  225. * @return {array.varchar} {name} {} {组织名称}
  226. * @return {array.varchar} {type} {} {组织类型编码}
  227. */
  228. static public function getSuperiors(int $org_id)
  229. {
  230. $list = self::cache(600)->column('org_id,name,path,type', 'org_id');
  231. $info = $list[$org_id];
  232. $ids = is_string($info['path']) ? explode(',', $info['path']) : $info['path'];
  233. $res = [];
  234. foreach ($ids as $id) {
  235. $res[] = $list[$id];
  236. }
  237. return $res;
  238. }
  239. // 地区编码设置
  240. public function setAreaCodeAttr($value, $row)
  241. {
  242. // 处理area_code
  243. if (isset($row['type'])) {
  244. // 集团无需地区,幼儿园、小学、初中、高中是创建时选择的,无需继承上级组织所属地区,
  245. if (!in_array($row['type'], ['GROUP', 'KINDER', 'PRIMARY', 'JUNIOR', 'HIGH'])) {
  246. $parent = (new self)->find($row['pid']);
  247. return $parent['area_code'] ?? null;
  248. } else {
  249. return $value ?? $row['area_code'];
  250. }
  251. }
  252. }
  253. }