update.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package maintainance
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "adm-vehicle-style/consts"
  8. "adm-vehicle-style/errors"
  9. "adm-vehicle-style/model"
  10. v1 "adm-vehicle-style/pb/v1"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. func Update(ctx context.Context, req *v1.UpdateMaintainManualRequest) (reply *v1.EmptyReply, err error) {
  18. reply = &v1.EmptyReply{}
  19. defer func() {
  20. if r := recover(); r != nil {
  21. err = fmt.Errorf("%+v", r)
  22. e := &status.Status{}
  23. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  24. logger.Error("err",
  25. zap.String("system_err", err.Error()),
  26. zap.Stack("stacktrace"))
  27. }
  28. }
  29. }()
  30. // 获取maintain_title
  31. title, err := model.NewMaintainTitleModel().Get(database.DB().Where("style_id = ?", req.StyleId))
  32. if err != nil && err != gorm.ErrRecordNotFound {
  33. return nil, errors.SystemError
  34. }
  35. // 判断是否插入机油
  36. info, err := model.NewMaintainDetailModel().Get(database.DB().Where("style_id = ? AND item_id = ?", req.StyleId, consts.Oil))
  37. if err != nil && err != gorm.ErrRecordNotFound {
  38. return nil, errors.SystemError
  39. }
  40. var (
  41. hasOil bool
  42. )
  43. if err == nil && info != nil && info.ID != 0 {
  44. hasOil = true
  45. }
  46. var (
  47. tx = database.DB().Begin()
  48. )
  49. defer func() {
  50. if err != nil {
  51. tx.Rollback()
  52. return
  53. }
  54. tx.Commit()
  55. }()
  56. if req.Id == 0 {
  57. insert := model.GdMaintainDetail{
  58. StyleId: req.StyleId,
  59. ItemId: req.ItemId,
  60. StartMile: req.StartMile,
  61. MileCycle: req.MileCycle,
  62. StartDate: req.StartDate,
  63. DateCycle: req.DateCycle,
  64. CreatedAt: time.Now(),
  65. UpdatedAt: time.Now(),
  66. }
  67. if err := model.NewMaintainDetailModel().Insert(tx, &insert); err != nil {
  68. return reply, errors.SystemError
  69. }
  70. } else {
  71. values := map[string]interface{}{
  72. "start_mile": req.StartMile,
  73. "mile_cycle": req.MileCycle,
  74. "start_date": req.StartDate,
  75. "date_cycle": req.DateCycle,
  76. "item_id": req.ItemId,
  77. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  78. }
  79. if err := model.NewMaintainDetailModel().Update(tx.Where("id = ?", req.Id), values); err != nil {
  80. return reply, errors.SystemError
  81. }
  82. }
  83. // 以前插入机油,直接结束, 且当次插入不是机油
  84. if hasOil && req.ItemId != consts.Oil {
  85. return reply, nil
  86. }
  87. // 如果此条为机油
  88. if req.ItemId == consts.Oil {
  89. if title == nil || title.ID == 0 {
  90. // 没有title
  91. title = &model.GdMaintainTitle{
  92. StartMile: req.StartMile,
  93. StartDate: req.StartDate,
  94. StyleId: req.StyleId,
  95. MaintainMileMinCycle: req.MileCycle,
  96. MaintainDateMinCycle: req.DateCycle,
  97. RepairCycle: req.MileCycle,
  98. WashCycle: req.MileCycle,
  99. CreatedAt: time.Now(),
  100. UpdatedAt: time.Now(),
  101. }
  102. if err := model.NewMaintainTitleModel().Insert(tx, title); err != nil {
  103. return reply, errors.SystemError
  104. }
  105. } else {
  106. values := map[string]interface{}{
  107. "start_mile": req.StartMile,
  108. "start_date": req.StartDate,
  109. "maintain_mile_min_cycle": req.MileCycle,
  110. "maintain_date_min_cycle": req.DateCycle,
  111. "repair_cycle": req.MileCycle,
  112. "wash_cycle": req.MileCycle,
  113. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  114. }
  115. if err := model.NewMaintainTitleModel().Update(tx.Where("id = ?", title.ID), values); err != nil {
  116. return reply, errors.SystemError
  117. }
  118. }
  119. return reply, nil
  120. }
  121. // 如果都不是
  122. // 获取已有的维保数据
  123. list, err := model.NewMaintainDetailModel().
  124. MaintainDetailList(database.DB().Where("style_id = ?", req.StyleId))
  125. if err != nil && err != gorm.ErrRecordNotFound {
  126. return nil, errors.SystemError
  127. }
  128. var (
  129. idx int
  130. miniMile int64
  131. )
  132. list = append(list, model.MaintainDetailItem{
  133. StartMile: req.StartMile,
  134. MileCycle: req.MileCycle,
  135. StartDate: req.StartDate,
  136. DateCycle: req.DateCycle,
  137. })
  138. for k, v := range list {
  139. if miniMile == 0 {
  140. miniMile = v.MileCycle
  141. idx = k
  142. }
  143. if v.MileCycle != 0 && v.MileCycle < miniMile {
  144. miniMile = v.MileCycle
  145. idx = k
  146. }
  147. }
  148. if title == nil || title.ID == 0 {
  149. // 没有title
  150. title = &model.GdMaintainTitle{
  151. StyleId: req.StyleId,
  152. StartMile: list[idx].StartMile,
  153. StartDate: list[idx].StartDate,
  154. MaintainMileMinCycle: list[idx].MileCycle,
  155. MaintainDateMinCycle: list[idx].DateCycle,
  156. RepairCycle: list[idx].MileCycle,
  157. WashCycle: list[idx].MileCycle,
  158. CreatedAt: time.Now(),
  159. UpdatedAt: time.Now(),
  160. }
  161. if err := model.NewMaintainTitleModel().Insert(tx, title); err != nil {
  162. return reply, errors.SystemError
  163. }
  164. } else {
  165. values := map[string]interface{}{
  166. "start_mile": list[idx].StartMile,
  167. "start_date": list[idx].StartDate,
  168. "maintain_mile_min_cycle": list[idx].MileCycle,
  169. "maintain_date_min_cycle": list[idx].DateCycle,
  170. "repair_cycle": list[idx].MileCycle,
  171. "wash_cycle": list[idx].MileCycle,
  172. "updated_at": time.Now().Format("2006-01-02 15:04:05"),
  173. }
  174. if err := model.NewMaintainTitleModel().Update(tx.Where("id = ?", title.ID), values); err != nil {
  175. return reply, errors.SystemError
  176. }
  177. }
  178. return reply, nil
  179. }
  180. func min(a, b int64) int64 {
  181. if a < b {
  182. return a
  183. }
  184. return b
  185. }