智慧食堂
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

150 行
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. merchant.CreateAt = req.CreateAt
  112. _, err = merchantDb.MerchantUpdate(merchant, "name", "enterprise_id", "update_at", "create_at")
  113. if err != nil {
  114. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  115. return
  116. }
  117. e.OutSuc(c, "success", nil)
  118. return
  119. }
  120. func MerchantDelete(c *gin.Context) {
  121. id := c.Param("merchant_id")
  122. merchantDb := db.MerchantDb{}
  123. merchantDb.Set()
  124. merchant, err := merchantDb.GetMerchant(utils.StrToInt(id))
  125. if err != nil {
  126. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  127. return
  128. }
  129. if merchant == nil {
  130. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  131. return
  132. }
  133. _, err = merchantDb.MerchantDelete(id)
  134. if err != nil {
  135. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  136. return
  137. }
  138. e.OutSuc(c, "success", nil)
  139. return
  140. }