instrument.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package instrument
  2. import (
  3. "dashoo.cn/micro_libary/response"
  4. "dashoo.cn/modi_webapi/app/service/instrument"
  5. "dashoo.cn/modi_webapi/library/request"
  6. "fmt"
  7. "github.com/gogf/gf/net/ghttp"
  8. "github.com/gogf/gf/os/gtime"
  9. "github.com/gogf/gf/util/gvalid"
  10. "time"
  11. )
  12. type Controller struct {
  13. }
  14. func (c *Controller) GetAllInstrument(r *ghttp.Request) {
  15. page := request.GetPageInfo(r)
  16. where := ""
  17. if productway := r.GetString("ProductWay"); productway != "" {
  18. if where == "" {
  19. where = fmt.Sprintf(" ProductWay = %v", productway)
  20. } else {
  21. where += fmt.Sprintf(" AND ProductWay = %v", productway)
  22. }
  23. }
  24. if name := r.GetString("Name"); name != "" {
  25. if where == "" {
  26. where = fmt.Sprintf(" Name LIKE '%%%v%%'", name)
  27. } else {
  28. where += fmt.Sprintf(" AND Name LIKE '%%%v%%'", name)
  29. }
  30. }
  31. var result []instrument.Entity
  32. if err := instrument.GetAllInstrument(page, where, &result); err != nil {
  33. if err.Error() == "sql: no rows in result set" {
  34. response.Json(r, 0, "")
  35. return
  36. }
  37. response.Json(r, 1, err.Error())
  38. } else {
  39. count, err1 := instrument.FindInstrumentCount(where)
  40. if err1 != nil {
  41. response.Json(r, 1, err1.Error())
  42. } else {
  43. var records response.PagedRecords
  44. records.Size = page.Size
  45. records.Current = page.Current
  46. records.Total = count
  47. records.Records = result
  48. response.Json(r, 0, "ok", records)
  49. }
  50. }
  51. }
  52. func (c *Controller) GetOneInstrument(r *ghttp.Request) {
  53. id := r.GetInt("id")
  54. if result, err := instrument.FindOne(id); err != nil {
  55. response.Json(r, 1, err.Error())
  56. r.ExitAll()
  57. } else {
  58. var records response.PagedRecords
  59. records.Records = result
  60. response.Json(r, 0, "", records)
  61. }
  62. }
  63. func (c *Controller) AddInstrument(r *ghttp.Request) {
  64. Instrument := new(instrument.Entity)
  65. if err := r.Parse(Instrument); err != nil {
  66. // 数据验证错误
  67. if v, ok := err.(*gvalid.Error); ok {
  68. response.Json(r, 1, v.FirstString())
  69. r.ExitAll()
  70. }
  71. // 其他错误
  72. response.Json(r, 1, err.Error())
  73. r.ExitAll()
  74. }
  75. realName := r.GetParamVar("realname").String()
  76. userId := r.GetParamVar("userid").Int()
  77. currentTime := gtime.Now()
  78. Instrument.CreateOn = currentTime
  79. Instrument.CreateBy = realName
  80. Instrument.CreateUserId = userId
  81. Instrument.ModifiedOn = currentTime
  82. Instrument.ModifiedBy = realName
  83. Instrument.ModifiedUserId = userId
  84. hh, _ := time.ParseDuration("1h")
  85. Instrument.CalibrationTime = Instrument.CalibrationTime.Add(hh*8)
  86. if result, err := instrument.Insert(Instrument); err != nil {
  87. response.Json(r, 1, "保存失败")
  88. } else {
  89. var records response.PagedRecords
  90. id, _ := result.LastInsertId()
  91. Instrument.Id = int(id)
  92. records.Records = Instrument
  93. response.Json(r, 0, "保存成功", records)
  94. }
  95. }
  96. func (c *Controller) UpdateInstrument(r *ghttp.Request) {
  97. Instrument := new(instrument.Entity)
  98. if err := r.Parse(Instrument); err != nil {
  99. // 数据验证错误
  100. if v, ok := err.(*gvalid.Error); ok {
  101. response.Json(r, 1, v.FirstString())
  102. r.ExitAll()
  103. }
  104. // 其他错误
  105. response.Json(r, 1, err.Error())
  106. r.ExitAll()
  107. }
  108. realName := r.GetParamVar("realname").String()
  109. userId := r.GetParamVar("userid").Int()
  110. currentTime := gtime.Now()
  111. Instrument.ModifiedOn = currentTime
  112. Instrument.ModifiedBy = realName
  113. Instrument.ModifiedUserId = userId
  114. hh, _ := time.ParseDuration("1h")
  115. Instrument.CalibrationTime = Instrument.CalibrationTime.Add(hh*8)
  116. if _, err := instrument.Replace(Instrument); err != nil {
  117. response.Json(r, 1, "保存失败")
  118. } else {
  119. var records response.PagedRecords
  120. //id, _ := result.LastInsertId()
  121. //theChargeRecord.Id = int(id)
  122. records.Records = Instrument
  123. response.Json(r, 0, "保存成功", records)
  124. }
  125. }
  126. func (c *Controller) DeleteInstrument(r *ghttp.Request) {
  127. id := r.GetInt("id")
  128. if _, err := instrument.Delete(fmt.Sprintf("Id=%v", id)); err != nil {
  129. response.Json(r, 1, err.Error())
  130. r.ExitAll()
  131. } else {
  132. response.Json(r, 0, "该记录已删除!")
  133. }
  134. }