package train import ( "context" dao "dashoo.cn/micro/app/dao/train" model "dashoo.cn/micro/app/model/train" "dashoo.cn/micro/app/service" "dashoo.cn/opms_libary/myerrors" "github.com/gogf/gf/database/gdb" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" ) type SaleApplyService struct { *service.ContextService Dao *dao.TrainSaleApplyDao } func NewSaleApplyService(ctx context.Context) (svc *SaleApplyService, err error) { svc = new(SaleApplyService) if svc.ContextService, err = svc.Init(ctx); err != nil { return nil, err } svc.Dao = dao.NewTrainSaleApplyDao(svc.Tenant) return svc, nil } // GetList 销售申请信息列表 func (s *SaleApplyService) GetList(ctx context.Context, req *model.TrainSaleApplySearchReq) (total int, distributorList []*model.TrainSaleApplyRes, err error) { distributorModel := s.Dao.Ctx(ctx).FieldsEx(s.Dao.C.DeletedTime) if req.CreatedBy != 0 { distributorModel = distributorModel.Where(s.Dao.C.CreatedBy, req.CreatedBy) } if req.DistributorId != 0 { distributorModel = distributorModel.Where(s.Dao.C.DistributorId, req.DistributorId) } if req.MainProduct != "" { distributorModel = distributorModel.WhereLike(s.Dao.C.MainProduct, "%"+req.MainProduct+"%") } total, _ = distributorModel.Count() err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList) if err != nil { return 0, nil, err } return } // GetEntityById 详情 func (s *SaleApplyService) GetEntityById(ctx context.Context, id int64) (distributorInfo *model.TrainSaleApplyRes, err error) { err = s.Dao.Ctx(ctx).Where(s.Dao.C.Id, id).Scan(&distributorInfo) if err != nil { return nil, err } return } // Create 销售申请创建 func (s *SaleApplyService) Create(ctx context.Context, req *model.TrainSaleApplyCreateReq) (lastId int64, err error) { DistributorData := new(model.TrainSaleApply) if err = gconv.Struct(req, DistributorData); err != nil { return 0, err } service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName()) txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error { DistributorData.ApplyType = "10" lastId, err = tx.InsertAndGetId("train_sale_apply", DistributorData) if err != nil { return err } return err }) if txerr != nil { return 0, txerr } return lastId, nil } // UpdateById 修改数据 func (s *SaleApplyService) UpdateById(ctx context.Context, req *model.TrainSaleApplyUpdateReq) (err error) { ent, err := s.Dao.Ctx(ctx).Where("id = ", req.Id).One() if err != nil { g.Log().Error(err) return } if ent == nil { err = myerrors.TipsError("无修改数据") return } distData := new(model.TrainSaleApply) if err = gconv.Struct(req, distData); err != nil { return } service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName()) _, err = s.Dao.Ctx(ctx).FieldsEx(s.Dao.C.Id, s.Dao.C.CreatedName, s.Dao.C.CreatedBy, s.Dao.C.CreatedTime). WherePri(s.Dao.C.Id, req.Id).Update(distData) if err != nil { g.Log().Error(err) return } return } // DeleteByIds 删除 func (s *SaleApplyService) DeleteByIds(ctx context.Context, ids []int64) (err error) { _, err = s.Dao.Ctx(ctx).WhereIn(s.Dao.C.Id, ids).Delete() if err != nil { return err } return }