|
|
@@ -1,9 +1,9 @@
|
|
|
package class
|
|
|
|
|
|
import (
|
|
|
+ "dashoo.cn/micro_libary/response"
|
|
|
"dashoo.cn/modi_webapi/app/service/class"
|
|
|
"dashoo.cn/modi_webapi/library/request"
|
|
|
- "dashoo.cn/modi_webapi/library/response"
|
|
|
"fmt"
|
|
|
"github.com/gogf/gf/net/ghttp"
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
@@ -12,21 +12,22 @@ import (
|
|
|
|
|
|
type Controller struct {
|
|
|
}
|
|
|
+
|
|
|
// 获取班级管理列表
|
|
|
-func (c *Controller) GetAllClass(r *ghttp.Request){
|
|
|
+func (c *Controller) GetAllClass(r *ghttp.Request) {
|
|
|
page := request.GetPageInfo(r)
|
|
|
where := ""
|
|
|
|
|
|
if year := r.GetInt("Year"); year != 0 {
|
|
|
- if where == ""{
|
|
|
+ if where == "" {
|
|
|
where = fmt.Sprintf(" Year = %v", year)
|
|
|
} else {
|
|
|
where += fmt.Sprintf(" AND Year = %v", year)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if name := r.GetString("Name"); name != ""{
|
|
|
- if where == ""{
|
|
|
+ if name := r.GetString("Name"); name != "" {
|
|
|
+ if where == "" {
|
|
|
where = fmt.Sprintf(" Name LIKE '%%%v%%'", name)
|
|
|
} else {
|
|
|
where += fmt.Sprintf(" AND Name LIKE '%%%v%%'", name)
|
|
|
@@ -34,13 +35,13 @@ func (c *Controller) GetAllClass(r *ghttp.Request){
|
|
|
}
|
|
|
|
|
|
var result []class.Entity
|
|
|
- if err := class.GetAllClass(page, where, &result); err != nil{
|
|
|
- if err.Error() == "sql: no rows in result set"{
|
|
|
+ if err := class.GetAllClass(page, where, &result); err != nil {
|
|
|
+ if err.Error() == "sql: no rows in result set" {
|
|
|
response.Json(r, 0, "")
|
|
|
return
|
|
|
}
|
|
|
response.Json(r, -1, err.Error())
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
count, err1 := class.FindClassCount(where)
|
|
|
if err1 != nil {
|
|
|
response.Json(r, -1, err1.Error())
|
|
|
@@ -50,22 +51,22 @@ func (c *Controller) GetAllClass(r *ghttp.Request){
|
|
|
records.Current = page.Current
|
|
|
records.Total = count
|
|
|
records.Records = result
|
|
|
- response.Json(r, 0, "", records)
|
|
|
+ response.Json(r, 0, "ok", records)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 添加一条班级信息
|
|
|
-func (c *Controller) AddClass(r *ghttp.Request){
|
|
|
+func (c *Controller) AddClass(r *ghttp.Request) {
|
|
|
Class := new(class.Entity)
|
|
|
if err := r.Parse(Class); err != nil {
|
|
|
// 数据验证错误
|
|
|
if v, ok := err.(*gvalid.Error); ok {
|
|
|
- response.Json(r, 1, v.FirstString())
|
|
|
+ response.Json(r, -1, v.FirstString())
|
|
|
r.ExitAll()
|
|
|
}
|
|
|
// 其他错误
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
r.ExitAll()
|
|
|
}
|
|
|
realName := r.GetParamVar("realname").String()
|
|
|
@@ -75,41 +76,49 @@ func (c *Controller) AddClass(r *ghttp.Request){
|
|
|
Class.UpdatedTime = currentTime
|
|
|
Class.UpdatedBy = realName
|
|
|
|
|
|
- if result,err := class.Insert(Class); err != nil {
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ if result, err := class.Insert(Class); err != nil {
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
} else {
|
|
|
var records response.PagedRecords
|
|
|
id, _ := result.LastInsertId()
|
|
|
Class.Id = int(id)
|
|
|
records.Records = Class
|
|
|
- response.Json(r, 0, "", records)
|
|
|
+ response.Json(r, 0, "ok", records)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 获取一条班级信息
|
|
|
-func (c *Controller) GetOneClass(r *ghttp.Request){
|
|
|
+func (c *Controller) GetOneClass(r *ghttp.Request) {
|
|
|
id := r.GetInt("id")
|
|
|
+ if id == 0 {
|
|
|
+ response.Json(r, -1, "id不能为空")
|
|
|
+ }
|
|
|
if result, err := class.FindOne(id); err != nil {
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
r.ExitAll()
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
var records response.PagedRecords
|
|
|
records.Records = result
|
|
|
- response.Json(r, 0, "", records)
|
|
|
+ response.Json(r, 0, "ok", records)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 修改一条班级信息
|
|
|
-func (c *Controller) UpdateClass(r *ghttp.Request){
|
|
|
+func (c *Controller) UpdateClass(r *ghttp.Request) {
|
|
|
Class := new(class.Entity)
|
|
|
+ if Class.Id == 0 {
|
|
|
+ response.Json(r, -1, "Id不能为空")
|
|
|
+ }
|
|
|
+
|
|
|
if err := r.Parse(Class); err != nil {
|
|
|
// 数据验证错误
|
|
|
if v, ok := err.(*gvalid.Error); ok {
|
|
|
- response.Json(r, 1, v.FirstString())
|
|
|
+ response.Json(r, -1, v.FirstString())
|
|
|
r.ExitAll()
|
|
|
}
|
|
|
// 其他错误
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
r.ExitAll()
|
|
|
}
|
|
|
|
|
|
@@ -119,21 +128,25 @@ func (c *Controller) UpdateClass(r *ghttp.Request){
|
|
|
Class.UpdatedTime = currentTime
|
|
|
Class.UpdatedBy = realName
|
|
|
|
|
|
- if _,err := class.Replace(Class); err != nil {
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ if _, err := class.Replace(Class); err != nil {
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
} else {
|
|
|
var records response.PagedRecords
|
|
|
records.Records = Class
|
|
|
- response.Json(r, 0, "", records)
|
|
|
+ response.Json(r, 0, "ok", records)
|
|
|
}
|
|
|
}
|
|
|
- // 删除一条班级信息
|
|
|
-func (c *Controller) DeleteClass(r *ghttp.Request){
|
|
|
+
|
|
|
+// 删除一条班级信息
|
|
|
+func (c *Controller) DeleteClass(r *ghttp.Request) {
|
|
|
id := r.GetInt("id")
|
|
|
- if _,err := class.Delete(fmt.Sprintf("Id=%v", id)); err != nil{
|
|
|
- response.Json(r, 1, err.Error())
|
|
|
+ if id == 0 {
|
|
|
+ response.Json(r, -1, "id不能为空")
|
|
|
+ }
|
|
|
+ if _, err := class.Delete(fmt.Sprintf("Id=%v", id)); err != nil {
|
|
|
+ response.Json(r, -1, err.Error())
|
|
|
r.ExitAll()
|
|
|
} else {
|
|
|
- response.Json(r, 0, "该记录已删除!")
|
|
|
+ response.Json(r, 0, "记录已删除!")
|
|
|
}
|
|
|
}
|