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