# @Date: 2022-05-22T07:34:47+08:00 # @Email: fm453@lukegzs.com # @Last modified by: fm453 # @Last modified time: 2022-05-22T07:34:47+08:00 # @Copyright: www.hiluker.cn namespace backend\controllers; use Yii; use yii\data\Pagination; use yii\helpers\Url; use addons\models\AcEmployee; use addons\models\AcStore; use common\models\Member as User; use addons\models\AcUserExt; class EmployeeController extends Common { public function actionIndex() { return $this->render('index'); } //用户列表 public function actionList() { $model = new AcEmployee(); $where = $where2 = []; $where2[]='and'; $where['pid'] = Yii::$app->session->get('pid'); $post = Yii::$app->request->post(); $search = []; $search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','name',$search['title']]; } $where['deleted'] = 0; $data = $model->find()->where($where); if ($search['title']) { $data = $data->andwhere($where2); } $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']); $res = $data->offset($pages->offset)->limit($pages->limit)->all(); $status = Yii::$app->params['EmployeeStatus']; $gender = Yii::$app->params['gender']; $model = new AcStore(); $where = []; $where['pid'] = Yii::$app->session->get('pid'); $_stores = $model->find()->where($where)->all(); $stores = []; foreach ($_stores as $s) { $stores[$s->id] = $s->toArray(); } return $this->render('list', [ 'employees'=>$res, 'pager' => $pages, 'stores'=>$stores, 'status'=>$status, 'gender'=>$gender, 'search'=>$search ]); } public function actionSelect() { $model = new AcEmployee(); $where = $where2 = []; $where2[]='and'; $where['pid'] = Yii::$app->session->get('pid'); $post = Yii::$app->request->post(); $search = []; $search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','name',$search['title']]; } $where['deleted'] = 0; $data = $model->find()->where($where); if ($search['title']) { $data = $data->andwhere($where2); } $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']); $res = $data->offset($pages->offset)->limit($pages->limit)->all(); $status = Yii::$app->params['EmployeeStatus']; $gender = Yii::$app->params['gender']; $model = new AcStore(); $where = []; $where['pid'] = Yii::$app->session->get('pid'); $_stores = $model->find()->where($where)->all(); $stores = []; foreach ($_stores as $s) { $stores[$s->id] = $s->toArray(); } $callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'employee'; return $this->render('select', [ 'employees'=>$res, 'pager' => $pages, 'stores'=>$stores, 'status'=>$status, 'gender'=>$gender, 'search'=>$search, 'callback'=>$callback ]); } public function actionNew() { return $this->render('modify', []); } //编辑员工资料 public function actionEdit() { $model = new AcEmployee(); $id = Yii::$app->request->get('id'); $res = $model->find()->where(['id' => $id])->one(); if ($res) { $res = $res->toArray(); } $store = AcStore::find()->where(['id' => $res['store_id']])->one(); if ($store) { $res['store_title'] = $store->title; } return $this->render('modify', ['detail'=>$res]); } //保存员工资料 public function actionSave() { $post = Yii::$app->request->post(); $id = (int)$post['id']; $pid = Yii::$app->session->get('pid'); //格式化数据 //目标键=》POST键 $cols = ['name'=>'name','mobile'=>'mobile']; foreach ($cols as $col=>$key) { $data[$col] = isset($post[$key]) ? trim($post[$key]) : ''; } $cols = ['gender'=>'gender','status'=>'status','store_id'=>'store','deleted'=>'deleted']; foreach ($cols as $col=>$key) { $data[$col] = isset($post[$key]) ? (int)$post[$key] : 0; } $cols = ['remark'=>'editor']; foreach ($cols as $col=>$key) { $data[$col] = isset($post[$key]) ? htmlspecialchars($post[$key]) : ''; } $data['create_at'] = time(); $data['update_at'] = $data['create_at']; $data['status_code'] = 1; $data['pid'] = $pid; //保存员工资料 $AcEmployee = new AcEmployee(); foreach ($data as $key=>$val) { $AcEmployee->$key = $val; } //查询员工手机号是否在库 $employee = AcEmployee::find()->where(['mobile'=>$data['mobile']])->one(); if ($employee) { $id = $employee->id; } if ($id) { $AcEmployee->id = $id; unset($data['create_at']); $res = $AcEmployee->updateAll($data, ['id'=>$id]); } else { $res = $AcEmployee->save(); $id = $AcEmployee->attributes['id']; //获取插入后id } //查询员工对应的系统用户 $userModel = new User(); $user = User::find()->where(['mobile'=>$data['mobile']])->one(); if (empty($user)) { $userModel->username = $data['mobile']; $userModel->mobile = $data['mobile']; $userModel->email = $data['mobile'].'@hiluker.com'; $userModel->setPassword(Yii::$app->params['defaultUserPassword']); $userModel->generateAuthKey(); $userModel->avatar = Yii::$app->params['defaultUserAvatar']; $userModel->created_at = time(); $userModel->status = 1; $userModel->save(); $user_id = $userModel->attributes['id']; //获取插入后id } \Yii::$app->getSession()->setFlash('info', '已成功注册该用户!'); if ($res) { \Yii::$app->getSession()->setFlash('success', '员工信息保存成功!'); return $this->redirect(['edit','id'=>$id]); } else { \Yii::$app->getSession()->setFlash('warning', '员工信息保存失败!'); return $this->goBack(); } } //删除员工 public function actionDelete() { $get = Yii::$app->request->get(); $post = Yii::$app->request->post(); $id = (int)$get['id']; $data = $where = []; $where['pid'] = Yii::$app->session->get('pid'); $where['id'] = $id; $data['update_at'] = time(); $data['deleted'] = 1; $model = new AcEmployee(); foreach ($data as $key=>$val) { $model->$key = $val; } $model->id = $id; $res = $model->updateAll($data, $where); $return = []; $return['url'] = Url::toRoute('employee/list'); $return['timeout'] = 3; //3秒后自动跳转 $return['status'] = 0; if ($res) { $return['msg'] = '员工删除成功'; $return['errorcode'] = 200; $return['data'] = $id; } else { $return['msg'] = '员工删除失败'; $return['errorcode'] = 0; } exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } public function actionAjax() { $get = Yii::$app->request->get(); $post = Yii::$app->request->post(); $id = (int)$get['id']; $data = $where = []; $where['pid'] = Yii::$app->session->get('pid'); $where['id'] = $id; $data['update_at'] = time(); $model = new AcEmployee(); $res = $model->find()->where(['id' => $id])->one(); $return = []; $return['timeout'] = 1; //3秒后自动跳转 $return['status'] = 0; $return['ajax'] = 1; if ($res) { $res = $res->toArray(); } else { $return['msg'] = '员工数据不存在'; $return['errorcode'] = 404; exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } switch ($get['do']) { case 'out': $data['status'] = 0; $model->id = $id; $res = $model->updateAll($data, $where); if ($res) { $return['msg'] = '员工离职标记成功'; $return['errorcode'] = 200; $return['data'] = ['css'=>'info']; } else { $return['msg'] = '员工离职标记失败'; $return['errorcode'] = 0; } break; case 'in': $data['status'] = 1; $model->id = $id; $res = $model->updateAll($data, $where); if ($res) { $return['msg'] = '员工在职标记成功'; $return['errorcode'] = 200; $return['data'] = ['css'=>'default']; } else { $return['msg'] = '员工在职标记失败'; $return['errorcode'] = 0; } break; default: break; } exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } }