智慧食堂
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

161 rinda
3.9 KiB

  1. package hdl
  2. import (
  3. enum2 "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. svc3 "applet/app/admin/svc"
  7. "applet/app/db"
  8. model2 "applet/app/db/model"
  9. "applet/app/e"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func DeviceList(c *gin.Context) {
  14. var req md.DeviceListReq
  15. err := c.ShouldBindJSON(&req)
  16. if err != nil {
  17. err = validate.HandleValidateErr(err)
  18. err1 := err.(e.E)
  19. e.OutErr(c, err1.Code, err1.Error())
  20. return
  21. }
  22. if req.Limit == 0 {
  23. req.Limit = 10
  24. }
  25. if req.Page == 0 {
  26. req.Page = 10
  27. }
  28. sess := db.Db.Desc("device.id")
  29. admin := svc3.GetUser(c)
  30. if admin.IsSuperAdministrator != enum2.IsSuperAdministratorTure {
  31. adminWithEnterpriseDb := db.AdminWithEnterpriseDb{}
  32. adminWithEnterpriseDb.Set()
  33. adminWithEnterprise, err1 := adminWithEnterpriseDb.FindAdminWithEnterprise(admin.AdmId)
  34. if err1 != nil {
  35. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  36. return
  37. }
  38. var enterPriseIds []int
  39. for _, v1 := range *adminWithEnterprise {
  40. enterPriseIds = append(enterPriseIds, v1.EnterpriseId)
  41. }
  42. if len(enterPriseIds) == 0 {
  43. return
  44. }
  45. sess.In("device.enterprise_id", enterPriseIds)
  46. }
  47. if req.Name != "" {
  48. sess.And("device.name like ?", "%"+req.Name+"%")
  49. }
  50. if req.EnterpriseName != "" {
  51. sess.And("enterprise.name like ?", "%"+req.EnterpriseName+"%")
  52. }
  53. if req.StartDate != "" {
  54. sess.And("device.create_at >=?", req.StartDate)
  55. }
  56. if req.EndDate != "" {
  57. sess.And("device.create_at <=?", req.EndDate)
  58. }
  59. var m []*db.DeviceWithEnterprise
  60. total, err := sess.
  61. Join("LEFT", "enterprise", "enterprise.id = device.enterprise_id").Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  62. if err != nil {
  63. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  64. return
  65. }
  66. var resp []interface{}
  67. for _, v := range m {
  68. resp = append(resp, map[string]interface{}{
  69. "id": v.Device.Id,
  70. "name": v.Device.Name,
  71. "device_sn": v.Device.DeviceSn,
  72. "enterprise_id": v.Device.EnterpriseId,
  73. "memo": v.Device.Memo,
  74. "create_at": v.Device.CreateAt,
  75. "update_at": v.Device.UpdateAt,
  76. "enterprise_name": v.Enterprise.Name,
  77. })
  78. }
  79. e.OutSuc(c, map[string]interface{}{
  80. "list": resp,
  81. "total": total,
  82. }, nil)
  83. return
  84. }
  85. func DeviceSave(c *gin.Context) {
  86. var req md.DeviceSaveReq
  87. err := c.ShouldBindJSON(&req)
  88. if err != nil {
  89. err = validate.HandleValidateErr(err)
  90. err1 := err.(e.E)
  91. e.OutErr(c, err1.Code, err1.Error())
  92. return
  93. }
  94. deviceDd := db.Device{}
  95. deviceDd.Set(req.DeviceSn)
  96. //查找 device_sn
  97. device, err := deviceDd.GetDevice()
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. if device == nil {
  103. insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{
  104. Name: req.Name,
  105. DeviceSn: req.DeviceSn,
  106. EnterpriseId: req.EnterpriseId,
  107. Memo: req.Memo,
  108. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  109. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  110. })
  111. if err1 != nil {
  112. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  113. return
  114. }
  115. if insertAffected <= 0 {
  116. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  117. return
  118. }
  119. } else {
  120. device.EnterpriseId = req.EnterpriseId
  121. device.Name = req.Name
  122. device.Memo = req.Memo
  123. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  124. updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "name", "memo", "update_at")
  125. if err1 != nil {
  126. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  127. return
  128. }
  129. if updateAffected <= 0 {
  130. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  131. return
  132. }
  133. }
  134. e.OutSuc(c, "success", nil)
  135. return
  136. }
  137. func DeviceDelete(c *gin.Context) {
  138. deviceSn := c.Param("device_sn")
  139. deviceDd := db.Device{}
  140. deviceDd.Set(deviceSn)
  141. deviceDdAffected, err := deviceDd.DeviceDelete()
  142. if err != nil {
  143. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  144. return
  145. }
  146. if deviceDdAffected <= 0 {
  147. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  148. return
  149. }
  150. e.OutSuc(c, "success", nil)
  151. return
  152. }