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

import (
    "${.table.PackageName}/app/service/cache_service"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	${.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/util/gvalid"
)

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


// List 列表页
func (c *${.table.BusinessName|UcFirst}) List(r *ghttp.Request) {
	// 定义一个结构体存储请求参数
	var req *${.table.BusinessName}Model.ReqSearchList
	// 获取参数
	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())
		}
	}
	list, err := ${.table.BusinessName}Service.GetListSearch(req)
	if err != nil {
		response.FailJson(true, r, err.Error())
	}
	result := g.Map{
		"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.ReqAdd
		// 通过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())
		}
		cache_service.New().RemoveByTag("${.table.ModuleName}_${.table.BusinessName}_tag")
		response.SusJson(true, r, "添加成功")
	}
	//获取上级分类
    cates, err := ${.table.BusinessName}Service.GetList()
    if err != nil {
        response.FailJson(true, r, err.Error())
    }
    response.SusJson(true, r, "ok", g.Map{
        "parentList": cates,
    })
}


// Edit 修改
func (c *${.table.BusinessName|UcFirst}) Edit(r *ghttp.Request) {
	// 如果是post提交的请求就执行修改操作
	if r.Method == "POST" {
		var editReq *${.table.BusinessName}Model.ReqEdit
		// 通过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())
		}
		cache_service.New().RemoveByTag("${.table.ModuleName}_${.table.BusinessName}_tag")
		response.SusJson(true, r, "修改成功")
	}
	// 不是post提交的请求就到修改页面后查询出要修改的记录
	//获取上级
    cates, err := ${.table.BusinessName}Service.GetList()
    if err != nil {
        response.FailJson(true, r, err.Error())
    }
	//获取信息
    id := r.GetUint("id")
    menuInfo, err := ${.table.BusinessName}Service.GetInfoById(id)
    if err != nil {
        response.FailJson(true, r, err.Error())
    }
    if menuInfo == nil {
        response.FailJson(true, r, "参数错误")
    }
    response.SusJson(true, r, "ok", g.Map{
        "parentList": cates,
        "menuInfo":   menuInfo,
    })
}


// 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, "删除失败")
	}
	cache_service.New().RemoveByTag("${.table.ModuleName}_${.table.BusinessName}_tag")
	response.SusJson(true, r, "删除成功")
}
