智慧食堂
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

hdl_device.go 3.3 KiB

1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
1 ano atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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("device.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.
  41. Join("LEFT", "enterprise", "enterprise.id = device.enterprise_id").Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  42. if err != nil {
  43. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  44. return
  45. }
  46. var resp []interface{}
  47. for _, v := range m {
  48. resp = append(resp, map[string]interface{}{
  49. "id": v.Device.Id,
  50. "name": v.Device.Name,
  51. "device_sn": v.Device.DeviceSn,
  52. "enterprise_id": v.Device.EnterpriseId,
  53. "memo": v.Device.Memo,
  54. "create_at": v.Device.CreateAt,
  55. "update_at": v.Device.UpdateAt,
  56. "enterprise_name": v.Enterprise.Name,
  57. })
  58. }
  59. e.OutSuc(c, map[string]interface{}{
  60. "list": resp,
  61. "total": total,
  62. }, nil)
  63. return
  64. }
  65. func DeviceSave(c *gin.Context) {
  66. var req md.DeviceSaveReq
  67. err := c.ShouldBindJSON(&req)
  68. if err != nil {
  69. err = validate.HandleValidateErr(err)
  70. err1 := err.(e.E)
  71. e.OutErr(c, err1.Code, err1.Error())
  72. return
  73. }
  74. deviceDd := db.Device{}
  75. deviceDd.Set(req.DeviceSn)
  76. //查找 device_sn
  77. device, err := deviceDd.GetDevice()
  78. if err != nil {
  79. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  80. return
  81. }
  82. if device == nil {
  83. insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{
  84. Name: req.Name,
  85. DeviceSn: req.DeviceSn,
  86. EnterpriseId: req.EnterpriseId,
  87. Memo: req.Memo,
  88. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  89. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  90. })
  91. if err1 != nil {
  92. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  93. return
  94. }
  95. if insertAffected <= 0 {
  96. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  97. return
  98. }
  99. } else {
  100. device.EnterpriseId = req.EnterpriseId
  101. device.Name = req.Name
  102. device.Memo = req.Memo
  103. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  104. updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "name", "memo", "update_at")
  105. if err1 != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. if updateAffected <= 0 {
  110. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  111. return
  112. }
  113. }
  114. e.OutSuc(c, "success", nil)
  115. return
  116. }
  117. func DeviceDelete(c *gin.Context) {
  118. deviceSn := c.Param("device_sn")
  119. deviceDd := db.Device{}
  120. deviceDd.Set(deviceSn)
  121. deviceDdAffected, err := deviceDd.DeviceDelete()
  122. if err != nil {
  123. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  124. return
  125. }
  126. if deviceDdAffected <= 0 {
  127. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  128. return
  129. }
  130. e.OutSuc(c, "success", nil)
  131. return
  132. }