MDepartment.Class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * 部门管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/10/31
  7. * Time: 17:00
  8. */
  9. namespace JinDouYun\Model\Department;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Department\DDepartment;
  14. class MDepartment
  15. {
  16. private $objDDepartment;
  17. public function __construct($enterpriseId)
  18. {
  19. $this->objDDepartment = new DDepartment('default');
  20. $this->objDDepartment->setTable($this->objDDepartment->get_Table().'_'.$enterpriseId);
  21. }
  22. /**
  23. * 部门添加
  24. * @param $params
  25. * @return ResultWrapper
  26. */
  27. public function addDepartment($params)
  28. {
  29. $params['createTime'] = $params['updateTime'];
  30. #777 TODO 编码生成
  31. $params['enableStatus'] = StatusCode::$standard;
  32. $dbResult = $this->objDDepartment->insert($params);
  33. if($dbResult === false){
  34. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  35. }else{
  36. return ResultWrapper::success($dbResult);
  37. }
  38. }
  39. /**
  40. * 部门删除
  41. * @param $id
  42. * @return ResultWrapper
  43. */
  44. public function deleteDepartment($id)
  45. {
  46. $params = [
  47. 'deleteStatus' => StatusCode::$delete,
  48. 'updateTime' => time(),
  49. ];
  50. $dbResult = $this->objDDepartment->update($params, $id);
  51. if($dbResult === false){
  52. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  53. }else{
  54. return ResultWrapper::success($dbResult);
  55. }
  56. }
  57. /**
  58. * 改
  59. */
  60. /**
  61. * 部门修改
  62. * @param $params
  63. * @param $id
  64. * @return ResultWrapper
  65. */
  66. public function updateDepartment($params, $id)
  67. {
  68. if($params['pid'] == $id['id']){
  69. return ResultWrapper::fail('父级部门不能选择自己', ErrorCode::$paramError);
  70. }
  71. //查询要修改的部门的所有子级
  72. $dbResult = $this->objDDepartment->select(['pid' => $id, 'deleteStatus' => StatusCode::$standard]);
  73. if($dbResult === false){
  74. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  75. }
  76. foreach($dbResult as $value){
  77. if($value['id'] == $params['pid']){
  78. return ResultWrapper::fail('父子部门不能选择自己的子级', ErrorCode::$paramError);
  79. }
  80. }
  81. $dbResult = $this->objDDepartment->update($params,$id);
  82. if($dbResult === false){
  83. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  84. }else{
  85. return ResultWrapper::success($dbResult);
  86. }
  87. }
  88. /**
  89. * 查
  90. */
  91. /**
  92. * 部门列表
  93. * @param $selectParams
  94. * @return ResultWrapper
  95. */
  96. public function getAllDepartment($selectParams)
  97. {
  98. $limit = $selectParams['limit'];
  99. unset($selectParams['limit']);
  100. $offset = $selectParams['offset'];
  101. unset($selectParams['offset']);
  102. $where = 'deleteStatus=' . StatusCode::$standard . ' AND enableStatus=' . StatusCode::$standard.' AND shopId = '.$selectParams['shopId'];
  103. if(isset($selectParams['departmentName'])) {
  104. $where .= ' AND departmentName LIKE "%' . $selectParams['departmentName'] . '%"';
  105. }
  106. $dbResult = $this->objDDepartment->select($where, '*', 'createTime desc', $limit, $offset);
  107. if ($dbResult === false) {
  108. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  109. }
  110. $total = $this->objDDepartment->count($where);
  111. $return = [
  112. 'data' => self::formatList($dbResult),
  113. 'total' => ($total) ? intval($total) : 0,
  114. ];
  115. if($return === false){
  116. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  117. }else{
  118. return ResultWrapper::success($return);
  119. }
  120. }
  121. /**
  122. * 部门详情
  123. * @param $params
  124. * @return ResultWrapper
  125. */
  126. public function getDepartmentInfo($params)
  127. {
  128. $dbResult = $this->objDDepartment->get($params);
  129. if ($dbResult){
  130. $dbResult['extend'] = json_decode($dbResult['extend'],true);
  131. }
  132. if($dbResult === false){
  133. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  134. }else{
  135. return ResultWrapper::success($dbResult);
  136. }
  137. }
  138. /**
  139. * 根据部门id获取上级部门id和部门名称
  140. * @param $id
  141. * @return array|ResultWrapper
  142. */
  143. public function departmentDataByDepartmentId($id)
  144. {
  145. //查询 id 部门名称
  146. $where = [
  147. 'deleteStatus' => StatusCode::$standard,
  148. 'id' => $id,
  149. ];
  150. $fields = 'id, departmentName';
  151. $departmentData = $this->objDDepartment->get($where, $fields);
  152. if($departmentData === false){
  153. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  154. }
  155. return $departmentData;
  156. }
  157. /**
  158. * 根据部门ids获取部门id和部门名称
  159. * @param array $ids
  160. * @return bool|ResultWrapper
  161. */
  162. public function departmentDataByDepartmentIds(array $ids)
  163. {
  164. //查询所有 id 部门名称
  165. $where = [
  166. 'id' => array_values($ids),
  167. 'deleteStatus' => StatusCode::$standard,
  168. ];
  169. $fields = 'id,departmentName';
  170. $departmentData = $this->objDDepartment->select($where, $fields);
  171. if($departmentData === false){
  172. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  173. }
  174. return $departmentData;
  175. }
  176. /**
  177. * 格式化部门列表
  178. * @param $data
  179. * @return array
  180. */
  181. public function formatList($data)
  182. {
  183. foreach($data as &$v) {
  184. $v['extend'] = json_decode($v['extend'],true);
  185. }
  186. return self::arr2tree($data);
  187. }
  188. /**
  189. * 多维数组转树状结构
  190. *
  191. * @param $tree
  192. * @param int $rootId 顶级id
  193. * @param string $name 需要拼接的索引名称
  194. * @return array
  195. */
  196. public function arr2tree($tree, $rootId = 0, $name = 'departmentName')
  197. {
  198. $return = [];
  199. foreach ($tree as &$leaf) {
  200. if ($leaf['pid'] == $rootId) {
  201. if(!isset($leaf['title'])){
  202. $leaf['title'] = $leaf[$name];
  203. }
  204. foreach ($tree as &$subleaf) {
  205. if ($subleaf['pid'] == $leaf['id']) {
  206. $subleaf['title'] = $leaf['title'].'>'.$subleaf[$name];
  207. $leaf['children'] = self::arr2tree($tree, $leaf['id'], $name);
  208. break;
  209. }
  210. }
  211. $return[] = $leaf;
  212. }
  213. }
  214. return $return;
  215. }
  216. /**
  217. * 单个部门Id查询部门名称
  218. * @param $departmentId
  219. * @return string
  220. */
  221. public function getNameById($departmentId)
  222. {
  223. $return = '';
  224. if(empty($departmentId)) {
  225. return $return;
  226. }
  227. $dbResult = $this->objDDepartment->get($departmentId, 'departmentName');
  228. if($dbResult === false || empty($dbResult)) {
  229. return $return;
  230. }
  231. return isset($dbResult['departmentName']) ? $dbResult['departmentName'] : $return;
  232. }
  233. /**
  234. * 多个部门Id查询部门名称
  235. * @param $departmentIds
  236. * @return ResultWrapper
  237. */
  238. public function getNameByIds($departmentIds)
  239. {
  240. if(empty($departmentIds)) {
  241. return ResultWrapper::success([]);
  242. }
  243. $dbResult = $this->objDDepartment->select(['id'=> $departmentIds], 'departmentName,id');
  244. if($dbResult === false ) {
  245. return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror);
  246. }
  247. return ResultWrapper::success($dbResult);
  248. }
  249. }