p08.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package query
  2. import (
  3. "adm-data/consts"
  4. "adm-data/errors"
  5. "adm-data/model"
  6. v1 "adm-data/pb/v1"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. jsoniter "github.com/json-iterator/go"
  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. )
  16. type maintainInfo struct {
  17. ItemName string `json:"item_name"`
  18. ItemId int64 `json:"item_id"`
  19. StartMile int64 `json:"start_mile"`
  20. StartDate int64 `json:"start_date"`
  21. MileCycle int64 `json:"mile_cycle"`
  22. DateCycle int64 `json:"date_cycle"`
  23. StyleId string `json:"style_id"`
  24. }
  25. type maintainItem struct {
  26. Level int64 `json:"level"`
  27. ItemName string `json:"item_name"`
  28. ItemId int64 `json:"item_id"`
  29. }
  30. type maintain struct {
  31. Age int64 `json:"age"`
  32. Mile int64 `json:"mile"`
  33. Items []maintainItem `json:"items"`
  34. }
  35. type item struct {
  36. ItemId int64 `json:"item_id"`
  37. ItemName string `json:"item_name"`
  38. StartMile int64 `json:"start_mile"`
  39. StartDate int64 `json:"start_date"`
  40. MileCycle int64 `json:"mile_cycle"`
  41. DateCycle int64 `json:"date_cycle"`
  42. StyleId string `json:"style_id"`
  43. }
  44. type cycle struct {
  45. StartMile int64 `json:"start_mile"`
  46. StartDate int64 `json:"start_date"`
  47. MileCycle int64 `json:"mile_cycle"`
  48. DateCycle int64 `json:"date_cycle"`
  49. }
  50. type MaintainManualResponse struct {
  51. // 保养
  52. Maintain []maintain `json:"maintain"`
  53. // 维修
  54. Repair []maintainInfo `json:"repair"`
  55. // 清洗
  56. Wash []maintainInfo `json:"wash"`
  57. MaintainItem []item `json:"maintain_item"`
  58. Cycle cycle `json:"cycle"`
  59. }
  60. type p08Request struct {
  61. StyleId string `json:"style_id"`
  62. }
  63. func p08(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  64. reply = &v1.QueryResponse{}
  65. // 捕获各个task中的异常并返回给调用者
  66. defer func() {
  67. if r := recover(); r != nil {
  68. err = fmt.Errorf("%+v", r)
  69. e := &status.Status{}
  70. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  71. logger.Error("err",
  72. zap.String("system_err", err.Error()),
  73. zap.Stack("stacktrace"))
  74. }
  75. }
  76. }()
  77. var req p08Request
  78. err = jsoniter.UnmarshalFromString(params, &req)
  79. if err != nil && req.StyleId == "" {
  80. return nil, errors.ParamsError
  81. }
  82. list, err := model.NewAds7Model().List(database.DB().Where("t1.style_id = ?", req.StyleId))
  83. if err != nil {
  84. return nil, errors.DataNotExistError
  85. }
  86. title, err := model.NewAds13Model().Get(database.DB().Where("style_id = ?", req.StyleId))
  87. if err != nil {
  88. return nil, errors.DataNotExistError
  89. }
  90. maintainList := []model.Ads7Item{}
  91. var res MaintainManualResponse
  92. for _, v := range list {
  93. switch v.ItemType {
  94. case consts.Maintainance:
  95. res.MaintainItem = append(res.MaintainItem, item{
  96. ItemId: v.ItemId,
  97. ItemName: v.ItemName,
  98. StartMile: v.StartMile,
  99. StartDate: v.StartDate,
  100. MileCycle: v.MileCycle,
  101. DateCycle: v.DateCycle,
  102. StyleId: v.StyleId,
  103. })
  104. maintainList = append(maintainList, v)
  105. case consts.Repair:
  106. res.Repair = append(res.Repair, maintainInfo{
  107. ItemName: v.ItemName,
  108. ItemId: v.ItemId,
  109. StartMile: v.StartMile,
  110. StartDate: v.StartDate,
  111. MileCycle: v.MileCycle,
  112. DateCycle: v.DateCycle,
  113. StyleId: v.StyleId,
  114. })
  115. case consts.Wash:
  116. res.Wash = append(res.Wash, maintainInfo{
  117. ItemName: v.ItemName,
  118. ItemId: v.ItemId,
  119. StartMile: v.StartMile,
  120. StartDate: v.StartDate,
  121. MileCycle: v.MileCycle,
  122. DateCycle: v.DateCycle,
  123. StyleId: v.StyleId,
  124. })
  125. }
  126. }
  127. // sort.Slice(res.MaintainItem, func(i, j int) bool {
  128. // return res.MaintainItem[i].ItemId < res.MaintainItem[j].ItemId
  129. // })
  130. if len(maintainList) > 0 {
  131. generateMaintainace(maintainList, title, &res)
  132. }
  133. reply.Data, _ = jsoniter.MarshalToString(res)
  134. /*response := MaintainManualResponse{}
  135. err = jsoniter.UnmarshalFromString(reply.Data, &response)
  136. err = insertTable("P08", "保养手册", "34,39", req, response)
  137. if err != nil {
  138. return nil, err
  139. }*/
  140. return reply, nil
  141. }
  142. func judgeCycle(mile, age int64, v model.Ads7Item, m, d int64) bool {
  143. if mile > 0 && v.MileCycle > 0 {
  144. if mile >= v.StartMile && (mile-m)%v.MileCycle == 0 {
  145. return true
  146. }
  147. }
  148. if age > 0 && v.DateCycle > 0 {
  149. if age >= v.StartDate && (age-d)%v.DateCycle == 0 {
  150. return true
  151. }
  152. }
  153. return false
  154. }
  155. func generateMaintainace(
  156. list []model.Ads7Item,
  157. title *model.Ads13,
  158. reply *MaintainManualResponse,
  159. ) {
  160. var (
  161. mile int64
  162. age int64
  163. maintainLen int64
  164. )
  165. if title.MaintainDateMinCycle == 0 && title.MaintainMileMinCycle == 0 {
  166. return
  167. }
  168. for {
  169. // 当前周期
  170. if title.StartMile != 0 && mile < title.StartMile {
  171. mile = title.StartMile
  172. } else {
  173. mile += title.MaintainMileMinCycle
  174. }
  175. if title.StartDate != 0 && age < title.StartDate {
  176. age = title.StartDate
  177. } else {
  178. age += title.MaintainDateMinCycle
  179. }
  180. if mile > 300000 {
  181. break
  182. }
  183. if age > 180 {
  184. break
  185. }
  186. maintain := maintain{
  187. Mile: mile,
  188. Age: age,
  189. }
  190. for _, v := range list {
  191. item := maintainItem{
  192. ItemId: v.ItemId,
  193. ItemName: v.ItemName,
  194. }
  195. if judgeCycle(mile, age, v, title.StartMile, title.StartDate) {
  196. item.Level = 2
  197. if maintainLen > 0 {
  198. for k := range reply.Maintain[maintainLen-1].Items {
  199. if reply.Maintain[maintainLen-1].Items[k].ItemId == item.ItemId {
  200. if reply.Maintain[maintainLen-1].Items[k].Level == 0 {
  201. reply.Maintain[maintainLen-1].Items[k].Level = 1
  202. }
  203. }
  204. }
  205. }
  206. }
  207. maintain.Items = append(maintain.Items, item)
  208. }
  209. // sort.Slice(maintain.Items, func(i, j int) bool {
  210. // return maintain.Items[i].ItemId < maintain.Items[j].ItemId
  211. // })
  212. reply.Maintain = append(reply.Maintain, maintain)
  213. maintainLen++
  214. }
  215. reply.Cycle.StartMile = title.StartMile
  216. reply.Cycle.MileCycle = title.MaintainMileMinCycle
  217. reply.Cycle.StartDate = title.StartDate
  218. reply.Cycle.DateCycle = title.MaintainDateMinCycle
  219. }