TextFile 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package router
  2. import (
  3. "demo/data/dao/manage"
  4. "demo/data/domain"
  5. "demo/share"
  6. "encoding/json"
  7. "github.com/gin-gonic/gin"
  8. "github.com/spf13/cast"
  9. )
  10. func Base" + table.AaBbName + "Router(group *gin.RouterGroup) {
  11. Base" + table.AaBbName + "Group := group.Group("/back/base")
  12. PushRouter(Base" + table.AaBbName + "Group, "GET", "/" + table.aaBbName + "", GetBase" + table.AaBbName + "ById)
  13. PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "/list", GetBase" + table.AaBbName + "List)
  14. PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "/in", GetBase" + table.AaBbName + "ListInId)
  15. PushRouter(Base" + table.AaBbName + "Group, "POST", "/" + table.aaBbName + "", SaveBase" + table.AaBbName + ")
  16. PushRouter(Base" + table.AaBbName + "Group, "PUT", "/" + table.aaBbName + "", UpdateBase" + table.AaBbName + ")
  17. PushRouter(Base" + table.AaBbName + "Group, "DELETE", "/" + table.aaBbName + "", DeleteBase" + table.AaBbName + ")
  18. }
  19. func GetBase" + table.AaBbName + "ById(c *gin.Context) {
  20. id := c.Param("id")
  21. " + table.aaBbName + ", err := manage.Get" + table.AaBbName + "ById(cast.ToInt64(id))
  22. if err != nil {
  23. c.JSON(200, CreateResultError(500, "用户查询不存在"))
  24. }
  25. c.JSON(200, CreateResultData(" + table.aaBbName + "))
  26. }
  27. func GetBase" + table.AaBbName + "List(c *gin.Context) {
  28. data := share.GetJsonAnyParam(c)
  29. param, _ := data("param")
  30. pageNum, _ := data("pageNum")
  31. pageSize, _ := data("pageSize")
  32. " + table.aaBbName + " := domain." + table.AaBbName + "{}
  33. err := json.Unmarshal([]byte(cast.ToString(param)), &" + table.aaBbName + ")
  34. list, err := manage.Get" + table.AaBbName + "List(" + table.aaBbName + ", cast.ToInt(pageNum), cast.ToInt(pageSize))
  35. if err != nil {
  36. c.JSON(200, CreateResultError(500, "查询错误"))
  37. }
  38. c.JSON(200, CreateResultData(list))
  39. }
  40. func GetBase" + table.AaBbName + "ListInId(c *gin.Context) {
  41. data := share.GetJsonAnyParam(c)
  42. idsString, _ := data("ids")
  43. var ids []int64
  44. err := json.Unmarshal([]byte(cast.ToString(idsString)), &ids)
  45. list, err := manage.Get" + table.AaBbName + "ListInId(ids)
  46. if err != nil {
  47. c.JSON(200, CreateResultError(500, "查询错误"))
  48. }
  49. c.JSON(200, CreateResultData(list))
  50. }
  51. func SaveBase" + table.AaBbName + "(c *gin.Context) {
  52. data := share.GetJsonAnyParam(c)
  53. " + table.aaBbName + "Param, _ := data("" + table.aaBbName + "")
  54. " + table.aaBbName + " := domain." + table.AaBbName + "{}
  55. err := json.Unmarshal([]byte(cast.ToString(" + table.aaBbName + "Param)), &" + table.aaBbName + ")
  56. save" + table.AaBbName + ", err := manage.Save" + table.AaBbName + "(&" + table.aaBbName + ")
  57. if err != nil {
  58. c.JSON(200, CreateResultError(500, "保存错误"))
  59. }
  60. c.JSON(200, CreateResultData(save" + table.AaBbName + "))
  61. }
  62. func UpdateBase" + table.AaBbName + "(c *gin.Context) {
  63. data := share.GetJsonAnyParam(c)
  64. " + table.aaBbName + ", _ := data("" + table.aaBbName + "")
  65. id, _ := data("id")
  66. set" + table.AaBbName + ", b, err := manage.Set" + table.AaBbName + "(cast.ToStringMap(" + table.aaBbName + "), cast.ToInt64(id))
  67. if err != nil || !b {
  68. c.JSON(200, CreateResultError(500, "修改错误"))
  69. }
  70. c.JSON(200, CreateResultData(set" + table.AaBbName + "))
  71. }
  72. func DeleteBase" + table.AaBbName + "(c *gin.Context) {
  73. id := c.Param("id")
  74. res := manage.Delete" + table.AaBbName + "(cast.ToInt64(id))
  75. if !res {
  76. c.JSON(200, CreateResultError(500, "删除错误"))
  77. }
  78. c.JSON(200, CreateResult())
  79. }