123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * 部门管理Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/10/31
- * Time: 17:00
- */
- namespace JinDouYun\Model\Department;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Department\DDepartment;
- class MDepartment
- {
- private $objDDepartment;
- public function __construct($enterpriseId)
- {
- $this->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);
- }
- }
|