package query import ( "context" "gd_adm_data/apis" "gd_adm_data/consts" "gd_adm_data/errors" "gd_adm_data/model" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/database" ) type maintainInfo struct { ItemName string `json:"item_name"` ItemId int64 `json:"item_id"` StartMile int64 `json:"start_mile"` StartDate int64 `json:"start_date"` MileCycle int64 `json:"mile_cycle"` DateCycle int64 `json:"date_cycle"` StyleId string `json:"style_id"` } type maintainItem struct { Level int64 `json:"level"` ItemName string `json:"item_name"` ItemId int64 `json:"item_id"` } type maintain struct { Age int64 `json:"age"` Mile int64 `json:"mile"` Items []maintainItem `json:"items"` } type item struct { ItemId int64 `json:"item_id"` ItemName string `json:"item_name"` StartMile int64 `json:"start_mile"` StartDate int64 `json:"start_date"` MileCycle int64 `json:"mile_cycle"` DateCycle int64 `json:"date_cycle"` StyleId string `json:"style_id"` } type cycle struct { StartMile int64 `json:"start_mile"` StartDate int64 `json:"start_date"` MileCycle int64 `json:"mile_cycle"` DateCycle int64 `json:"date_cycle"` } type MaintainManualResponse struct { // 保养 Maintain []maintain `json:"maintain"` // 维修 Repair []maintainInfo `json:"repair"` // 清洗 Wash []maintainInfo `json:"wash"` MaintainItem []item `json:"maintain_item"` Cycle cycle `json:"cycle"` } type p08Request struct { StyleId string `json:"style_id"` } // 维保手册 func P08(ctx context.Context, params string) (reply *apis.QueryResponse, err error) { reply = &apis.QueryResponse{} var req p08Request err = jsoniter.UnmarshalFromString(params, &req) if err != nil && req.StyleId == "" { return nil, errors.ParamsError } list, err := model.NewAds7Model().List(database.DB().Where("t1.style_id = ?", req.StyleId)) if err != nil { return nil, errors.DataNotExistError } title, err := model.NewAds13Model().Get(database.DB().Where("style_id = ?", req.StyleId)) if err != nil { return nil, errors.DataNotExistError } maintainList := []model.Ads7Item{} var res MaintainManualResponse for _, v := range list { switch v.ItemType { case consts.Maintainance: res.MaintainItem = append(res.MaintainItem, item{ ItemId: v.ItemId, ItemName: v.ItemName, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) maintainList = append(maintainList, v) case consts.Repair: res.Repair = append(res.Repair, maintainInfo{ ItemName: v.ItemName, ItemId: v.ItemId, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) case consts.Wash: res.Wash = append(res.Wash, maintainInfo{ ItemName: v.ItemName, ItemId: v.ItemId, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) } } // sort.Slice(res.MaintainItem, func(i, j int) bool { // return res.MaintainItem[i].ItemId < res.MaintainItem[j].ItemId // }) if len(maintainList) > 0 { generateMaintainace(maintainList, title, &res) } reply.Data, _ = jsoniter.MarshalToString(res) /*response := MaintainManualResponse{} err = jsoniter.UnmarshalFromString(reply.Data, &response) err = insertTable("P08", "保养手册", "34,39", req, response) if err != nil { return nil, err }*/ return reply, nil } func judgeCycle(mile, age int64, v model.Ads7Item, m, d int64) bool { if mile > 0 && v.MileCycle > 0 { if mile >= v.StartMile && (mile-m)%v.MileCycle == 0 { return true } } if age > 0 && v.DateCycle > 0 { if age >= v.StartDate && (age-d)%v.DateCycle == 0 { return true } } return false } func generateMaintainace( list []model.Ads7Item, title *model.Ads13, reply *MaintainManualResponse, ) { var ( mile int64 age int64 maintainLen int64 ) if title.MaintainDateMinCycle == 0 && title.MaintainMileMinCycle == 0 { return } for { // 当前周期 if title.StartMile != 0 && mile < title.StartMile { mile = title.StartMile } else { mile += title.MaintainMileMinCycle } if title.StartDate != 0 && age < title.StartDate { age = title.StartDate } else { age += title.MaintainDateMinCycle } if mile > 300000 { break } if age > 180 { break } maintain := maintain{ Mile: mile, Age: age, } for _, v := range list { item := maintainItem{ ItemId: v.ItemId, ItemName: v.ItemName, } if judgeCycle(mile, age, v, title.StartMile, title.StartDate) { item.Level = 2 if maintainLen > 0 { for k := range reply.Maintain[maintainLen-1].Items { if reply.Maintain[maintainLen-1].Items[k].ItemId == item.ItemId { if reply.Maintain[maintainLen-1].Items[k].Level == 0 { reply.Maintain[maintainLen-1].Items[k].Level = 1 } } } } } maintain.Items = append(maintain.Items, item) } // sort.Slice(maintain.Items, func(i, j int) bool { // return maintain.Items[i].ItemId < maintain.Items[j].ItemId // }) reply.Maintain = append(reply.Maintain, maintain) maintainLen++ } reply.Cycle.StartMile = title.StartMile reply.Cycle.MileCycle = title.MaintainMileMinCycle reply.Cycle.StartDate = title.StartDate reply.Cycle.DateCycle = title.MaintainDateMinCycle }