package router

import (
	"demo/data/dao/manage"
	"demo/data/domain"
	"demo/share"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

func Base" + table.AaBbName + "Router(group *gin.RouterGroup) {
	Base" + table.AaBbName + "Group := group.Group("/back/base")
	PushRouter(Base" + table.AaBbName + "Group, "GET", "/" + table.aaBbName + "", GetBase" + table.AaBbName + "ById)
	PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "/list", GetBase" + table.AaBbName + "List)
	PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "/in", GetBase" + table.AaBbName + "ListInId)
	PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "", SaveBase" + table.AaBbName + ")
	PushRouter(Base" + table.AaBbName + "Group, "PUT", "/" + table.aaBbName + "", UpdateBase" + table.AaBbName + ")
	PushRouter(Base" + table.AaBbName + "Group, "DELETE", "/" + table.aaBbName + "", DeleteBase" + table.AaBbName + ")
}

func GetBase" + table.AaBbName + "ById(c *gin.Context) {
	id := c.Param("id")
	" + table.aaBbName + ", err := manage.Get" + table.AaBbName + "ById(cast.ToInt64(id))
	if err != nil {
		c.JSON(200, CreateResultError(500, "用户查询不存在"))
	}
	c.JSON(200, CreateResultData(" + table.aaBbName + "))
}

func GetBase" + table.AaBbName + "List(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	param, _ := data("param")
	pageNum, _ := data("pageNum")
	pageSize, _ := data("pageSize")
	" + table.aaBbName + " := domain." + table.AaBbName + "{}
	err := json.Unmarshal([]byte(cast.ToString(param)), &" + table.aaBbName + ")
	list, err := manage.Get" + table.AaBbName + "List(" + table.aaBbName + ", cast.ToInt(pageNum), cast.ToInt(pageSize))
	if err != nil {
		c.JSON(200, CreateResultError(500, "查询错误"))
	}
	c.JSON(200, CreateResultData(list))
}

func GetBase" + table.AaBbName + "ListInId(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	idsString, _ := data("ids")
	var ids []int64
	err := json.Unmarshal([]byte(cast.ToString(idsString)), &ids)
	list, err := manage.Get" + table.AaBbName + "ListInId(ids)
	if err != nil {
		c.JSON(200, CreateResultError(500, "查询错误"))
	}
	c.JSON(200, CreateResultData(list))
}

func SaveBase" + table.AaBbName + "(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	" + table.aaBbName + "Param, _ := data("" + table.aaBbName + "")
	" + table.aaBbName + " := domain." + table.AaBbName + "{}
	err := json.Unmarshal([]byte(cast.ToString(" + table.aaBbName + "Param)), &" + table.aaBbName + ")
	save" + table.AaBbName + ", err := manage.Save" + table.AaBbName + "(&" + table.aaBbName + ")
	if err != nil {
		c.JSON(200, CreateResultError(500, "保存错误"))
	}
	c.JSON(200, CreateResultData(save" + table.AaBbName + "))
}

func UpdateBase" + table.AaBbName + "(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	" + table.aaBbName + ", _ := data("" + table.aaBbName + "")
	id, _ := data("id")

	set" + table.AaBbName + ", b, err := manage.Set" + table.AaBbName + "(cast.ToStringMap(" + table.aaBbName + "), cast.ToInt64(id))
	if err != nil || !b {
		c.JSON(200, CreateResultError(500, "修改错误"))
	}
	c.JSON(200, CreateResultData(set" + table.AaBbName + "))
}

func DeleteBase" + table.AaBbName + "(c *gin.Context) {
	id := c.Param("id")
	res := manage.Delete" + table.AaBbName + "(cast.ToInt64(id))
	if !res {
		c.JSON(200, CreateResultError(500, "删除错误"))
	}
	c.JSON(200, CreateResult())
}
