123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package maintainance
- import (
- "context"
- "encoding/json"
- "fmt"
- "time"
- "adm-vehicle-style/consts"
- "adm-vehicle-style/errors"
- "adm-vehicle-style/model"
- v1 "adm-vehicle-style/pb/v1"
- "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 Update(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"))
- }
- }
- }()
- // 获取maintain_title
- title, err := model.NewMaintainTitleModel().Get(database.DB().Where("style_id = ?", req.StyleId))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.SystemError
- }
- // 判断是否插入机油
- info, err := model.NewMaintainDetailModel().Get(database.DB().Where("style_id = ? AND item_id = ?", req.StyleId, consts.Oil))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.SystemError
- }
- var (
- hasOil bool
- )
- if err == nil && info != nil && info.ID != 0 {
- hasOil = true
- }
- var (
- tx = database.DB().Begin()
- )
- defer func() {
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }()
- if req.Id == 0 {
- insert := model.GdMaintainDetail{
- StyleId: req.StyleId,
- ItemId: req.ItemId,
- StartMile: req.StartMile,
- MileCycle: req.MileCycle,
- StartDate: req.StartDate,
- DateCycle: req.DateCycle,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- }
- if err := model.NewMaintainDetailModel().Insert(tx, &insert); err != nil {
- return reply, errors.SystemError
- }
- } else {
- 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.NewMaintainDetailModel().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 title == nil || title.ID == 0 {
- // 没有title
- title = &model.GdMaintainTitle{
- StartMile: req.StartMile,
- StartDate: req.StartDate,
- StyleId: req.StyleId,
- MaintainMileMinCycle: req.MileCycle,
- MaintainDateMinCycle: req.DateCycle,
- RepairCycle: req.MileCycle,
- WashCycle: req.MileCycle,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- }
- if err := model.NewMaintainTitleModel().Insert(tx, title); err != nil {
- return reply, errors.SystemError
- }
- } else {
- values := map[string]interface{}{
- "start_mile": req.StartMile,
- "start_date": req.StartDate,
- "maintain_mile_min_cycle": req.MileCycle,
- "maintain_date_min_cycle": req.DateCycle,
- "repair_cycle": req.MileCycle,
- "wash_cycle": req.MileCycle,
- "updated_at": time.Now().Format("2006-01-02 15:04:05"),
- }
- if err := model.NewMaintainTitleModel().Update(tx.Where("id = ?", title.ID), values); err != nil {
- return reply, errors.SystemError
- }
- }
- return reply, nil
- }
- // 如果都不是
- // 获取已有的维保数据
- list, err := model.NewMaintainDetailModel().
- MaintainDetailList(database.DB().Where("style_id = ?", req.StyleId))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.SystemError
- }
- var (
- idx int
- miniMile int64
- )
- list = append(list, model.MaintainDetailItem{
- 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 title == nil || title.ID == 0 {
- // 没有title
- title = &model.GdMaintainTitle{
- StyleId: req.StyleId,
- StartMile: list[idx].StartMile,
- StartDate: list[idx].StartDate,
- MaintainMileMinCycle: list[idx].MileCycle,
- MaintainDateMinCycle: list[idx].DateCycle,
- RepairCycle: list[idx].MileCycle,
- WashCycle: list[idx].MileCycle,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- }
- if err := model.NewMaintainTitleModel().Insert(tx, title); err != nil {
- return reply, errors.SystemError
- }
- } else {
- values := map[string]interface{}{
- "start_mile": list[idx].StartMile,
- "start_date": list[idx].StartDate,
- "maintain_mile_min_cycle": list[idx].MileCycle,
- "maintain_date_min_cycle": list[idx].DateCycle,
- "repair_cycle": list[idx].MileCycle,
- "wash_cycle": list[idx].MileCycle,
- "updated_at": time.Now().Format("2006-01-02 15:04:05"),
- }
- if err := model.NewMaintainTitleModel().Update(tx.Where("id = ?", title.ID), values); err != nil {
- return reply, errors.SystemError
- }
- }
- return reply, nil
- }
- func min(a, b int64) int64 {
- if a < b {
- return a
- }
- return b
- }
|