get.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package maintainance
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-vehicle-style/consts"
  7. "adm-vehicle-style/errors"
  8. "adm-vehicle-style/model"
  9. v1 "adm-vehicle-style/pb/v1"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. )
  16. func Manual(ctx context.Context, req *v1.MaintainManualRequest) (reply *v1.MaintainManualReply, err error) {
  17. reply = &v1.MaintainManualReply{}
  18. defer func() {
  19. if r := recover(); r != nil {
  20. err = fmt.Errorf("%+v", r)
  21. e := &status.Status{}
  22. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  23. logger.Error("err",
  24. zap.String("system_err", err.Error()),
  25. zap.Stack("stacktrace"))
  26. }
  27. }
  28. }()
  29. if req.StyleId == "" {
  30. return reply, errors.ParamsError
  31. }
  32. list, err := model.NewMaintainDetailModel().MaintainDetailList(database.DB().Where("t1.style_id = ?", req.StyleId))
  33. if err != nil && err != gorm.ErrRecordNotFound {
  34. return nil, errors.SystemError
  35. }
  36. if err == gorm.ErrRecordNotFound {
  37. return reply, nil
  38. }
  39. for _, v := range list {
  40. i := v1.MaintainItem{
  41. Id: v.ID,
  42. Item: v.ItemName,
  43. StartMile: v.StartMile,
  44. StartDate: v.StartDate,
  45. MileCycle: v.MileCycle,
  46. DateCycle: v.DateCycle,
  47. StyleId: req.StyleId,
  48. ItemId: v.ItemId,
  49. }
  50. switch v.ItemType {
  51. case consts.Maintainance:
  52. reply.Maintainance = append(reply.Maintainance, &i)
  53. case consts.Repair:
  54. reply.Repair = append(reply.Repair, &i)
  55. case consts.Wash:
  56. reply.Wash = append(reply.Wash, &i)
  57. }
  58. }
  59. return reply, nil
  60. }
  61. func judgeCycle(mile, age int64, v model.MaintainDetailItem, m, d int64) bool {
  62. if mile > 0 && v.MileCycle > 0 {
  63. if mile >= v.StartMile && (mile-m)%v.MileCycle == 0 {
  64. return true
  65. }
  66. }
  67. if age > 0 && v.DateCycle > 0 {
  68. if age >= v.StartDate && (age-d)%v.DateCycle == 0 {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. func generateMaintainace(
  75. list []model.MaintainDetailItem,
  76. title *model.GdMaintainTitle,
  77. reply *v1.MaintainManualInfoReply,
  78. ) {
  79. var (
  80. mile int64
  81. age int64
  82. maitainLen int64
  83. )
  84. if title.MaintainDateMinCycle == 0 && title.MaintainMileMinCycle == 0 {
  85. return
  86. }
  87. for {
  88. // 当前周期
  89. if title.StartMile != 0 && mile < title.StartMile {
  90. mile = title.StartMile
  91. } else {
  92. mile += title.MaintainMileMinCycle
  93. }
  94. if title.StartDate != 0 && age < title.StartDate {
  95. age = title.StartDate
  96. } else {
  97. age += title.MaintainDateMinCycle
  98. }
  99. if mile > 300000 {
  100. break
  101. }
  102. if age > 180 {
  103. break
  104. }
  105. maintain := v1.MaintainManualInfoReply_Maintain{
  106. Mile: uint32(mile),
  107. Age: uint32(age),
  108. }
  109. for _, v := range list {
  110. item := v1.MaintainManualInfoReply_Items{
  111. ItemId: uint32(v.ItemId),
  112. ItemName: v.ItemName,
  113. }
  114. if judgeCycle(mile, age, v, title.StartMile, title.StartDate) {
  115. item.Level = 2
  116. if maitainLen > 0 {
  117. for k := range reply.Maintain[maitainLen-1].Items {
  118. if reply.Maintain[maitainLen-1].Items[k].ItemId == item.ItemId {
  119. if reply.Maintain[maitainLen-1].Items[k].Level == 0 {
  120. reply.Maintain[maitainLen-1].Items[k].Level = 1
  121. }
  122. }
  123. }
  124. }
  125. }
  126. maintain.Items = append(maintain.Items, &item)
  127. }
  128. reply.Maintain = append(reply.Maintain, &maintain)
  129. maitainLen++
  130. }
  131. reply.Cycle = &v1.MaintainManualInfoReply_Cycle{
  132. StartMile: title.StartMile,
  133. MileCycle: title.MaintainMileMinCycle,
  134. StartDate: title.StartDate,
  135. DateCycle: title.MaintainDateMinCycle,
  136. }
  137. }
  138. func Info(ctx context.Context, req *v1.MaintainManualInfoRequest) (reply *v1.MaintainManualInfoReply, err error) {
  139. reply = &v1.MaintainManualInfoReply{}
  140. defer func() {
  141. if r := recover(); r != nil {
  142. err = fmt.Errorf("%+v", r)
  143. e := &status.Status{}
  144. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  145. logger.Error("err",
  146. zap.String("system_err", err.Error()),
  147. zap.Stack("stacktrace"))
  148. }
  149. }
  150. }()
  151. if req.StyleId == "" {
  152. return reply, errors.ParamsError
  153. }
  154. list, err := model.NewMaintainDetailModel().MaintainDetailList(database.DB().Where("t1.style_id = ?", req.StyleId))
  155. if err != nil && err != gorm.ErrRecordNotFound {
  156. return nil, errors.SystemError
  157. }
  158. if err == gorm.ErrRecordNotFound {
  159. return reply, nil
  160. }
  161. title, err := model.NewMaintainTitleModel().Get(database.DB().Where("style_id = ?", req.StyleId))
  162. if err != nil && err != gorm.ErrRecordNotFound {
  163. return nil, errors.SystemError
  164. }
  165. if err == gorm.ErrRecordNotFound {
  166. return reply, nil
  167. }
  168. maintainList := []model.MaintainDetailItem{}
  169. for _, v := range list {
  170. switch v.ItemType {
  171. case consts.Maintainance:
  172. reply.MaintainItem = append(reply.MaintainItem, &v1.MaintainManualInfoReply_MaintainItem{
  173. ItemId: uint32(v.ItemId),
  174. ItemName: v.ItemName,
  175. StartMile: v.StartMile,
  176. StartDate: v.StartDate,
  177. MileCycle: v.MileCycle,
  178. DateCycle: v.DateCycle,
  179. StyleId: v.StyleId,
  180. })
  181. maintainList = append(maintainList, v)
  182. case consts.Repair:
  183. reply.Repair = append(reply.Repair, &v1.MaintainManualInfoReply_Repair{
  184. Cycle: uint32(v.MileCycle),
  185. ItemName: v.ItemName,
  186. ItemId: uint32(v.ItemId),
  187. StartMile: v.StartMile,
  188. StartDate: v.StartDate,
  189. MileCycle: v.MileCycle,
  190. DateCycle: v.DateCycle,
  191. StyleId: v.StyleId,
  192. })
  193. case consts.Wash:
  194. reply.Wash = append(reply.Wash, &v1.MaintainManualInfoReply_Wash{
  195. Cycle: uint32(v.MileCycle),
  196. ItemName: v.ItemName,
  197. ItemId: uint32(v.ItemId),
  198. StartMile: v.StartMile,
  199. StartDate: v.StartDate,
  200. MileCycle: v.MileCycle,
  201. DateCycle: v.DateCycle,
  202. StyleId: v.StyleId,
  203. })
  204. }
  205. }
  206. if len(maintainList) > 0 {
  207. generateMaintainace(maintainList, title, reply)
  208. }
  209. return reply, nil
  210. }