智慧食堂
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.4 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").
  62. Join("LEFT", "merchant", "merchant.id = device.merchant_id").
  63. Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
  64. if err != nil {
  65. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  66. return
  67. }
  68. var resp []interface{}
  69. for _, v := range m {
  70. resp = append(resp, map[string]interface{}{
  71. "id": v.Device.Id,
  72. "name": v.Device.Name,
  73. "device_sn": v.Device.DeviceSn,
  74. "enterprise_id": v.Device.EnterpriseId,
  75. "merchant_id": v.Device.MerchantId,
  76. "memo": v.Device.Memo,
  77. "create_at": v.Device.CreateAt,
  78. "update_at": v.Device.UpdateAt,
  79. "enterprise_name": v.Enterprise.Name,
  80. "merchant_name": v.Merchant.Name,
  81. })
  82. }
  83. e.OutSuc(c, map[string]interface{}{
  84. "list": resp,
  85. "total": total,
  86. }, nil)
  87. return
  88. }
  89. func DeviceSave(c *gin.Context) {
  90. var req md.DeviceSaveReq
  91. err := c.ShouldBindJSON(&req)
  92. if err != nil {
  93. err = validate.HandleValidateErr(err)
  94. err1 := err.(e.E)
  95. e.OutErr(c, err1.Code, err1.Error())
  96. return
  97. }
  98. deviceDd := db.Device{}
  99. deviceDd.Set(req.DeviceSn)
  100. //查找 device_sn
  101. device, err := deviceDd.GetDevice()
  102. if err != nil {
  103. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  104. return
  105. }
  106. if device == nil {
  107. insertAffected, err1 := deviceDd.DeviceInsert(&model2.Device{
  108. Name: req.Name,
  109. DeviceSn: req.DeviceSn,
  110. EnterpriseId: req.EnterpriseId,
  111. MerchantId: req.MerchantId,
  112. Memo: req.Memo,
  113. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  114. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  115. })
  116. if err1 != nil {
  117. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  118. return
  119. }
  120. if insertAffected <= 0 {
  121. e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
  122. return
  123. }
  124. } else {
  125. device.EnterpriseId = req.EnterpriseId
  126. device.Name = req.Name
  127. device.Memo = req.Memo
  128. device.MerchantId = req.MerchantId
  129. device.UpdateAt = time.Now().Format("2006-01-02 15:04:05")
  130. updateAffected, err1 := deviceDd.DeviceUpdate(device, "enterprise_id", "merchant_id", "name", "memo", "update_at")
  131. if err1 != nil {
  132. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  133. return
  134. }
  135. if updateAffected <= 0 {
  136. e.OutErr(c, e.ERR_DB_ORM, "更新数据失败")
  137. return
  138. }
  139. }
  140. e.OutSuc(c, "success", nil)
  141. return
  142. }
  143. func DeviceDelete(c *gin.Context) {
  144. deviceSn := c.Param("device_sn")
  145. deviceDd := db.Device{}
  146. deviceDd.Set(deviceSn)
  147. deviceDdAffected, err := deviceDd.DeviceDelete()
  148. if err != nil {
  149. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  150. return
  151. }
  152. if deviceDdAffected <= 0 {
  153. e.OutErr(c, e.ERR_DB_ORM, "删除数据失败")
  154. return
  155. }
  156. e.OutSuc(c, "success", nil)
  157. return
  158. }
  159. func EnterPriseMerchantList(c *gin.Context) {
  160. enterpriseId := c.DefaultQuery("enterprise_id", "0")
  161. var m []model2.Merchant
  162. if err := db.Db.Where("enterprise_id = ?", enterpriseId).Find(&m); err != nil {
  163. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  164. return
  165. }
  166. e.OutSuc(c, map[string]interface{}{
  167. "list": m,
  168. }, nil)
  169. return
  170. }