package base import ( "context" "fmt" "strconv" "dashoo.cn/opms_libary/myerrors" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" "dashoo.cn/micro/app/dao/base" model "dashoo.cn/micro/app/model/base" "dashoo.cn/micro/app/service" ) type distributorService struct { *service.ContextService Dao *base.BaseDistributorDao } func NewDistributorService(ctx context.Context) (svc *distributorService, err error) { svc = new(distributorService) if svc.ContextService, err = svc.Init(ctx); err != nil { return nil, err } svc.Dao = base.NewBaseDistributorDao(svc.Tenant) return svc, nil } // GetList 经销商信息列表 func (s *distributorService) GetList(req *model.BaseDistributorSearchReq) (total int, distributorList []*model.DistributorRonp, err error) { distributorModel := s.Dao.M if req.DistCode != "" { distributorModel = distributorModel.Where(s.Dao.C.DistCode+" like ?", "%"+req.DistCode+"%") } if req.DistName != "" { distributorModel = distributorModel.Where(s.Dao.C.DistName+" like ?", "%"+req.DistName+"%") } if req.BelongSale != "" { distributorModel = distributorModel.Where(s.Dao.C.BelongSale+" like ?", "%"+req.BelongSale+"%") } if req.ProvinceId > 0 { distributorModel = distributorModel.Where(s.Dao.C.ProvinceId+" in (?)", req.ProvinceId) } total, err = distributorModel.Count() if err != nil { err = myerrors.DbError("获取总行数失败。") return } err = distributorModel.Page(req.GetPage()).Order("id desc").Scan(&distributorList) return } // Create 经销商创建 func (s *distributorService) Create(req *model.AddDistributor) (lastId int64, err error) { DistributorData := new(model.BaseDistributor) if err = gconv.Struct(req, DistributorData); err != nil { return } service.SetCreatedInfo(DistributorData, s.GetCxtUserId(), s.GetCxtUserName()) DistributorData.DistCode = gstr.SubStr(strconv.Itoa(int(gtime.Now().UnixNano()/1e6))+"Code", 0, -5) lastId, err = s.Dao.InsertAndGetId(DistributorData) if err != nil { return 0, err } return } // GetEntityById 详情 func (s *distributorService) GetEntityById(id int64) (distributorInfo *model.DistributorRonp, err error) { err = s.Dao.Where(base.BaseProduct.C.Id, id).Scan(&distributorInfo) if err != nil { return } return } // UpdateById 修改数据 func (s *distributorService) UpdateById(req *model.UpdateDistributorReq) (err error) { count, err := s.Dao.Where("id = ", req.Id).Count() if err != nil { g.Log().Error(err) return } if count == 0 { err = myerrors.TipsError("无修改数据") return } distData := new(model.BaseDistributor) if err = gconv.Struct(req, distData); err != nil { return } service.SetUpdatedInfo(distData, s.GetCxtUserId(), s.GetCxtUserName()) _, err = s.Dao.FieldsEx(s.Dao.C.DistCode, 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 *distributorService) DeleteByIds(ids []int64) (err error) { if len(ids) == 1 { count, err := s.Dao.WherePri(ids[0]).Count() if err != nil { g.Log().Error(err) return err } if count == 0 { return myerrors.TipsError("数据不存在或已被删除,请刷新页面") } } else { _, err := s.Dao.WhereIn(s.Dao.C.Id, ids).LockShared().Count() if err != nil { g.Log().Error(err) return err } result, err := s.Dao.WhereIn(s.Dao.C.Id, ids).Delete() if err != nil { g.Log().Error(err) return err } rows, err := result.RowsAffected() if err == nil { if len(ids) != int(rows) { return myerrors.TipsError(fmt.Sprintf("应更新%s行,实际更新%s行", len(ids), int(rows))) } } } return }