package apis

import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"ferry/models"
"ferry/tools"
"ferry/tools/app"
"ferry/tools/app/msg"
)

func Get{{.ClassName}}List(c *gin.Context) {
var data models.{{.ClassName}}
var err error
var pageSize = 10
var pageIndex = 1

if size := c.Request.FormValue("pageSize"); size != "" {
pageSize = tools.StrToInt(err, size)
}
if index := c.Request.FormValue("pageIndex"); index != "" {
pageIndex = tools.StrToInt(err, index)
}

{{ range .Columns -}}
    {{$z := .IsQuery}}
    {{- if ($z) -}}data.{{.GoField}} = c.Request.FormValue("{{.JsonField}}")
    {{ end -}}
{{end}}

result, count, err := data.GetPage(pageSize, pageIndex)
tools.HasError(err, "", -1)

app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func Get{{.ClassName}}(c *gin.Context) {
var data models.{{.ClassName}}
data.{{.PkGoField}}, _ = tools.StringToInt(c.Param("{{.PkJsonField}}"))
result, err := data.Get()
tools.HasError(err, "抱歉未找到相关信息", -1)

app.OK(c, result, "")
}

func Insert{{.ClassName}}(c *gin.Context) {
var data models.{{.ClassName}}
err := c.ShouldBindJSON(&data)
data.CreateBy = tools.GetUserIdStr(c)
tools.HasError(err, "", 500)
result, err := data.Create()
tools.HasError(err, "", -1)
app.OK(c, result, "")
}

func Update{{.ClassName}}(c *gin.Context) {
var data models.{{.ClassName}}
err := c.BindWith(&data, binding.JSON)
tools.HasError(err, "数据解析失败", -1)
data.UpdateBy = tools.GetUserIdStr(c)
result, err := data.Update(data.{{.PkGoField}})
tools.HasError(err, "", -1)

app.OK(c, result, "")
}

func Delete{{.ClassName}}(c *gin.Context) {
var data models.{{.ClassName}}
data.UpdateBy = tools.GetUserIdStr(c)

IDS := tools.IdsStrToIdsIntGroup("{{.PkJsonField}}", c)
_, err := data.BatchDelete(IDS)
tools.HasError(err, msg.DeletedFail, 500)
app.OK(c, nil, msg.DeletedSuccess)
}