123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package query
- import (
- "adm-data/consts"
- "adm-data/errors"
- "adm-data/model"
- v1 "adm-data/pb/v1"
- "context"
- "encoding/json"
- "fmt"
- jsoniter "github.com/json-iterator/go"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- 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 *v1.QueryResponse, err error) {
- reply = &v1.QueryResponse{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- 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
- }
|