智慧食堂
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

149 rader
3.4 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/lib/validate"
  4. "applet/app/admin/md"
  5. "applet/app/db"
  6. "applet/app/db/model"
  7. "applet/app/e"
  8. "applet/app/utils"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func MerchantList(c *gin.Context) {
  13. var req md.MerchantListReq
  14. err := c.ShouldBindJSON(&req)
  15. if err != nil {
  16. err = validate.HandleValidateErr(err)
  17. err1 := err.(e.E)
  18. e.OutErr(c, err1.Code, err1.Error())
  19. return
  20. }
  21. if req.Limit == 0 {
  22. req.Limit = 10
  23. }
  24. if req.Page == 0 {
  25. req.Page = 1
  26. }
  27. var m []*db.MerchantWithEnterpriseInfo
  28. engine := db.Db.Where("1=1")
  29. if req.EnterpriseId != 0 {
  30. engine = engine.And("merchant.enterprise_id =?", req.EnterpriseId)
  31. }
  32. if req.Name != "" {
  33. engine = engine.And("merchant.name like ?", "%"+req.Name+"%")
  34. }
  35. total, err := engine.Join("LEFT", "enterprise", "enterprise.id = merchant.enterprise_id").
  36. Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  37. if err != nil {
  38. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  39. return
  40. }
  41. var resp []interface{}
  42. for _, v := range m {
  43. resp = append(resp, map[string]interface{}{
  44. "id": v.Merchant.Id,
  45. "name": v.Merchant.Name,
  46. "enterprise_id": v.Merchant.EnterpriseId,
  47. "create_at": v.Merchant.CreateAt,
  48. "update_at": v.Merchant.UpdateAt,
  49. "enterprise_name": v.Enterprise.Name,
  50. })
  51. }
  52. e.OutSuc(c, map[string]interface{}{
  53. "list": resp,
  54. "total": total,
  55. }, nil)
  56. return
  57. }
  58. func MerchantAdd(c *gin.Context) {
  59. var req md.MerchantAddReq
  60. err := c.ShouldBindJSON(&req)
  61. if err != nil {
  62. err = validate.HandleValidateErr(err)
  63. err1 := err.(e.E)
  64. e.OutErr(c, err1.Code, err1.Error())
  65. return
  66. }
  67. merchantDb := db.MerchantDb{}
  68. merchantDb.Set()
  69. now := time.Now()
  70. insertAffected, err := merchantDb.MerchantInsert(&model.Merchant{
  71. EnterpriseId: req.EnterpriseId,
  72. Name: req.Name,
  73. CreateAt: now.Format("2006-01-02 15:04:05"),
  74. UpdateAt: now.Format("2006-01-02 15:04:05"),
  75. })
  76. if err != nil {
  77. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  78. return
  79. }
  80. if insertAffected <= 0 {
  81. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  82. return
  83. }
  84. e.OutSuc(c, "success", nil)
  85. return
  86. }
  87. func MerchantUpdate(c *gin.Context) {
  88. var req md.MerchantUpdateReq
  89. err := c.ShouldBindJSON(&req)
  90. if err != nil {
  91. err = validate.HandleValidateErr(err)
  92. err1 := err.(e.E)
  93. e.OutErr(c, err1.Code, err1.Error())
  94. return
  95. }
  96. merchantDb := db.MerchantDb{}
  97. merchantDb.Set()
  98. merchant, err := merchantDb.GetMerchant(req.Id)
  99. if err != nil {
  100. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  101. return
  102. }
  103. if merchant == nil {
  104. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  105. return
  106. }
  107. now := time.Now()
  108. merchant.EnterpriseId = req.EnterpriseId
  109. merchant.Name = req.Name
  110. merchant.UpdateAt = now.Format("2006-01-02 15:04:05")
  111. _, err = merchantDb.MerchantUpdate(merchant, "name", "enterprise_id", "update_at")
  112. if err != nil {
  113. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  114. return
  115. }
  116. e.OutSuc(c, "success", nil)
  117. return
  118. }
  119. func MerchantDelete(c *gin.Context) {
  120. id := c.Param("merchant_id")
  121. merchantDb := db.MerchantDb{}
  122. merchantDb.Set()
  123. merchant, err := merchantDb.GetMerchant(utils.StrToInt(id))
  124. if err != nil {
  125. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  126. return
  127. }
  128. if merchant == nil {
  129. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  130. return
  131. }
  132. _, err = merchantDb.MerchantDelete(id)
  133. if err != nil {
  134. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  135. return
  136. }
  137. e.OutSuc(c, "success", nil)
  138. return
  139. }