123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package maintainance
- import (
- "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 ChooseTemplateDetail(ctx context.Context, req *v1.ChooseTemplateDetailRequest) (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"))
- }
- }
- }()
- var (
- tx = database.DB().Begin()
- )
- defer func() {
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }()
- // 删除原有维保手册
- if err := model.NewMaintainDetailModel().Delete(tx.Where("style_id = ?", req.StyleId)); err != nil {
- return reply, errors.SystemError
- }
- if err := model.NewMaintainTitleModel().Delete(tx.Where("style_id = ?", req.StyleId)); err != nil {
- return reply, errors.SystemError
- }
- // 查询模板详情
- list, err := model.NewMaintainDetailTemplateModel().List(database.DB().Where("tid = ?", req.Tid))
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, errors.DataNotExistError
- }
- return reply, errors.SystemError
- }
- inserts := make([]model.GdMaintainDetail, 0, len(list))
- for _, v := range list {
- inserts = append(inserts, model.GdMaintainDetail{
- StyleId: req.StyleId,
- ItemId: v.ItemId,
- StartMile: v.StartMile,
- MileCycle: v.MileCycle,
- StartDate: v.StartDate,
- DateCycle: v.DateCycle,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- })
- }
- if err := model.NewMaintainDetailModel().Insert(tx, inserts); err != nil {
- return reply, errors.SystemError
- }
- // 查询模板标题
- title, err := model.NewMaintenanceTitleTemplateModel().Get(database.DB().Where("tid = ?", req.Tid))
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return reply, errors.DataNotExistError
- }
- return reply, errors.SystemError
- }
- // 没有title
- insert := &model.GdMaintainTitle{
- StartMile: title.StartMile,
- StartDate: title.StartDate,
- StyleId: req.StyleId,
- MaintainMileMinCycle: title.MaintainMileMinCycle,
- MaintainDateMinCycle: title.MaintainDateMinCycle,
- RepairCycle: title.RepairCycle,
- WashCycle: title.WashCycle,
- CreatedAt: time.Now(),
- UpdatedAt: time.Now(),
- }
- if err := model.NewMaintainTitleModel().Insert(tx, insert); err != nil {
- return reply, errors.SystemError
- }
- return reply, nil
- }
|