48], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'parent_id' => 'Parent ID', 'name' => 'Name', 'sort' => 'Sort', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'is_default_position' => 'Is Default Position', ]; } public function getDepartmentPosition() { return $this->hasMany(self::class, ['parent_id' => 'id'])->where(['status' => StatusEnum::ENABLED]); } /** * 获取部门 * * @author hpei * @return array */ public static function getDepartment($mall_id) { return self::find() ->select(['name', 'id']) ->where(['mall_id' => $mall_id, 'parent_id' => 0]) ->andWhere(['<>','status', StatusEnum::DELETE]) ->asArray()->all(); } /** * 获取职位 * * @author hpei * @return array */ public static function getPosition($mall_id, $select, $index = false) { $model = self::find() ->select($select) ->where(['mall_id' => $mall_id]) ->andWhere(['<>','status', StatusEnum::DELETE]) ->andWhere(['<>','parent_id', 0]); if ($index !== false) { $list = $model->indexBy($index)->column(); } else { $list = $model->asArray()->all(); } return $list; } /** * 保存数据 * * @author hpei * @return bool|Exception */ public static function setData($params) { try { $position = self::findOne(['mall_id' => $params['mall_id'],'status'=>StatusEnum::ENABLED,'name'=>$params['name'],'parent_id' =>$params['parent_id']]); if (empty($params['id'])) { $model = new self(); $model->loadDefaultValues(); if ($position) { if($params['parent_id']>0){ self::$static_error = '该职位已经存在'; }else{ self::$static_error = '该部门已经存在'; } return false; } } else { $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if ($position && $model->name != $params['name']) { if($params['parent_id']>0){ self::$static_error = '该职位已经存在'; }else{ self::$static_error = '该部门已经存在'; } return false; } if (empty($model)) throw new \Exception('数据不存在或者已删除'); } if(!$params['sort']) $params['sort'] = 99; if (!empty($params['is_default_position']) && $params['parent_id'] > 0) { self::updateAll( [ 'is_default_position' => StatusEnum::DISABLED ], [ 'mall_id' => $params['mall_id'], 'is_default_position' => StatusEnum::ENABLED ] ); } if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage()); return $model->id; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 新部门创建接口 * @param array $params * @return false */ public static function newSaveData(array $params): bool { try { $departmentInfo = self::find()->where(['name' => $params['name'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED])->one(); if($params['id']) { //编辑 if($departmentInfo && $departmentInfo->id != $params['id']) throw new Exception('部门已存在!'); $model = self::findOne(['id' => $params['id']]); } else { if ($departmentInfo) throw new Exception("部门已存在!"); $model = new self(); } $model->name = $params['name']; $model->mall_id = $params['mall_id']; $model->sort = $params['sort']; $res = $model->save(); if ($res === false) throw new Exception("操作失败"); foreach ($params['position'] as $item) { //进行职位插入 $positionModel = new self(); if ($item['id']) $positionModel = self::findOne(['id' => $item['id']]); $positionModel->name = $item['name']; $positionModel->mall_id = $params['mall_id']; $positionModel->parent_id = $model->id; $positionModel->sort = $params['sort']; $positionModel->save(); } return true; } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 检测 * * @author hpei * @return bool */ public static function checkPosition($params) { $model = self::find(); $model->where(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if ($params['isParent'] === false) { $model->andWhere(['>', 'parent_id', 0]); } else { $model->andWhere(['=', 'parent_id', 0]); } return $model->count() > 0; } /** * 获取部门和对应的职位 * * @author hepi * @return array */ public static function departmentPosition($mall_id) { $params = [ 'where' => [['=', 'mall_id', $mall_id], ['<>', 'status' , StatusEnum::DELETE], ['=', 'parent_id', 0]], 'select' => ['id', 'parent_id', 'name'], 'order' => 'sort desc', 'with' => ['departmentPosition'], ]; $model = self::find(); self::paramPack($params, $model); return $model->asArray()->all(); } //删除 public static function del($mall_id,$id){ $res = BusinessCardUser::find()->where(['department_id'=>$id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]) ->one(); if(!empty($res)) { throw new \Exception('该部门存在人员'); } BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'id'=>$id]); BusinessCardPosition::updateAll(['status'=>StatusEnum::DELETE],['mall_id'=>$mall_id,'parent_id'=>$id]); } /** * 切换是否职位 * * @param array $params * * @return void */ public static function changeIsDefaultPosition(array $params) { if ($params['is_default_position']) { self::updateAll( [ 'is_default_position' => StatusEnum::DISABLED ], [ 'mall_id' => $params['mall_id'], 'is_default_position' => StatusEnum::ENABLED ] ); } self::updateAll( [ 'is_default_position' => $params['is_default_position'] ], [ 'mall_id' => $params['mall_id'], 'id' => $params['id'] ] ); } }