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

126 行
2.8 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/lib/validate"
  4. "applet/app/admin/md"
  5. "applet/app/db"
  6. model2 "applet/app/db/model"
  7. "applet/app/e"
  8. "github.com/gin-gonic/gin"
  9. "time"
  10. )
  11. func DeviceList(c *gin.Context) {
  12. var req md.DeviceListReq
  13. err := c.ShouldBindJSON(&req)
  14. if err != nil {
  15. err = validate.HandleValidateErr(err)
  16. err1 := err.(e.E)
  17. e.OutErr(c, err1.Code, err1.Error())
  18. return
  19. }
  20. if req.Limit == 0 {
  21. req.Limit = 10
  22. }
  23. if req.Page == 0 {
  24. req.Page = 10
  25. }
  26. sess := db.Db.Desc("id")
  27. if req.Name != "" {
  28. sess.And("device.name like ?", "%"+req.Name+"%")
  29. }
  30. if req.EnterpriseName != "" {
  31. sess.And("enterprise.name like ?", "%"+req.EnterpriseName+"%")
  32. }
  33. if req.StartDate != "" {
  34. sess.And("device.create_at >=?", req.StartDate)
  35. }
  36. if req.EndDate != "" {
  37. sess.And("device.create_at <=?", req.EndDate)
  38. }
  39. var m []*db.DeviceWithEnterprise
  40. total, err := sess.Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  41. if err != nil {
  42. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  43. return
  44. }
  45. e.OutSuc(c, map[string]interface{}{
  46. "list": m,
  47. "total": total,
  48. }, nil)
  49. return
  50. }
  51. func DeviceSave(c *gin.Context) {
  52. var req md.DeviceSaveReq
  53. err := c.ShouldBindJSON(&req)
  54. if err != nil {
  55. err = validate.HandleValidateErr(err)
  56. err1 := err.(e.E)
  57. e.OutErr(c, err1.Code, err1.Error())
  58. return
  59. }
  60. deviceDd := db.Device{}
  61. deviceDd.Set(req.DeviceSn)
  62. //查找 device_sn
  63. device, err := deviceDd.GetDevice()
  64. if err != nil {
  65. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  66. return
  67. }
  68. if device == nil {
  69. insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{
  70. Name: req.Name,
  71. DeviceSn: req.DeviceSn,
  72. EnterpriseId: req.EnterpriseId,
  73. Memo: req.Memo,
  74. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  75. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  76. })
  77. if err1 != nil {
  78. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  79. return
  80. }
  81. if insertAffected <= 0 {
  82. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  83. return
  84. }
  85. } else {
  86. device.EnterpriseId = req.EnterpriseId
  87. device.Name = req.Name
  88. device.Memo = req.Memo
  89. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  90. updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "name", "memo", "update_at")
  91. if err1 != nil {
  92. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  93. return
  94. }
  95. if updateAffected <= 0 {
  96. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  97. return
  98. }
  99. }
  100. e.OutSuc(c, "success", nil)
  101. return
  102. }
  103. func DeviceDelete(c *gin.Context) {
  104. deviceSn := c.Param("device_sn")
  105. deviceDd := db.Device{}
  106. deviceDd.Set(deviceSn)
  107. deviceDdAffected, err := deviceDd.DeviceDelete()
  108. if err != nil {
  109. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  110. return
  111. }
  112. if deviceDdAffected <= 0 {
  113. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  114. return
  115. }
  116. e.OutSuc(c, "success", nil)
  117. return
  118. }