[FIX]取值bug
This commit is contained in:
parent
17ce95596b
commit
2051f39c3f
|
@ -17,4 +17,7 @@ const (
|
||||||
ErrorCheckExistEmployeeFail = 20003
|
ErrorCheckExistEmployeeFail = 20003
|
||||||
ErrorGetEmployeeFail = 20004
|
ErrorGetEmployeeFail = 20004
|
||||||
ErrorCreatedEmployeeFail = 20005
|
ErrorCreatedEmployeeFail = 20005
|
||||||
|
ErrorCountEmployeesFail = 20006
|
||||||
|
ErrorGetEmployeesFail = 20007
|
||||||
|
ErrorUpdatedEmployeeFail = 20008
|
||||||
)
|
)
|
||||||
|
|
11
pkg/e/msg.go
11
pkg/e/msg.go
|
@ -11,11 +11,14 @@ var MsgFlags = map[int]string{
|
||||||
ErrorUploadSaveImageFail: "保存图片错误",
|
ErrorUploadSaveImageFail: "保存图片错误",
|
||||||
ErrorUploadCheckImageFail: "检查图片失败",
|
ErrorUploadCheckImageFail: "检查图片失败",
|
||||||
ErrorUploadCheckImageFormat: "校验图片错误,图片格式或大小有问题",
|
ErrorUploadCheckImageFormat: "校验图片错误,图片格式或大小有问题",
|
||||||
ErrorExistEmployee: "已存在该员工用户名",
|
ErrorExistEmployee: "该员工用户名已存在",
|
||||||
ErrorNotExistEmployee: "该员工不存在",
|
ErrorNotExistEmployee: "该员工不存在",
|
||||||
ErrorCheckExistEmployeeFail: "该员工信息缓存失败",
|
ErrorCheckExistEmployeeFail: "检查员工失败",
|
||||||
ErrorGetEmployeeFail: "获取员工信息缓存失败",
|
ErrorGetEmployeeFail: "获取员工失败",
|
||||||
ErrorCreatedEmployeeFail: "创建员工失败,入数据库",
|
ErrorCreatedEmployeeFail: "创建员工失败",
|
||||||
|
ErrorCountEmployeesFail: "计数员工失败",
|
||||||
|
ErrorGetEmployeesFail: "获取员工列表失败",
|
||||||
|
ErrorUpdatedEmployeeFail: "更新员工失败",
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMsg(code int) string {
|
func GetMsg(code int) string {
|
||||||
|
|
|
@ -42,10 +42,11 @@ func AddEmployee(c *gin.Context) {
|
||||||
|
|
||||||
if valid.HasErrors() {
|
if valid.HasErrors() {
|
||||||
app.MarkErrors(valid.Errors)
|
app.MarkErrors(valid.Errors)
|
||||||
appG.Response(http.StatusOK, e.InvalidParams, nil)
|
appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 该员工用户名已存在,未入数据库
|
||||||
if models.ExistEmployeeByUsername(username) {
|
if models.ExistEmployeeByUsername(username) {
|
||||||
appG.Response(http.StatusInternalServerError, e.ErrorExistEmployee, nil)
|
appG.Response(http.StatusInternalServerError, e.ErrorExistEmployee, nil)
|
||||||
}
|
}
|
||||||
|
@ -57,9 +58,13 @@ func AddEmployee(c *gin.Context) {
|
||||||
Department: department,
|
Department: department,
|
||||||
Position: position,
|
Position: position,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 入库
|
||||||
if err := employeeService.Add(); err == nil {
|
if err := employeeService.Add(); err == nil {
|
||||||
|
// 入数据库成功,缓存数据成功
|
||||||
appG.Response(http.StatusOK, e.SUCCESS, employeeService)
|
appG.Response(http.StatusOK, e.SUCCESS, employeeService)
|
||||||
} else {
|
} else {
|
||||||
|
// 数据格式正确,服务器创建员工失败
|
||||||
appG.Response(http.StatusOK, e.ErrorCreatedEmployeeFail, employeeService)
|
appG.Response(http.StatusOK, e.ErrorCreatedEmployeeFail, employeeService)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,17 +93,18 @@ func GetEmployee(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 调用service对象存储数据
|
// 调用service对象进行数据同步
|
||||||
employeeService := employee_service.Employee{
|
employeeService := employee_service.Employee{
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 员工不存在的情况
|
// 验证数据是否存在时发生的错误
|
||||||
exists, err := employeeService.ExistByID()
|
exists, err := employeeService.ExistByID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appG.Response(http.StatusInternalServerError, e.ErrorCheckExistEmployeeFail, nil)
|
appG.Response(http.StatusInternalServerError, e.ErrorCheckExistEmployeeFail, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 数据不存在,无法返回缓存的数据
|
||||||
if !exists {
|
if !exists {
|
||||||
appG.Response(http.StatusOK, e.ErrorNotExistEmployee, nil)
|
appG.Response(http.StatusOK, e.ErrorNotExistEmployee, nil)
|
||||||
return
|
return
|
||||||
|
@ -146,13 +152,13 @@ func GetEmployees(c *gin.Context) {
|
||||||
|
|
||||||
total, err := employeeService.Count()
|
total, err := employeeService.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appG.Response(http.StatusInternalServerError, e.InvalidParams, nil)
|
appG.Response(http.StatusInternalServerError, e.ErrorCountEmployeesFail, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
employees, err := employeeService.GetAll()
|
employees, err := employeeService.GetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
appG.Response(http.StatusInternalServerError, e.InvalidParams, nil)
|
appG.Response(http.StatusInternalServerError, e.ErrorGetEmployeesFail, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,6 +185,7 @@ func GetEmployees(c *gin.Context) {
|
||||||
// @Success 200 {string} json "{"code": 200, "data":{}, "msg":"ok"}"
|
// @Success 200 {string} json "{"code": 200, "data":{}, "msg":"ok"}"
|
||||||
// @Router /admin/v1/employees/{id} [put]
|
// @Router /admin/v1/employees/{id} [put]
|
||||||
func EditEmployee(c *gin.Context) {
|
func EditEmployee(c *gin.Context) {
|
||||||
|
appG := app.Gin{C: c}
|
||||||
id := com.StrTo(c.Param("id")).MustInt()
|
id := com.StrTo(c.Param("id")).MustInt()
|
||||||
avatarUrl := c.PostForm("avatar_url")
|
avatarUrl := c.PostForm("avatar_url")
|
||||||
username := c.PostForm("username")
|
username := c.PostForm("username")
|
||||||
|
@ -190,44 +197,58 @@ func EditEmployee(c *gin.Context) {
|
||||||
valid := validation.Validation{}
|
valid := validation.Validation{}
|
||||||
valid.Min(id, 1, "id").Message("必须是有效的员工id")
|
valid.Min(id, 1, "id").Message("必须是有效的员工id")
|
||||||
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
|
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
if valid.HasErrors() {
|
||||||
code := e.InvalidParams
|
app.MarkErrors(valid.Errors)
|
||||||
|
appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
|
||||||
if !valid.HasErrors() {
|
return
|
||||||
if _, err := models.ExistEmployeeByID(id); err == nil {
|
|
||||||
code = e.SUCCESS
|
|
||||||
if avatarUrl != "" {
|
|
||||||
data["avatar_url"] = avatarUrl
|
|
||||||
}
|
|
||||||
if username != "" {
|
|
||||||
data["username"] = username
|
|
||||||
}
|
|
||||||
if password != "" {
|
|
||||||
data["password"] = password
|
|
||||||
}
|
|
||||||
if department != "" {
|
|
||||||
data["department"] = department
|
|
||||||
}
|
|
||||||
if position != "" {
|
|
||||||
data["position"] = position
|
|
||||||
}
|
|
||||||
if state >= 0 {
|
|
||||||
data["state"] = state
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
code = e.ErrorNotExistEmployee
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if code == 200 {
|
employeeService := employee_service.Employee{
|
||||||
models.EditEmployee(id, data)
|
ID: id,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
exists, err := employeeService.ExistByID()
|
||||||
"code": code,
|
if err != nil {
|
||||||
"msg": e.GetMsg(code),
|
appG.Response(http.StatusInternalServerError, e.ErrorCheckExistEmployeeFail, nil)
|
||||||
"data": data,
|
}
|
||||||
})
|
|
||||||
|
if !exists {
|
||||||
|
appG.Response(http.StatusOK, e.ErrorNotExistEmployee, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if models.ExistEmployeeByUsername(username) {
|
||||||
|
appG.Response(http.StatusInternalServerError, e.ErrorExistEmployee, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if avatarUrl != "" {
|
||||||
|
employeeService.AvatarUrl = avatarUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
if username != "" {
|
||||||
|
employeeService.Username = username
|
||||||
|
}
|
||||||
|
|
||||||
|
if password != "" {
|
||||||
|
employeeService.Password = password
|
||||||
|
}
|
||||||
|
|
||||||
|
if department != "" {
|
||||||
|
employeeService.Department = department
|
||||||
|
}
|
||||||
|
|
||||||
|
if position != "" {
|
||||||
|
employeeService.Position = position
|
||||||
|
}
|
||||||
|
|
||||||
|
if state >= 0 {
|
||||||
|
employeeService.State = state
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := employeeService.Edit(); err == nil {
|
||||||
|
appG.Response(http.StatusOK, e.SUCCESS, employeeService)
|
||||||
|
} else {
|
||||||
|
appG.Response(http.StatusOK, e.ErrorUpdatedEmployeeFail, employeeService)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,14 +25,14 @@ type Employee struct {
|
||||||
PageSize int
|
PageSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) Add() error {
|
func (syncEmployee *Employee) Add() error {
|
||||||
employee := map[string]interface{}{
|
employee := map[string]interface{}{
|
||||||
"avatar_url": e.AvatarUrl,
|
"avatar_url": syncEmployee.AvatarUrl,
|
||||||
"username": e.Username,
|
"username": syncEmployee.Username,
|
||||||
"password": e.Password,
|
"password": syncEmployee.Password,
|
||||||
"department": e.Department,
|
"department": syncEmployee.Department,
|
||||||
"position": e.Position,
|
"position": syncEmployee.Position,
|
||||||
"state": e.State,
|
"state": syncEmployee.State,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := models.AddEmployee(employee); err != nil {
|
if err := models.AddEmployee(employee); err != nil {
|
||||||
|
@ -42,22 +42,22 @@ func (e *Employee) Add() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) Edit() error {
|
func (syncEmployee *Employee) Edit() error {
|
||||||
return models.EditEmployee(e.ID, map[string]interface{}{
|
return models.EditEmployee(syncEmployee.ID, map[string]interface{}{
|
||||||
"avatar_url": e.AvatarUrl,
|
"avatar_url": syncEmployee.AvatarUrl,
|
||||||
"username": e.Username,
|
"username": syncEmployee.Username,
|
||||||
"password": e.Password,
|
"password": syncEmployee.Password,
|
||||||
"department": e.Department,
|
"department": syncEmployee.Department,
|
||||||
"position": e.Position,
|
"position": syncEmployee.Position,
|
||||||
"state": e.State,
|
"state": syncEmployee.State,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) Get() (*models.Employee, error) {
|
func (syncEmployee *Employee) Get() (*models.Employee, error) {
|
||||||
var cacheEmployee *models.Employee
|
var cacheEmployee *models.Employee
|
||||||
|
|
||||||
cache := cache_service.Employee{
|
cache := cache_service.Employee{
|
||||||
ID: e.ID,
|
ID: syncEmployee.ID,
|
||||||
}
|
}
|
||||||
key := cache.GetEmployeeKey()
|
key := cache.GetEmployeeKey()
|
||||||
if gredis.Exists(key) {
|
if gredis.Exists(key) {
|
||||||
|
@ -71,24 +71,24 @@ func (e *Employee) Get() (*models.Employee, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if employee, err := models.GetEmployee(e.ID); err != nil {
|
if employee, err := models.GetEmployee(syncEmployee.ID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
gredis.Set(key, employee, 3600)
|
gredis.Set(key, employee, 3600)
|
||||||
//cacheEmployee.ID = employee.ID
|
return employee, nil
|
||||||
return cacheEmployee, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) GetAll() ([]*models.Employee, error) {
|
func (syncEmployee *Employee) GetAll() ([]*models.Employee, error) {
|
||||||
var (
|
var (
|
||||||
employees, cacheEmployees []*models.Employee
|
employees, cacheEmployees []*models.Employee
|
||||||
)
|
)
|
||||||
|
|
||||||
cache := cache_service.Employee{
|
cache := cache_service.Employee{
|
||||||
State: e.State,
|
State: syncEmployee.State,
|
||||||
PageNum: e.PageNum,
|
PageNum: syncEmployee.PageNum,
|
||||||
PageSize: e.PageSize,
|
PageSize: syncEmployee.PageSize,
|
||||||
}
|
}
|
||||||
key := cache.GetEmployeesKey()
|
key := cache.GetEmployeesKey()
|
||||||
if gredis.Exists(key) {
|
if gredis.Exists(key) {
|
||||||
|
@ -101,7 +101,7 @@ func (e *Employee) GetAll() ([]*models.Employee, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
employees, err := models.GetEmployees(e.PageNum, e.PageSize, e.getMaps())
|
employees, err := models.GetEmployees(syncEmployee.PageNum, syncEmployee.PageSize, syncEmployee.getMaps())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -110,23 +110,23 @@ func (e *Employee) GetAll() ([]*models.Employee, error) {
|
||||||
return employees, nil
|
return employees, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Employee) Delete() error {
|
func (syncEmployee *Employee) Delete() error {
|
||||||
return models.DeleteEmployee(a.ID)
|
return models.DeleteEmployee(syncEmployee.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) ExistByID() (bool, error) {
|
func (syncEmployee *Employee) ExistByID() (bool, error) {
|
||||||
return models.ExistEmployeeByID(e.ID)
|
return models.ExistEmployeeByID(syncEmployee.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) Count() (int, error) {
|
func (syncEmployee *Employee) Count() (int, error) {
|
||||||
return models.GetEmployeeTotal(e.getMaps())
|
return models.GetEmployeeTotal(syncEmployee.getMaps())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Employee) getMaps() map[string]interface{} {
|
func (syncEmployee *Employee) getMaps() map[string]interface{} {
|
||||||
maps := make(map[string]interface{})
|
maps := make(map[string]interface{})
|
||||||
maps["deleted_on"] = 0
|
maps["deleted_on"] = 0
|
||||||
if e.State != -1 {
|
if syncEmployee.State != -1 {
|
||||||
maps["state"] = e.State
|
maps["state"] = syncEmployee.State
|
||||||
}
|
}
|
||||||
|
|
||||||
return maps
|
return maps
|
||||||
|
|
Loading…
Reference in New Issue