P08.go 5.4 KB

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