db->beginTransaction(); foreach ($content as $val) { $row = User::find()->where(['unid' => $val['unid']])->one(); if(empty($row)){ $row = User::find()->where(['user' => $val['user']])->one(); } if ($row) { $this_content[] = [ 'user' => $val['user'], 'password' => $val['password'], 'name' => $val['name'], 'status' => $val['status'], 'unid' => $val['unid'], ]; $row->user = $val['user']; $row->password = $val['password']; $row->name = $val['name']; $row->status = $val['status']; $row->unid = $val['unid']; $row->update(false); $code = 'edit'; } else { $row = new user(); $row->user = $val['user']; $row->password = $val['password']; $row->name = $val['name']; $row->status = $val['status']; $row->unid = $val['unid']; if (!($row->save())) { $Transaction->rollBack(); return '插入失败-2'; } $code = 'add'; } $UserInfo = [ 'user_list' => $userNameList, 'auditor_name' => $auditor_name, 'this_content' => json_encode($this_content), 'oss_content' => $oss_content, 'code' => $code ]; if (!empty($val['role']) && !$this->addUserRole($row->uid, $val['role'])) { $Transaction->rollBack(); return '用户角色分配失败!'; } if (!($this->addUserRecord($UserInfo))) { $Transaction->rollBack(); return '纪录表记录失败'; } $Transaction->commit(); return true; } return $userNameList; } catch (\Exception $e) { return $e->getMessage(); } } public function addUserRecord($UserInfo) { $row = new \common\models\Syncuserrecord(); $row->user_list = $UserInfo['user_list']; $row->auditor_name = $UserInfo['auditor_name']; $row->this_content = $UserInfo['this_content']; $row->oss_content = $UserInfo['oss_content']; $row->code = $UserInfo['code']; if ($row->insert()) { return true; } else { return false; } } //用户角色分配 public function addUserRole($uid, $role) { $roleModel = UserRole::find()->where(['name' => $role])->select('id')->column(); UserUr::deleteAll(['uid' => $uid]); $urList = []; $time = time(); foreach ($roleModel as $val) { $t['uid'] = $uid; $t['rid'] = $val; $t['create_at'] = $time; $urList[] = $t; } $state = Yii::$app->db->createCommand()->batchInsert('pfg_user_ur', ['uid', 'rid', 'create_at'], $urList)->execute(); if ($state) { return true; } return false; } }