package maintainance import ( "adm-vehicle-style/consts" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" "context" "encoding/json" "fmt" "time" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" ) func TemplateList(ctx context.Context, req *v1.TemplateListRequest) (reply *v1.TemplateListReply, err error) { reply = &v1.TemplateListReply{} 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")) } } }() db := database.DB() if req.Name != "" { db = db.Where("name like", "%"+req.Name+"%") } list, err := model.NewMaintenanceTemplateModel().List(db) if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } } reply.List = make([]*v1.Template, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.Template{ Id: v.ID, Name: v.Name, }) } return reply, nil } func TemplateDetailList(ctx context.Context, req *v1.TemplateDetailListRequest) (reply *v1.TemplateDetailListReply, err error) { reply = &v1.TemplateDetailListReply{} 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")) } } }() db := database.DB() if req.Tid > 0 { db = db.Where("tid = ?", req.Tid) } count, err := model.NewMaintenanceTitleTemplateModel().Count(db) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound || count == 0 { return reply, nil } pageSize := consts.PageSize if req.PageSize != 0 { pageSize = int(req.PageSize) } // 构造分页类 pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset() list, err := model.NewMaintenanceTitleTemplateModel().List(db, pagination) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.TemplateDetail, 0, len(list)) for _, v := range list { template, err := model.NewMaintenanceTemplateModel().Get(database.DB().Where("id = ?", v.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = append(reply.List, &v1.TemplateDetail{ Id: v.ID, Name: template.Name, StartMile: v.StartMile, StartDate: v.StartDate, MaintainMileMinCycle: v.MaintainMileMinCycle, MaintainDateMinCycle: v.MaintainDateMinCycle, Tid: v.Tid, }) } reply.Total = count return reply, nil } func TemplateDetail(ctx context.Context, req *v1.TemplateDetailRequest) (reply *v1.TemplateDetailReply, err error) { reply = &v1.TemplateDetailReply{} 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")) } } }() list, err := model.NewMaintainDetailTemplateModel().ListByItem(database.DB().Where("tid = ?", req.Tid)) if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } return reply, errors.SystemError } template, err := model.NewMaintenanceTemplateModel().Get(database.DB().Where("id = ?", req.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } for _, v := range list { i := v1.MaintainItem{ Id: v.ID, Item: v.ItemName, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, ItemId: v.ItemId, } switch v.ItemType { case consts.Maintainance: reply.Maintainance = append(reply.Maintainance, &i) case consts.Repair: reply.Repair = append(reply.Repair, &i) case consts.Wash: reply.Wash = append(reply.Wash, &i) } } reply.Name = template.Name return reply, nil } func addTemplateTitle(db *gorm.DB, titleId, tid, startMile, mileCycle, startDate, dateCycle int64) error { if titleId > 0 { values := map[string]interface{}{ "start_mile": startMile, "start_date": startDate, "maintain_mile_min_cycle": mileCycle, "maintain_date_min_cycle": dateCycle, "repair_cycle": mileCycle, "wash_cycle": mileCycle, "updated_at": time.Now().Format("2006-01-02 15:04:05"), } if err := model.NewMaintenanceTitleTemplateModel().Update(db.Where("id = ?", titleId), values); err != nil { return errors.SystemError } } else { // 没有title title := &model.MaintenanceTitleTemplate{ Tid: tid, StartMile: startMile, StartDate: startDate, MaintainMileMinCycle: mileCycle, MaintainDateMinCycle: dateCycle, RepairCycle: mileCycle, WashCycle: mileCycle, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err := model.NewMaintenanceTitleTemplateModel().Insert(db, title); err != nil { return errors.SystemError } } return nil } func AddTemplateDetail(ctx context.Context, req *v1.AddTemplateDetailRequest) (reply *v1.AddTemplateDetailReply, err error) { reply = &v1.AddTemplateDetailReply{} 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")) } } }() if req.Tid == 0 && req.Name == "" { return reply, errors.ParamsError } var ( hasOil bool tid = req.Tid titleId int64 tx = database.DB().Begin() ) defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() if req.Tid > 0 { // 判断是否插入机油 info, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("tid = ? AND item_id = ?", req.Tid, consts.Oil)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == nil && info != nil && info.ID != 0 { hasOil = true } // 获取maintain_title title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", req.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == nil && title != nil && title.ID > 0 { titleId = title.ID } } else if req.Tid == 0 && req.Name != "" { // 写入新模板 template := &model.MaintenanceTemplate{ Name: req.Name, } err = model.NewMaintenanceTemplateModel().Insert(tx, template) if err != nil { return reply, errors.SystemError } tid = template.ID } // 写入详情 detail := &model.MaintenanceDetailTemplate{ Tid: tid, ItemId: req.ItemId, StartMile: req.StartMile, MileCycle: req.MileCycle, StartDate: req.StartDate, DateCycle: req.DateCycle, CreatedAt: time.Now(), UpdatedAt: time.Now(), } if err = model.NewMaintainDetailTemplateModel().Insert(tx, detail); err != nil { return reply, errors.SystemError } reply.Tid = tid // 以前插入机油,直接结束, 且当次插入不是机油 if hasOil && req.ItemId != consts.Oil { return reply, nil } // 如果此条为机油 if req.ItemId == consts.Oil { if err := addTemplateTitle(tx, titleId, tid, req.StartMile, req.MileCycle, req.StartDate, req.DateCycle); err != nil { return reply, err } return reply, nil } // 如果都不是 // 获取已有的维保数据 list, err := model.NewMaintainDetailTemplateModel(). List(database.DB().Where("tid = ?", req.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } var ( idx int miniMile int64 ) list = append(list, model.MaintenanceDetailTemplate{ StartMile: req.StartMile, MileCycle: req.MileCycle, StartDate: req.StartDate, DateCycle: req.DateCycle, }) for k, v := range list { if miniMile == 0 { miniMile = v.MileCycle idx = k } if v.MileCycle != 0 && v.MileCycle < miniMile { miniMile = v.MileCycle idx = k } } if err := addTemplateTitle(tx, titleId, tid, list[idx].StartMile, list[idx].MileCycle, list[idx].StartDate, list[idx].DateCycle); err != nil { return reply, err } return reply, nil } func UpdateTemplateDetail(ctx context.Context, req *v1.UpdateMaintainManualRequest) (reply *v1.EmptyReply, err error) { reply = &v1.EmptyReply{} 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")) } } }() if req.Name != "" && req.Tid > 0 { if err := model.NewMaintenanceTemplateModel().Update(database.DB().Where("id = ?", req.Tid), map[string]interface{}{ "name": req.Name, }); err != nil { return reply, errors.SystemError } } if req.Id == 0 { return reply, nil } info, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("id = ?", req.Id)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } // 获取maintain_title title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", info.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } // 判断是否插入机油 oil, err := model.NewMaintainDetailTemplateModel().Get(database.DB().Where("tid = ? AND item_id = ?", info.Tid, consts.Oil)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } var ( hasOil bool ) if err == nil && oil != nil && oil.ID != 0 { hasOil = true } var ( tx = database.DB().Begin() ) defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() values := map[string]interface{}{ "start_mile": req.StartMile, "mile_cycle": req.MileCycle, "start_date": req.StartDate, "date_cycle": req.DateCycle, "item_id": req.ItemId, "updated_at": time.Now().Format("2006-01-02 15:04:05"), } if err := model.NewMaintainDetailTemplateModel().Update(tx.Where("id = ?", req.Id), values); err != nil { return reply, errors.SystemError } // 以前插入机油,直接结束, 且当次插入不是机油 if hasOil && req.ItemId != consts.Oil { return reply, nil } // 如果此条为机油 if req.ItemId == consts.Oil { if err := addTemplateTitle(tx, title.ID, info.Tid, req.StartMile, req.MileCycle, req.StartDate, req.DateCycle); err != nil { return reply, err } return reply, nil } // 如果都不是 // 获取已有的维保数据 list, err := model.NewMaintainDetailTemplateModel().List(database.DB().Where("tid = ?", info.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } var ( idx int miniMile int64 ) if len(list) > 0 { for k := range list { if list[k].ID == req.Id { list[k].StartMile = req.StartMile list[k].StartDate = req.StartDate list[k].MileCycle = req.MileCycle list[k].DateCycle = req.DateCycle } } } else { list = append(list, model.MaintenanceDetailTemplate{ StartMile: req.StartMile, MileCycle: req.MileCycle, StartDate: req.StartDate, DateCycle: req.DateCycle, }) } for k, v := range list { if miniMile == 0 { miniMile = v.MileCycle idx = k } if v.MileCycle != 0 && v.MileCycle < miniMile { miniMile = v.MileCycle idx = k } } if err := addTemplateTitle(tx, title.ID, info.Tid, list[idx].StartMile, list[idx].MileCycle, list[idx].StartDate, list[idx].DateCycle); err != nil { return reply, err } return reply, nil }