// ==========================================================================
// 生成日期：${.table.CreateTime}
// 生成人：${.table.FunctionAuthor}
// ==========================================================================
package ${.table.ModuleName}

import (
	${.table.BusinessName}Model "${.table.PackageName}/app/model/${.table.ModuleName}/${.table.BusinessName}"
	${.table.BusinessName}Service "${.table.PackageName}/app/service/${.table.ModuleName}/${.table.BusinessName}_service"
	"${.table.PackageName}/library/response"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gvalid"
)

//控制器
type ${.table.BusinessName|UcFirst} struct{}


// List 列表页
func (c *${.table.BusinessName|UcFirst}) List(r *ghttp.Request) {
	// 定义一个结构体存储请求参数
	var req *${.table.BusinessName}Model.SelectPageReq
	// 获取参数
	err := r.Parse(&req)
	if err != nil {
		if _,ok := err.(*gvalid.Error); ok {
			response.FailJson(true, r, err.(*gvalid.Error).FirstString())
		} else {
			response.FailJson(true, r, err.Error())
		}
	}
	total, page, list, err := ${.table.BusinessName}Service.SelectListByPage(req)
	if err != nil {
		response.FailJson(true, r, err.Error())
	}
	result := g.Map{
		"currentPage": page,
		"total":       total,
		"list":        list,
	}
	response.SusJson(true, r, "获取列表数据成功", result)
}


// Add 新增
func (c *${.table.BusinessName|UcFirst}) Add(r *ghttp.Request) {
	if r.Method == "POST" {
		var req *${.table.BusinessName}Model.AddReq
		// 通过Parse方法解析获取参数
		err := r.Parse(&req)
		if err != nil {
			if _,ok := err.(*gvalid.Error); ok {
				response.FailJson(true, r, err.(*gvalid.Error).FirstString())
			} else {
				response.FailJson(true, r, err.Error())
			}
		}
		// 调用service中的添加函数添加
		err = ${.table.BusinessName}Service.AddSave(req)
		if err != nil {
			response.FailJson(true, r, err.Error())
		}
		response.SusJson(true, r, "添加成功")
	}
}


// Edit 修改
func (c *${.table.BusinessName|UcFirst}) Edit(r *ghttp.Request) {
	// 如果是post提交的请求就执行修改操作
	if r.Method == "POST" {
		var editReq *${.table.BusinessName}Model.EditReq
		// 通过Parse方法解析获取参数
		err := r.Parse(&editReq)
		if err != nil {
			if _,ok := err.(*gvalid.Error); ok {
				response.FailJson(true, r, err.(*gvalid.Error).FirstString())
			} else {
				response.FailJson(true, r, err.Error())
			}
		}
		err = ${.table.BusinessName}Service.EditSave(editReq)
		if err != nil {
		response.FailJson(true, r, err.Error())
		}
		response.SusJson(true, r, "修改参数成功")
	}
	// 不是post提交的请求就到修改页面后查询出要修改的记录
	id := r.Get${.table.PkColumn.GoType|UcFirst}("id")
	params, err := ${.table.BusinessName}Service.GetByID(id)

	if err != nil {
		response.FailJson(true, r, err.Error())
	}
	response.SusJson(true, r, "ok", params)
}


// Delete 删除
func (c *${.table.BusinessName|UcFirst}) Delete(r *ghttp.Request) {
	var req *${.table.BusinessName}Model.RemoveReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		if _,ok := err.(*gvalid.Error); ok {
			response.FailJson(true, r, err.(*gvalid.Error).FirstString())
		} else {
			response.FailJson(true, r, err.Error())
		}
	}
	err := ${.table.BusinessName}Service.DeleteByIds(req.Ids)
	if err != nil {
	response.FailJson(true, r, "删除失败")
	}
	response.SusJson(true, r, "删除成功")
}
