objDDepartment = new DDepartment('default'); $this->objDDepartment->setTable($this->objDDepartment->get_Table().'_'.$enterpriseId); } /** * 部门添加 * @param $params * @return ResultWrapper */ public function addDepartment($params) { $params['createTime'] = $params['updateTime']; #777 TODO 编码生成 $params['enableStatus'] = StatusCode::$standard; $dbResult = $this->objDDepartment->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 部门删除 * @param $id * @return ResultWrapper */ public function deleteDepartment($id) { $params = [ 'deleteStatus' => StatusCode::$delete, 'updateTime' => time(), ]; $dbResult = $this->objDDepartment->update($params, $id); if($dbResult === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 改 */ /** * 部门修改 * @param $params * @param $id * @return ResultWrapper */ public function updateDepartment($params, $id) { if($params['pid'] == $id['id']){ return ResultWrapper::fail('父级部门不能选择自己', ErrorCode::$paramError); } //查询要修改的部门的所有子级 $dbResult = $this->objDDepartment->select(['pid' => $id, 'deleteStatus' => StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); } foreach($dbResult as $value){ if($value['id'] == $params['pid']){ return ResultWrapper::fail('父子部门不能选择自己的子级', ErrorCode::$paramError); } } $dbResult = $this->objDDepartment->update($params,$id); if($dbResult === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 查 */ /** * 部门列表 * @param $selectParams * @return ResultWrapper */ public function getAllDepartment($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $where = 'deleteStatus=' . StatusCode::$standard . ' AND enableStatus=' . StatusCode::$standard.' AND shopId = '.$selectParams['shopId']; if(isset($selectParams['departmentName'])) { $where .= ' AND departmentName LIKE "%' . $selectParams['departmentName'] . '%"'; } $dbResult = $this->objDDepartment->select($where, '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); } $total = $this->objDDepartment->count($where); $return = [ 'data' => self::formatList($dbResult), 'total' => ($total) ? intval($total) : 0, ]; if($return === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($return); } } /** * 部门详情 * @param $params * @return ResultWrapper */ public function getDepartmentInfo($params) { $dbResult = $this->objDDepartment->get($params); if ($dbResult){ $dbResult['extend'] = json_decode($dbResult['extend'],true); } if($dbResult === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 根据部门id获取上级部门id和部门名称 * @param $id * @return array|ResultWrapper */ public function departmentDataByDepartmentId($id) { //查询 id 部门名称 $where = [ 'deleteStatus' => StatusCode::$standard, 'id' => $id, ]; $fields = 'id, departmentName'; $departmentData = $this->objDDepartment->get($where, $fields); if($departmentData === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); } return $departmentData; } /** * 根据部门ids获取部门id和部门名称 * @param array $ids * @return bool|ResultWrapper */ public function departmentDataByDepartmentIds(array $ids) { //查询所有 id 部门名称 $where = [ 'id' => array_values($ids), 'deleteStatus' => StatusCode::$standard, ]; $fields = 'id,departmentName'; $departmentData = $this->objDDepartment->select($where, $fields); if($departmentData === false){ return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); } return $departmentData; } /** * 格式化部门列表 * @param $data * @return array */ public function formatList($data) { foreach($data as &$v) { $v['extend'] = json_decode($v['extend'],true); } return self::arr2tree($data); } /** * 多维数组转树状结构 * * @param $tree * @param int $rootId 顶级id * @param string $name 需要拼接的索引名称 * @return array */ public function arr2tree($tree, $rootId = 0, $name = 'departmentName') { $return = []; foreach ($tree as &$leaf) { if ($leaf['pid'] == $rootId) { if(!isset($leaf['title'])){ $leaf['title'] = $leaf[$name]; } foreach ($tree as &$subleaf) { if ($subleaf['pid'] == $leaf['id']) { $subleaf['title'] = $leaf['title'].'>'.$subleaf[$name]; $leaf['children'] = self::arr2tree($tree, $leaf['id'], $name); break; } } $return[] = $leaf; } } return $return; } /** * 单个部门Id查询部门名称 * @param $departmentId * @return string */ public function getNameById($departmentId) { $return = ''; if(empty($departmentId)) { return $return; } $dbResult = $this->objDDepartment->get($departmentId, 'departmentName'); if($dbResult === false || empty($dbResult)) { return $return; } return isset($dbResult['departmentName']) ? $dbResult['departmentName'] : $return; } /** * 多个部门Id查询部门名称 * @param $departmentIds * @return ResultWrapper */ public function getNameByIds($departmentIds) { if(empty($departmentIds)) { return ResultWrapper::success([]); } $dbResult = $this->objDDepartment->select(['id'=> $departmentIds], 'departmentName,id'); if($dbResult === false ) { return ResultWrapper::fail($this->objDDepartment->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } }