123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- 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
- }
|